blob: a203557324a0f0173026cf20e0622c6287e1f1db [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 Vaskoa6669ba2020-08-06 16:14:26 +020035#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010036#include "hash_table.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020038#include "parser_data.h"
39#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020040#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "plugins_exts.h"
Radek Krejci38d85362019-09-05 16:26:38 +020042#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010043#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020044#include "plugins_types.h"
45#include "set.h"
46#include "tree.h"
47#include "tree_data_internal.h"
48#include "tree_schema.h"
49#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020050#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020051#include "xml.h"
52#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020053
Michal Vaskob104f112020-07-17 09:54:54 +020054static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
55 struct lyd_node **match);
56
Radek Krejci084289f2019-07-09 17:35:30 +020057LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +020058lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second, int value_hint,
Michal Vaskoc8a230d2020-08-14 12:17:10 +020059 LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020060{
Michal Vasko90932a92020-02-12 14:33:03 +010061 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020062 struct ly_err_item *err = NULL;
63 struct ly_ctx *ctx;
64 struct lysc_type *type;
Michal Vaskoba99a3e2020-08-18 15:50:05 +020065 int options = value_hint | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010066 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020067 assert(node);
68
69 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020070
Radek Krejci73dead22019-07-11 16:46:16 +020071 type = ((struct lysc_node_leaf*)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +020072 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data,
Michal Vaskoba99a3e2020-08-18 15:50:05 +020073 tree ? (void *)node : (void *)node->schema, tree, &node->value, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010074 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020075 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020076 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020077 if (!node->parent && lysc_data_parent(node->schema)) {
78 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
79 } else {
80 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
81 }
Radek Krejci73dead22019-07-11 16:46:16 +020082 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020083 }
Radek Krejci73dead22019-07-11 16:46:16 +020084 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010085 } else if (dynamic) {
86 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020087 }
88
89error:
90 return ret;
91}
92
Michal Vasko00cbf532020-06-15 13:58:47 +020093/* 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 +020094LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010095lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
Michal Vaskoc8a230d2020-08-14 12:17:10 +020096 LY_PREFIX_FORMAT format, void *prefix_data)
Michal Vasko90932a92020-02-12 14:33:03 +010097{
98 LY_ERR ret = LY_SUCCESS;
99 struct ly_err_item *err = NULL;
100 struct ly_ctx *ctx;
101 struct lysc_type *type;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200102 int options = LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100103
104 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
105
106 ctx = schema->module->ctx;
107 type = ((struct lysc_node_leaf *)schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200108 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, (void *)schema, NULL,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200109 val, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100110 if (ret == LY_EINCOMPLETE) {
111 /* this is fine, we do not need it resolved */
112 ret = LY_SUCCESS;
113 } else if (ret && err) {
114 ly_err_print(err);
115 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
116 ly_err_free(err);
117 }
118 if (!ret && dynamic) {
119 *dynamic = 0;
120 }
121
122 return ret;
123}
124
Radek Krejci38d85362019-09-05 16:26:38 +0200125LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +0200126lyd_value_parse_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200127 int second, int value_hint, LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_snode,
128 const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200129{
Michal Vasko90932a92020-02-12 14:33:03 +0100130 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200131 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200132 struct lyext_metadata *ant;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200133 int options = value_hint | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100134 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200135
Michal Vasko9f96a052020-03-10 09:41:45 +0100136 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100137
Michal Vasko9f96a052020-03-10 09:41:45 +0100138 ant = meta->annotation->data;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200139 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, format, prefix_data,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200140 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100141 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200142 if (err) {
143 ly_err_print(err);
144 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
145 ly_err_free(err);
146 }
147 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100148 } else if (dynamic) {
149 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200150 }
151
152error:
153 return ret;
154}
155
Michal Vaskof937cfe2020-08-03 16:07:12 +0200156LY_ERR
157_lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200158 LY_PREFIX_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200159{
160 LY_ERR rc = LY_SUCCESS;
161 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200162 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200163 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200164
165 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
166
167 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
168 LOGARG(ctx, node);
169 return LY_EINVAL;
170 }
171
172 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200173 /* just validate, no storing of enything */
174 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200175 format, prefix_data, node, NULL, &storage, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200176 if (rc == LY_EINCOMPLETE) {
177 /* actually success since we do not provide the context tree and call validation with
178 * LY_TYPE_OPTS_INCOMPLETE_DATA */
179 rc = LY_SUCCESS;
180 } else if (rc && err) {
181 if (ctx) {
182 /* log only in case the ctx was provided as input parameter */
183 ly_err_print(err);
184 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200185 }
Radek Krejci73dead22019-07-11 16:46:16 +0200186 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200187 }
188
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200189 if (!rc) {
190 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
191 }
Radek Krejci084289f2019-07-09 17:35:30 +0200192 return rc;
193}
194
195API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200196lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len)
197{
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200198 return _lys_value_validate(ctx, node, value, value_len, LY_PREF_JSON, NULL);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200199}
200
201API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100202lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vasko3701af52020-08-03 14:29:38 +0200203 const struct lyd_node *tree, struct lysc_type **realtype)
Radek Krejci084289f2019-07-09 17:35:30 +0200204{
205 LY_ERR rc;
206 struct ly_err_item *err = NULL;
207 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200208 struct lyd_value val = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200209 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200210
211 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
212
213 type = ((struct lysc_node_leaf*)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200214 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options, LY_PREF_JSON, NULL,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200215 tree ? (void*)node : (void*)node->schema, tree, &val, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200216 if (rc == LY_EINCOMPLETE) {
217 return rc;
218 } else if (rc) {
219 if (err) {
220 if (ctx) {
221 ly_err_print(err);
222 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200223 }
Radek Krejci73dead22019-07-11 16:46:16 +0200224 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200225 }
Radek Krejci73dead22019-07-11 16:46:16 +0200226 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200227 }
228
Michal Vasko3701af52020-08-03 14:29:38 +0200229 if (realtype) {
230 *realtype = val.realtype;
231 }
232
233 type->plugin->free(ctx ? ctx : node->schema->module->ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200234 return LY_SUCCESS;
235}
236
237API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200238lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200239{
240 LY_ERR ret = LY_SUCCESS, rc;
241 struct ly_err_item *err = NULL;
242 struct ly_ctx *ctx;
243 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200244 struct lyd_value data = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200245 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200246
247 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
248
249 ctx = node->schema->module->ctx;
250 type = ((struct lysc_node_leaf*)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200251 rc = type->plugin->store(ctx, type, value, value_len, options, LY_PREF_JSON, NULL, (struct lyd_node *)node, tree, &data,
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200252 &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200253 if (rc == LY_EINCOMPLETE) {
254 ret = rc;
255 /* continue with comparing, just remember what to return if storing is ok */
256 } else if (rc) {
257 /* value to compare is invalid */
258 ret = LY_EINVAL;
259 if (err) {
260 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200261 }
Radek Krejci73dead22019-07-11 16:46:16 +0200262 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200263 }
264
265 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200266 if (type->plugin->compare(&node->value, &data)) {
267 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200268 ret = LY_ENOT;
Radek Krejci5af04842019-07-12 11:32:07 +0200269 }
Radek Krejci084289f2019-07-09 17:35:30 +0200270
271cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200272 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200273
274 return ret;
275}
276
Radek Krejci7931b192020-06-25 17:05:03 +0200277static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200278lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200279{
Radek Krejcie7b95092019-05-15 11:03:07 +0200280
Radek Krejci7931b192020-06-25 17:05:03 +0200281 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200282 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200283 const char *path = in->method.fpath.filepath;
284 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200285
286 /* ignore trailing whitespaces */
287 for (; len > 0 && isspace(path[len - 1]); len--);
288
289 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
290 format = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200291 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
292 format = LYD_JSON;
293 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
294 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200295 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200296 }
297
Radek Krejci7931b192020-06-25 17:05:03 +0200298 return format;
299}
Radek Krejcie7b95092019-05-15 11:03:07 +0200300
Radek Krejci7931b192020-06-25 17:05:03 +0200301API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200302lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, int parse_options, int validate_options,
303 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200304{
Radek Krejci1798aae2020-07-14 13:26:06 +0200305 LY_ERR ret = LY_SUCCESS;
306 struct lyd_ctx *lydctx = NULL;
307
Radek Krejci7931b192020-06-25 17:05:03 +0200308 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
309 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
310 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
311
312 format = lyd_parse_get_format(in, format);
313 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
314
Radek Krejci1798aae2020-07-14 13:26:06 +0200315 /* init */
316 *tree = NULL;
317
Michal Vasko63f3d842020-07-08 10:10:14 +0200318 /* remember input position */
319 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200320
321 switch (format) {
322 case LYD_XML:
Radek Krejci1798aae2020-07-14 13:26:06 +0200323 LY_CHECK_RET(lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree, &lydctx));
324 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200325 case LYD_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +0200326 LY_CHECK_RET(lyd_parse_json_data(ctx, in, parse_options, validate_options, tree, &lydctx));
327 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200328 case LYD_LYB:
Radek Krejci1798aae2020-07-14 13:26:06 +0200329 LY_CHECK_RET(lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree, &lydctx));
330 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200331 case LYD_UNKNOWN:
Radek Krejci7931b192020-06-25 17:05:03 +0200332 LOGINT_RET(ctx);
333 }
334
Radek Krejci1798aae2020-07-14 13:26:06 +0200335 if (!(parse_options & LYD_PARSE_ONLY)) {
336 uint32_t i = 0;
337 const struct lys_module *mod;
338 struct lyd_node *first, *next, **first2;
Radek Krejci7931b192020-06-25 17:05:03 +0200339
Radek Krejci1798aae2020-07-14 13:26:06 +0200340 next = *tree;
341 while (1) {
342 if (validate_options & LYD_VALIDATE_PRESENT) {
343 mod = lyd_data_next_module(&next, &first);
344 } else {
345 mod = lyd_mod_next_module(next, NULL, ctx, &i, &first);
346 }
347 if (!mod) {
348 break;
349 }
350 if (first == *tree) {
351 /* make sure first2 changes are carried to tree */
352 first2 = tree;
353 } else {
354 first2 = &first;
355 }
356
357 /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
358 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod, NULL), cleanup);
359
360 /* add all top-level defaults for this module */
361 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &lydctx->unres_node_type, &lydctx->when_check,
362 (validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
363 LY_CHECK_GOTO(ret, cleanup);
364
365 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200366 switch (format) {
367 case LYD_XML:
368 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type,
369 LY_PREF_XML, &((struct lyxml_ctx *)lydctx->data_ctx)->ns, NULL);
370 break;
371 case LYD_JSON:
372 case LYD_LYB:
373 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type,
374 LY_PREF_JSON, NULL, NULL);
375 break;
376 case LYD_UNKNOWN:
377 LOGINT_RET(ctx);
378 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200379 LY_CHECK_GOTO(ret, cleanup);
380
381 /* perform final validation that assumes the data tree is final */
382 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, validate_options, 0), cleanup);
383 }
384 }
385
386cleanup:
387 lydctx->free((struct lyd_ctx *)lydctx);
388 if (ret) {
389 lyd_free_all(*tree);
390 *tree = NULL;
391 }
392 return ret;
Radek Krejci7931b192020-06-25 17:05:03 +0200393}
394
395API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200396lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int parse_options, int validate_options,
397 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200398{
399 LY_ERR ret;
400 struct ly_in *in;
401
402 LY_CHECK_RET(ly_in_new_memory(data, &in));
403 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
404
405 ly_in_free(in, 0);
406 return ret;
407}
408
409API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200410lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options,
411 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200412{
413 LY_ERR ret;
414 struct ly_in *in;
415
416 LY_CHECK_RET(ly_in_new_fd(fd, &in));
417 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
418
419 ly_in_free(in, 0);
420 return ret;
421}
422
423API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200424lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options,
425 int validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200426{
427 LY_ERR ret;
428 struct ly_in *in;
429
430 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
431 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
432
433 ly_in_free(in, 0);
434 return ret;
435}
436
Radek Krejci7931b192020-06-25 17:05:03 +0200437API LY_ERR
438lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
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
Radek Krejci1798aae2020-07-14 13:26:06 +0200445 /* init */
446 *tree = NULL;
447 if (op) {
448 *op = NULL;
449 }
450
Michal Vasko63f3d842020-07-08 10:10:14 +0200451 /* remember input position */
452 in->func_start = in->current;
453
Radek Krejci7931b192020-06-25 17:05:03 +0200454 switch (format) {
455 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200456 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200457 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200458 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200459 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200460 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200461 case LYD_UNKNOWN:
462 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200463 }
464
465 LOGINT_RET(ctx);
466}
467
468API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200469lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
470 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200471{
472 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200473 LY_CHECK_ARG_RET(LYD_CTX(request), in, tree || op, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200474
475 format = lyd_parse_get_format(in, format);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200476 LY_CHECK_ARG_RET(LYD_CTX(request), format, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200477
Radek Krejci1798aae2020-07-14 13:26:06 +0200478 /* init */
479 if (tree) {
480 *tree = NULL;
481 }
482 if (op) {
483 *op = NULL;
484 }
485
Michal Vasko63f3d842020-07-08 10:10:14 +0200486 /* remember input position */
487 in->func_start = in->current;
488
Radek Krejci7931b192020-06-25 17:05:03 +0200489 switch (format) {
490 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200491 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200492 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200493 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200494 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200495 return lyd_parse_lyb_reply(request, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200496 case LYD_UNKNOWN:
497 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200498 }
499
Michal Vaskob7be7a82020-08-20 09:09:04 +0200500 LOGINT_RET(LYD_CTX(request));
Radek Krejci7931b192020-06-25 17:05:03 +0200501}
502
503API LY_ERR
504lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
505{
Radek Krejci1798aae2020-07-14 13:26:06 +0200506 LY_CHECK_ARG_RET(ctx, ctx, in, tree || ntf, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200507
508 format = lyd_parse_get_format(in, format);
509 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
510
Radek Krejci1798aae2020-07-14 13:26:06 +0200511 /* init */
512 if (tree) {
513 *tree = NULL;
514 }
515 if (ntf) {
516 *ntf = NULL;
517 }
518
Michal Vasko63f3d842020-07-08 10:10:14 +0200519 /* remember input position */
520 in->func_start = in->current;
521
Radek Krejci7931b192020-06-25 17:05:03 +0200522 switch (format) {
523 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200524 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200525 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200526 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200527 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200528 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200529 case LYD_UNKNOWN:
530 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200531 }
532
533 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200534}
Radek Krejci084289f2019-07-09 17:35:30 +0200535
Michal Vasko90932a92020-02-12 14:33:03 +0100536LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200537lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic, int value_hint,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200538 LY_PREFIX_FORMAT format, void *prefix_data, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100539{
540 LY_ERR ret;
541 struct lyd_node_term *term;
542
Michal Vasko9b368d32020-02-14 13:53:31 +0100543 assert(schema->nodetype & LYD_NODE_TERM);
544
Michal Vasko90932a92020-02-12 14:33:03 +0100545 term = calloc(1, sizeof *term);
546 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
547
548 term->schema = schema;
549 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100550 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100551
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200552 ret = lyd_value_parse(term, value, value_len, dynamic, 0, value_hint, format, prefix_data, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +0100553 if (ret && (ret != LY_EINCOMPLETE)) {
554 free(term);
555 return ret;
556 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100557 lyd_hash((struct lyd_node *)term);
558
559 *node = (struct lyd_node *)term;
560 return ret;
561}
562
563LY_ERR
564lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
565{
566 LY_ERR ret;
567 struct lyd_node_term *term;
568 struct lysc_type *type;
569
570 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200571 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100572
573 term = calloc(1, sizeof *term);
574 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
575
576 term->schema = schema;
577 term->prev = (struct lyd_node *)term;
578 term->flags = LYD_NEW;
579
580 type = ((struct lysc_node_leaf *)schema)->type;
581 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
582 if (ret) {
583 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
584 free(term);
585 return ret;
586 }
587 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100588
589 *node = (struct lyd_node *)term;
590 return ret;
591}
592
593LY_ERR
594lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
595{
596 struct lyd_node_inner *in;
597
Michal Vasko9b368d32020-02-14 13:53:31 +0100598 assert(schema->nodetype & LYD_NODE_INNER);
599
Michal Vasko90932a92020-02-12 14:33:03 +0100600 in = calloc(1, sizeof *in);
601 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
602
603 in->schema = schema;
604 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100605 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100606
Michal Vasko9b368d32020-02-14 13:53:31 +0100607 /* do not hash list with keys, we need them for the hash */
608 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
609 lyd_hash((struct lyd_node *)in);
610 }
Michal Vasko90932a92020-02-12 14:33:03 +0100611
612 *node = (struct lyd_node *)in;
613 return LY_SUCCESS;
614}
615
Michal Vasko90932a92020-02-12 14:33:03 +0100616LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200617lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100618{
619 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100620 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200621 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100622
Michal Vasko004d3152020-06-11 19:59:22 +0200623 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100624
625 /* create list */
626 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
627
Michal Vasko90932a92020-02-12 14:33:03 +0100628 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200629 LY_ARRAY_FOR(predicates, u) {
630 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100631 lyd_insert_node(list, NULL, key);
632 }
633
Michal Vasko9b368d32020-02-14 13:53:31 +0100634 /* hash having all the keys */
635 lyd_hash(list);
636
Michal Vasko90932a92020-02-12 14:33:03 +0100637 /* success */
638 *node = list;
639 list = NULL;
640
641cleanup:
642 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200643 return ret;
644}
645
646static LY_ERR
647lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
648{
649 LY_ERR ret = LY_SUCCESS;
650 struct lyxp_expr *expr = NULL;
651 uint16_t exp_idx = 0;
652 enum ly_path_pred_type pred_type = 0;
653 struct ly_path_predicate *predicates = NULL;
654
655 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200656 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
Michal Vasko004d3152020-06-11 19:59:22 +0200657 LY_PATH_PRED_KEYS, &expr), cleanup);
658
659 /* compile them */
Michal Vasko6b26e742020-07-17 15:02:10 +0200660 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200661 LY_PREF_JSON, NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200662
663 /* create the list node */
664 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
665
666cleanup:
667 lyxp_expr_free(schema->module->ctx, expr);
668 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100669 return ret;
670}
671
672LY_ERR
673lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
674{
675 struct lyd_node_any *any;
676
Michal Vasko9b368d32020-02-14 13:53:31 +0100677 assert(schema->nodetype & LYD_NODE_ANY);
678
Michal Vasko90932a92020-02-12 14:33:03 +0100679 any = calloc(1, sizeof *any);
680 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
681
682 any->schema = schema;
683 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100684 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100685
Radek Krejci1798aae2020-07-14 13:26:06 +0200686 /* TODO: convert XML/JSON strings into a opaq data tree */
687 any->value.str = value;
Michal Vasko90932a92020-02-12 14:33:03 +0100688 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100689 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100690
691 *node = (struct lyd_node *)any;
692 return LY_SUCCESS;
693}
694
Michal Vasko52927e22020-03-16 17:26:14 +0100695LY_ERR
696lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
Radek Krejci1798aae2020-07-14 13:26:06 +0200697 int *dynamic, int value_hint, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
698 const char *module_key, size_t module_key_len, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100699{
700 struct lyd_node_opaq *opaq;
701
Radek Krejci1798aae2020-07-14 13:26:06 +0200702 assert(ctx && name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100703
704 if (!value_len) {
705 value = "";
706 }
707
708 opaq = calloc(1, sizeof *opaq);
709 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
710
711 opaq->prev = (struct lyd_node *)opaq;
712
713 opaq->name = lydict_insert(ctx, name, name_len);
714 opaq->format = format;
715 if (pref_len) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200716 opaq->prefix.id = lydict_insert(ctx, prefix, pref_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100717 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200718 if (module_key_len) {
719 opaq->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
720 }
721
Michal Vasko52927e22020-03-16 17:26:14 +0100722 opaq->val_prefs = val_prefs;
723 if (dynamic && *dynamic) {
724 opaq->value = lydict_insert_zc(ctx, (char *)value);
725 *dynamic = 0;
726 } else {
727 opaq->value = lydict_insert(ctx, value, value_len);
728 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200729 opaq->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +0100730 opaq->ctx = ctx;
731
732 *node = (struct lyd_node *)opaq;
733 return LY_SUCCESS;
734}
735
Michal Vasko3a41dff2020-07-15 14:30:28 +0200736API LY_ERR
737lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100738{
739 struct lyd_node *ret = NULL;
740 const struct lysc_node *schema;
741 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
742
Michal Vasko6027eb92020-07-15 16:37:30 +0200743 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100744
Michal Vaskof03ed032020-03-04 13:31:44 +0100745 if (!module) {
746 module = parent->schema->module;
747 }
748
Michal Vasko3a41dff2020-07-15 14:30:28 +0200749 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
750 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
751 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100752
Michal Vasko3a41dff2020-07-15 14:30:28 +0200753 LY_CHECK_RET(lyd_create_inner(schema, &ret));
754 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100755 lyd_insert_node(parent, NULL, ret);
756 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200757
758 if (node) {
759 *node = ret;
760 }
761 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100762}
763
Michal Vasko3a41dff2020-07-15 14:30:28 +0200764API LY_ERR
765lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100766{
767 struct lyd_node *ret = NULL, *key;
768 const struct lysc_node *schema, *key_s;
769 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
770 va_list ap;
771 const char *key_val;
772 LY_ERR rc = LY_SUCCESS;
773
Michal Vasko6027eb92020-07-15 16:37:30 +0200774 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100775
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, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200781 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100782
783 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200784 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100785
Michal Vasko3a41dff2020-07-15 14:30:28 +0200786 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100787
788 /* create and insert all the keys */
789 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
790 key_val = va_arg(ap, const char *);
791
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200792 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, 0, LY_PREF_JSON, NULL, &key);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200793 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
794 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100795 lyd_insert_node(ret, NULL, key);
796 }
797
Michal Vasko013a8182020-03-03 10:46:53 +0100798 if (parent) {
799 lyd_insert_node(parent, NULL, ret);
800 }
801
802cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200803 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100804 if (rc) {
805 lyd_free_tree(ret);
806 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200807 } else if (node) {
808 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100809 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200810 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100811}
812
Michal Vasko3a41dff2020-07-15 14:30:28 +0200813API LY_ERR
814lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
815 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100816{
817 struct lyd_node *ret = NULL;
818 const struct lysc_node *schema;
819 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
820
Michal Vasko6027eb92020-07-15 16:37:30 +0200821 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100822
Michal Vaskof03ed032020-03-04 13:31:44 +0100823 if (!module) {
824 module = parent->schema->module;
825 }
Michal Vasko004d3152020-06-11 19:59:22 +0200826 if (!keys) {
827 keys = "";
828 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100829
Michal Vasko004d3152020-06-11 19:59:22 +0200830 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100831 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200832 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100833
Michal Vasko004d3152020-06-11 19:59:22 +0200834 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
835 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200836 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200837 } else {
838 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200839 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200840 }
Michal Vasko004d3152020-06-11 19:59:22 +0200841 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100842 lyd_insert_node(parent, NULL, ret);
843 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200844
845 if (node) {
846 *node = ret;
847 }
848 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100849}
850
Michal Vasko3a41dff2020-07-15 14:30:28 +0200851API LY_ERR
852lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
853 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100854{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200855 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100856 struct lyd_node *ret = NULL;
857 const struct lysc_node *schema;
858 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
859
Michal Vasko6027eb92020-07-15 16:37:30 +0200860 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100861
Michal Vaskof03ed032020-03-04 13:31:44 +0100862 if (!module) {
863 module = parent->schema->module;
864 }
865
Michal Vasko013a8182020-03-03 10:46:53 +0100866 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200867 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100868
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200869 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, 0, LY_PREF_JSON, NULL, &ret);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200870 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200871 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100872 lyd_insert_node(parent, NULL, ret);
873 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200874
875 if (node) {
876 *node = ret;
877 }
878 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100879}
880
Michal Vasko3a41dff2020-07-15 14:30:28 +0200881API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100882lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200883 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100884{
885 struct lyd_node *ret = NULL;
886 const struct lysc_node *schema;
887 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
888
Michal Vasko6027eb92020-07-15 16:37:30 +0200889 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100890
Michal Vaskof03ed032020-03-04 13:31:44 +0100891 if (!module) {
892 module = parent->schema->module;
893 }
894
Michal Vasko013a8182020-03-03 10:46:53 +0100895 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200896 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100897
Michal Vasko3a41dff2020-07-15 14:30:28 +0200898 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
899 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100900 lyd_insert_node(parent, NULL, ret);
901 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200902
903 if (node) {
904 *node = ret;
905 }
906 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100907}
908
Michal Vasko4490d312020-06-16 13:08:55 +0200909/**
910 * @brief Update node value.
911 *
912 * @param[in] node Node to update.
913 * @param[in] value New value to set.
914 * @param[in] value_type Type of @p value for any node.
915 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
916 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
917 * @return LY_ERR value.
918 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200919static LY_ERR
920lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
921 struct lyd_node **new_parent, struct lyd_node **new_node)
922{
923 LY_ERR ret = LY_SUCCESS;
924 struct lyd_node *new_any;
925
926 switch (node->schema->nodetype) {
927 case LYS_CONTAINER:
928 case LYS_NOTIF:
929 case LYS_RPC:
930 case LYS_ACTION:
931 case LYS_LIST:
932 case LYS_LEAFLIST:
933 /* if it exists, there is nothing to update */
934 *new_parent = NULL;
935 *new_node = NULL;
936 break;
937 case LYS_LEAF:
938 ret = lyd_change_term(node, value);
939 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
940 /* there was an actual change (at least of the default flag) */
941 *new_parent = node;
942 *new_node = node;
943 ret = LY_SUCCESS;
944 } else if (ret == LY_ENOT) {
945 /* no change */
946 *new_parent = NULL;
947 *new_node = NULL;
948 ret = LY_SUCCESS;
949 } /* else error */
950 break;
951 case LYS_ANYDATA:
952 case LYS_ANYXML:
953 /* create a new any node */
954 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
955
956 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200957 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200958 /* not equal, switch values (so that we can use generic node free) */
959 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
960 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
961 ((struct lyd_node_any *)node)->value.str = value;
962 ((struct lyd_node_any *)node)->value_type = value_type;
963
964 *new_parent = node;
965 *new_node = node;
966 } else {
967 /* they are equal */
968 *new_parent = NULL;
969 *new_node = NULL;
970 }
971 lyd_free_tree(new_any);
972 break;
973 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200974 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200975 ret = LY_EINT;
976 break;
977 }
978
979 return ret;
980}
981
Michal Vasko3a41dff2020-07-15 14:30:28 +0200982API LY_ERR
983lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
984 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200985{
986 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200987 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200988 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +0200989 size_t pref_len, name_len;
990
Michal Vasko3a41dff2020-07-15 14:30:28 +0200991 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200992
Michal Vaskob7be7a82020-08-20 09:09:04 +0200993 ctx = LYD_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200994
995 /* parse the name */
996 tmp = name;
997 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
998 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200999 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +02001000 }
1001
1002 /* find the module */
1003 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001004 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001005 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), LY_ENOTFOUND);
Michal Vaskod86997b2020-05-26 15:19:54 +02001006 }
1007
1008 /* set value if none */
1009 if (!val_str) {
1010 val_str = "";
1011 }
1012
Radek Krejci1798aae2020-07-14 13:26:06 +02001013 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, 0,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001014 LY_PREF_JSON, NULL, parent->schema));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001015
1016 if (meta) {
1017 *meta = ret;
1018 }
1019 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +02001020}
1021
Michal Vasko3a41dff2020-07-15 14:30:28 +02001022API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001023lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +02001024 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001025{
1026 struct lyd_node *ret = NULL;
1027
Michal Vasko6027eb92020-07-15 16:37:30 +02001028 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001029
1030 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001031 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001032 }
1033 if (!value) {
1034 value = "";
1035 }
1036
Radek Krejci1798aae2020-07-14 13:26:06 +02001037 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, 0,
1038 LYD_JSON, NULL, NULL, 0, module_name, strlen(module_name), &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001039 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001040 lyd_insert_node(parent, NULL, ret);
1041 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001042
1043 if (node) {
1044 *node = ret;
1045 }
1046 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001047}
1048
Michal Vasko3a41dff2020-07-15 14:30:28 +02001049API LY_ERR
1050lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci1798aae2020-07-14 13:26:06 +02001051 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001052{
Radek Krejci1798aae2020-07-14 13:26:06 +02001053 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001054 const struct ly_ctx *ctx;
1055 const char *prefix, *tmp;
1056 size_t pref_len, name_len;
1057
Michal Vasko3a41dff2020-07-15 14:30:28 +02001058 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001059
Michal Vaskob7be7a82020-08-20 09:09:04 +02001060 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001061
1062 /* parse the name */
1063 tmp = name;
1064 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1065 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001066 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001067 }
1068
1069 /* set value if none */
1070 if (!val_str) {
1071 val_str = "";
1072 }
1073
Radek Krejci1798aae2020-07-14 13:26:06 +02001074 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, 0, LYD_JSON, NULL,
1075 prefix, pref_len, module_name, module_name ? strlen(module_name) : 0));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001076
1077 if (attr) {
1078 *attr = ret;
1079 }
1080 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001081}
1082
1083API LY_ERR
1084lyd_change_term(struct lyd_node *term, const char *val_str)
1085{
1086 LY_ERR ret = LY_SUCCESS;
1087 struct lysc_type *type;
1088 struct lyd_node_term *t;
1089 struct lyd_node *parent;
1090 struct lyd_value val = {0};
1091 int dflt_change, val_change;
1092
1093 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1094
1095 if (!val_str) {
1096 val_str = "";
1097 }
1098 t = (struct lyd_node_term *)term;
1099 type = ((struct lysc_node_leaf *)term->schema)->type;
1100
1101 /* parse the new value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001102 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, LY_PREF_JSON, NULL), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001103
1104 /* compare original and new value */
1105 if (type->plugin->compare(&t->value, &val)) {
1106 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001107 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001108 t->value = val;
1109 memset(&val, 0, sizeof val);
1110 val_change = 1;
1111 } else {
1112 val_change = 0;
1113 }
1114
1115 /* always clear the default flag */
1116 if (term->flags & LYD_DEFAULT) {
1117 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1118 parent->flags &= ~LYD_DEFAULT;
1119 }
1120 dflt_change = 1;
1121 } else {
1122 dflt_change = 0;
1123 }
1124
1125 if (val_change || dflt_change) {
1126 /* make the node non-validated */
1127 term->flags &= LYD_NEW;
1128 }
1129
1130 if (val_change) {
1131 if (term->schema->nodetype == LYS_LEAFLIST) {
1132 /* leaf-list needs to be hashed again and re-inserted into parent */
1133 lyd_unlink_hash(term);
1134 lyd_hash(term);
1135 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1136 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1137 /* list needs to be updated if its key was changed */
1138 assert(term->parent->schema->nodetype == LYS_LIST);
1139 lyd_unlink_hash((struct lyd_node *)term->parent);
1140 lyd_hash((struct lyd_node *)term->parent);
1141 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1142 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1143 }
1144
1145 /* retrun value */
1146 if (!val_change) {
1147 if (dflt_change) {
1148 /* only default flag change */
1149 ret = LY_EEXIST;
1150 } else {
1151 /* no change */
1152 ret = LY_ENOT;
1153 }
1154 } /* else value changed, LY_SUCCESS */
1155
1156cleanup:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001157 type->plugin->free(LYD_CTX(term), &val);
Michal Vasko00cbf532020-06-15 13:58:47 +02001158 return ret;
1159}
1160
Michal Vasko41586352020-07-13 13:54:25 +02001161API LY_ERR
1162lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1163{
1164 LY_ERR ret = LY_SUCCESS;
1165 struct lyd_meta *m2;
1166 struct lyd_value val;
1167 int val_change;
1168
1169 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1170
1171 if (!val_str) {
1172 val_str = "";
1173 }
1174
1175 /* parse the new value into a new meta structure */
1176 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001177 strlen(val_str), NULL, 0, LY_PREF_JSON, NULL, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001178
1179 /* compare original and new value */
1180 if (lyd_compare_meta(meta, m2)) {
1181 /* values differ, switch them */
1182 val = meta->value;
1183 meta->value = m2->value;
1184 m2->value = val;
1185 val_change = 1;
1186 } else {
1187 val_change = 0;
1188 }
1189
1190 /* retrun value */
1191 if (!val_change) {
1192 /* no change */
1193 ret = LY_ENOT;
1194 } /* else value changed, LY_SUCCESS */
1195
1196cleanup:
1197 return ret;
1198}
1199
Michal Vasko3a41dff2020-07-15 14:30:28 +02001200API LY_ERR
1201lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options,
1202 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001203{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001204 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001205}
1206
1207API LY_ERR
1208lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1209 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1210{
1211 LY_ERR ret = LY_SUCCESS, r;
1212 struct lyxp_expr *exp = NULL;
1213 struct ly_path *p = NULL;
1214 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1215 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001216 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001217 struct ly_path_predicate *pred;
1218
1219 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1220
1221 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001222 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001223 }
1224
1225 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001226 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
Michal Vasko00cbf532020-06-15 13:58:47 +02001227 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1228
1229 /* compile path */
1230 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1231 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001232 LY_PATH_TARGET_MANY, LY_PREF_JSON, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001233
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001234 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1235 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001236 && !(options & LYD_NEWOPT_OPAQ)) {
1237 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1238 lys_nodetype2str(schema->nodetype), schema->name);
1239 ret = LY_EINVAL;
1240 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001241 } 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 +02001242 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001243 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1244 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001245
1246 if (!value) {
1247 value = "";
1248 }
1249
1250 r = LY_SUCCESS;
1251 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001252 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001253 }
1254 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001255 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, LY_PREF_JSON, NULL), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001256 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1257 }
1258
1259 /* try to find any existing nodes in the path */
1260 if (parent) {
1261 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1262 if (ret == LY_SUCCESS) {
1263 /* the node exists, are we supposed to update it or is it just a default? */
1264 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1265 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1266 ret = LY_EEXIST;
1267 goto cleanup;
1268 }
1269
1270 /* update the existing node */
1271 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1272 goto cleanup;
1273 } else if (ret == LY_EINCOMPLETE) {
1274 /* some nodes were found, adjust the iterator to the next segment */
1275 ++path_idx;
1276 } else if (ret == LY_ENOTFOUND) {
1277 /* 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 +02001278 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001279 node = parent;
1280 }
1281 } else {
1282 /* error */
1283 goto cleanup;
1284 }
1285 }
1286
1287 /* create all the non-existing nodes in a loop */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001288 for (; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001289 cur_parent = node;
1290 schema = p[path_idx].node;
1291
1292 switch (schema->nodetype) {
1293 case LYS_LIST:
1294 if (!(schema->flags & LYS_KEYLESS)) {
1295 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1296 /* creating opaque list without keys */
Radek Krejci1798aae2020-07-14 13:26:06 +02001297 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1298 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1299 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001300 } else {
1301 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1302 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1303 }
1304 break;
1305 }
1306 /* fallthrough */
1307 case LYS_CONTAINER:
1308 case LYS_NOTIF:
1309 case LYS_RPC:
1310 case LYS_ACTION:
1311 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1312 break;
1313 case LYS_LEAFLIST:
1314 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1315 /* creating opaque leaf-list without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001316 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1317 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1318 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001319 } else {
1320 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1321 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1322 }
1323 break;
1324 case LYS_LEAF:
1325 /* make there is some value */
1326 if (!value) {
1327 value = "";
1328 }
1329
1330 r = LY_SUCCESS;
1331 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001332 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001333 }
1334 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001335 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, 0, LY_PREF_JSON, NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001336 } else {
1337 /* creating opaque leaf without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001338 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001339 LYD_JSON, NULL, NULL, 0, schema->module->name,
1340 strlen(schema->module->name), &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001341 }
1342 break;
1343 case LYS_ANYDATA:
1344 case LYS_ANYXML:
1345 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1346 break;
1347 default:
1348 LOGINT(ctx);
1349 ret = LY_EINT;
1350 goto cleanup;
1351 }
1352
1353 if (cur_parent) {
1354 /* connect to the parent */
1355 lyd_insert_node(cur_parent, NULL, node);
1356 } else if (parent) {
1357 /* connect to top-level siblings */
1358 lyd_insert_node(NULL, &parent, node);
1359 }
1360
1361 /* update remembered nodes */
1362 if (!nparent) {
1363 nparent = node;
1364 }
1365 nnode = node;
1366 }
1367
1368cleanup:
1369 lyxp_expr_free(ctx, exp);
1370 ly_path_free(ctx, p);
1371 if (!ret) {
1372 /* set out params only on success */
1373 if (new_parent) {
1374 *new_parent = nparent;
1375 }
1376 if (new_node) {
1377 *new_node = nnode;
1378 }
1379 }
1380 return ret;
1381}
1382
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001383LY_ERR
1384lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
1385 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, int impl_opts,
1386 struct lyd_node **diff)
1387{
1388 LY_ERR ret;
1389 const struct lysc_node *iter = NULL;
1390 struct lyd_node *node;
1391 struct lyd_value **dflts;
1392 LY_ARRAY_COUNT_TYPE u;
1393
1394 assert(first && (parent || sparent || mod));
1395
1396 if (!sparent && parent) {
1397 sparent = parent->schema;
1398 }
1399
1400 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
1401 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1402 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001403 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1404 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001405 }
1406
1407 switch (iter->nodetype) {
1408 case LYS_CHOICE:
1409 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
1410 /* create default case data */
1411 LY_CHECK_RET(lyd_new_implicit_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
1412 NULL, node_types, node_when, impl_opts, diff));
1413 }
1414 break;
1415 case LYS_CONTAINER:
1416 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1417 /* create default NP container */
1418 LY_CHECK_RET(lyd_create_inner(iter, &node));
1419 node->flags = LYD_DEFAULT;
1420 lyd_insert_node(parent, first, node);
1421
1422 /* cannot be a NP container with when */
1423 assert(!iter->when);
1424
1425 /* create any default children */
1426 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
1427 impl_opts, diff));
1428 }
1429 break;
1430 case LYS_LEAF:
1431 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt
1432 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1433 /* create default leaf */
1434 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1435 if (ret == LY_EINCOMPLETE) {
1436 if (node_types) {
1437 /* remember to resolve type */
1438 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1439 }
1440 } else if (ret) {
1441 return ret;
1442 }
1443 node->flags = LYD_DEFAULT;
1444 lyd_insert_node(parent, first, node);
1445
1446 if (iter->when && node_when) {
1447 /* remember to resolve when */
1448 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1449 }
1450 if (diff) {
1451 /* add into diff */
1452 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1453 }
1454 }
1455 break;
1456 case LYS_LEAFLIST:
1457 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts
1458 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1459 /* create all default leaf-lists */
1460 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1461 LY_ARRAY_FOR(dflts, u) {
1462 ret = lyd_create_term2(iter, dflts[u], &node);
1463 if (ret == LY_EINCOMPLETE) {
1464 if (node_types) {
1465 /* remember to resolve type */
1466 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1467 }
1468 } else if (ret) {
1469 return ret;
1470 }
1471 node->flags = LYD_DEFAULT;
1472 lyd_insert_node(parent, first, node);
1473
1474 if (iter->when && node_when) {
1475 /* remember to resolve when */
1476 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1477 }
1478 if (diff) {
1479 /* add into diff */
1480 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1481 }
1482 }
1483 }
1484 break;
1485 default:
1486 /* without defaults */
1487 break;
1488 }
1489 }
1490
1491 return LY_SUCCESS;
1492}
1493
1494API LY_ERR
1495lyd_new_implicit_tree(struct lyd_node *tree, int implicit_options, struct lyd_node **diff)
1496{
Michal Vasko56daf732020-08-10 10:57:18 +02001497 struct lyd_node *node;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001498 LY_ERR ret = LY_SUCCESS;
1499
1500 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1501 if (diff) {
1502 *diff = NULL;
1503 }
1504
Michal Vasko56daf732020-08-10 10:57:18 +02001505 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001506 /* skip added default nodes */
1507 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW))
1508 && (node->schema->nodetype & LYD_NODE_INNER)) {
1509 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
1510 NULL, implicit_options, diff), cleanup);
1511 }
1512
Michal Vasko56daf732020-08-10 10:57:18 +02001513 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001514 }
1515
1516cleanup:
1517 if (ret && diff) {
1518 lyd_free_all(*diff);
1519 *diff = NULL;
1520 }
1521 return ret;
1522}
1523
1524API LY_ERR
1525lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, int implicit_options, struct lyd_node **diff)
1526{
1527 const struct lys_module *mod;
1528 struct lyd_node *d = NULL;
1529 uint32_t i = 0;
1530 LY_ERR ret = LY_SUCCESS;
1531
1532 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1533 if (diff) {
1534 *diff = NULL;
1535 }
1536 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001537 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001538 }
1539
1540 /* add nodes for each module one-by-one */
1541 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1542 if (!mod->implemented) {
1543 continue;
1544 }
1545
1546 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1547 if (d) {
1548 /* merge into one diff */
1549 lyd_insert_sibling(*diff, d, diff);
1550
1551 d = NULL;
1552 }
1553 }
1554
1555cleanup:
1556 if (ret && diff) {
1557 lyd_free_all(*diff);
1558 *diff = NULL;
1559 }
1560 return ret;
1561}
1562
1563API LY_ERR
1564lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, int implicit_options, struct lyd_node **diff)
1565{
1566 struct lyd_node *root, *d = NULL;
1567 LY_ERR ret = LY_SUCCESS;
1568
1569 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1570 if (diff) {
1571 *diff = NULL;
1572 }
1573
1574 /* add all top-level defaults for this module */
1575 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup);
1576
1577 /* process nested nodes */
1578 LY_LIST_FOR(*tree, root) {
1579 /* skip added default nodes */
1580 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1581 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1582
1583 if (d) {
1584 /* merge into one diff */
1585 lyd_insert_sibling(*diff, d, diff);
1586
1587 d = NULL;
1588 }
1589 }
1590 }
1591
1592cleanup:
1593 if (ret && diff) {
1594 lyd_free_all(*diff);
1595 *diff = NULL;
1596 }
1597 return ret;
1598}
1599
Michal Vasko90932a92020-02-12 14:33:03 +01001600struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001601lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001602{
Michal Vaskob104f112020-07-17 09:54:54 +02001603 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001604 struct lyd_node *match = NULL;
Michal Vaskob104f112020-07-17 09:54:54 +02001605 int found;
Michal Vasko90932a92020-02-12 14:33:03 +01001606
Michal Vaskob104f112020-07-17 09:54:54 +02001607 assert(new_node);
1608
1609 if (!first_sibling || !new_node->schema) {
1610 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001611 return NULL;
1612 }
1613
Michal Vaskob104f112020-07-17 09:54:54 +02001614 if (first_sibling->parent && first_sibling->parent->children_ht) {
1615 /* find the anchor using hashes */
1616 sparent = first_sibling->parent->schema;
1617 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1618 while (schema) {
1619 /* keep trying to find the first existing instance of the closest following schema sibling,
1620 * otherwise return NULL - inserting at the end */
1621 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1622 break;
1623 }
1624
1625 schema = lys_getnext(schema, sparent, NULL, 0);
1626 }
1627 } else {
1628 /* find the anchor without hashes */
1629 match = (struct lyd_node *)first_sibling;
1630 if (!lysc_data_parent(new_node->schema)) {
1631 /* we are in top-level, skip all the data from preceding modules */
1632 LY_LIST_FOR(match, match) {
1633 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1634 break;
1635 }
1636 }
1637 }
1638
1639 /* get the first schema sibling */
1640 sparent = lysc_data_parent(new_node->schema);
1641 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1642
1643 found = 0;
1644 LY_LIST_FOR(match, match) {
1645 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1646 /* we have found an opaque node, which must be at the end, so use it OR
1647 * modules do not match, so we must have traversed all the data from new_node module (if any),
1648 * we have found the first node of the next module, that is what we want */
1649 break;
1650 }
1651
1652 /* skip schema nodes until we find the instantiated one */
1653 while (!found) {
1654 if (new_node->schema == schema) {
1655 /* we have found the schema of the new node, continue search to find the first
1656 * data node with a different schema (after our schema) */
1657 found = 1;
1658 break;
1659 }
1660 if (match->schema == schema) {
1661 /* current node (match) is a data node still before the new node, continue search in data */
1662 break;
1663 }
1664 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1665 assert(schema);
1666 }
1667
1668 if (found && (match->schema != new_node->schema)) {
1669 /* find the next node after we have found our node schema data instance */
1670 break;
1671 }
1672 }
Michal Vasko90932a92020-02-12 14:33:03 +01001673 }
1674
1675 return match;
1676}
1677
1678/**
1679 * @brief Insert node after a sibling.
1680 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001681 * Handles inserting into NP containers and key-less lists.
1682 *
Michal Vasko90932a92020-02-12 14:33:03 +01001683 * @param[in] sibling Sibling to insert after.
1684 * @param[in] node Node to insert.
1685 */
1686static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001687lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001688{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001689 struct lyd_node_inner *par;
1690
Michal Vasko90932a92020-02-12 14:33:03 +01001691 assert(!node->next && (node->prev == node));
1692
1693 node->next = sibling->next;
1694 node->prev = sibling;
1695 sibling->next = node;
1696 if (node->next) {
1697 /* sibling had a succeeding node */
1698 node->next->prev = node;
1699 } else {
1700 /* sibling was last, find first sibling and change its prev */
1701 if (sibling->parent) {
1702 sibling = sibling->parent->child;
1703 } else {
1704 for (; sibling->prev->next != node; sibling = sibling->prev);
1705 }
1706 sibling->prev = node;
1707 }
1708 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001709
Michal Vasko9f96a052020-03-10 09:41:45 +01001710 for (par = node->parent; par; par = par->parent) {
1711 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1712 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001713 par->flags &= ~LYD_DEFAULT;
1714 }
Michal Vaskob104f112020-07-17 09:54:54 +02001715 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001716 /* rehash key-less list */
1717 lyd_hash((struct lyd_node *)par);
1718 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001719 }
Michal Vasko90932a92020-02-12 14:33:03 +01001720}
1721
1722/**
1723 * @brief Insert node before a sibling.
1724 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001725 * Handles inserting into NP containers and key-less lists.
1726 *
Michal Vasko90932a92020-02-12 14:33:03 +01001727 * @param[in] sibling Sibling to insert before.
1728 * @param[in] node Node to insert.
1729 */
1730static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001731lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001732{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001733 struct lyd_node_inner *par;
1734
Michal Vasko90932a92020-02-12 14:33:03 +01001735 assert(!node->next && (node->prev == node));
1736
1737 node->next = sibling;
1738 /* covers situation of sibling being first */
1739 node->prev = sibling->prev;
1740 sibling->prev = node;
1741 if (node->prev->next) {
1742 /* sibling had a preceding node */
1743 node->prev->next = node;
1744 } else if (sibling->parent) {
1745 /* sibling was first and we must also change parent child pointer */
1746 sibling->parent->child = node;
1747 }
1748 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001749
Michal Vasko9f96a052020-03-10 09:41:45 +01001750 for (par = node->parent; par; par = par->parent) {
1751 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1752 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001753 par->flags &= ~LYD_DEFAULT;
1754 }
Michal Vaskob104f112020-07-17 09:54:54 +02001755 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001756 /* rehash key-less list */
1757 lyd_hash((struct lyd_node *)par);
1758 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001759 }
Michal Vasko90932a92020-02-12 14:33:03 +01001760}
1761
1762/**
Michal Vaskob104f112020-07-17 09:54:54 +02001763 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001764 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001765 * Handles inserting into NP containers and key-less lists.
1766 *
Michal Vasko90932a92020-02-12 14:33:03 +01001767 * @param[in] parent Parent to insert into.
1768 * @param[in] node Node to insert.
1769 */
1770static void
Michal Vaskob104f112020-07-17 09:54:54 +02001771lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001772{
1773 struct lyd_node_inner *par;
1774
Michal Vaskob104f112020-07-17 09:54:54 +02001775 assert(parent && !lyd_node_children(parent, 0) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001776 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001777
1778 par = (struct lyd_node_inner *)parent;
1779
Michal Vaskob104f112020-07-17 09:54:54 +02001780 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001781 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001782
Michal Vasko9f96a052020-03-10 09:41:45 +01001783 for (; par; par = par->parent) {
1784 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1785 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001786 par->flags &= ~LYD_DEFAULT;
1787 }
Michal Vasko52927e22020-03-16 17:26:14 +01001788 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001789 /* rehash key-less list */
1790 lyd_hash((struct lyd_node *)par);
1791 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001792 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001793}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001794
Michal Vasko751cb4d2020-07-14 12:25:28 +02001795/**
1796 * @brief Learn whether a list instance has all the keys.
1797 *
1798 * @param[in] list List instance to check.
1799 * @return non-zero if all the keys were found,
1800 * @return 0 otherwise.
1801 */
1802static int
1803lyd_insert_has_keys(const struct lyd_node *list)
1804{
1805 const struct lyd_node *key;
1806 const struct lysc_node *skey = NULL;
1807
1808 assert(list->schema->nodetype == LYS_LIST);
1809 key = lyd_node_children(list, 0);
1810 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1811 if (!key || (key->schema != skey)) {
1812 /* key missing */
1813 return 0;
1814 }
1815
1816 key = key->next;
1817 }
1818
1819 /* all keys found */
1820 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001821}
1822
1823void
Michal Vaskob104f112020-07-17 09:54:54 +02001824lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001825{
Michal Vaskob104f112020-07-17 09:54:54 +02001826 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001827
Michal Vaskob104f112020-07-17 09:54:54 +02001828 /* inserting list without its keys is not supported */
1829 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001830
Michal Vaskob104f112020-07-17 09:54:54 +02001831 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1832 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001833 }
Michal Vasko90932a92020-02-12 14:33:03 +01001834
Michal Vaskob104f112020-07-17 09:54:54 +02001835 /* get first sibling */
1836 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001837
Michal Vaskob104f112020-07-17 09:54:54 +02001838 /* find the anchor, our next node, so we can insert before it */
1839 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1840 if (anchor) {
1841 lyd_insert_before_node(anchor, node);
1842 } else if (first_sibling) {
1843 lyd_insert_after_node(first_sibling->prev, node);
1844 } else if (parent) {
1845 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001846 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001847 *first_sibling_p = node;
1848 }
1849
1850 /* insert into parent HT */
1851 lyd_insert_hash(node);
1852
1853 /* finish hashes for our parent, if needed and possible */
1854 if (node->schema && (node->schema->flags & LYS_KEY) && lyd_insert_has_keys(parent)) {
1855 lyd_hash(parent);
1856
1857 /* now we can insert even the list into its parent HT */
1858 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001859 }
Michal Vasko90932a92020-02-12 14:33:03 +01001860}
1861
Michal Vaskof03ed032020-03-04 13:31:44 +01001862static LY_ERR
1863lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1864{
1865 const struct lysc_node *par2;
1866
1867 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001868 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001869
1870 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001871 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001872
1873 if (parent) {
1874 /* inner node */
1875 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001876 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
1877 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001878 return LY_EINVAL;
1879 }
1880 } else {
1881 /* top-level node */
1882 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001883 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001884 return LY_EINVAL;
1885 }
1886 }
1887
1888 return LY_SUCCESS;
1889}
1890
1891API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001892lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001893{
1894 struct lyd_node *iter;
1895
Michal Vasko654bc852020-06-23 13:28:06 +02001896 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001897
1898 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1899
1900 if (node->schema->flags & LYS_KEY) {
1901 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1902 return LY_EINVAL;
1903 }
1904
1905 if (node->parent || node->prev->next) {
1906 lyd_unlink_tree(node);
1907 }
1908
1909 while (node) {
1910 iter = node->next;
1911 lyd_unlink_tree(node);
1912 lyd_insert_node(parent, NULL, node);
1913 node = iter;
1914 }
1915 return LY_SUCCESS;
1916}
1917
1918API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001919lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001920{
1921 struct lyd_node *iter;
1922
Michal Vaskob104f112020-07-17 09:54:54 +02001923 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001924
Michal Vaskob104f112020-07-17 09:54:54 +02001925 if (sibling) {
1926 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001927 }
1928
1929 if (node->parent || node->prev->next) {
1930 lyd_unlink_tree(node);
1931 }
1932
1933 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001934 if (node->schema->flags & LYS_KEY) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001935 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02001936 return LY_EINVAL;
1937 }
1938
Michal Vaskob1b5c262020-03-05 14:29:47 +01001939 iter = node->next;
1940 lyd_unlink_tree(node);
1941 lyd_insert_node(NULL, &sibling, node);
1942 node = iter;
1943 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001944
Michal Vaskob104f112020-07-17 09:54:54 +02001945 if (first) {
1946 /* find the first sibling */
1947 *first = sibling;
1948 while ((*first)->prev->next) {
1949 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001950 }
1951 }
1952
1953 return LY_SUCCESS;
1954}
1955
Michal Vaskob1b5c262020-03-05 14:29:47 +01001956API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001957lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1958{
1959 struct lyd_node *iter;
1960
1961 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1962
Michal Vasko62ed12d2020-05-21 10:08:25 +02001963 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001964
Michal Vaskob104f112020-07-17 09:54:54 +02001965 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001966 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001967 return LY_EINVAL;
1968 }
1969
1970 if (node->parent || node->prev->next) {
1971 lyd_unlink_tree(node);
1972 }
1973
1974 /* insert in reverse order to get the original order */
1975 node = node->prev;
1976 while (node) {
1977 iter = node->prev;
1978 lyd_unlink_tree(node);
1979
1980 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001981 lyd_insert_hash(node);
1982
Michal Vaskof03ed032020-03-04 13:31:44 +01001983 /* move the anchor accordingly */
1984 sibling = node;
1985
1986 node = (iter == node) ? NULL : iter;
1987 }
1988 return LY_SUCCESS;
1989}
1990
1991API LY_ERR
1992lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1993{
1994 struct lyd_node *iter;
1995
1996 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1997
Michal Vasko62ed12d2020-05-21 10:08:25 +02001998 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001999
Michal Vaskob104f112020-07-17 09:54:54 +02002000 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002001 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002002 return LY_EINVAL;
2003 }
2004
2005 if (node->parent || node->prev->next) {
2006 lyd_unlink_tree(node);
2007 }
2008
2009 while (node) {
2010 iter = node->next;
2011 lyd_unlink_tree(node);
2012
2013 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002014 lyd_insert_hash(node);
2015
Michal Vaskof03ed032020-03-04 13:31:44 +01002016 /* move the anchor accordingly */
2017 sibling = node;
2018
2019 node = iter;
2020 }
2021 return LY_SUCCESS;
2022}
2023
2024API void
2025lyd_unlink_tree(struct lyd_node *node)
2026{
2027 struct lyd_node *iter;
2028
2029 if (!node) {
2030 return;
2031 }
2032
Michal Vaskob104f112020-07-17 09:54:54 +02002033 /* update hashes while still linked into the tree */
2034 lyd_unlink_hash(node);
2035
Michal Vaskof03ed032020-03-04 13:31:44 +01002036 /* unlink from siblings */
2037 if (node->prev->next) {
2038 node->prev->next = node->next;
2039 }
2040 if (node->next) {
2041 node->next->prev = node->prev;
2042 } else {
2043 /* unlinking the last node */
2044 if (node->parent) {
2045 iter = node->parent->child;
2046 } else {
2047 iter = node->prev;
2048 while (iter->prev != node) {
2049 iter = iter->prev;
2050 }
2051 }
2052 /* update the "last" pointer from the first node */
2053 iter->prev = node->prev;
2054 }
2055
2056 /* unlink from parent */
2057 if (node->parent) {
2058 if (node->parent->child == node) {
2059 /* the node is the first child */
2060 node->parent->child = node->next;
2061 }
2062
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002063 /* check for NP container whether its last non-default node is not being unlinked */
2064 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER)
2065 && !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
2066 LY_LIST_FOR(node->parent->child, iter) {
2067 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2068 break;
2069 }
2070 }
2071 if (!iter) {
2072 node->parent->flags |= LYD_DEFAULT;
2073 }
2074 }
2075
Michal Vaskof03ed032020-03-04 13:31:44 +01002076 /* check for keyless list and update its hash */
2077 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002078 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002079 lyd_hash(iter);
2080 }
2081 }
2082
2083 node->parent = NULL;
2084 }
2085
2086 node->next = NULL;
2087 node->prev = node;
2088}
2089
Michal Vaskoa5da3292020-08-12 13:10:50 +02002090void
Radek Krejci1798aae2020-07-14 13:26:06 +02002091lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta)
2092{
2093 struct lyd_meta *last, *iter;
2094
2095 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002096
2097 if (!meta) {
2098 return;
2099 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002100
2101 for (iter = meta; iter; iter = iter->next) {
2102 iter->parent = parent;
2103 }
2104
2105 /* insert as the last attribute */
2106 if (parent->meta) {
2107 for (last = parent->meta; last->next; last = last->next);
2108 last->next = meta;
2109 } else {
2110 parent->meta = meta;
2111 }
2112
2113 /* remove default flags from NP containers */
2114 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2115 parent->flags &= ~LYD_DEFAULT;
2116 parent = (struct lyd_node *)parent->parent;
2117 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002118}
2119
2120LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002121lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002122 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, LY_PREFIX_FORMAT format,
2123 void *prefix_data, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01002124{
2125 LY_ERR ret;
2126 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002127 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002128 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002129
Michal Vasko9f96a052020-03-10 09:41:45 +01002130 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002131
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002132 LY_ARRAY_FOR(mod->compiled->exts, u) {
2133 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
2134 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002135 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002136 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002137 break;
2138 }
2139 }
2140 if (!ant) {
2141 /* attribute is not defined as a metadata annotation (RFC 7952) */
2142 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2143 mod->name, name_len, name);
2144 return LY_EINVAL;
2145 }
2146
Michal Vasko9f96a052020-03-10 09:41:45 +01002147 mt = calloc(1, sizeof *mt);
2148 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2149 mt->parent = parent;
2150 mt->annotation = ant;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002151 ret = lyd_value_parse_meta(mod->ctx, mt, value, value_len, dynamic, 0, value_hint, format, prefix_data, ctx_snode, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +01002152 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002153 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01002154 return ret;
2155 }
Michal Vasko9f96a052020-03-10 09:41:45 +01002156 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01002157
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002158 /* insert as the last attribute */
2159 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002160 lyd_insert_meta(parent, mt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002161 } else if (*meta) {
2162 for (last = *meta; last->next; last = last->next);
2163 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002164 }
2165
Michal Vasko9f96a052020-03-10 09:41:45 +01002166 if (meta) {
2167 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002168 }
2169 return ret;
2170}
2171
Michal Vaskoa5da3292020-08-12 13:10:50 +02002172void
2173lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2174{
2175 struct lyd_attr *last, *iter;
2176 struct lyd_node_opaq *opaq;
2177
2178 assert(parent && !parent->schema);
2179
2180 if (!attr) {
2181 return;
2182 }
2183
2184 opaq = (struct lyd_node_opaq *)parent;
2185 for (iter = attr; iter; iter = iter->next) {
2186 iter->parent = opaq;
2187 }
2188
2189 /* insert as the last attribute */
2190 if (opaq->attr) {
2191 for (last = opaq->attr; last->next; last = last->next);
2192 last->next = attr;
2193 } else {
2194 opaq->attr = attr;
2195 }
2196}
2197
Michal Vasko52927e22020-03-16 17:26:14 +01002198LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02002199lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name,
2200 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, LYD_FORMAT format,
2201 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len)
Michal Vasko52927e22020-03-16 17:26:14 +01002202{
Radek Krejci1798aae2020-07-14 13:26:06 +02002203 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002204
2205 assert(ctx && (parent || attr) && (!parent || !parent->schema));
2206 assert(name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002207
2208 if (!value_len) {
2209 value = "";
2210 }
2211
2212 at = calloc(1, sizeof *at);
2213 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
2214 at->parent = (struct lyd_node_opaq *)parent;
2215 at->name = lydict_insert(ctx, name, name_len);
2216 if (dynamic && *dynamic) {
2217 at->value = lydict_insert_zc(ctx, (char *)value);
2218 *dynamic = 0;
2219 } else {
2220 at->value = lydict_insert(ctx, value, value_len);
2221 }
2222
Radek Krejci1798aae2020-07-14 13:26:06 +02002223 at->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +01002224 at->format = format;
2225 at->val_prefs = val_prefs;
Radek Krejci1798aae2020-07-14 13:26:06 +02002226 if (prefix_len) {
2227 at->prefix.id = lydict_insert(ctx, prefix, prefix_len);
2228 }
2229 if (module_key_len) {
2230 at->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002231 }
2232
2233 /* insert as the last attribute */
2234 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002235 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002236 } else if (*attr) {
2237 for (last = *attr; last->next; last = last->next);
2238 last->next = at;
2239 }
2240
2241 if (attr) {
2242 *attr = at;
2243 }
2244 return LY_SUCCESS;
2245}
2246
Radek Krejci084289f2019-07-09 17:35:30 +02002247API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002248lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002249{
Michal Vasko004d3152020-06-11 19:59:22 +02002250 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002251
Michal Vasko004d3152020-06-11 19:59:22 +02002252 if (ly_path_eval(path, tree, &target)) {
2253 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002254 }
2255
Michal Vasko004d3152020-06-11 19:59:22 +02002256 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002257}
2258
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002259API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002260lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002261{
2262 const struct lyd_node *iter1, *iter2;
2263 struct lyd_node_term *term1, *term2;
2264 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002265 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002266 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002267
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002268 if (!node1 || !node2) {
2269 if (node1 == node2) {
2270 return LY_SUCCESS;
2271 } else {
2272 return LY_ENOT;
2273 }
2274 }
2275
Michal Vaskob7be7a82020-08-20 09:09:04 +02002276 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002277 return LY_ENOT;
2278 }
2279
2280 if (node1->hash != node2->hash) {
2281 return LY_ENOT;
2282 }
Michal Vasko52927e22020-03-16 17:26:14 +01002283 /* 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 +02002284
Michal Vasko52927e22020-03-16 17:26:14 +01002285 if (!node1->schema) {
2286 opaq1 = (struct lyd_node_opaq *)node1;
2287 opaq2 = (struct lyd_node_opaq *)node2;
Radek Krejci1798aae2020-07-14 13:26:06 +02002288 if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002289 return LY_ENOT;
2290 }
Michal Vasko52927e22020-03-16 17:26:14 +01002291 switch (opaq1->format) {
2292 case LYD_XML:
2293 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
2294 return LY_ENOT;
2295 }
2296 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02002297 case LYD_JSON:
2298 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2299 if (strcmp(opaq1->value, opaq2->value)) {
2300 return LY_ENOT;
2301 }
2302 break;
Michal Vasko60ea6352020-06-29 13:39:39 +02002303 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002304 case LYD_UNKNOWN:
Michal Vasko52927e22020-03-16 17:26:14 +01002305 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002306 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002307 return LY_EINT;
2308 }
2309 if (options & LYD_COMPARE_FULL_RECURSION) {
2310 iter1 = opaq1->child;
2311 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002312 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002313 }
2314 return LY_SUCCESS;
2315 } else {
2316 switch (node1->schema->nodetype) {
2317 case LYS_LEAF:
2318 case LYS_LEAFLIST:
2319 if (options & LYD_COMPARE_DEFAULTS) {
2320 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2321 return LY_ENOT;
2322 }
2323 }
2324
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002325 term1 = (struct lyd_node_term *)node1;
2326 term2 = (struct lyd_node_term *)node2;
2327 if (term1->value.realtype != term2->value.realtype) {
2328 return LY_ENOT;
2329 }
Michal Vasko52927e22020-03-16 17:26:14 +01002330
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002331 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002332 case LYS_CONTAINER:
2333 if (options & LYD_COMPARE_DEFAULTS) {
2334 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2335 return LY_ENOT;
2336 }
2337 }
2338 if (options & LYD_COMPARE_FULL_RECURSION) {
2339 iter1 = ((struct lyd_node_inner*)node1)->child;
2340 iter2 = ((struct lyd_node_inner*)node2)->child;
2341 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002342 }
2343 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002344 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002345 case LYS_ACTION:
2346 if (options & LYD_COMPARE_FULL_RECURSION) {
2347 /* TODO action/RPC
2348 goto all_children_compare;
2349 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002350 }
2351 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002352 case LYS_NOTIF:
2353 if (options & LYD_COMPARE_FULL_RECURSION) {
2354 /* TODO Notification
2355 goto all_children_compare;
2356 */
2357 }
2358 return LY_SUCCESS;
2359 case LYS_LIST:
2360 iter1 = ((struct lyd_node_inner*)node1)->child;
2361 iter2 = ((struct lyd_node_inner*)node2)->child;
2362
2363 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2364 /* lists with keys, their equivalence is based on their keys */
2365 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002366 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002367 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002368 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002369 return LY_ENOT;
2370 }
2371 iter1 = iter1->next;
2372 iter2 = iter2->next;
2373 }
2374 } else {
2375 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2376
2377 all_children_compare:
2378 if (!iter1 && !iter2) {
2379 /* no children, nothing to compare */
2380 return LY_SUCCESS;
2381 }
2382
2383 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002384 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002385 return LY_ENOT;
2386 }
2387 }
2388 if (iter1 || iter2) {
2389 return LY_ENOT;
2390 }
2391 }
2392 return LY_SUCCESS;
2393 case LYS_ANYXML:
2394 case LYS_ANYDATA:
2395 any1 = (struct lyd_node_any*)node1;
2396 any2 = (struct lyd_node_any*)node2;
2397
2398 if (any1->value_type != any2->value_type) {
2399 return LY_ENOT;
2400 }
2401 switch (any1->value_type) {
2402 case LYD_ANYDATA_DATATREE:
2403 iter1 = any1->value.tree;
2404 iter2 = any2->value.tree;
2405 goto all_children_compare;
2406 case LYD_ANYDATA_STRING:
2407 case LYD_ANYDATA_XML:
2408 case LYD_ANYDATA_JSON:
2409 len1 = strlen(any1->value.str);
2410 len2 = strlen(any2->value.str);
2411 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2412 return LY_ENOT;
2413 }
2414 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002415 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002416 len1 = lyd_lyb_data_length(any1->value.mem);
2417 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002418 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2419 return LY_ENOT;
2420 }
2421 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002422 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002423 }
2424 }
2425
Michal Vaskob7be7a82020-08-20 09:09:04 +02002426 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002427 return LY_EINT;
2428}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002429
Michal Vasko21725742020-06-29 11:49:25 +02002430API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002431lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options)
2432{
2433 for (; node1 && node2; node1 = node1->next, node2 = node2->next) {
2434 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2435 }
2436
Michal Vasko11a81432020-07-28 16:31:29 +02002437 if (node1 == node2) {
2438 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002439 }
Michal Vasko11a81432020-07-28 16:31:29 +02002440 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002441}
2442
2443API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002444lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2445{
2446 if (!meta1 || !meta2) {
2447 if (meta1 == meta2) {
2448 return LY_SUCCESS;
2449 } else {
2450 return LY_ENOT;
2451 }
2452 }
2453
Michal Vaskob7be7a82020-08-20 09:09:04 +02002454 if ((LYD_CTX(meta1->parent) != LYD_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002455 return LY_ENOT;
2456 }
2457
2458 if (meta1->value.realtype != meta2->value.realtype) {
2459 return LY_ENOT;
2460 }
2461
2462 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2463}
2464
Radek Krejci22ebdba2019-07-25 13:59:43 +02002465/**
Michal Vasko52927e22020-03-16 17:26:14 +01002466 * @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 +02002467 *
2468 * 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 +02002469 *
2470 * @param[in] node Original node to duplicate
2471 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2472 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2473 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2474 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2475 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002476 */
Michal Vasko52927e22020-03-16 17:26:14 +01002477static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002478lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2479 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002480{
Michal Vasko52927e22020-03-16 17:26:14 +01002481 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002482 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002483 struct lyd_meta *meta;
2484 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002485 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002486
Michal Vasko52927e22020-03-16 17:26:14 +01002487 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002488
Michal Vasko52927e22020-03-16 17:26:14 +01002489 if (!node->schema) {
2490 dup = calloc(1, sizeof(struct lyd_node_opaq));
2491 } else {
2492 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002493 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002494 case LYS_ACTION:
2495 case LYS_NOTIF:
2496 case LYS_CONTAINER:
2497 case LYS_LIST:
2498 dup = calloc(1, sizeof(struct lyd_node_inner));
2499 break;
2500 case LYS_LEAF:
2501 case LYS_LEAFLIST:
2502 dup = calloc(1, sizeof(struct lyd_node_term));
2503 break;
2504 case LYS_ANYDATA:
2505 case LYS_ANYXML:
2506 dup = calloc(1, sizeof(struct lyd_node_any));
2507 break;
2508 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002509 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002510 ret = LY_EINT;
2511 goto error;
2512 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002513 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002514 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002515
Michal Vaskof6df0a02020-06-16 13:08:34 +02002516 if (options & LYD_DUP_WITH_FLAGS) {
2517 dup->flags = node->flags;
2518 } else {
2519 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2520 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002521 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002522 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002523
Michal Vasko25a32822020-07-09 15:48:22 +02002524 /* duplicate metadata */
2525 if (!(options & LYD_DUP_NO_META)) {
2526 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002527 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002528 }
2529 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002530
2531 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002532 if (!dup->schema) {
2533 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2534 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2535 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002536
2537 if (options & LYD_DUP_RECURSIVE) {
2538 /* duplicate all the children */
2539 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002540 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002541 }
2542 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002543 opaq->name = lydict_insert(LYD_CTX(node), orig->name, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002544 opaq->format = orig->format;
Radek Krejci1798aae2020-07-14 13:26:06 +02002545 if (orig->prefix.id) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002546 opaq->prefix.id = lydict_insert(LYD_CTX(node), orig->prefix.id, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002547 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002548 opaq->prefix.module_ns = lydict_insert(LYD_CTX(node), orig->prefix.module_ns, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002549 if (orig->val_prefs) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002550 LY_ARRAY_CREATE_GOTO(LYD_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002551 LY_ARRAY_FOR(orig->val_prefs, u) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002552 opaq->val_prefs[u].id = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0);
2553 opaq->val_prefs[u].module_ns = lydict_insert(LYD_CTX(node), orig->val_prefs[u].module_ns, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002554 LY_ARRAY_INCREMENT(opaq->val_prefs);
2555 }
2556 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002557 opaq->value = lydict_insert(LYD_CTX(node), orig->value, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002558 opaq->ctx = orig->ctx;
2559 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2560 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2561 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2562
2563 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002564 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
2565 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002566 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2567 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2568 struct lyd_node *child;
2569
2570 if (options & LYD_DUP_RECURSIVE) {
2571 /* duplicate all the children */
2572 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002573 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002574 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002575 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002576 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002577 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002578 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002579 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002580 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002581 if (!child) {
2582 /* possibly not keys are present in filtered tree */
2583 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002584 } else if (child->schema != key) {
2585 /* possibly not all keys are present in filtered tree,
2586 * but there can be also some non-key nodes */
2587 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002588 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002589 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002590 child = child->next;
2591 }
2592 }
2593 lyd_hash(dup);
2594 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002595 dup->hash = node->hash;
2596 any = (struct lyd_node_any *)node;
2597 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002598 }
2599
Michal Vasko52927e22020-03-16 17:26:14 +01002600 /* insert */
2601 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002602
2603 if (dup_p) {
2604 *dup_p = dup;
2605 }
2606 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002607
2608error:
Michal Vasko52927e22020-03-16 17:26:14 +01002609 lyd_free_tree(dup);
2610 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002611}
2612
Michal Vasko3a41dff2020-07-15 14:30:28 +02002613static LY_ERR
2614lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
2615 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002616{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002617 const struct lyd_node_inner *orig_parent, *iter;
2618 int repeat = 1;
2619
2620 *dup_parent = NULL;
2621 *local_parent = NULL;
2622
2623 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2624 if (parent && (parent->schema == orig_parent->schema)) {
2625 /* stop creating parents, connect what we have into the provided parent */
2626 iter = parent;
2627 repeat = 0;
2628 } else {
2629 iter = NULL;
2630 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2631 (struct lyd_node **)&iter));
2632 }
2633 if (!*local_parent) {
2634 *local_parent = (struct lyd_node_inner *)iter;
2635 }
2636 if (iter->child) {
2637 /* 1) list - add after keys
2638 * 2) provided parent with some children */
2639 iter->child->prev->next = *dup_parent;
2640 if (*dup_parent) {
2641 (*dup_parent)->prev = iter->child->prev;
2642 iter->child->prev = *dup_parent;
2643 }
2644 } else {
2645 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2646 }
2647 if (*dup_parent) {
2648 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2649 }
2650 *dup_parent = (struct lyd_node *)iter;
2651 }
2652
2653 if (repeat && parent) {
2654 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002655 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko3a41dff2020-07-15 14:30:28 +02002656 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2657 return LY_EINVAL;
2658 }
2659
2660 return LY_SUCCESS;
2661}
2662
2663static LY_ERR
2664lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options, int nosiblings, struct lyd_node **dup)
2665{
2666 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002667 const struct lyd_node *orig; /* original node to be duplicated */
2668 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002669 struct lyd_node *top = NULL; /* the most higher created node */
2670 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002671
Michal Vasko3a41dff2020-07-15 14:30:28 +02002672 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002673
2674 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002675 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002676 } else {
2677 local_parent = parent;
2678 }
2679
Radek Krejci22ebdba2019-07-25 13:59:43 +02002680 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002681 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002682 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2683 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002684 break;
2685 }
2686 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002687
2688 /* rehash if needed */
2689 for (; local_parent; local_parent = local_parent->parent) {
2690 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2691 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002692 }
2693 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002694
2695 if (dup) {
2696 *dup = first;
2697 }
2698 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002699
2700error:
2701 if (top) {
2702 lyd_free_tree(top);
2703 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002704 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002705 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002706 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002707}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002708
Michal Vasko3a41dff2020-07-15 14:30:28 +02002709API LY_ERR
2710lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2711{
2712 return lyd_dup(node, parent, options, 1, dup);
2713}
2714
2715API LY_ERR
2716lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2717{
2718 return lyd_dup(node, parent, options, 0, dup);
2719}
2720
2721API LY_ERR
2722lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002723{
2724 LY_ERR ret;
2725 struct lyd_meta *mt, *last;
2726
Michal Vasko3a41dff2020-07-15 14:30:28 +02002727 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002728
2729 /* create a copy */
2730 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002731 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002732 mt->parent = node;
2733 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002734 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
2735 LY_CHECK_ERR_RET(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), ret);
2736 mt->name = lydict_insert(LYD_CTX(node), meta->name, 0);
Michal Vasko25a32822020-07-09 15:48:22 +02002737
2738 /* insert as the last attribute */
2739 if (node->meta) {
2740 for (last = node->meta; last->next; last = last->next);
2741 last->next = mt;
2742 } else {
2743 node->meta = mt;
2744 }
2745
Michal Vasko3a41dff2020-07-15 14:30:28 +02002746 if (dup) {
2747 *dup = mt;
2748 }
2749 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002750}
2751
Michal Vasko4490d312020-06-16 13:08:55 +02002752/**
2753 * @brief Merge a source sibling into target siblings.
2754 *
2755 * @param[in,out] first_trg First target sibling, is updated if top-level.
2756 * @param[in] parent_trg Target parent.
2757 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2758 * @param[in] options Merge options.
2759 * @return LY_ERR value.
2760 */
2761static LY_ERR
2762lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2763 int options)
2764{
2765 LY_ERR ret;
2766 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002767 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02002768 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002769
2770 sibling_src = *sibling_src_p;
2771 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2772 /* try to find the exact instance */
2773 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2774 } else {
2775 /* try to simply find the node, there cannot be more instances */
2776 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2777 }
2778
2779 if (!ret) {
2780 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002781 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002782 /* since they are different, they cannot both be default */
2783 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2784
Michal Vasko3a41dff2020-07-15 14:30:28 +02002785 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2786 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002787 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002788 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2789 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko4490d312020-06-16 13:08:55 +02002790 &((struct lyd_node_term *)match_trg)->value));
2791
2792 /* copy flags and add LYD_NEW */
2793 match_trg->flags = sibling_src->flags | LYD_NEW;
2794 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002795 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002796 /* update value */
2797 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
2798 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002799
2800 /* copy flags and add LYD_NEW */
2801 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002802 } else {
2803 /* check descendants, recursively */
Michal Vasko8ee464a2020-08-11 14:12:50 +02002804 LY_LIST_FOR_SAFE(LYD_CHILD_NO_KEYS(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002805 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2806 }
2807 }
2808 } else {
2809 /* node not found, merge it */
2810 if (options & LYD_MERGE_DESTRUCT) {
2811 dup_src = (struct lyd_node *)sibling_src;
2812 lyd_unlink_tree(dup_src);
2813 /* spend it */
2814 *sibling_src_p = NULL;
2815 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002816 LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &dup_src));
Michal Vasko4490d312020-06-16 13:08:55 +02002817 }
2818
2819 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02002820 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02002821 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02002822 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02002823 }
2824
2825 lyd_insert_node(parent_trg, first_trg, dup_src);
2826 }
2827
2828 return LY_SUCCESS;
2829}
2830
Michal Vasko3a41dff2020-07-15 14:30:28 +02002831static LY_ERR
2832lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options, int nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002833{
2834 const struct lyd_node *sibling_src, *tmp;
2835 int first;
2836
2837 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2838
2839 if (!source) {
2840 /* nothing to merge */
2841 return LY_SUCCESS;
2842 }
2843
2844 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002845 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002846 return LY_EINVAL;
2847 }
2848
2849 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2850 first = sibling_src == source ? 1 : 0;
2851 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2852 if (first && !sibling_src) {
2853 /* source was spent (unlinked), move to the next node */
2854 source = tmp;
2855 }
2856
Michal Vasko3a41dff2020-07-15 14:30:28 +02002857 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002858 break;
2859 }
2860 }
2861
2862 if (options & LYD_MERGE_DESTRUCT) {
2863 /* free any leftover source data that were not merged */
2864 lyd_free_siblings((struct lyd_node *)source);
2865 }
2866
2867 return LY_SUCCESS;
2868}
2869
Michal Vasko3a41dff2020-07-15 14:30:28 +02002870API LY_ERR
2871lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options)
2872{
2873 return lyd_merge(target, source, options, 1);
2874}
2875
2876API LY_ERR
2877lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options)
2878{
2879 return lyd_merge(target, source, options, 0);
2880}
2881
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002882static LY_ERR
2883lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2884{
Michal Vasko14654712020-02-06 08:35:21 +01002885 /* ending \0 */
2886 ++reqlen;
2887
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002888 if (reqlen > *buflen) {
2889 if (is_static) {
2890 return LY_EINCOMPLETE;
2891 }
2892
2893 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2894 if (!*buffer) {
2895 return LY_EMEM;
2896 }
2897
2898 *buflen = reqlen;
2899 }
2900
2901 return LY_SUCCESS;
2902}
2903
Michal Vaskod59035b2020-07-08 12:00:06 +02002904LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002905lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2906{
2907 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002908 size_t len;
2909 const char *val;
2910 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002911
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002912 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002913 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002914 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002915 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002916
2917 quot = '\'';
2918 if (strchr(val, '\'')) {
2919 quot = '"';
2920 }
2921 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002922 }
2923
2924 return LY_SUCCESS;
2925}
2926
2927/**
2928 * @brief Append leaf-list value predicate to path.
2929 *
2930 * @param[in] node Node to print.
2931 * @param[in,out] buffer Buffer to print to.
2932 * @param[in,out] buflen Current buffer length.
2933 * @param[in,out] bufused Current number of characters used in @p buffer.
2934 * @param[in] is_static Whether buffer is static or can be reallocated.
2935 * @return LY_ERR
2936 */
2937static LY_ERR
2938lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2939{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002940 size_t len;
2941 const char *val;
2942 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002943
Michal Vaskob7be7a82020-08-20 09:09:04 +02002944 val = LYD_CANON_VALUE(node);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002945 len = 4 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002946 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002947
2948 quot = '\'';
2949 if (strchr(val, '\'')) {
2950 quot = '"';
2951 }
2952 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2953
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002954 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002955}
2956
2957/**
2958 * @brief Append node position (relative to its other instances) predicate to path.
2959 *
2960 * @param[in] node Node to print.
2961 * @param[in,out] buffer Buffer to print to.
2962 * @param[in,out] buflen Current buffer length.
2963 * @param[in,out] bufused Current number of characters used in @p buffer.
2964 * @param[in] is_static Whether buffer is static or can be reallocated.
2965 * @return LY_ERR
2966 */
2967static LY_ERR
2968lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2969{
2970 const struct lyd_node *first, *iter;
2971 size_t len;
2972 int pos;
2973 char *val = NULL;
2974 LY_ERR rc;
2975
2976 if (node->parent) {
2977 first = node->parent->child;
2978 } else {
2979 for (first = node; node->prev->next; node = node->prev);
2980 }
2981 pos = 1;
2982 for (iter = first; iter != node; iter = iter->next) {
2983 if (iter->schema == node->schema) {
2984 ++pos;
2985 }
2986 }
2987 if (asprintf(&val, "%d", pos) == -1) {
2988 return LY_EMEM;
2989 }
2990
2991 len = 1 + strlen(val) + 1;
2992 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2993 if (rc != LY_SUCCESS) {
2994 goto cleanup;
2995 }
2996
2997 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2998
2999cleanup:
3000 free(val);
3001 return rc;
3002}
3003
3004API char *
3005lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3006{
Michal Vasko14654712020-02-06 08:35:21 +01003007 int is_static = 0, i, depth;
3008 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003009 const struct lyd_node *iter;
3010 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003011 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003012
3013 LY_CHECK_ARG_RET(NULL, node, NULL);
3014 if (buffer) {
3015 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3016 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003017 } else {
3018 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003019 }
3020
3021 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003022 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02003023 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003024 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003025 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3026 ++depth;
3027 }
3028
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003029 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003030 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003031 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01003032 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003033iter_print:
3034 /* print prefix and name */
3035 mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02003036 if (iter->schema && (!iter->parent || iter->schema->module != iter->parent->schema->module)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003037 mod = iter->schema->module;
3038 }
3039
3040 /* realloc string */
Radek Krejci1798aae2020-07-14 13:26:06 +02003041 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) : strlen(((struct lyd_node_opaq*)iter)->name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003042 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3043 if (rc != LY_SUCCESS) {
3044 break;
3045 }
3046
3047 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003048 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
3049 iter->schema ? iter->schema->name : ((struct lyd_node_opaq*)iter)->name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003050
Michal Vasko790b2bc2020-08-03 13:35:06 +02003051 /* do not always print the last (first) predicate */
Radek Krejci1798aae2020-07-14 13:26:06 +02003052 if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003053 switch (iter->schema->nodetype) {
3054 case LYS_LIST:
3055 if (iter->schema->flags & LYS_KEYLESS) {
3056 /* print its position */
3057 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3058 } else {
3059 /* print all list keys in predicates */
3060 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3061 }
3062 break;
3063 case LYS_LEAFLIST:
3064 if (iter->schema->flags & LYS_CONFIG_W) {
3065 /* print leaf-list value */
3066 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3067 } else {
3068 /* print its position */
3069 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3070 }
3071 break;
3072 default:
3073 /* nothing to print more */
3074 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003075 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003076 }
3077 if (rc != LY_SUCCESS) {
3078 break;
3079 }
3080
Michal Vasko14654712020-02-06 08:35:21 +01003081 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003082 }
3083 break;
3084 }
3085
3086 return buffer;
3087}
Michal Vaskoe444f752020-02-10 12:20:06 +01003088
Michal Vasko25a32822020-07-09 15:48:22 +02003089API struct lyd_meta *
3090lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3091{
3092 struct lyd_meta *ret = NULL;
3093 const struct ly_ctx *ctx;
3094 const char *prefix, *tmp;
3095 char *str;
3096 size_t pref_len, name_len;
3097
3098 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3099
3100 if (!first) {
3101 return NULL;
3102 }
3103
3104 ctx = first->annotation->module->ctx;
3105
3106 /* parse the name */
3107 tmp = name;
3108 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3109 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3110 return NULL;
3111 }
3112
3113 /* find the module */
3114 if (prefix) {
3115 str = strndup(prefix, pref_len);
3116 module = ly_ctx_get_module_latest(ctx, str);
3117 free(str);
3118 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3119 }
3120
3121 /* find the metadata */
3122 LY_LIST_FOR(first, first) {
3123 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3124 ret = (struct lyd_meta *)first;
3125 break;
3126 }
3127 }
3128
3129 return ret;
3130}
3131
Michal Vasko9b368d32020-02-14 13:53:31 +01003132API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003133lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3134{
3135 struct lyd_node **match_p;
3136 struct lyd_node_inner *parent;
3137
Michal Vaskof03ed032020-03-04 13:31:44 +01003138 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003139
Michal Vasko62ed12d2020-05-21 10:08:25 +02003140 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3141 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003142 if (match) {
3143 *match = NULL;
3144 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003145 return LY_ENOTFOUND;
3146 }
3147
3148 /* find first sibling */
3149 if (siblings->parent) {
3150 siblings = siblings->parent->child;
3151 } else {
3152 while (siblings->prev->next) {
3153 siblings = siblings->prev;
3154 }
3155 }
3156
3157 parent = (struct lyd_node_inner *)siblings->parent;
3158 if (parent && parent->children_ht) {
3159 assert(target->hash);
3160
3161 /* find by hash */
3162 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003163 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003164 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003165 siblings = *match_p;
3166 } else {
3167 siblings = NULL;
3168 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003169 } else {
3170 /* not found */
3171 siblings = NULL;
3172 }
3173 } else {
3174 /* no children hash table */
3175 for (; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003176 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003177 break;
3178 }
3179 }
3180 }
3181
3182 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003183 if (match) {
3184 *match = NULL;
3185 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003186 return LY_ENOTFOUND;
3187 }
3188
Michal Vasko9b368d32020-02-14 13:53:31 +01003189 if (match) {
3190 *match = (struct lyd_node *)siblings;
3191 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003192 return LY_SUCCESS;
3193}
3194
Michal Vasko90932a92020-02-12 14:33:03 +01003195static int
3196lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3197{
3198 struct lysc_node *val1;
3199 struct lyd_node *val2;
3200
3201 val1 = *((struct lysc_node **)val1_p);
3202 val2 = *((struct lyd_node **)val2_p);
3203
Michal Vasko90932a92020-02-12 14:33:03 +01003204 if (val1 == val2->schema) {
3205 /* schema match is enough */
3206 return 1;
3207 } else {
3208 return 0;
3209 }
3210}
3211
3212static LY_ERR
3213lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3214{
3215 struct lyd_node **match_p;
3216 struct lyd_node_inner *parent;
3217 uint32_t hash;
3218 values_equal_cb ht_cb;
3219
Michal Vaskob104f112020-07-17 09:54:54 +02003220 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003221
3222 parent = (struct lyd_node_inner *)siblings->parent;
3223 if (parent && parent->children_ht) {
3224 /* calculate our hash */
3225 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3226 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3227 hash = dict_hash_multi(hash, NULL, 0);
3228
3229 /* use special hash table function */
3230 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3231
3232 /* find by hash */
3233 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3234 siblings = *match_p;
3235 } else {
3236 /* not found */
3237 siblings = NULL;
3238 }
3239
3240 /* set the original hash table compare function back */
3241 lyht_set_cb(parent->children_ht, ht_cb);
3242 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003243 /* find first sibling */
3244 if (siblings->parent) {
3245 siblings = siblings->parent->child;
3246 } else {
3247 while (siblings->prev->next) {
3248 siblings = siblings->prev;
3249 }
3250 }
3251
3252 /* search manually without hashes */
Michal Vasko90932a92020-02-12 14:33:03 +01003253 for (; siblings; siblings = siblings->next) {
3254 if (siblings->schema == schema) {
3255 /* schema match is enough */
3256 break;
3257 }
3258 }
3259 }
3260
3261 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003262 if (match) {
3263 *match = NULL;
3264 }
Michal Vasko90932a92020-02-12 14:33:03 +01003265 return LY_ENOTFOUND;
3266 }
3267
Michal Vasko9b368d32020-02-14 13:53:31 +01003268 if (match) {
3269 *match = (struct lyd_node *)siblings;
3270 }
Michal Vasko90932a92020-02-12 14:33:03 +01003271 return LY_SUCCESS;
3272}
3273
Michal Vaskoe444f752020-02-10 12:20:06 +01003274API LY_ERR
3275lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
3276 size_t val_len, struct lyd_node **match)
3277{
3278 LY_ERR rc;
3279 struct lyd_node *target = NULL;
3280
Michal Vasko4c583e82020-07-17 12:16:14 +02003281 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003282
Michal Vasko62ed12d2020-05-21 10:08:25 +02003283 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3284 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003285 if (match) {
3286 *match = NULL;
3287 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003288 return LY_ENOTFOUND;
3289 }
3290
Michal Vaskof03ed032020-03-04 13:31:44 +01003291 if (key_or_value && !val_len) {
3292 val_len = strlen(key_or_value);
3293 }
3294
Michal Vaskob104f112020-07-17 09:54:54 +02003295 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3296 /* create a data node and find the instance */
3297 if (schema->nodetype == LYS_LEAFLIST) {
3298 /* target used attributes: schema, hash, value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003299 rc = lyd_create_term(schema, key_or_value, val_len, NULL, 0, LY_PREF_JSON, NULL, &target);
Michal Vaskob104f112020-07-17 09:54:54 +02003300 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3301 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003302 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003303 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003304 }
3305
3306 /* find it */
3307 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003308 } else {
3309 /* find the first schema node instance */
3310 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003311 }
3312
Michal Vaskoe444f752020-02-10 12:20:06 +01003313 lyd_free_tree(target);
3314 return rc;
3315}
Michal Vaskoccc02342020-05-21 10:09:21 +02003316
3317API LY_ERR
3318lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3319{
3320 LY_ERR ret = LY_SUCCESS;
3321 struct lyxp_set xp_set;
3322 struct lyxp_expr *exp;
3323 uint32_t i;
3324
3325 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3326
3327 memset(&xp_set, 0, sizeof xp_set);
3328
3329 /* compile expression */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003330 exp = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003331 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3332
3333 /* evaluate expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003334 ret = lyxp_eval(exp, LY_PREF_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
Michal Vaskoccc02342020-05-21 10:09:21 +02003335 LY_CHECK_GOTO(ret, cleanup);
3336
3337 /* allocate return set */
3338 *set = ly_set_new();
Michal Vaskob7be7a82020-08-20 09:09:04 +02003339 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003340
3341 /* transform into ly_set */
3342 if (xp_set.type == LYXP_SET_NODE_SET) {
3343 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3344 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003345 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003346 (*set)->size = xp_set.used;
3347
3348 for (i = 0; i < xp_set.used; ++i) {
3349 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3350 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3351 }
3352 }
3353 }
3354
3355cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003356 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003357 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Michal Vaskoccc02342020-05-21 10:09:21 +02003358 return ret;
3359}