Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2 | * @file tree_data.c |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 6cd9b6b | 2020-06-22 10:05:22 +0200 | [diff] [blame] | 4 | * @brief Data tree functions |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 6cd9b6b | 2020-06-22 10:05:22 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include <ctype.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 21 | #include <inttypes.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdarg.h> |
| 23 | #include <stdint.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include <string.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 29 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 30 | #include "context.h" |
| 31 | #include "dict.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 32 | #include "diff.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 33 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 34 | #include "in.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 35 | #include "in_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "log.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 37 | #include "parser_data.h" |
| 38 | #include "parser_internal.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 39 | #include "path.h" |
Radek Krejci | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 40 | #include "plugins.h" |
Radek Krejci | f1ca0ac | 2021-04-12 16:00:06 +0200 | [diff] [blame] | 41 | #include "plugins_exts/metadata.h" |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 42 | #include "plugins_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 43 | #include "plugins_types.h" |
| 44 | #include "set.h" |
| 45 | #include "tree.h" |
| 46 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 47 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 48 | #include "tree_schema.h" |
| 49 | #include "tree_schema_internal.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 50 | #include "validation.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 51 | #include "xml.h" |
| 52 | #include "xpath.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 53 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 54 | static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 55 | struct lyd_node **match); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 56 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 57 | LY_ERR |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 58 | lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 59 | size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 60 | const struct lysc_node *ctx_node, ly_bool *incomplete) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 61 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 62 | LY_ERR ret; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 63 | struct ly_err_item *err = NULL; |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 64 | uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 65 | |
Michal Vasko | 62ed865 | 2021-04-28 09:38:37 +0200 | [diff] [blame] | 66 | if (!value) { |
| 67 | value = ""; |
| 68 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 69 | if (incomplete) { |
| 70 | *incomplete = 0; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 73 | ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err); |
Radek Iša | 96b2de1 | 2021-02-16 10:32:57 +0100 | [diff] [blame] | 74 | if (dynamic) { |
| 75 | *dynamic = 0; |
| 76 | } |
| 77 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 78 | if (ret == LY_EINCOMPLETE) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 79 | if (incomplete) { |
| 80 | *incomplete = 1; |
| 81 | } |
| 82 | } else if (ret) { |
| 83 | if (err) { |
aPiecek | 4204b61 | 2021-05-03 09:06:16 +0200 | [diff] [blame] | 84 | LOGVAL_ERRITEM(ctx, err); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 85 | ly_err_free(err); |
| 86 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 87 | LOGVAL(ctx, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 88 | } |
| 89 | return ret; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 92 | return LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 95 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 96 | lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 97 | const struct lyd_node *ctx_node, const struct lyd_node *tree) |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 98 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 99 | LY_ERR ret; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 100 | struct ly_err_item *err = NULL; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 101 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 102 | assert(type->plugin->validate); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 103 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 104 | ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err); |
| 105 | if (ret) { |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 106 | if (err) { |
aPiecek | 4204b61 | 2021-05-03 09:06:16 +0200 | [diff] [blame] | 107 | LOGVAL_ERRITEM(ctx, err); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 108 | ly_err_free(err); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 109 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 110 | LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON, |
| 111 | NULL, NULL, NULL)); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 112 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 113 | return ret; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 116 | return LY_SUCCESS; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 119 | LY_ERR |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 120 | lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 121 | LY_VALUE_FORMAT format, void *prefix_data) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 122 | { |
| 123 | LY_ERR rc = LY_SUCCESS; |
| 124 | struct ly_err_item *err = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 125 | struct lyd_value storage; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 126 | struct lysc_type *type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 127 | |
| 128 | LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL); |
| 129 | |
| 130 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 131 | LOGARG(ctx, node); |
| 132 | return LY_EINVAL; |
| 133 | } |
| 134 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 135 | type = ((struct lysc_node_leaf *)node)->type; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 136 | rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 137 | LYD_HINT_SCHEMA, node, &storage, NULL, &err); |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 138 | if (rc == LY_EINCOMPLETE) { |
| 139 | /* actually success since we do not provide the context tree and call validation with |
| 140 | * LY_TYPE_OPTS_INCOMPLETE_DATA */ |
| 141 | rc = LY_SUCCESS; |
| 142 | } else if (rc && err) { |
| 143 | if (ctx) { |
| 144 | /* log only in case the ctx was provided as input parameter */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 145 | LOG_LOCSET(NULL, NULL, err->path, NULL); |
aPiecek | 4204b61 | 2021-05-03 09:06:16 +0200 | [diff] [blame] | 146 | LOGVAL_ERRITEM(ctx, err); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 147 | LOG_LOCBACK(0, 0, 1, 0); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 148 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 149 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 152 | if (!rc) { |
| 153 | type->plugin->free(ctx ? ctx : node->module->ctx, &storage); |
| 154 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 155 | return rc; |
| 156 | } |
| 157 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 158 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 159 | lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len, |
| 160 | const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 161 | { |
| 162 | LY_ERR rc; |
| 163 | struct ly_err_item *err = NULL; |
| 164 | struct lysc_type *type; |
Michal Vasko | 3701af5 | 2020-08-03 14:29:38 +0200 | [diff] [blame] | 165 | struct lyd_value val = {0}; |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 166 | ly_bool stored = 0, log = 1; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 167 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 168 | LY_CHECK_ARG_RET(ctx, schema, value, LY_EINVAL); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 169 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 170 | if (!ctx) { |
| 171 | ctx = schema->module->ctx; |
| 172 | log = 0; |
| 173 | } |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 174 | type = ((struct lysc_node_leaf *)schema)->type; |
| 175 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 176 | /* store */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 177 | rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL, |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 178 | LYD_HINT_DATA, schema, &val, NULL, &err); |
| 179 | if (!rc || (rc == LY_EINCOMPLETE)) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 180 | stored = 1; |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 181 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 182 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 183 | if (ctx_node && (rc == LY_EINCOMPLETE)) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 184 | /* resolve */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 185 | rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 188 | if (rc && (rc != LY_EINCOMPLETE) && err) { |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 189 | if (log) { |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 190 | /* log error */ |
| 191 | if (err->path) { |
| 192 | LOG_LOCSET(NULL, NULL, err->path, NULL); |
| 193 | } else if (ctx_node) { |
| 194 | LOG_LOCSET(NULL, ctx_node, NULL, NULL); |
| 195 | } else { |
| 196 | LOG_LOCSET(schema, NULL, NULL, NULL); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 197 | } |
aPiecek | 4204b61 | 2021-05-03 09:06:16 +0200 | [diff] [blame] | 198 | LOGVAL_ERRITEM(ctx, err); |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 199 | if (err->path) { |
| 200 | LOG_LOCBACK(0, 0, 1, 0); |
| 201 | } else if (ctx_node) { |
| 202 | LOG_LOCBACK(0, 1, 0, 0); |
| 203 | } else { |
| 204 | LOG_LOCBACK(1, 0, 0, 0); |
| 205 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 206 | } |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 207 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 210 | if (!rc || (rc == LY_EINCOMPLETE)) { |
| 211 | if (realtype) { |
| 212 | /* return realtype */ |
| 213 | if (val.realtype->basetype == LY_TYPE_UNION) { |
| 214 | *realtype = val.subvalue->value.realtype; |
| 215 | } else { |
| 216 | *realtype = val.realtype; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (canonical) { |
| 221 | /* return canonical value */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 222 | lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical); |
Michal Vasko | 29134f8 | 2020-11-13 18:03:20 +0100 | [diff] [blame] | 223 | } |
Michal Vasko | 3701af5 | 2020-08-03 14:29:38 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 226 | if (stored) { |
| 227 | /* free value */ |
| 228 | type->plugin->free(ctx ? ctx : schema->module->ctx, &val); |
| 229 | } |
| 230 | return rc; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 233 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 234 | lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 235 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 236 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 237 | struct ly_ctx *ctx; |
| 238 | struct lysc_type *type; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 239 | struct lyd_value val = {0}; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 240 | |
| 241 | LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL); |
| 242 | |
| 243 | ctx = node->schema->module->ctx; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 244 | type = ((struct lysc_node_leaf *)node->schema)->type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 245 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 246 | /* store the value */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 247 | LOG_LOCSET(node->schema, &node->node, NULL, NULL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 248 | ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 249 | LOG_LOCBACK(1, 1, 0, 0); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 250 | LY_CHECK_RET(ret); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 251 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 252 | /* compare values */ |
| 253 | ret = type->plugin->compare(&node->value, &val); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 254 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 255 | type->plugin->free(ctx, &val); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 259 | LIBYANG_API_DEF ly_bool |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 260 | lyd_is_default(const struct lyd_node *node) |
| 261 | { |
| 262 | const struct lysc_node_leaf *leaf; |
| 263 | const struct lysc_node_leaflist *llist; |
| 264 | const struct lyd_node_term *term; |
| 265 | LY_ARRAY_COUNT_TYPE u; |
| 266 | |
Michal Vasko | 39eb688 | 2021-09-16 08:53:13 +0200 | [diff] [blame] | 267 | if (!(node->schema->nodetype & LYD_NODE_TERM)) { |
| 268 | return 0; |
| 269 | } |
| 270 | |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 271 | term = (const struct lyd_node_term *)node; |
| 272 | |
| 273 | if (node->schema->nodetype == LYS_LEAF) { |
| 274 | leaf = (const struct lysc_node_leaf *)node->schema; |
| 275 | if (!leaf->dflt) { |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /* compare with the default value */ |
Michal Vasko | 39eb688 | 2021-09-16 08:53:13 +0200 | [diff] [blame] | 280 | if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) { |
| 281 | return 1; |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 282 | } |
| 283 | } else { |
| 284 | llist = (const struct lysc_node_leaflist *)node->schema; |
| 285 | if (!llist->dflts) { |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | LY_ARRAY_FOR(llist->dflts, u) { |
| 290 | /* compare with each possible default value */ |
Michal Vasko | 39eb688 | 2021-09-16 08:53:13 +0200 | [diff] [blame] | 291 | if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) { |
| 292 | return 1; |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
Michal Vasko | 39eb688 | 2021-09-16 08:53:13 +0200 | [diff] [blame] | 297 | return 0; |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 300 | static LYD_FORMAT |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 301 | lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 302 | { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 303 | if (!format && (in->type == LY_IN_FILEPATH)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 304 | /* unknown format - try to detect it from filename's suffix */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 305 | const char *path = in->method.fpath.filepath; |
| 306 | size_t len = strlen(path); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 307 | |
| 308 | /* ignore trailing whitespaces */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 309 | for ( ; len > 0 && isspace(path[len - 1]); len--) {} |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 310 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 311 | if ((len >= LY_XML_SUFFIX_LEN + 1) && |
| 312 | !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 313 | format = LYD_XML; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 314 | } else if ((len >= LY_JSON_SUFFIX_LEN + 1) && |
| 315 | !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 316 | format = LYD_JSON; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 317 | } else if ((len >= LY_LYB_SUFFIX_LEN + 1) && |
| 318 | !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 319 | format = LYD_LYB; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 320 | } /* else still unknown */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 323 | return format; |
| 324 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 325 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 326 | /** |
| 327 | * @brief Parse YANG data into a data tree. |
| 328 | * |
| 329 | * @param[in] ctx libyang context. |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 330 | * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 331 | * @param[in] parent Parent to connect the parsed nodes to, if any. |
| 332 | * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL. |
| 333 | * @param[in] in Input handle to read the input from. |
| 334 | * @param[in] format Expected format of the data in @p in. |
| 335 | * @param[in] parse_opts Options for parser. |
| 336 | * @param[in] val_opts Options for validation. |
| 337 | * @param[out] op Optional pointer to the parsed operation, if any. |
| 338 | * @return LY_ERR value. |
| 339 | */ |
| 340 | static LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 341 | lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p, |
| 342 | struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 343 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 344 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 345 | struct lyd_ctx *lydctx = NULL; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 346 | struct ly_set parsed = {0}; |
| 347 | struct lyd_node *first; |
| 348 | uint32_t i; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 349 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 350 | assert(ctx && (parent || first_p)); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 351 | |
| 352 | format = lyd_parse_get_format(in, format); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 353 | if (first_p) { |
| 354 | *first_p = NULL; |
| 355 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 356 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 357 | /* remember input position */ |
| 358 | in->func_start = in->current; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 359 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 360 | /* parse the data */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 361 | switch (format) { |
| 362 | case LYD_XML: |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 363 | rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, NULL, &parsed, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 364 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 365 | case LYD_JSON: |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 366 | rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 367 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 368 | case LYD_LYB: |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 369 | rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 370 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 371 | case LYD_UNKNOWN: |
Michal Vasko | d74978f | 2021-02-12 11:59:36 +0100 | [diff] [blame] | 372 | LOGARG(ctx, format); |
| 373 | rc = LY_EINVAL; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | LY_CHECK_GOTO(rc, cleanup); |
| 377 | |
| 378 | if (parent) { |
| 379 | /* get first top-level sibling */ |
| 380 | for (first = parent; first->parent; first = lyd_parent(first)) {} |
| 381 | first = lyd_first_sibling(first); |
| 382 | first_p = &first; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 385 | if (!(parse_opts & LYD_PARSE_ONLY)) { |
| 386 | /* validate data */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 387 | rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_exts, &lydctx->node_types, |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 388 | &lydctx->meta_types, NULL); |
| 389 | LY_CHECK_GOTO(rc, cleanup); |
| 390 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 391 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 392 | /* set the operation node */ |
| 393 | if (op) { |
| 394 | *op = lydctx->op_node; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 398 | if (lydctx) { |
| 399 | lydctx->free(lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 400 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 401 | if (rc) { |
| 402 | if (parent) { |
| 403 | /* free all the parsed subtrees */ |
| 404 | for (i = 0; i < parsed.count; ++i) { |
| 405 | lyd_free_tree(parsed.dnodes[i]); |
| 406 | } |
| 407 | } else { |
| 408 | /* free everything */ |
| 409 | lyd_free_all(*first_p); |
| 410 | *first_p = NULL; |
| 411 | } |
| 412 | } |
| 413 | ly_set_erase(&parsed, NULL); |
| 414 | return rc; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 417 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 418 | lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 419 | uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree) |
| 420 | { |
| 421 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 422 | |
| 423 | LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL); |
| 424 | LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL); |
| 425 | LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL); |
| 426 | |
| 427 | return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL); |
| 428 | } |
| 429 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 430 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 431 | lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 432 | uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree) |
| 433 | { |
| 434 | LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL); |
| 435 | LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL); |
| 436 | LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL); |
| 437 | |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 438 | return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 441 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 442 | lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options, |
| 443 | uint32_t validate_options, struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 444 | { |
| 445 | LY_ERR ret; |
| 446 | struct ly_in *in; |
| 447 | |
| 448 | LY_CHECK_RET(ly_in_new_memory(data, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 449 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 450 | |
| 451 | ly_in_free(in, 0); |
| 452 | return ret; |
| 453 | } |
| 454 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 455 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 456 | lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 457 | struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 458 | { |
| 459 | LY_ERR ret; |
| 460 | struct ly_in *in; |
| 461 | |
| 462 | LY_CHECK_RET(ly_in_new_fd(fd, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 463 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 464 | |
| 465 | ly_in_free(in, 0); |
| 466 | return ret; |
| 467 | } |
| 468 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 469 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 470 | lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options, |
| 471 | uint32_t validate_options, struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 472 | { |
| 473 | LY_ERR ret; |
| 474 | struct ly_in *in; |
| 475 | |
| 476 | LY_CHECK_RET(ly_in_new_filepath(path, 0, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 477 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 478 | |
| 479 | ly_in_free(in, 0); |
| 480 | return ret; |
| 481 | } |
| 482 | |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 483 | /** |
| 484 | * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching |
| 485 | * for schema nodes locked inside the extension instance. |
| 486 | * |
| 487 | * At least one of @p parent, @p tree, or @p op must always be set. |
| 488 | * |
| 489 | * Specific @p data_type values have different parameter meaning as follows: |
| 490 | * - ::LYD_TYPE_RPC_NETCONF: |
| 491 | * - @p parent - must be NULL, the whole RPC is expected; |
| 492 | * - @p format - must be ::LYD_XML, NETCONF supports only this format; |
| 493 | * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as |
| 494 | * a separate opaque data tree, even if the function fails, this may be returned; |
| 495 | * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation; |
| 496 | * |
| 497 | * - ::LYD_TYPE_NOTIF_NETCONF: |
| 498 | * - @p parent - must be NULL, the whole notification is expected; |
| 499 | * - @p format - must be ::LYD_XML, NETCONF supports only this format; |
| 500 | * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as |
| 501 | * a separate opaque data tree, even if the function fails, this may be returned; |
| 502 | * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation; |
| 503 | * |
| 504 | * - ::LYD_TYPE_REPLY_NETCONF: |
| 505 | * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node; |
| 506 | * - @p format - must be ::LYD_XML, NETCONF supports only this format; |
| 507 | * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as |
| 508 | * a separate opaque data tree, even if the function fails, this may be returned; |
| 509 | * - @p op - must be NULL, the reply is appended to the RPC; |
| 510 | * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC. |
| 511 | * |
| 512 | * @param[in] ctx libyang context. |
| 513 | * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed. |
| 514 | * @param[in] parent Optional parent to connect the parsed nodes to. |
| 515 | * @param[in] in Input handle to read the input from. |
| 516 | * @param[in] format Expected format of the data in @p in. |
| 517 | * @param[in] data_type Expected operation to parse (@ref datatype). |
| 518 | * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL. |
| 519 | * @param[out] op Optional parsed operation node. |
| 520 | * @return LY_ERR value. |
| 521 | * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one. |
| 522 | */ |
| 523 | static LY_ERR |
| 524 | lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 525 | struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 526 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 527 | LY_ERR rc = LY_SUCCESS; |
| 528 | struct lyd_ctx *lydctx = NULL; |
| 529 | struct ly_set parsed = {0}; |
| 530 | struct lyd_node *first = NULL, *envp = NULL; |
| 531 | uint32_t i, parse_opts, val_opts; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 532 | |
Michal Vasko | 27fb026 | 2021-02-23 09:42:01 +0100 | [diff] [blame] | 533 | if (!ctx) { |
| 534 | ctx = LYD_CTX(parent); |
| 535 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 536 | if (tree) { |
| 537 | *tree = NULL; |
| 538 | } |
| 539 | if (op) { |
| 540 | *op = NULL; |
| 541 | } |
| 542 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 543 | format = lyd_parse_get_format(in, format); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 544 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 545 | /* remember input position */ |
| 546 | in->func_start = in->current; |
| 547 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 548 | /* check params based on the data type */ |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 549 | if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 550 | LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL); |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 551 | } else if (data_type == LYD_TYPE_REPLY_NETCONF) { |
| 552 | LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, |
| 553 | LY_EINVAL); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 554 | } |
Michal Vasko | 9da78cf | 2021-07-21 13:45:10 +0200 | [diff] [blame] | 555 | parse_opts = LYD_PARSE_ONLY | LYD_PARSE_OPAQ; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 556 | val_opts = 0; |
| 557 | |
| 558 | /* parse the data */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 559 | switch (format) { |
| 560 | case LYD_XML: |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 561 | rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx); |
Michal Vasko | 299d5d1 | 2021-02-16 16:36:37 +0100 | [diff] [blame] | 562 | if (rc && envp) { |
| 563 | /* special situation when the envelopes were parsed successfully */ |
| 564 | if (tree) { |
| 565 | *tree = envp; |
| 566 | } |
| 567 | ly_set_erase(&parsed, NULL); |
| 568 | return rc; |
| 569 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 570 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 571 | case LYD_JSON: |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 572 | rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 573 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 574 | case LYD_LYB: |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 575 | rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 576 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 577 | case LYD_UNKNOWN: |
Michal Vasko | d74978f | 2021-02-12 11:59:36 +0100 | [diff] [blame] | 578 | LOGARG(ctx, format); |
| 579 | rc = LY_EINVAL; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 580 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 581 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 582 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 583 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 584 | /* set out params correctly */ |
| 585 | if (tree) { |
| 586 | if (envp) { |
| 587 | /* special out param meaning */ |
| 588 | *tree = envp; |
| 589 | } else { |
| 590 | *tree = parent ? NULL : first; |
| 591 | } |
| 592 | } |
| 593 | if (op) { |
| 594 | *op = lydctx->op_node; |
| 595 | } |
| 596 | |
| 597 | cleanup: |
| 598 | if (lydctx) { |
| 599 | lydctx->free(lydctx); |
| 600 | } |
| 601 | if (rc) { |
| 602 | if (parent) { |
| 603 | /* free all the parsed subtrees */ |
| 604 | for (i = 0; i < parsed.count; ++i) { |
| 605 | lyd_free_tree(parsed.dnodes[i]); |
| 606 | } |
| 607 | } else { |
| 608 | /* free everything (cannot occur in the current code, a safety) */ |
| 609 | lyd_free_all(first); |
| 610 | if (tree) { |
| 611 | *tree = NULL; |
| 612 | } |
| 613 | if (op) { |
| 614 | *op = NULL; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | ly_set_erase(&parsed, NULL); |
| 619 | return rc; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 620 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 621 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 622 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 623 | lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 624 | enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
| 625 | { |
| 626 | LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL); |
| 627 | |
| 628 | return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op); |
| 629 | } |
| 630 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 631 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 632 | lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 633 | enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
| 634 | { |
| 635 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 636 | |
| 637 | LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL); |
| 638 | |
| 639 | return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op); |
| 640 | } |
| 641 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 642 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 643 | lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 644 | LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 645 | { |
| 646 | LY_ERR ret; |
| 647 | struct lyd_node_term *term; |
| 648 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 649 | assert(schema->nodetype & LYD_NODE_TERM); |
| 650 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 651 | term = calloc(1, sizeof *term); |
| 652 | LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM); |
| 653 | |
| 654 | term->schema = schema; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 655 | term->prev = &term->node; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 656 | term->flags = LYD_NEW; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 657 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 658 | LOG_LOCSET(schema, NULL, NULL, NULL); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 659 | ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 660 | value_len, dynamic, format, prefix_data, hints, schema, incomplete); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 661 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 662 | LY_CHECK_ERR_RET(ret, free(term), ret); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 663 | lyd_hash(&term->node); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 664 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 665 | *node = &term->node; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | LY_ERR |
| 670 | lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node) |
| 671 | { |
| 672 | LY_ERR ret; |
| 673 | struct lyd_node_term *term; |
| 674 | struct lysc_type *type; |
| 675 | |
| 676 | assert(schema->nodetype & LYD_NODE_TERM); |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 677 | assert(val && val->realtype); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 678 | |
| 679 | term = calloc(1, sizeof *term); |
| 680 | LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM); |
| 681 | |
| 682 | term->schema = schema; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 683 | term->prev = &term->node; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 684 | term->flags = LYD_NEW; |
| 685 | |
| 686 | type = ((struct lysc_node_leaf *)schema)->type; |
| 687 | ret = type->plugin->duplicate(schema->module->ctx, val, &term->value); |
| 688 | if (ret) { |
| 689 | LOGERR(schema->module->ctx, ret, "Value duplication failed."); |
| 690 | free(term); |
| 691 | return ret; |
| 692 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 693 | lyd_hash(&term->node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 694 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 695 | *node = &term->node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 696 | return ret; |
| 697 | } |
| 698 | |
| 699 | LY_ERR |
| 700 | lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node) |
| 701 | { |
| 702 | struct lyd_node_inner *in; |
| 703 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 704 | assert(schema->nodetype & LYD_NODE_INNER); |
| 705 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 706 | in = calloc(1, sizeof *in); |
| 707 | LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM); |
| 708 | |
| 709 | in->schema = schema; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 710 | in->prev = &in->node; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 711 | in->flags = LYD_NEW; |
Michal Vasko | 3383be7 | 2020-11-03 17:15:31 +0100 | [diff] [blame] | 712 | if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) { |
| 713 | in->flags |= LYD_DEFAULT; |
| 714 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 715 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 716 | /* do not hash list with keys, we need them for the hash */ |
| 717 | if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 718 | lyd_hash(&in->node); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 719 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 720 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 721 | *node = &in->node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 722 | return LY_SUCCESS; |
| 723 | } |
| 724 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 725 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 726 | lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 727 | { |
| 728 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 729 | struct lyd_node *list = NULL, *key; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 730 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 731 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 732 | assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 733 | |
| 734 | /* create list */ |
| 735 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup); |
| 736 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 737 | LOG_LOCSET(NULL, list, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 738 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 739 | /* create and insert all the keys */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 740 | LY_ARRAY_FOR(predicates, u) { |
| 741 | LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 742 | lyd_insert_node(list, NULL, key, 0); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 743 | } |
| 744 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 745 | /* hash having all the keys */ |
| 746 | lyd_hash(list); |
| 747 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 748 | /* success */ |
| 749 | *node = list; |
| 750 | list = NULL; |
| 751 | |
| 752 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 753 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 754 | lyd_free_tree(list); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | static LY_ERR |
| 759 | lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node) |
| 760 | { |
| 761 | LY_ERR ret = LY_SUCCESS; |
| 762 | struct lyxp_expr *expr = NULL; |
| 763 | uint16_t exp_idx = 0; |
| 764 | enum ly_path_pred_type pred_type = 0; |
| 765 | struct ly_path_predicate *predicates = NULL; |
| 766 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 767 | LOG_LOCSET(schema, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 768 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 769 | /* parse keys */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 770 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 771 | LY_PATH_PRED_KEYS, &expr), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 772 | |
| 773 | /* compile them */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 774 | LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_VALUE_JSON, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 775 | NULL, &predicates, &pred_type), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 776 | |
| 777 | /* create the list node */ |
| 778 | LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup); |
| 779 | |
| 780 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 781 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 782 | lyxp_expr_free(schema->module->ctx, expr); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 783 | ly_path_predicates_free(schema->module->ctx, pred_type, predicates); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 784 | return ret; |
| 785 | } |
| 786 | |
| 787 | LY_ERR |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 788 | lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value, |
| 789 | struct lyd_node **node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 790 | { |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 791 | LY_ERR ret; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 792 | struct lyd_node_any *any; |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 793 | union lyd_any_value any_val; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 794 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 795 | assert(schema->nodetype & LYD_NODE_ANY); |
| 796 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 797 | any = calloc(1, sizeof *any); |
| 798 | LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM); |
| 799 | |
| 800 | any->schema = schema; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 801 | any->prev = &any->node; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 802 | any->flags = LYD_NEW; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 803 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 804 | /* TODO: convert XML/JSON strings into a opaq data tree */ |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 805 | |
| 806 | if (use_value) { |
| 807 | any->value.str = value; |
| 808 | any->value_type = value_type; |
| 809 | } else { |
| 810 | any_val.str = value; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 811 | ret = lyd_any_copy_value(&any->node, &any_val, value_type); |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 812 | LY_CHECK_ERR_RET(ret, free(any), ret); |
| 813 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 814 | lyd_hash(&any->node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 815 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 816 | *node = &any->node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 817 | return LY_SUCCESS; |
| 818 | } |
| 819 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 820 | LY_ERR |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 821 | lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len, |
| 822 | const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 823 | LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 824 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 825 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 826 | struct lyd_node_opaq *opaq; |
| 827 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 828 | assert(ctx && name && name_len && format); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 829 | |
Michal Vasko | 4cd0a60 | 2021-05-31 11:11:58 +0200 | [diff] [blame] | 830 | if (!value_len && (!dynamic || !*dynamic)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 831 | value = ""; |
| 832 | } |
| 833 | |
| 834 | opaq = calloc(1, sizeof *opaq); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 835 | LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 836 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 837 | opaq->prev = &opaq->node; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 838 | LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 839 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 840 | if (pref_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 841 | LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 842 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 843 | if (module_key_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 844 | LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 845 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 846 | if (dynamic && *dynamic) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 847 | LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 848 | *dynamic = 0; |
| 849 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 850 | LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 851 | } |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 852 | |
| 853 | opaq->format = format; |
| 854 | opaq->val_prefix_data = val_prefix_data; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 855 | opaq->hints = hints; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 856 | opaq->ctx = ctx; |
| 857 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 858 | finish: |
| 859 | if (ret) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 860 | lyd_free_tree(&opaq->node); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 861 | ly_free_prefix_data(format, val_prefix_data); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 862 | } else { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 863 | *node = &opaq->node; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 864 | } |
| 865 | return ret; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 866 | } |
| 867 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 868 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 6524389 | 2020-12-04 16:26:21 +0100 | [diff] [blame] | 869 | lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 870 | struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 871 | { |
| 872 | struct lyd_node *ret = NULL; |
| 873 | const struct lysc_node *schema; |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 874 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 875 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 876 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 877 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 878 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 879 | if (!module) { |
| 880 | module = parent->schema->module; |
| 881 | } |
| 882 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 883 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 884 | LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0); |
Radek Krejci | dd2a766 | 2021-03-12 11:26:34 +0100 | [diff] [blame] | 885 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 886 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 887 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 888 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 889 | lyd_insert_node(parent, NULL, ret, 0); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 890 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 891 | |
| 892 | if (node) { |
| 893 | *node = ret; |
| 894 | } |
| 895 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 896 | } |
| 897 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 898 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | dd2a766 | 2021-03-12 11:26:34 +0100 | [diff] [blame] | 899 | lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node) |
| 900 | { |
| 901 | struct lyd_node *ret = NULL; |
| 902 | const struct lysc_node *schema; |
| 903 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 904 | |
| 905 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 906 | |
| 907 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0); |
| 908 | if (!schema) { |
| 909 | if (ext->argument) { |
| 910 | LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.", |
| 911 | name, ext->argument, ext->def->name); |
| 912 | } else { |
| 913 | LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.", |
| 914 | name, ext->def->name); |
| 915 | } |
| 916 | return LY_ENOTFOUND; |
| 917 | } |
| 918 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 919 | |
| 920 | *node = ret; |
| 921 | |
| 922 | return LY_SUCCESS; |
| 923 | } |
| 924 | |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 925 | /** |
| 926 | * @brief Create a new list node in the data tree. |
| 927 | * |
| 928 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
| 929 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
| 930 | * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST. |
| 931 | * @param[in] format Format of key values. |
| 932 | * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are |
| 933 | * taken into consideration. Otherwise, the output's data node is going to be created. |
| 934 | * @param[out] node Optional created node. |
| 935 | * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must |
| 936 | * be followed by the value length. |
| 937 | * @return LY_ERR value. |
| 938 | */ |
| 939 | static LY_ERR |
| 940 | _lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format, |
| 941 | ly_bool output, struct lyd_node **node, va_list ap) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 942 | { |
| 943 | struct lyd_node *ret = NULL, *key; |
| 944 | const struct lysc_node *schema, *key_s; |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 945 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 946 | const void *key_val; |
| 947 | uint32_t key_len; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 948 | LY_ERR rc = LY_SUCCESS; |
| 949 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 950 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 951 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 952 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 953 | if (!module) { |
| 954 | module = parent->schema->module; |
| 955 | } |
| 956 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 957 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 958 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 959 | |
| 960 | /* create list inner node */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 961 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 962 | |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 963 | /* create and insert all the keys */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 964 | for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 965 | if (format == LY_VALUE_LYB) { |
| 966 | key_val = va_arg(ap, const void *); |
| 967 | key_len = va_arg(ap, uint32_t); |
| 968 | } else { |
| 969 | key_val = va_arg(ap, const char *); |
| 970 | key_len = key_val ? strlen((char *)key_val) : 0; |
| 971 | } |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 972 | |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 973 | rc = lyd_create_term(key_s, key_val, key_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &key); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 974 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 975 | lyd_insert_node(ret, NULL, key, 1); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 978 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 979 | lyd_insert_node(parent, NULL, ret, 0); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | cleanup: |
| 983 | if (rc) { |
| 984 | lyd_free_tree(ret); |
| 985 | ret = NULL; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 986 | } else if (node) { |
| 987 | *node = ret; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 988 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 989 | return rc; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 990 | } |
| 991 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 992 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 993 | lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 994 | struct lyd_node **node, ...) |
| 995 | { |
| 996 | LY_ERR rc; |
| 997 | va_list ap; |
| 998 | |
| 999 | va_start(ap, node); |
| 1000 | |
| 1001 | rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap); |
| 1002 | |
| 1003 | va_end(ap); |
| 1004 | return rc; |
| 1005 | } |
| 1006 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1007 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 1008 | lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 1009 | struct lyd_node **node, ...) |
| 1010 | { |
| 1011 | LY_ERR rc; |
| 1012 | va_list ap; |
| 1013 | |
| 1014 | va_start(ap, node); |
| 1015 | |
| 1016 | rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap); |
| 1017 | |
| 1018 | va_end(ap); |
| 1019 | return rc; |
| 1020 | } |
| 1021 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1022 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 1023 | lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 1024 | struct lyd_node **node, ...) |
| 1025 | { |
| 1026 | LY_ERR rc; |
| 1027 | va_list ap; |
| 1028 | |
| 1029 | va_start(ap, node); |
| 1030 | |
| 1031 | rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap); |
| 1032 | |
| 1033 | va_end(ap); |
| 1034 | return rc; |
| 1035 | } |
| 1036 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1037 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 8247bae | 2021-03-12 11:47:02 +0100 | [diff] [blame] | 1038 | lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...) |
| 1039 | { |
| 1040 | struct lyd_node *ret = NULL, *key; |
| 1041 | const struct lysc_node *schema, *key_s; |
| 1042 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 1043 | va_list ap; |
| 1044 | const char *key_val; |
| 1045 | LY_ERR rc = LY_SUCCESS; |
| 1046 | |
| 1047 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 1048 | |
| 1049 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0); |
| 1050 | if (!schema) { |
| 1051 | if (ext->argument) { |
| 1052 | LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.", |
| 1053 | name, ext->argument, ext->def->name); |
| 1054 | } else { |
| 1055 | LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 1056 | } |
| 1057 | return LY_ENOTFOUND; |
| 1058 | } |
| 1059 | /* create list inner node */ |
| 1060 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 1061 | |
| 1062 | va_start(ap, node); |
| 1063 | |
| 1064 | /* create and insert all the keys */ |
| 1065 | for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
| 1066 | key_val = va_arg(ap, const char *); |
| 1067 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1068 | rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, |
Radek Krejci | 8247bae | 2021-03-12 11:47:02 +0100 | [diff] [blame] | 1069 | NULL, &key); |
| 1070 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1071 | lyd_insert_node(ret, NULL, key, 1); |
Radek Krejci | 8247bae | 2021-03-12 11:47:02 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | cleanup: |
| 1075 | va_end(ap); |
| 1076 | if (rc) { |
| 1077 | lyd_free_tree(ret); |
| 1078 | ret = NULL; |
Radek Krejci | 8247bae | 2021-03-12 11:47:02 +0100 | [diff] [blame] | 1079 | } |
Radek Krejci | 8919fbd | 2021-03-15 09:35:25 +0100 | [diff] [blame] | 1080 | *node = ret; |
Radek Krejci | 8247bae | 2021-03-12 11:47:02 +0100 | [diff] [blame] | 1081 | return rc; |
| 1082 | } |
| 1083 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1084 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1085 | lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys, |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 1086 | ly_bool output, struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1087 | { |
| 1088 | struct lyd_node *ret = NULL; |
| 1089 | const struct lysc_node *schema; |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1090 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1091 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 1092 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1093 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1094 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1095 | if (!module) { |
| 1096 | module = parent->schema->module; |
| 1097 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1098 | if (!keys) { |
| 1099 | keys = ""; |
| 1100 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1101 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1102 | /* find schema node */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 1103 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1104 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1105 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1106 | if ((schema->flags & LYS_KEYLESS) && !keys[0]) { |
| 1107 | /* key-less list */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1108 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1109 | } else { |
| 1110 | /* create the list node */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1111 | LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1112 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1113 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1114 | lyd_insert_node(parent, NULL, ret, 0); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1115 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1116 | |
| 1117 | if (node) { |
| 1118 | *node = ret; |
| 1119 | } |
| 1120 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1121 | } |
| 1122 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1123 | /** |
| 1124 | * @brief Create a new term node in the data tree. |
| 1125 | * |
| 1126 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
| 1127 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
| 1128 | * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST. |
| 1129 | * @param[in] value Value of the node being created. |
| 1130 | * @param[in] value_len Length of @p value. |
| 1131 | * @param[in] format Format of @p value. |
| 1132 | * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are |
| 1133 | * taken into consideration. Otherwise, the output's data node is going to be created. |
| 1134 | * @param[out] node Optional created node. |
| 1135 | * @return LY_ERR value. |
| 1136 | */ |
| 1137 | static LY_ERR |
| 1138 | _lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 1139 | size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1140 | { |
Michal Vasko | cbff3e9 | 2020-05-27 12:56:41 +0200 | [diff] [blame] | 1141 | LY_ERR rc; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1142 | struct lyd_node *ret = NULL; |
| 1143 | const struct lysc_node *schema; |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1144 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1145 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 1146 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1147 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1148 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1149 | if (!module) { |
| 1150 | module = parent->schema->module; |
| 1151 | } |
| 1152 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 1153 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1154 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1155 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1156 | rc = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1157 | LY_CHECK_RET(rc); |
Michal Vasko | cbff3e9 | 2020-05-27 12:56:41 +0200 | [diff] [blame] | 1158 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1159 | lyd_insert_node(parent, NULL, ret, 0); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1160 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1161 | |
| 1162 | if (node) { |
| 1163 | *node = ret; |
| 1164 | } |
| 1165 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1166 | } |
| 1167 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1168 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1169 | lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
| 1170 | ly_bool output, struct lyd_node **node) |
| 1171 | { |
| 1172 | return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node); |
| 1173 | } |
| 1174 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1175 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1176 | lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 1177 | size_t value_len, ly_bool output, struct lyd_node **node) |
| 1178 | { |
| 1179 | return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node); |
| 1180 | } |
| 1181 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1182 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1183 | lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
| 1184 | ly_bool output, struct lyd_node **node) |
| 1185 | { |
| 1186 | return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node); |
| 1187 | } |
| 1188 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1189 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 8a5afc2 | 2021-03-12 11:10:47 +0100 | [diff] [blame] | 1190 | lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node) |
| 1191 | { |
| 1192 | LY_ERR rc; |
| 1193 | struct lyd_node *ret = NULL; |
| 1194 | const struct lysc_node *schema; |
| 1195 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 1196 | |
| 1197 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 1198 | |
| 1199 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0); |
| 1200 | if (!schema) { |
| 1201 | if (ext->argument) { |
| 1202 | LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.", |
| 1203 | name, ext->argument, ext->def->name); |
| 1204 | } else { |
| 1205 | LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 1206 | } |
| 1207 | return LY_ENOTFOUND; |
| 1208 | } |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1209 | rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &ret); |
Radek Krejci | 8a5afc2 | 2021-03-12 11:10:47 +0100 | [diff] [blame] | 1210 | LY_CHECK_RET(rc); |
| 1211 | |
| 1212 | *node = ret; |
| 1213 | |
| 1214 | return LY_SUCCESS; |
| 1215 | } |
| 1216 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1217 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2a4ab2b | 2021-03-04 15:52:40 +0100 | [diff] [blame] | 1218 | lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 1219 | ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1220 | { |
| 1221 | struct lyd_node *ret = NULL; |
| 1222 | const struct lysc_node *schema; |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1223 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1224 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 1225 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1226 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1227 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1228 | if (!module) { |
| 1229 | module = parent->schema->module; |
| 1230 | } |
| 1231 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 1232 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1233 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1234 | |
Michal Vasko | 2a4ab2b | 2021-03-04 15:52:40 +0100 | [diff] [blame] | 1235 | LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1236 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1237 | lyd_insert_node(parent, NULL, ret, 0); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1238 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1239 | |
| 1240 | if (node) { |
| 1241 | *node = ret; |
| 1242 | } |
| 1243 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1244 | } |
| 1245 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1246 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 0b963da | 2021-03-12 13:23:44 +0100 | [diff] [blame] | 1247 | lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value, |
| 1248 | LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node) |
| 1249 | { |
| 1250 | struct lyd_node *ret = NULL; |
| 1251 | const struct lysc_node *schema; |
| 1252 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 1253 | |
| 1254 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 1255 | |
| 1256 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0); |
| 1257 | if (!schema) { |
| 1258 | if (ext->argument) { |
| 1259 | LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.", |
| 1260 | name, ext->argument, ext->def->name); |
| 1261 | } else { |
| 1262 | LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 1263 | } |
| 1264 | return LY_ENOTFOUND; |
| 1265 | } |
| 1266 | LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret)); |
| 1267 | |
| 1268 | *node = ret; |
| 1269 | |
| 1270 | return LY_SUCCESS; |
| 1271 | } |
| 1272 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1273 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1274 | lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 1275 | const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta) |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1276 | { |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1277 | const char *prefix, *tmp; |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1278 | size_t pref_len, name_len; |
| 1279 | |
Michal Vasko | 2b5344c | 2021-02-26 10:12:05 +0100 | [diff] [blame] | 1280 | LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1281 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 2b5344c | 2021-02-26 10:12:05 +0100 | [diff] [blame] | 1282 | if (!ctx) { |
| 1283 | ctx = LYD_CTX(parent); |
| 1284 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1285 | |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1286 | if (parent && !parent->schema) { |
Michal Vasko | 33b4fab | 2021-03-04 15:56:12 +0100 | [diff] [blame] | 1287 | LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent)); |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1288 | return LY_EINVAL; |
| 1289 | } |
Michal Vasko | 610553d | 2020-11-18 18:15:05 +0100 | [diff] [blame] | 1290 | if (meta) { |
| 1291 | *meta = NULL; |
| 1292 | } |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1293 | |
| 1294 | /* parse the name */ |
| 1295 | tmp = name; |
| 1296 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 1297 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1298 | return LY_EINVAL; |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | /* find the module */ |
| 1302 | if (prefix) { |
Radek Krejci | 0ad51f1 | 2020-07-16 12:08:12 +0200 | [diff] [blame] | 1303 | module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len); |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1304 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND); |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | /* set value if none */ |
| 1308 | if (!val_str) { |
| 1309 | val_str = ""; |
| 1310 | } |
| 1311 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1312 | return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON, |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1313 | NULL, LYD_HINT_DATA, clear_dflt, NULL); |
| 1314 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1315 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1316 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1317 | lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr, |
| 1318 | struct lyd_meta **meta) |
| 1319 | { |
| 1320 | const struct lys_module *mod; |
| 1321 | |
| 1322 | LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1323 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1324 | |
| 1325 | if (parent && !parent->schema) { |
| 1326 | LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name); |
| 1327 | return LY_EINVAL; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1328 | } |
Michal Vasko | 610553d | 2020-11-18 18:15:05 +0100 | [diff] [blame] | 1329 | if (meta) { |
| 1330 | *meta = NULL; |
| 1331 | } |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1332 | |
| 1333 | switch (attr->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1334 | case LY_VALUE_XML: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1335 | mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns); |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1336 | if (!mod) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1337 | LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns); |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1338 | return LY_ENOTFOUND; |
| 1339 | } |
| 1340 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1341 | case LY_VALUE_JSON: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1342 | mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name); |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1343 | if (!mod) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1344 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name); |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1345 | return LY_ENOTFOUND; |
| 1346 | } |
| 1347 | break; |
| 1348 | default: |
| 1349 | LOGINT_RET(ctx); |
| 1350 | } |
| 1351 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1352 | return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value), |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1353 | NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL); |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1354 | } |
| 1355 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1356 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1357 | lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value, |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1358 | const char *prefix, const char *module_name, struct lyd_node **node) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1359 | { |
| 1360 | struct lyd_node *ret = NULL; |
Radek Krejci | 07a5596 | 2021-03-02 20:16:43 +0100 | [diff] [blame] | 1361 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1362 | LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1363 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1364 | |
| 1365 | if (!ctx) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1366 | ctx = LYD_CTX(parent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1367 | } |
| 1368 | if (!value) { |
| 1369 | value = ""; |
| 1370 | } |
| 1371 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1372 | LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1373 | strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1374 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1375 | lyd_insert_node(parent, NULL, ret, 1); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1376 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1377 | |
| 1378 | if (node) { |
| 1379 | *node = ret; |
| 1380 | } |
| 1381 | return LY_SUCCESS; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1384 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1385 | lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value, |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1386 | const char *prefix, const char *module_ns, struct lyd_node **node) |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1387 | { |
| 1388 | struct lyd_node *ret = NULL; |
| 1389 | |
| 1390 | LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1391 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1392 | |
| 1393 | if (!ctx) { |
| 1394 | ctx = LYD_CTX(parent); |
| 1395 | } |
| 1396 | if (!value) { |
| 1397 | value = ""; |
| 1398 | } |
| 1399 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1400 | LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1401 | strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret)); |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1402 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1403 | lyd_insert_node(parent, NULL, ret, 1); |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | if (node) { |
| 1407 | *node = ret; |
| 1408 | } |
| 1409 | return LY_SUCCESS; |
| 1410 | } |
| 1411 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1412 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1413 | lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1414 | struct lyd_attr **attr) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1415 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1416 | struct lyd_attr *ret = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1417 | const struct ly_ctx *ctx; |
| 1418 | const char *prefix, *tmp; |
Michal Vasko | 51d21b8 | 2020-11-13 18:03:54 +0100 | [diff] [blame] | 1419 | size_t pref_len, name_len, mod_len; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1420 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1421 | LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1422 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1423 | ctx = LYD_CTX(parent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1424 | |
| 1425 | /* parse the name */ |
| 1426 | tmp = name; |
| 1427 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
Michal Vasko | b69b68c | 2021-02-17 11:18:16 +0100 | [diff] [blame] | 1428 | LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1429 | return LY_EVALID; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
Michal Vasko | e137fc4 | 2021-07-22 11:53:13 +0200 | [diff] [blame] | 1432 | if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) { |
| 1433 | /* not a prefix but special name */ |
| 1434 | name = prefix; |
| 1435 | name_len += 1 + pref_len; |
| 1436 | prefix = NULL; |
| 1437 | pref_len = 0; |
| 1438 | } |
| 1439 | |
Michal Vasko | 51d21b8 | 2020-11-13 18:03:54 +0100 | [diff] [blame] | 1440 | /* get the module */ |
| 1441 | if (module_name) { |
| 1442 | mod_len = strlen(module_name); |
| 1443 | } else { |
| 1444 | module_name = prefix; |
| 1445 | mod_len = pref_len; |
| 1446 | } |
| 1447 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1448 | /* set value if none */ |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1449 | if (!value) { |
| 1450 | value = ""; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1453 | LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1454 | strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA)); |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1455 | |
| 1456 | if (attr) { |
| 1457 | *attr = ret; |
| 1458 | } |
| 1459 | return LY_SUCCESS; |
| 1460 | } |
| 1461 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1462 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1463 | lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value, |
| 1464 | struct lyd_attr **attr) |
| 1465 | { |
| 1466 | struct lyd_attr *ret = NULL; |
| 1467 | const struct ly_ctx *ctx; |
| 1468 | const char *prefix, *tmp; |
| 1469 | size_t pref_len, name_len; |
| 1470 | |
| 1471 | LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL); |
| 1472 | |
| 1473 | ctx = LYD_CTX(parent); |
| 1474 | |
| 1475 | /* parse the name */ |
| 1476 | tmp = name; |
| 1477 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 1478 | LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name); |
| 1479 | return LY_EVALID; |
| 1480 | } |
| 1481 | |
Michal Vasko | e137fc4 | 2021-07-22 11:53:13 +0200 | [diff] [blame] | 1482 | if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) { |
| 1483 | /* not a prefix but special name */ |
| 1484 | name = prefix; |
| 1485 | name_len += 1 + pref_len; |
| 1486 | prefix = NULL; |
| 1487 | pref_len = 0; |
| 1488 | } |
| 1489 | |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1490 | /* set value if none */ |
| 1491 | if (!value) { |
| 1492 | value = ""; |
| 1493 | } |
| 1494 | |
| 1495 | LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1496 | module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1497 | |
| 1498 | if (attr) { |
| 1499 | *attr = ret; |
| 1500 | } |
| 1501 | return LY_SUCCESS; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1502 | } |
| 1503 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1504 | /** |
| 1505 | * @brief Change the value of a term (leaf or leaf-list) node. |
| 1506 | * |
| 1507 | * Node changed this way is always considered explicitly set, meaning its default flag |
| 1508 | * is always cleared. |
| 1509 | * |
| 1510 | * @param[in] term Term node to change. |
| 1511 | * @param[in] value New value to set. |
| 1512 | * @param[in] value_len Length of @p value. |
| 1513 | * @param[in] format Format of @p value. |
| 1514 | * @return LY_SUCCESS if value was changed, |
| 1515 | * @return LY_EEXIST if value was the same and only the default flag was cleared, |
| 1516 | * @return LY_ENOT if the values were equal and no change occured, |
| 1517 | * @return LY_ERR value on other errors. |
| 1518 | */ |
| 1519 | static LY_ERR |
| 1520 | _lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1521 | { |
| 1522 | LY_ERR ret = LY_SUCCESS; |
| 1523 | struct lysc_type *type; |
| 1524 | struct lyd_node_term *t; |
| 1525 | struct lyd_node *parent; |
| 1526 | struct lyd_value val = {0}; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1527 | ly_bool dflt_change, val_change; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1528 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1529 | assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1530 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1531 | t = (struct lyd_node_term *)term; |
| 1532 | type = ((struct lysc_node_leaf *)term->schema)->type; |
| 1533 | |
| 1534 | /* parse the new value */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1535 | LOG_LOCSET(term->schema, term, NULL, NULL); |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1536 | ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, NULL, format, NULL, LYD_HINT_DATA, term->schema, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1537 | LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1538 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1539 | |
| 1540 | /* compare original and new value */ |
| 1541 | if (type->plugin->compare(&t->value, &val)) { |
| 1542 | /* values differ, switch them */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1543 | type->plugin->free(LYD_CTX(term), &t->value); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1544 | t->value = val; |
| 1545 | memset(&val, 0, sizeof val); |
| 1546 | val_change = 1; |
| 1547 | } else { |
| 1548 | val_change = 0; |
| 1549 | } |
| 1550 | |
| 1551 | /* always clear the default flag */ |
| 1552 | if (term->flags & LYD_DEFAULT) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1553 | for (parent = term; parent; parent = lyd_parent(parent)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1554 | parent->flags &= ~LYD_DEFAULT; |
| 1555 | } |
| 1556 | dflt_change = 1; |
| 1557 | } else { |
| 1558 | dflt_change = 0; |
| 1559 | } |
| 1560 | |
| 1561 | if (val_change || dflt_change) { |
| 1562 | /* make the node non-validated */ |
| 1563 | term->flags &= LYD_NEW; |
| 1564 | } |
| 1565 | |
| 1566 | if (val_change) { |
| 1567 | if (term->schema->nodetype == LYS_LEAFLIST) { |
| 1568 | /* leaf-list needs to be hashed again and re-inserted into parent */ |
| 1569 | lyd_unlink_hash(term); |
| 1570 | lyd_hash(term); |
| 1571 | LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup); |
| 1572 | } else if ((term->schema->flags & LYS_KEY) && term->parent) { |
| 1573 | /* list needs to be updated if its key was changed */ |
| 1574 | assert(term->parent->schema->nodetype == LYS_LIST); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1575 | lyd_unlink_hash(lyd_parent(term)); |
| 1576 | lyd_hash(lyd_parent(term)); |
| 1577 | LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1578 | } /* else leaf that is not a key, its value is not used for its hash so it does not change */ |
| 1579 | } |
| 1580 | |
| 1581 | /* retrun value */ |
| 1582 | if (!val_change) { |
| 1583 | if (dflt_change) { |
| 1584 | /* only default flag change */ |
| 1585 | ret = LY_EEXIST; |
| 1586 | } else { |
| 1587 | /* no change */ |
| 1588 | ret = LY_ENOT; |
| 1589 | } |
| 1590 | } /* else value changed, LY_SUCCESS */ |
| 1591 | |
| 1592 | cleanup: |
Michal Vasko | 59512fc | 2020-12-09 18:13:29 +0100 | [diff] [blame] | 1593 | if (val.realtype) { |
| 1594 | type->plugin->free(LYD_CTX(term), &val); |
| 1595 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1596 | return ret; |
| 1597 | } |
| 1598 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1599 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1600 | lyd_change_term(struct lyd_node *term, const char *val_str) |
| 1601 | { |
| 1602 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1603 | |
| 1604 | return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON); |
| 1605 | } |
| 1606 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1607 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1608 | lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len) |
| 1609 | { |
| 1610 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1611 | |
| 1612 | return _lyd_change_term(term, value, value_len, LY_VALUE_LYB); |
| 1613 | } |
| 1614 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1615 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1616 | lyd_change_term_canon(struct lyd_node *term, const char *val_str) |
| 1617 | { |
| 1618 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1619 | |
| 1620 | return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON); |
| 1621 | } |
| 1622 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1623 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1624 | lyd_change_meta(struct lyd_meta *meta, const char *val_str) |
| 1625 | { |
| 1626 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 1627 | struct lyd_meta *m2 = NULL; |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1628 | struct lyd_value val; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1629 | ly_bool val_change; |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1630 | |
| 1631 | LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL); |
| 1632 | |
| 1633 | if (!val_str) { |
| 1634 | val_str = ""; |
| 1635 | } |
| 1636 | |
| 1637 | /* parse the new value into a new meta structure */ |
| 1638 | LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1639 | strlen(val_str), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup); |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1640 | |
| 1641 | /* compare original and new value */ |
| 1642 | if (lyd_compare_meta(meta, m2)) { |
| 1643 | /* values differ, switch them */ |
| 1644 | val = meta->value; |
| 1645 | meta->value = m2->value; |
| 1646 | m2->value = val; |
| 1647 | val_change = 1; |
| 1648 | } else { |
| 1649 | val_change = 0; |
| 1650 | } |
| 1651 | |
| 1652 | /* retrun value */ |
| 1653 | if (!val_change) { |
| 1654 | /* no change */ |
| 1655 | ret = LY_ENOT; |
| 1656 | } /* else value changed, LY_SUCCESS */ |
| 1657 | |
| 1658 | cleanup: |
Michal Vasko | 1a66a5d | 2020-11-18 18:15:37 +0100 | [diff] [blame] | 1659 | lyd_free_meta_single(m2); |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1660 | return ret; |
| 1661 | } |
| 1662 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1663 | /** |
| 1664 | * @brief Update node value. |
| 1665 | * |
| 1666 | * @param[in] node Node to update. |
| 1667 | * @param[in] value New value to set. |
| 1668 | * @param[in] value_len Length of @p value. |
| 1669 | * @param[in] value_type Type of @p value for anydata/anyxml node. |
| 1670 | * @param[in] format Format of @p value. |
| 1671 | * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL. |
| 1672 | * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL. |
| 1673 | * @return LY_ERR value. |
| 1674 | */ |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1675 | static LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1676 | lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, |
| 1677 | LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node) |
| 1678 | { |
| 1679 | LY_ERR ret = LY_SUCCESS; |
| 1680 | struct lyd_node *new_any; |
| 1681 | |
| 1682 | switch (node->schema->nodetype) { |
| 1683 | case LYS_CONTAINER: |
| 1684 | case LYS_NOTIF: |
| 1685 | case LYS_RPC: |
| 1686 | case LYS_ACTION: |
| 1687 | case LYS_LIST: |
| 1688 | /* if it exists, there is nothing to update */ |
| 1689 | *new_parent = NULL; |
| 1690 | *new_node = NULL; |
| 1691 | break; |
| 1692 | case LYS_LEAFLIST: |
| 1693 | if (!lysc_is_dup_inst_list(node->schema)) { |
| 1694 | /* if it exists, there is nothing to update */ |
| 1695 | *new_parent = NULL; |
| 1696 | *new_node = NULL; |
| 1697 | break; |
| 1698 | } |
| 1699 | /* fallthrough */ |
| 1700 | case LYS_LEAF: |
| 1701 | ret = _lyd_change_term(node, value, value_len, format); |
| 1702 | if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) { |
| 1703 | /* there was an actual change (at least of the default flag) */ |
| 1704 | *new_parent = node; |
| 1705 | *new_node = node; |
| 1706 | ret = LY_SUCCESS; |
| 1707 | } else if (ret == LY_ENOT) { |
| 1708 | /* no change */ |
| 1709 | *new_parent = NULL; |
| 1710 | *new_node = NULL; |
| 1711 | ret = LY_SUCCESS; |
| 1712 | } /* else error */ |
| 1713 | break; |
| 1714 | case LYS_ANYDATA: |
| 1715 | case LYS_ANYXML: |
| 1716 | /* create a new any node */ |
| 1717 | LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any)); |
| 1718 | |
| 1719 | /* compare with the existing one */ |
| 1720 | if (lyd_compare_single(node, new_any, 0)) { |
| 1721 | /* not equal, switch values (so that we can use generic node free) */ |
| 1722 | ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value; |
| 1723 | ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type; |
| 1724 | ((struct lyd_node_any *)node)->value.str = value; |
| 1725 | ((struct lyd_node_any *)node)->value_type = value_type; |
| 1726 | |
| 1727 | *new_parent = node; |
| 1728 | *new_node = node; |
| 1729 | } else { |
| 1730 | /* they are equal */ |
| 1731 | *new_parent = NULL; |
| 1732 | *new_node = NULL; |
| 1733 | } |
| 1734 | lyd_free_tree(new_any); |
| 1735 | break; |
| 1736 | default: |
| 1737 | LOGINT(LYD_CTX(node)); |
| 1738 | ret = LY_EINT; |
| 1739 | break; |
| 1740 | } |
| 1741 | |
| 1742 | return ret; |
| 1743 | } |
| 1744 | |
| 1745 | static LY_ERR |
| 1746 | lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len, |
| 1747 | LY_VALUE_FORMAT format, uint32_t options) |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1748 | { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1749 | LY_ERR r; |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1750 | struct ly_path_predicate *pred; |
Michal Vasko | 0472624 | 2022-01-10 09:30:08 +0100 | [diff] [blame] | 1751 | struct lyd_value val; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1752 | const struct lysc_node *schema = NULL; |
| 1753 | LY_ARRAY_COUNT_TYPE u, new_count; |
| 1754 | int create = 0; |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1755 | |
| 1756 | assert(path); |
| 1757 | |
| 1758 | /* go through all the compiled nodes */ |
| 1759 | LY_ARRAY_FOR(path, u) { |
| 1760 | schema = path[u].node; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1761 | |
| 1762 | if (lysc_is_dup_inst_list(schema)) { |
| 1763 | if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) { |
| 1764 | /* creating a new key-less list or state leaf-list instance */ |
| 1765 | create = 1; |
| 1766 | new_count = u; |
| 1767 | } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) { |
| 1768 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 1769 | LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".", |
| 1770 | lys_nodetype2str(schema->nodetype), schema->name, str_path); |
| 1771 | LOG_LOCBACK(1, 0, 0, 0); |
| 1772 | return LY_EINVAL; |
| 1773 | } |
| 1774 | } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) { |
| 1775 | if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) { |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1776 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 1777 | LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".", |
| 1778 | lys_nodetype2str(schema->nodetype), schema->name, str_path); |
| 1779 | LOG_LOCBACK(1, 0, 0, 0); |
| 1780 | return LY_EINVAL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1781 | } /* else creating an opaque list */ |
| 1782 | } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1783 | r = LY_SUCCESS; |
| 1784 | if (options & LYD_NEW_PATH_OPAQ) { |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1785 | r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1786 | } |
| 1787 | if (!r) { |
Michal Vasko | 0472624 | 2022-01-10 09:30:08 +0100 | [diff] [blame] | 1788 | /* try to store the value */ |
| 1789 | LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type, |
| 1790 | value, value_len, NULL, format, NULL, LYD_HINT_DATA, schema, NULL)); |
| 1791 | ++((struct lysc_type *)val.realtype)->refcount; |
| 1792 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1793 | /* store the new predicate so that it is used when searching for this instance */ |
| 1794 | path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST; |
| 1795 | LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM); |
Michal Vasko | 0472624 | 2022-01-10 09:30:08 +0100 | [diff] [blame] | 1796 | pred->value = val; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1797 | } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */ |
| 1798 | } |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1799 | } |
| 1800 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1801 | if (create) { |
| 1802 | /* hide the nodes that should always be created so they are not found */ |
| 1803 | while (new_count < LY_ARRAY_COUNT(path)) { |
| 1804 | LY_ARRAY_DECREMENT(path); |
| 1805 | } |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1806 | } |
| 1807 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1808 | return LY_SUCCESS; |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1809 | } |
| 1810 | |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 1811 | /** |
| 1812 | * @brief Create a new node in the data tree based on a path. All node types can be created. |
| 1813 | * |
Michal Vasko | a918a63 | 2021-07-02 11:53:36 +0200 | [diff] [blame] | 1814 | * If @p path points to a list key, the key value from the predicate is used and @p value is ignored. |
| 1815 | * Also, if a leaf-list is being created and both a predicate is defined in @p path |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 1816 | * and @p value is set, the predicate is preferred. |
| 1817 | * |
| 1818 | * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these |
| 1819 | * nodes, they are always created. |
| 1820 | * |
| 1821 | * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used, |
| 1822 | * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted |
| 1823 | * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases. |
| 1824 | * @param[in] ctx libyang context, must be set if @p parent is NULL. |
| 1825 | * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute |
| 1826 | * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces |
| 1827 | * searching for the appropriate module. |
| 1828 | * @param[in] path [Path](@ref howtoXPath) to create. |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1829 | * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an |
| 1830 | * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL. |
| 1831 | * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when |
| 1832 | * creating anyxml/anydata nodes. |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 1833 | * @param[in] value_type Anyxml/anydata node @p value type. |
| 1834 | * @param[in] options Bitmask of options, see @ref pathoptions. |
| 1835 | * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node. |
| 1836 | * @param[out] new_node Optional last node created. |
| 1837 | * @return LY_ERR value. |
| 1838 | */ |
| 1839 | static LY_ERR |
| 1840 | lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path, |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1841 | const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 1842 | struct lyd_node **new_parent, struct lyd_node **new_node) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1843 | { |
| 1844 | LY_ERR ret = LY_SUCCESS, r; |
| 1845 | struct lyxp_expr *exp = NULL; |
| 1846 | struct ly_path *p = NULL; |
| 1847 | struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent; |
| 1848 | const struct lysc_node *schema; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1849 | const struct lyd_value *val = NULL; |
Radek Krejci | 910d013 | 2021-03-05 14:19:31 +0100 | [diff] [blame] | 1850 | LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0; |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1851 | LY_VALUE_FORMAT format; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1852 | |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 1853 | assert(parent || ctx); |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1854 | assert(path && ((path[0] == '/') || parent)); |
| 1855 | assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1856 | |
| 1857 | if (!ctx) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1858 | ctx = LYD_CTX(parent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1859 | } |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1860 | if (value && !value_len) { |
| 1861 | value_len = strlen(value); |
| 1862 | } |
| 1863 | if (options & LYD_NEW_PATH_BIN_VALUE) { |
| 1864 | format = LY_VALUE_LYB; |
| 1865 | } else if (options & LYD_NEW_PATH_CANON_VALUE) { |
| 1866 | format = LY_VALUE_CANON; |
| 1867 | } else { |
| 1868 | format = LY_VALUE_JSON; |
| 1869 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1870 | |
| 1871 | /* parse path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1872 | LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL, |
| 1873 | LY_PATH_PRED_SIMPLE, &exp), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1874 | |
| 1875 | /* compile path */ |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 1876 | LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ? |
| 1877 | LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1878 | |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1879 | /* check the compiled path before searching existing nodes, it may be shortened */ |
| 1880 | orig_count = LY_ARRAY_COUNT(p); |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1881 | LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1882 | |
| 1883 | /* try to find any existing nodes in the path */ |
| 1884 | if (parent) { |
| 1885 | ret = ly_path_eval_partial(p, parent, &path_idx, &node); |
| 1886 | if (ret == LY_SUCCESS) { |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1887 | if (orig_count == LY_ARRAY_COUNT(p)) { |
| 1888 | /* the node exists, are we supposed to update it or is it just a default? */ |
| 1889 | if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) { |
| 1890 | LOG_LOCSET(NULL, node, NULL, NULL); |
| 1891 | LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path); |
| 1892 | LOG_LOCBACK(0, 1, 0, 0); |
| 1893 | ret = LY_EEXIST; |
| 1894 | goto cleanup; |
| 1895 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1896 | |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1897 | /* update the existing node */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1898 | ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode); |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1899 | goto cleanup; |
| 1900 | } /* else we were not searching for the whole path */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1901 | } else if (ret == LY_EINCOMPLETE) { |
| 1902 | /* some nodes were found, adjust the iterator to the next segment */ |
| 1903 | ++path_idx; |
| 1904 | } else if (ret == LY_ENOTFOUND) { |
| 1905 | /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */ |
Michal Vasko | 45b0d20 | 2020-11-06 17:20:46 +0100 | [diff] [blame] | 1906 | if (lysc_data_parent(p[0].node)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1907 | node = parent; |
| 1908 | } |
| 1909 | } else { |
| 1910 | /* error */ |
| 1911 | goto cleanup; |
| 1912 | } |
| 1913 | } |
| 1914 | |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1915 | /* restore the full path for creating new nodes */ |
| 1916 | while (orig_count > LY_ARRAY_COUNT(p)) { |
| 1917 | LY_ARRAY_INCREMENT(p); |
| 1918 | } |
| 1919 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1920 | /* create all the non-existing nodes in a loop */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1921 | for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1922 | cur_parent = node; |
| 1923 | schema = p[path_idx].node; |
| 1924 | |
| 1925 | switch (schema->nodetype) { |
| 1926 | case LYS_LIST: |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1927 | if (lysc_is_dup_inst_list(schema)) { |
| 1928 | /* create key-less list instance */ |
| 1929 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup); |
| 1930 | } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) { |
| 1931 | /* creating opaque list without keys */ |
| 1932 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1933 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1934 | LYD_NODEHINT_LIST, &node), cleanup); |
| 1935 | } else { |
| 1936 | /* create standard list instance */ |
| 1937 | assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST); |
| 1938 | LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1939 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1940 | break; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1941 | case LYS_CONTAINER: |
| 1942 | case LYS_NOTIF: |
| 1943 | case LYS_RPC: |
| 1944 | case LYS_ACTION: |
| 1945 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup); |
| 1946 | break; |
| 1947 | case LYS_LEAFLIST: |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1948 | if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) { |
| 1949 | /* we have not checked this only for dup-inst lists, otherwise it must be opaque */ |
| 1950 | r = LY_EVALID; |
| 1951 | if (lysc_is_dup_inst_list(schema)) { |
| 1952 | /* validate value */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1953 | r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1954 | } |
| 1955 | if (r && (r != LY_EINCOMPLETE)) { |
| 1956 | /* creating opaque leaf-list */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1957 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len, |
| 1958 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, |
| 1959 | LYD_NODEHINT_LEAFLIST, &node), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1960 | break; |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | /* get value to set */ |
| 1965 | if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) { |
| 1966 | val = &p[path_idx].predicates[0].value; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | /* create a leaf-list instance */ |
| 1970 | if (val) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1971 | LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1972 | } else { |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1973 | LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, |
| 1974 | NULL, &node), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1975 | } |
| 1976 | break; |
| 1977 | case LYS_LEAF: |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 1978 | if (lysc_is_key(schema) && cur_parent->schema) { |
Michal Vasko | 3588033 | 2020-12-08 13:08:12 +0100 | [diff] [blame] | 1979 | /* it must have been already created or some error will occur later */ |
Michal Vasko | a918a63 | 2021-07-02 11:53:36 +0200 | [diff] [blame] | 1980 | lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node); |
| 1981 | assert(node); |
Michal Vasko | 3588033 | 2020-12-08 13:08:12 +0100 | [diff] [blame] | 1982 | goto next_iter; |
| 1983 | } |
| 1984 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1985 | if (options & LYD_NEW_PATH_OPAQ) { |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 1986 | if (cur_parent && !cur_parent->schema) { |
| 1987 | /* always create opaque nodes for opaque parents */ |
| 1988 | r = LY_ENOT; |
| 1989 | } else { |
| 1990 | /* validate value */ |
| 1991 | r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL); |
| 1992 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1993 | if (r && (r != LY_EINCOMPLETE)) { |
| 1994 | /* creating opaque leaf */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1995 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len, |
| 1996 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node), |
| 1997 | cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1998 | break; |
| 1999 | } |
| 2000 | } |
| 2001 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2002 | /* create a leaf instance */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2003 | LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, |
| 2004 | &node), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2005 | break; |
| 2006 | case LYS_ANYDATA: |
| 2007 | case LYS_ANYXML: |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 2008 | LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2009 | break; |
| 2010 | default: |
| 2011 | LOGINT(ctx); |
| 2012 | ret = LY_EINT; |
| 2013 | goto cleanup; |
| 2014 | } |
| 2015 | |
| 2016 | if (cur_parent) { |
| 2017 | /* connect to the parent */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2018 | lyd_insert_node(cur_parent, NULL, node, 0); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2019 | } else if (parent) { |
| 2020 | /* connect to top-level siblings */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2021 | lyd_insert_node(NULL, &parent, node, 0); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2022 | } |
| 2023 | |
Michal Vasko | 3588033 | 2020-12-08 13:08:12 +0100 | [diff] [blame] | 2024 | next_iter: |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2025 | /* update remembered nodes */ |
| 2026 | if (!nparent) { |
| 2027 | nparent = node; |
| 2028 | } |
| 2029 | nnode = node; |
| 2030 | } |
| 2031 | |
| 2032 | cleanup: |
| 2033 | lyxp_expr_free(ctx, exp); |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 2034 | if (p) { |
| 2035 | while (orig_count > LY_ARRAY_COUNT(p)) { |
| 2036 | LY_ARRAY_INCREMENT(p); |
| 2037 | } |
| 2038 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2039 | ly_path_free(ctx, p); |
| 2040 | if (!ret) { |
| 2041 | /* set out params only on success */ |
| 2042 | if (new_parent) { |
| 2043 | *new_parent = nparent; |
| 2044 | } |
| 2045 | if (new_node) { |
| 2046 | *new_node = nnode; |
| 2047 | } |
Michal Vasko | f4b95ba | 2020-12-11 08:42:33 +0100 | [diff] [blame] | 2048 | } else { |
| 2049 | lyd_free_tree(nparent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2050 | } |
| 2051 | return ret; |
| 2052 | } |
| 2053 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2054 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2055 | lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options, |
| 2056 | struct lyd_node **node) |
| 2057 | { |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2058 | LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, |
| 2059 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2060 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2061 | |
| 2062 | return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2063 | } |
| 2064 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2065 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2066 | lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value, |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2067 | size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, |
| 2068 | struct lyd_node **new_node) |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2069 | { |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2070 | LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, |
| 2071 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2072 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
| 2073 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2074 | return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2075 | } |
| 2076 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2077 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2078 | lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value, |
| 2079 | uint32_t options, struct lyd_node **node) |
| 2080 | { |
| 2081 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 2082 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2083 | LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent, |
| 2084 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2085 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
| 2086 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2087 | return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2088 | } |
| 2089 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2090 | LY_ERR |
| 2091 | lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent, |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2092 | const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_exts, struct ly_set *node_types, |
| 2093 | uint32_t impl_opts, struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2094 | { |
| 2095 | LY_ERR ret; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 2096 | const struct lysc_node *iter = NULL; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 2097 | struct lyd_node *node = NULL; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2098 | struct lyd_value **dflts; |
| 2099 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 2100 | uint32_t getnext_opts; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2101 | |
| 2102 | assert(first && (parent || sparent || mod)); |
| 2103 | |
| 2104 | if (!sparent && parent) { |
| 2105 | sparent = parent->schema; |
| 2106 | } |
| 2107 | |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 2108 | getnext_opts = LYS_GETNEXT_WITHCHOICE; |
| 2109 | if (impl_opts & LYD_IMPLICIT_OUTPUT) { |
| 2110 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 2111 | } |
| 2112 | |
| 2113 | while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2114 | if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) { |
| 2115 | continue; |
Michal Vasko | 44b19a1 | 2020-08-07 09:21:30 +0200 | [diff] [blame] | 2116 | } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) { |
| 2117 | continue; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | switch (iter->nodetype) { |
| 2121 | case LYS_CHOICE: |
Michal Vasko | bcf4d7d | 2020-11-18 18:16:49 +0100 | [diff] [blame] | 2122 | node = lys_getnext_data(NULL, *first, NULL, iter, NULL); |
| 2123 | if (!node && ((struct lysc_node_choice *)iter)->dflt) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2124 | /* create default case data */ |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 2125 | LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node, |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2126 | NULL, node_when, node_exts, node_types, impl_opts, diff)); |
Michal Vasko | bcf4d7d | 2020-11-18 18:16:49 +0100 | [diff] [blame] | 2127 | } else if (node) { |
| 2128 | /* create any default data in the existing case */ |
| 2129 | assert(node->schema->parent->nodetype == LYS_CASE); |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2130 | LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_exts, node_types, |
Michal Vasko | bcf4d7d | 2020-11-18 18:16:49 +0100 | [diff] [blame] | 2131 | impl_opts, diff)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2132 | } |
| 2133 | break; |
| 2134 | case LYS_CONTAINER: |
| 2135 | if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 2136 | /* create default NP container */ |
| 2137 | LY_CHECK_RET(lyd_create_inner(iter, &node)); |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2138 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2139 | lyd_insert_node(parent, first, node, 0); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2140 | |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2141 | if (lysc_has_when(iter) && node_when) { |
Michal Vasko | e16c7b7 | 2021-02-26 10:39:06 +0100 | [diff] [blame] | 2142 | /* remember to resolve when */ |
| 2143 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
| 2144 | } |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2145 | if (node_exts) { |
| 2146 | /* remember to call all the extension's validation callbacks */ |
| 2147 | LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node)); |
| 2148 | } |
Michal Vasko | e49b632 | 2020-11-05 17:38:36 +0100 | [diff] [blame] | 2149 | if (diff) { |
| 2150 | /* add into diff */ |
| 2151 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 2152 | } |
| 2153 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2154 | /* create any default children */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2155 | LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_exts, node_types, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2156 | impl_opts, diff)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2157 | } |
| 2158 | break; |
| 2159 | case LYS_LEAF: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2160 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt && |
| 2161 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2162 | /* create default leaf */ |
| 2163 | ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node); |
| 2164 | if (ret == LY_EINCOMPLETE) { |
| 2165 | if (node_types) { |
| 2166 | /* remember to resolve type */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 2167 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2168 | } |
| 2169 | } else if (ret) { |
| 2170 | return ret; |
| 2171 | } |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2172 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2173 | lyd_insert_node(parent, first, node, 0); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2174 | |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2175 | if (lysc_has_when(iter) && node_when) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2176 | /* remember to resolve when */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 2177 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2178 | } |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2179 | if (node_exts) { |
| 2180 | /* remember to call all the extension's validation callbacks */ |
| 2181 | LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node)); |
| 2182 | } |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2183 | if (diff) { |
| 2184 | /* add into diff */ |
| 2185 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 2186 | } |
| 2187 | } |
| 2188 | break; |
| 2189 | case LYS_LEAFLIST: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2190 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts && |
| 2191 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2192 | /* create all default leaf-lists */ |
| 2193 | dflts = ((struct lysc_node_leaflist *)iter)->dflts; |
| 2194 | LY_ARRAY_FOR(dflts, u) { |
| 2195 | ret = lyd_create_term2(iter, dflts[u], &node); |
| 2196 | if (ret == LY_EINCOMPLETE) { |
| 2197 | if (node_types) { |
| 2198 | /* remember to resolve type */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 2199 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2200 | } |
| 2201 | } else if (ret) { |
| 2202 | return ret; |
| 2203 | } |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2204 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2205 | lyd_insert_node(parent, first, node, 0); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2206 | |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2207 | if (lysc_has_when(iter) && node_when) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2208 | /* remember to resolve when */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 2209 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2210 | } |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2211 | if (node_exts) { |
| 2212 | /* remember to call all the extension's validation callbacks */ |
| 2213 | LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node)); |
| 2214 | } |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2215 | if (diff) { |
| 2216 | /* add into diff */ |
| 2217 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 2218 | } |
| 2219 | } |
| 2220 | } |
| 2221 | break; |
| 2222 | default: |
| 2223 | /* without defaults */ |
| 2224 | break; |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | return LY_SUCCESS; |
| 2229 | } |
| 2230 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2231 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2232 | lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2233 | { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2234 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2235 | struct lyd_node *node; |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2236 | struct ly_set node_when = {0}, node_exts = {0}; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2237 | |
| 2238 | LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL); |
| 2239 | if (diff) { |
| 2240 | *diff = NULL; |
| 2241 | } |
| 2242 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2243 | LYD_TREE_DFS_BEGIN(tree, node) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2244 | /* skip added default nodes */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2245 | if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) && |
| 2246 | (node->schema->nodetype & LYD_NODE_INNER)) { |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2247 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, &node_exts, |
Michal Vasko | c43c8ab | 2021-03-05 13:32:44 +0100 | [diff] [blame] | 2248 | NULL, implicit_options, diff), cleanup); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2249 | } |
| 2250 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2251 | LYD_TREE_DFS_END(tree, node); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2252 | } |
| 2253 | |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2254 | /* resolve when and remove any invalid defaults */ |
Michal Vasko | 0110cc9 | 2021-12-06 15:42:22 +0100 | [diff] [blame] | 2255 | LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, LYXP_IGNORE_WHEN, &node_exts, NULL, NULL, diff), |
| 2256 | cleanup); |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2257 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2258 | cleanup: |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2259 | ly_set_erase(&node_when, NULL); |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2260 | ly_set_erase(&node_exts, NULL); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2261 | if (ret && diff) { |
| 2262 | lyd_free_all(*diff); |
| 2263 | *diff = NULL; |
| 2264 | } |
| 2265 | return ret; |
| 2266 | } |
| 2267 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2268 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2269 | lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2270 | { |
| 2271 | const struct lys_module *mod; |
| 2272 | struct lyd_node *d = NULL; |
| 2273 | uint32_t i = 0; |
| 2274 | LY_ERR ret = LY_SUCCESS; |
| 2275 | |
| 2276 | LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2277 | LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2278 | if (diff) { |
| 2279 | *diff = NULL; |
| 2280 | } |
| 2281 | if (!ctx) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2282 | ctx = LYD_CTX(*tree); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | /* add nodes for each module one-by-one */ |
| 2286 | while ((mod = ly_ctx_get_module_iter(ctx, &i))) { |
| 2287 | if (!mod->implemented) { |
| 2288 | continue; |
| 2289 | } |
| 2290 | |
| 2291 | LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup); |
| 2292 | if (d) { |
| 2293 | /* merge into one diff */ |
| 2294 | lyd_insert_sibling(*diff, d, diff); |
| 2295 | |
| 2296 | d = NULL; |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | cleanup: |
| 2301 | if (ret && diff) { |
| 2302 | lyd_free_all(*diff); |
| 2303 | *diff = NULL; |
| 2304 | } |
| 2305 | return ret; |
| 2306 | } |
| 2307 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2308 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2309 | lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options, |
| 2310 | struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2311 | { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2312 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2313 | struct lyd_node *root, *d = NULL; |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2314 | struct ly_set node_when = {0}, node_exts = {0}; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2315 | |
| 2316 | LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2317 | LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2318 | if (diff) { |
| 2319 | *diff = NULL; |
| 2320 | } |
| 2321 | |
| 2322 | /* add all top-level defaults for this module */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2323 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, &node_exts, NULL, implicit_options, diff), cleanup); |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2324 | |
| 2325 | /* resolve when and remove any invalid defaults */ |
Michal Vasko | 0110cc9 | 2021-12-06 15:42:22 +0100 | [diff] [blame] | 2326 | LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, LYXP_IGNORE_WHEN, &node_exts, NULL, NULL, diff), |
| 2327 | cleanup); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2328 | |
| 2329 | /* process nested nodes */ |
| 2330 | LY_LIST_FOR(*tree, root) { |
| 2331 | /* skip added default nodes */ |
| 2332 | if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) { |
| 2333 | LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup); |
| 2334 | |
| 2335 | if (d) { |
| 2336 | /* merge into one diff */ |
| 2337 | lyd_insert_sibling(*diff, d, diff); |
| 2338 | |
| 2339 | d = NULL; |
| 2340 | } |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | cleanup: |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2345 | ly_set_erase(&node_when, NULL); |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2346 | ly_set_erase(&node_exts, NULL); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2347 | if (ret && diff) { |
| 2348 | lyd_free_all(*diff); |
| 2349 | *diff = NULL; |
| 2350 | } |
| 2351 | return ret; |
| 2352 | } |
| 2353 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2354 | struct lyd_node * |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2355 | lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2356 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2357 | const struct lysc_node *schema, *sparent; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2358 | struct lyd_node *match = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2359 | ly_bool found; |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2360 | uint32_t getnext_opts; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2361 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2362 | assert(new_node); |
| 2363 | |
| 2364 | if (!first_sibling || !new_node->schema) { |
| 2365 | /* insert at the end, no next anchor */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2366 | return NULL; |
| 2367 | } |
| 2368 | |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2369 | getnext_opts = 0; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 2370 | if (new_node->schema->flags & LYS_IS_OUTPUT) { |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2371 | getnext_opts = LYS_GETNEXT_OUTPUT; |
| 2372 | } |
| 2373 | |
Michal Vasko | a2756f0 | 2021-07-09 13:20:28 +0200 | [diff] [blame] | 2374 | if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2375 | /* find the anchor using hashes */ |
| 2376 | sparent = first_sibling->parent->schema; |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2377 | schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2378 | while (schema) { |
| 2379 | /* keep trying to find the first existing instance of the closest following schema sibling, |
| 2380 | * otherwise return NULL - inserting at the end */ |
| 2381 | if (!lyd_find_sibling_schema(first_sibling, schema, &match)) { |
| 2382 | break; |
| 2383 | } |
| 2384 | |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2385 | schema = lys_getnext(schema, sparent, NULL, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2386 | } |
| 2387 | } else { |
| 2388 | /* find the anchor without hashes */ |
| 2389 | match = (struct lyd_node *)first_sibling; |
Michal Vasko | 3a70862 | 2021-07-15 14:20:10 +0200 | [diff] [blame] | 2390 | sparent = lysc_data_parent(new_node->schema); |
| 2391 | if (!sparent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2392 | /* we are in top-level, skip all the data from preceding modules */ |
| 2393 | LY_LIST_FOR(match, match) { |
| 2394 | if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) { |
| 2395 | break; |
| 2396 | } |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | /* get the first schema sibling */ |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2401 | schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2402 | |
| 2403 | found = 0; |
| 2404 | LY_LIST_FOR(match, match) { |
| 2405 | if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) { |
| 2406 | /* we have found an opaque node, which must be at the end, so use it OR |
| 2407 | * modules do not match, so we must have traversed all the data from new_node module (if any), |
| 2408 | * we have found the first node of the next module, that is what we want */ |
| 2409 | break; |
| 2410 | } |
| 2411 | |
| 2412 | /* skip schema nodes until we find the instantiated one */ |
| 2413 | while (!found) { |
| 2414 | if (new_node->schema == schema) { |
| 2415 | /* we have found the schema of the new node, continue search to find the first |
| 2416 | * data node with a different schema (after our schema) */ |
| 2417 | found = 1; |
| 2418 | break; |
| 2419 | } |
| 2420 | if (match->schema == schema) { |
| 2421 | /* current node (match) is a data node still before the new node, continue search in data */ |
| 2422 | break; |
| 2423 | } |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2424 | schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2425 | assert(schema); |
| 2426 | } |
| 2427 | |
| 2428 | if (found && (match->schema != new_node->schema)) { |
| 2429 | /* find the next node after we have found our node schema data instance */ |
| 2430 | break; |
| 2431 | } |
| 2432 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | return match; |
| 2436 | } |
| 2437 | |
| 2438 | /** |
| 2439 | * @brief Insert node after a sibling. |
| 2440 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2441 | * Handles inserting into NP containers and key-less lists. |
| 2442 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2443 | * @param[in] sibling Sibling to insert after. |
| 2444 | * @param[in] node Node to insert. |
| 2445 | */ |
| 2446 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2447 | lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2448 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2449 | struct lyd_node_inner *par; |
| 2450 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2451 | assert(!node->next && (node->prev == node)); |
| 2452 | |
| 2453 | node->next = sibling->next; |
| 2454 | node->prev = sibling; |
| 2455 | sibling->next = node; |
| 2456 | if (node->next) { |
| 2457 | /* sibling had a succeeding node */ |
| 2458 | node->next->prev = node; |
| 2459 | } else { |
| 2460 | /* sibling was last, find first sibling and change its prev */ |
| 2461 | if (sibling->parent) { |
| 2462 | sibling = sibling->parent->child; |
| 2463 | } else { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2464 | for ( ; sibling->prev->next != node; sibling = sibling->prev) {} |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2465 | } |
| 2466 | sibling->prev = node; |
| 2467 | } |
| 2468 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2469 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2470 | for (par = node->parent; par; par = par->parent) { |
| 2471 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 2472 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2473 | par->flags &= ~LYD_DEFAULT; |
| 2474 | } |
| 2475 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | /** |
| 2479 | * @brief Insert node before a sibling. |
| 2480 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2481 | * Handles inserting into NP containers and key-less lists. |
| 2482 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2483 | * @param[in] sibling Sibling to insert before. |
| 2484 | * @param[in] node Node to insert. |
| 2485 | */ |
| 2486 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2487 | lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2488 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2489 | struct lyd_node_inner *par; |
| 2490 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2491 | assert(!node->next && (node->prev == node)); |
| 2492 | |
| 2493 | node->next = sibling; |
| 2494 | /* covers situation of sibling being first */ |
| 2495 | node->prev = sibling->prev; |
| 2496 | sibling->prev = node; |
| 2497 | if (node->prev->next) { |
| 2498 | /* sibling had a preceding node */ |
| 2499 | node->prev->next = node; |
| 2500 | } else if (sibling->parent) { |
| 2501 | /* sibling was first and we must also change parent child pointer */ |
| 2502 | sibling->parent->child = node; |
| 2503 | } |
| 2504 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2505 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2506 | for (par = node->parent; par; par = par->parent) { |
| 2507 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 2508 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2509 | par->flags &= ~LYD_DEFAULT; |
| 2510 | } |
| 2511 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2515 | * @brief Insert node as the first and only child of a parent. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2516 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2517 | * Handles inserting into NP containers and key-less lists. |
| 2518 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2519 | * @param[in] parent Parent to insert into. |
| 2520 | * @param[in] node Node to insert. |
| 2521 | */ |
| 2522 | static void |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2523 | lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2524 | { |
| 2525 | struct lyd_node_inner *par; |
| 2526 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 2527 | assert(parent && !lyd_child(parent) && !node->next && (node->prev == node)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2528 | assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2529 | |
| 2530 | par = (struct lyd_node_inner *)parent; |
| 2531 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2532 | par->child = node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2533 | node->parent = par; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2534 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2535 | for ( ; par; par = par->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2536 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 2537 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2538 | par->flags &= ~LYD_DEFAULT; |
| 2539 | } |
| 2540 | } |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 2541 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2542 | |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 2543 | /** |
| 2544 | * @brief Learn whether a list instance has all the keys. |
| 2545 | * |
| 2546 | * @param[in] list List instance to check. |
| 2547 | * @return non-zero if all the keys were found, |
| 2548 | * @return 0 otherwise. |
| 2549 | */ |
| 2550 | static int |
| 2551 | lyd_insert_has_keys(const struct lyd_node *list) |
| 2552 | { |
| 2553 | const struct lyd_node *key; |
| 2554 | const struct lysc_node *skey = NULL; |
| 2555 | |
| 2556 | assert(list->schema->nodetype == LYS_LIST); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 2557 | key = lyd_child(list); |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 2558 | while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 2559 | if (!key || (key->schema != skey)) { |
| 2560 | /* key missing */ |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
| 2564 | key = key->next; |
| 2565 | } |
| 2566 | |
| 2567 | /* all keys found */ |
| 2568 | return 1; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | void |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2572 | lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, ly_bool last) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2573 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2574 | struct lyd_node *anchor, *first_sibling; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2575 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2576 | /* inserting list without its keys is not supported */ |
| 2577 | assert((parent || first_sibling_p) && node && (node->hash || !node->schema)); |
Michal Vasko | f756c94 | 2020-11-06 17:21:37 +0100 | [diff] [blame] | 2578 | assert(!parent || !parent->schema || |
| 2579 | (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF))); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2580 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2581 | if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2582 | parent = lyd_parent(*first_sibling_p); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2583 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2584 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2585 | /* get first sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2586 | first_sibling = parent ? lyd_child(parent) : *first_sibling_p; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2587 | |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2588 | if (last) { |
| 2589 | /* no next anchor */ |
| 2590 | anchor = NULL; |
| 2591 | } else { |
| 2592 | /* find the anchor, our next node, so we can insert before it */ |
| 2593 | anchor = lyd_insert_get_next_anchor(first_sibling, node); |
| 2594 | } |
| 2595 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2596 | if (anchor) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2597 | /* insert before the anchor */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2598 | lyd_insert_before_node(anchor, node); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 2599 | if (!parent && (*first_sibling_p == anchor)) { |
| 2600 | /* move first sibling */ |
| 2601 | *first_sibling_p = node; |
| 2602 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2603 | } else if (first_sibling) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2604 | /* insert as the last node */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2605 | lyd_insert_after_node(first_sibling->prev, node); |
| 2606 | } else if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2607 | /* insert as the only child */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2608 | lyd_insert_only_child(parent, node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2609 | } else { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2610 | /* insert as the only sibling */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2611 | *first_sibling_p = node; |
| 2612 | } |
| 2613 | |
| 2614 | /* insert into parent HT */ |
| 2615 | lyd_insert_hash(node); |
| 2616 | |
| 2617 | /* finish hashes for our parent, if needed and possible */ |
Michal Vasko | 9e8de2d | 2020-09-01 08:17:10 +0200 | [diff] [blame] | 2618 | if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2619 | lyd_hash(parent); |
| 2620 | |
| 2621 | /* now we can insert even the list into its parent HT */ |
| 2622 | lyd_insert_hash(parent); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2623 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2624 | } |
| 2625 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2626 | /** |
| 2627 | * @brief Check schema place of a node to be inserted. |
| 2628 | * |
| 2629 | * @param[in] parent Schema node of the parent data node. |
| 2630 | * @param[in] sibling Schema node of a sibling data node. |
| 2631 | * @param[in] schema Schema node if the data node to be inserted. |
| 2632 | * @return LY_SUCCESS on success. |
| 2633 | * @return LY_EINVAL if the place is invalid. |
| 2634 | */ |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2635 | static LY_ERR |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2636 | lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *sibling, const struct lysc_node *schema) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2637 | { |
| 2638 | const struct lysc_node *par2; |
| 2639 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 2640 | assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2641 | assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 2642 | assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2643 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2644 | if (!schema || (!parent && !sibling)) { |
Michal Vasko | 71e795e | 2020-12-04 16:27:37 +0100 | [diff] [blame] | 2645 | /* opaque nodes can be inserted wherever */ |
| 2646 | return LY_SUCCESS; |
| 2647 | } |
| 2648 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2649 | if (!parent) { |
| 2650 | parent = lysc_data_parent(sibling); |
| 2651 | } |
| 2652 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2653 | /* find schema parent */ |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 2654 | par2 = lysc_data_parent(schema); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2655 | |
| 2656 | if (parent) { |
| 2657 | /* inner node */ |
| 2658 | if (par2 != parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2659 | LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2660 | parent->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2661 | return LY_EINVAL; |
| 2662 | } |
| 2663 | } else { |
| 2664 | /* top-level node */ |
| 2665 | if (par2) { |
Radek Krejci | f6d14cb | 2020-07-02 16:11:45 +0200 | [diff] [blame] | 2666 | LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2667 | return LY_EINVAL; |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | return LY_SUCCESS; |
| 2672 | } |
| 2673 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2674 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2675 | lyd_insert_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2676 | { |
| 2677 | struct lyd_node *iter; |
| 2678 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 2679 | LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2680 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2681 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2682 | LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2683 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 2684 | if (node->schema && (node->schema->flags & LYS_KEY)) { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2685 | LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
| 2686 | return LY_EINVAL; |
| 2687 | } |
| 2688 | |
| 2689 | if (node->parent || node->prev->next) { |
| 2690 | lyd_unlink_tree(node); |
| 2691 | } |
| 2692 | |
| 2693 | while (node) { |
| 2694 | iter = node->next; |
| 2695 | lyd_unlink_tree(node); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2696 | lyd_insert_node(parent, NULL, node, 0); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2697 | node = iter; |
| 2698 | } |
| 2699 | return LY_SUCCESS; |
| 2700 | } |
| 2701 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2702 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2703 | lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2704 | { |
| 2705 | struct lyd_node *iter; |
| 2706 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2707 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2708 | LY_CHECK_CTX_EQUAL_RET(sibling ? LYD_CTX(sibling) : NULL, LYD_CTX(node), LY_EINVAL); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2709 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2710 | if (sibling) { |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2711 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2712 | } |
| 2713 | |
Michal Vasko | 553d0b5 | 2020-12-04 16:27:52 +0100 | [diff] [blame] | 2714 | if (sibling == node) { |
| 2715 | /* we need to keep the connection to siblings so we can insert into them */ |
| 2716 | sibling = sibling->prev; |
| 2717 | } |
| 2718 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2719 | if (node->parent || node->prev->next) { |
| 2720 | lyd_unlink_tree(node); |
| 2721 | } |
| 2722 | |
| 2723 | while (node) { |
Michal Vasko | 71e795e | 2020-12-04 16:27:37 +0100 | [diff] [blame] | 2724 | if (lysc_is_key(node->schema)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2725 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2726 | return LY_EINVAL; |
| 2727 | } |
| 2728 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2729 | iter = node->next; |
| 2730 | lyd_unlink_tree(node); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2731 | lyd_insert_node(NULL, &sibling, node, 0); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2732 | node = iter; |
| 2733 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2734 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2735 | if (first) { |
| 2736 | /* find the first sibling */ |
| 2737 | *first = sibling; |
| 2738 | while ((*first)->prev->next) { |
| 2739 | *first = (*first)->prev; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2740 | } |
| 2741 | } |
| 2742 | |
| 2743 | return LY_SUCCESS; |
| 2744 | } |
| 2745 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2746 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2747 | lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node) |
| 2748 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2749 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2750 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2751 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2752 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2753 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2754 | if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2755 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes."); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2756 | return LY_EINVAL; |
| 2757 | } |
| 2758 | |
Michal Vasko | 4bc2ce3 | 2020-12-08 10:06:16 +0100 | [diff] [blame] | 2759 | lyd_unlink_tree(node); |
| 2760 | lyd_insert_before_node(sibling, node); |
| 2761 | lyd_insert_hash(node); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2762 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2763 | return LY_SUCCESS; |
| 2764 | } |
| 2765 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2766 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2767 | lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node) |
| 2768 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2769 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2770 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2771 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2772 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2773 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2774 | if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2775 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes."); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2776 | return LY_EINVAL; |
| 2777 | } |
| 2778 | |
Michal Vasko | 4bc2ce3 | 2020-12-08 10:06:16 +0100 | [diff] [blame] | 2779 | lyd_unlink_tree(node); |
| 2780 | lyd_insert_after_node(sibling, node); |
| 2781 | lyd_insert_hash(node); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2782 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2783 | return LY_SUCCESS; |
| 2784 | } |
| 2785 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2786 | LIBYANG_API_DEF void |
Michal Vasko | 66d508c | 2021-07-21 16:07:09 +0200 | [diff] [blame] | 2787 | lyd_unlink_siblings(struct lyd_node *node) |
| 2788 | { |
| 2789 | struct lyd_node *next, *elem, *first = NULL; |
| 2790 | |
| 2791 | LY_LIST_FOR_SAFE(node, next, elem) { |
| 2792 | lyd_unlink_tree(elem); |
| 2793 | lyd_insert_node(NULL, &first, elem, 1); |
| 2794 | } |
| 2795 | } |
| 2796 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2797 | LIBYANG_API_DEF void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2798 | lyd_unlink_tree(struct lyd_node *node) |
| 2799 | { |
| 2800 | struct lyd_node *iter; |
| 2801 | |
| 2802 | if (!node) { |
| 2803 | return; |
| 2804 | } |
| 2805 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2806 | /* update hashes while still linked into the tree */ |
| 2807 | lyd_unlink_hash(node); |
| 2808 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2809 | /* unlink from siblings */ |
| 2810 | if (node->prev->next) { |
| 2811 | node->prev->next = node->next; |
| 2812 | } |
| 2813 | if (node->next) { |
| 2814 | node->next->prev = node->prev; |
| 2815 | } else { |
| 2816 | /* unlinking the last node */ |
| 2817 | if (node->parent) { |
| 2818 | iter = node->parent->child; |
| 2819 | } else { |
| 2820 | iter = node->prev; |
| 2821 | while (iter->prev != node) { |
| 2822 | iter = iter->prev; |
| 2823 | } |
| 2824 | } |
| 2825 | /* update the "last" pointer from the first node */ |
| 2826 | iter->prev = node->prev; |
| 2827 | } |
| 2828 | |
| 2829 | /* unlink from parent */ |
| 2830 | if (node->parent) { |
| 2831 | if (node->parent->child == node) { |
| 2832 | /* the node is the first child */ |
| 2833 | node->parent->child = node->next; |
| 2834 | } |
| 2835 | |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 2836 | /* check for NP container whether its last non-default node is not being unlinked */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2837 | if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) && |
| 2838 | !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) { |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 2839 | LY_LIST_FOR(node->parent->child, iter) { |
| 2840 | if ((iter != node) && !(iter->flags & LYD_DEFAULT)) { |
| 2841 | break; |
| 2842 | } |
| 2843 | } |
| 2844 | if (!iter) { |
| 2845 | node->parent->flags |= LYD_DEFAULT; |
| 2846 | } |
| 2847 | } |
| 2848 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2849 | node->parent = NULL; |
| 2850 | } |
| 2851 | |
| 2852 | node->next = NULL; |
| 2853 | node->prev = node; |
| 2854 | } |
| 2855 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2856 | void |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 2857 | lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2858 | { |
| 2859 | struct lyd_meta *last, *iter; |
| 2860 | |
| 2861 | assert(parent); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2862 | |
| 2863 | if (!meta) { |
| 2864 | return; |
| 2865 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2866 | |
| 2867 | for (iter = meta; iter; iter = iter->next) { |
| 2868 | iter->parent = parent; |
| 2869 | } |
| 2870 | |
| 2871 | /* insert as the last attribute */ |
| 2872 | if (parent->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2873 | for (last = parent->meta; last->next; last = last->next) {} |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2874 | last->next = meta; |
| 2875 | } else { |
| 2876 | parent->meta = meta; |
| 2877 | } |
| 2878 | |
| 2879 | /* remove default flags from NP containers */ |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 2880 | while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2881 | parent->flags &= ~LYD_DEFAULT; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2882 | parent = lyd_parent(parent); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2883 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | LY_ERR |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2887 | lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 2888 | size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 2889 | void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2890 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2891 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2892 | struct lysc_ext_instance *ant = NULL; |
Michal Vasko | b0534f9 | 2021-07-01 13:24:03 +0200 | [diff] [blame] | 2893 | const struct lysc_type **ant_type; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2894 | struct lyd_meta *mt, *last; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2895 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2896 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2897 | assert((parent || meta) && mod); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 2898 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 2899 | LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2900 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2901 | LY_ARRAY_FOR(mod->compiled->exts, u) { |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 2902 | if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) && |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2903 | !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2904 | /* we have the annotation definition */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2905 | ant = &mod->compiled->exts[u]; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2906 | break; |
| 2907 | } |
| 2908 | } |
| 2909 | if (!ant) { |
| 2910 | /* attribute is not defined as a metadata annotation (RFC 7952) */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2911 | LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 2912 | mod->name, (int)name_len, name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2913 | ret = LY_EINVAL; |
| 2914 | goto cleanup; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2915 | } |
| 2916 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2917 | mt = calloc(1, sizeof *mt); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2918 | LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2919 | mt->parent = parent; |
| 2920 | mt->annotation = ant; |
Michal Vasko | b0534f9 | 2021-07-01 13:24:03 +0200 | [diff] [blame] | 2921 | ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage; |
| 2922 | ret = lyd_value_store(mod->ctx, &mt->value, *ant_type, value, value_len, dynamic, format, prefix_data, hints, |
| 2923 | parent ? parent->schema : NULL, incomplete); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2924 | LY_CHECK_ERR_GOTO(ret, free(mt), cleanup); |
| 2925 | ret = lydict_insert(mod->ctx, name, name_len, &mt->name); |
| 2926 | LY_CHECK_ERR_GOTO(ret, free(mt), cleanup); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2927 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 2928 | /* insert as the last attribute */ |
| 2929 | if (parent) { |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 2930 | lyd_insert_meta(parent, mt, clear_dflt); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2931 | } else if (*meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2932 | for (last = *meta; last->next; last = last->next) {} |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2933 | last->next = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2934 | } |
| 2935 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2936 | if (meta) { |
| 2937 | *meta = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2938 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2939 | |
| 2940 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 2941 | LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2942 | return ret; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2943 | } |
| 2944 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2945 | void |
| 2946 | lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr) |
| 2947 | { |
| 2948 | struct lyd_attr *last, *iter; |
| 2949 | struct lyd_node_opaq *opaq; |
| 2950 | |
| 2951 | assert(parent && !parent->schema); |
| 2952 | |
| 2953 | if (!attr) { |
| 2954 | return; |
| 2955 | } |
| 2956 | |
| 2957 | opaq = (struct lyd_node_opaq *)parent; |
| 2958 | for (iter = attr; iter; iter = iter->next) { |
| 2959 | iter->parent = opaq; |
| 2960 | } |
| 2961 | |
| 2962 | /* insert as the last attribute */ |
| 2963 | if (opaq->attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2964 | for (last = opaq->attr; last->next; last = last->next) {} |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2965 | last->next = attr; |
| 2966 | } else { |
| 2967 | opaq->attr = attr; |
| 2968 | } |
| 2969 | } |
| 2970 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2971 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2972 | lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len, |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 2973 | const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 2974 | size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2975 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2976 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2977 | struct lyd_attr *at, *last; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2978 | |
| 2979 | assert(ctx && (parent || attr) && (!parent || !parent->schema)); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2980 | assert(name && name_len && format); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2981 | |
Michal Vasko | 2a3722d | 2021-06-16 11:52:39 +0200 | [diff] [blame] | 2982 | if (!value_len && (!dynamic || !*dynamic)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2983 | value = ""; |
| 2984 | } |
| 2985 | |
| 2986 | at = calloc(1, sizeof *at); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 2987 | LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM); |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 2988 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 2989 | LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 2990 | if (prefix_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 2991 | LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 2992 | } |
| 2993 | if (module_key_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 2994 | LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 2995 | } |
| 2996 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2997 | if (dynamic && *dynamic) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2998 | ret = lydict_insert_zc(ctx, (char *)value, &at->value); |
| 2999 | LY_CHECK_GOTO(ret, finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3000 | *dynamic = 0; |
| 3001 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3002 | LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3003 | } |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 3004 | at->format = format; |
| 3005 | at->val_prefix_data = val_prefix_data; |
| 3006 | at->hints = hints; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3007 | |
| 3008 | /* insert as the last attribute */ |
| 3009 | if (parent) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 3010 | lyd_insert_attr(parent, at); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3011 | } else if (*attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 3012 | for (last = *attr; last->next; last = last->next) {} |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3013 | last->next = at; |
| 3014 | } |
| 3015 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3016 | finish: |
| 3017 | if (ret) { |
| 3018 | lyd_free_attr_single(ctx, at); |
| 3019 | } else if (attr) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3020 | *attr = at; |
| 3021 | } |
| 3022 | return LY_SUCCESS; |
| 3023 | } |
| 3024 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3025 | LIBYANG_API_DEF const struct lyd_node_term * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3026 | lyd_target(const struct ly_path *path, const struct lyd_node *tree) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 3027 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3028 | struct lyd_node *target; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 3029 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3030 | if (ly_path_eval(path, tree, &target)) { |
| 3031 | return NULL; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 3032 | } |
| 3033 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3034 | return (struct lyd_node_term *)target; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 3035 | } |
| 3036 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3037 | /** |
| 3038 | * @brief Check the equality of the two schemas from different contexts. |
| 3039 | * |
| 3040 | * @param schema1 of first node. |
| 3041 | * @param schema2 of second node. |
| 3042 | * @return 1 if the schemas are equal otherwise 0. |
| 3043 | */ |
| 3044 | static ly_bool |
| 3045 | lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2) |
| 3046 | { |
| 3047 | if (!schema1 && !schema2) { |
| 3048 | return 1; |
| 3049 | } else if (!schema1 || !schema2) { |
| 3050 | return 0; |
| 3051 | } |
| 3052 | |
| 3053 | assert(schema1->module->ctx != schema2->module->ctx); |
| 3054 | |
| 3055 | if (schema1->nodetype != schema2->nodetype) { |
| 3056 | return 0; |
| 3057 | } |
| 3058 | |
| 3059 | if (strcmp(schema1->name, schema2->name)) { |
| 3060 | return 0; |
| 3061 | } |
| 3062 | |
| 3063 | if (strcmp(schema1->module->name, schema2->module->name)) { |
| 3064 | return 0; |
| 3065 | } |
| 3066 | |
| 3067 | if (schema1->module->revision || schema2->module->revision) { |
| 3068 | if (!schema1->module->revision || !schema2->module->revision) { |
| 3069 | return 0; |
| 3070 | } |
| 3071 | if (strcmp(schema1->module->revision, schema2->module->revision)) { |
| 3072 | return 0; |
| 3073 | } |
| 3074 | } |
| 3075 | |
| 3076 | return 1; |
| 3077 | } |
| 3078 | |
| 3079 | /** |
| 3080 | * @brief Check the equality of the schemas for all parent nodes. |
| 3081 | * |
| 3082 | * Both nodes must be from different contexts. |
| 3083 | * |
| 3084 | * @param node1 Data of first node. |
| 3085 | * @param node2 Data of second node. |
| 3086 | * @return 1 if the all related parental schemas are equal otherwise 0. |
| 3087 | */ |
| 3088 | static ly_bool |
| 3089 | lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2) |
| 3090 | { |
| 3091 | const struct lysc_node *parent1, *parent2; |
| 3092 | |
| 3093 | assert(node1 && node2); |
| 3094 | |
| 3095 | for (parent1 = node1->schema->parent, parent2 = node2->schema->parent; |
| 3096 | parent1 && parent2; |
| 3097 | parent1 = parent1->parent, parent2 = parent2->parent) { |
| 3098 | if (!lyd_compare_schema_equal(parent1, parent2)) { |
| 3099 | return 0; |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | if (parent1 || parent2) { |
| 3104 | return 0; |
| 3105 | } |
| 3106 | |
| 3107 | return 1; |
| 3108 | } |
| 3109 | |
| 3110 | /** |
| 3111 | * @brief Internal implementation of @ref lyd_compare_single. |
| 3112 | * @copydoc lyd_compare_single |
| 3113 | * @param[in] parental_schemas_checked Flag used for optimization. |
| 3114 | * When this function is called for the first time, the flag must be set to 0. |
| 3115 | * The @ref lyd_compare_schema_parents_equal should be called only once during |
| 3116 | * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body. |
| 3117 | */ |
| 3118 | static LY_ERR |
| 3119 | lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2, |
| 3120 | uint32_t options, ly_bool parental_schemas_checked) |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3121 | { |
| 3122 | const struct lyd_node *iter1, *iter2; |
| 3123 | struct lyd_node_term *term1, *term2; |
| 3124 | struct lyd_node_any *any1, *any2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3125 | struct lyd_node_opaq *opaq1, *opaq2; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3126 | size_t len1, len2; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 3127 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3128 | if (!node1 || !node2) { |
| 3129 | if (node1 == node2) { |
| 3130 | return LY_SUCCESS; |
| 3131 | } else { |
| 3132 | return LY_ENOT; |
| 3133 | } |
| 3134 | } |
| 3135 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3136 | if (LYD_CTX(node1) == LYD_CTX(node2)) { |
| 3137 | /* same contexts */ |
| 3138 | if (node1->schema != node2->schema) { |
| 3139 | return LY_ENOT; |
| 3140 | } |
| 3141 | } else { |
| 3142 | /* different contexts */ |
| 3143 | if (!lyd_compare_schema_equal(node1->schema, node2->schema)) { |
| 3144 | return LY_ENOT; |
| 3145 | } |
| 3146 | if (!parental_schemas_checked) { |
| 3147 | if (!lyd_compare_schema_parents_equal(node1, node2)) { |
| 3148 | return LY_ENOT; |
| 3149 | } |
| 3150 | parental_schemas_checked = 1; |
| 3151 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | if (node1->hash != node2->hash) { |
| 3155 | return LY_ENOT; |
| 3156 | } |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3157 | /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3158 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3159 | if (!node1->schema) { |
| 3160 | opaq1 = (struct lyd_node_opaq *)node1; |
| 3161 | opaq2 = (struct lyd_node_opaq *)node2; |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3162 | if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) || |
| 3163 | (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3164 | return LY_ENOT; |
| 3165 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3166 | switch (opaq1->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 3167 | case LY_VALUE_XML: |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 3168 | if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value, |
| 3169 | opaq2->val_prefix_data)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3170 | return LY_ENOT; |
| 3171 | } |
| 3172 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 3173 | case LY_VALUE_JSON: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 3174 | /* prefixes in JSON are unique, so it is not necessary to canonize the values */ |
| 3175 | if (strcmp(opaq1->value, opaq2->value)) { |
| 3176 | return LY_ENOT; |
| 3177 | } |
| 3178 | break; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 3179 | default: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3180 | /* not allowed */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3181 | LOGINT(LYD_CTX(node1)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3182 | return LY_EINT; |
| 3183 | } |
| 3184 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 3185 | iter1 = opaq1->child; |
| 3186 | iter2 = opaq2->child; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3187 | goto all_children_compare; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3188 | } |
| 3189 | return LY_SUCCESS; |
| 3190 | } else { |
| 3191 | switch (node1->schema->nodetype) { |
| 3192 | case LYS_LEAF: |
| 3193 | case LYS_LEAFLIST: |
| 3194 | if (options & LYD_COMPARE_DEFAULTS) { |
| 3195 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 3196 | return LY_ENOT; |
| 3197 | } |
| 3198 | } |
| 3199 | |
Michal Vasko | 1a2bf2e | 2020-06-16 13:07:48 +0200 | [diff] [blame] | 3200 | term1 = (struct lyd_node_term *)node1; |
| 3201 | term2 = (struct lyd_node_term *)node2; |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3202 | |
| 3203 | /* same contexts */ |
| 3204 | if (LYD_CTX(node1) == LYD_CTX(node2)) { |
| 3205 | return term1->value.realtype->plugin->compare(&term1->value, &term2->value); |
| 3206 | } |
| 3207 | |
| 3208 | /* different contexts */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 3209 | if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3210 | return LY_ENOT; |
| 3211 | } |
| 3212 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3213 | case LYS_CONTAINER: |
| 3214 | if (options & LYD_COMPARE_DEFAULTS) { |
| 3215 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 3216 | return LY_ENOT; |
| 3217 | } |
| 3218 | } |
| 3219 | if (options & LYD_COMPARE_FULL_RECURSION) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 3220 | iter1 = lyd_child(node1); |
| 3221 | iter2 = lyd_child(node2); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3222 | goto all_children_compare; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3223 | } |
| 3224 | return LY_SUCCESS; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3225 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3226 | case LYS_ACTION: |
| 3227 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 3228 | /* TODO action/RPC |
| 3229 | goto all_children_compare; |
| 3230 | */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3231 | } |
| 3232 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3233 | case LYS_NOTIF: |
| 3234 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 3235 | /* TODO Notification |
| 3236 | goto all_children_compare; |
| 3237 | */ |
| 3238 | } |
| 3239 | return LY_SUCCESS; |
| 3240 | case LYS_LIST: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 3241 | iter1 = lyd_child(node1); |
| 3242 | iter2 = lyd_child(node2); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3243 | |
| 3244 | if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) { |
| 3245 | /* lists with keys, their equivalence is based on their keys */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 3246 | for (const struct lysc_node *key = lysc_node_child(node1->schema); |
Michal Vasko | c57d9a9 | 2020-06-23 13:28:27 +0200 | [diff] [blame] | 3247 | key && (key->flags & LYS_KEY); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3248 | key = key->next) { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3249 | if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3250 | return LY_ENOT; |
| 3251 | } |
| 3252 | iter1 = iter1->next; |
| 3253 | iter2 = iter2->next; |
| 3254 | } |
| 3255 | } else { |
| 3256 | /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */ |
| 3257 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3258 | all_children_compare: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3259 | if (!iter1 && !iter2) { |
| 3260 | /* no children, nothing to compare */ |
| 3261 | return LY_SUCCESS; |
| 3262 | } |
| 3263 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 3264 | for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3265 | if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3266 | return LY_ENOT; |
| 3267 | } |
| 3268 | } |
| 3269 | if (iter1 || iter2) { |
| 3270 | return LY_ENOT; |
| 3271 | } |
| 3272 | } |
| 3273 | return LY_SUCCESS; |
| 3274 | case LYS_ANYXML: |
| 3275 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3276 | any1 = (struct lyd_node_any *)node1; |
| 3277 | any2 = (struct lyd_node_any *)node2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3278 | |
| 3279 | if (any1->value_type != any2->value_type) { |
| 3280 | return LY_ENOT; |
| 3281 | } |
| 3282 | switch (any1->value_type) { |
| 3283 | case LYD_ANYDATA_DATATREE: |
| 3284 | iter1 = any1->value.tree; |
| 3285 | iter2 = any2->value.tree; |
| 3286 | goto all_children_compare; |
| 3287 | case LYD_ANYDATA_STRING: |
| 3288 | case LYD_ANYDATA_XML: |
| 3289 | case LYD_ANYDATA_JSON: |
| 3290 | len1 = strlen(any1->value.str); |
| 3291 | len2 = strlen(any2->value.str); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3292 | if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3293 | return LY_ENOT; |
| 3294 | } |
| 3295 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3296 | case LYD_ANYDATA_LYB: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 3297 | len1 = lyd_lyb_data_length(any1->value.mem); |
| 3298 | len2 = lyd_lyb_data_length(any2->value.mem); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3299 | if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3300 | return LY_ENOT; |
| 3301 | } |
| 3302 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3303 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3304 | } |
| 3305 | } |
| 3306 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3307 | LOGINT(LYD_CTX(node1)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3308 | return LY_EINT; |
| 3309 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3310 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3311 | LIBYANG_API_DEF LY_ERR |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3312 | lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
| 3313 | { |
| 3314 | return lyd_compare_single_(node1, node2, options, 0); |
| 3315 | } |
| 3316 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3317 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3318 | lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3319 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 3320 | for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3321 | LY_CHECK_RET(lyd_compare_single(node1, node2, options)); |
| 3322 | } |
| 3323 | |
Michal Vasko | 11a8143 | 2020-07-28 16:31:29 +0200 | [diff] [blame] | 3324 | if (node1 == node2) { |
| 3325 | return LY_SUCCESS; |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3326 | } |
Michal Vasko | 11a8143 | 2020-07-28 16:31:29 +0200 | [diff] [blame] | 3327 | return LY_ENOT; |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3328 | } |
| 3329 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3330 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 3331 | lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2) |
| 3332 | { |
| 3333 | if (!meta1 || !meta2) { |
| 3334 | if (meta1 == meta2) { |
| 3335 | return LY_SUCCESS; |
| 3336 | } else { |
| 3337 | return LY_ENOT; |
| 3338 | } |
| 3339 | } |
| 3340 | |
Michal Vasko | a808306 | 2020-11-06 17:22:10 +0100 | [diff] [blame] | 3341 | if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) { |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 3342 | return LY_ENOT; |
| 3343 | } |
| 3344 | |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 3345 | return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value); |
| 3346 | } |
| 3347 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3348 | /** |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3349 | * @brief Create a copy of the attribute. |
| 3350 | * |
| 3351 | * @param[in] attr Attribute to copy. |
| 3352 | * @param[in] node Opaque where to append the new attribute. |
| 3353 | * @param[out] dup Optional created attribute copy. |
| 3354 | * @return LY_ERR value. |
| 3355 | */ |
| 3356 | static LY_ERR |
| 3357 | lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup) |
| 3358 | { |
| 3359 | LY_ERR ret = LY_SUCCESS; |
| 3360 | struct lyd_attr *a, *last; |
| 3361 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node; |
| 3362 | |
| 3363 | LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL); |
| 3364 | |
| 3365 | /* create a copy */ |
| 3366 | a = calloc(1, sizeof *attr); |
| 3367 | LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM); |
| 3368 | |
| 3369 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish); |
| 3370 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish); |
| 3371 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish); |
| 3372 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish); |
| 3373 | a->hints = attr->hints; |
| 3374 | a->format = attr->format; |
| 3375 | if (attr->val_prefix_data) { |
| 3376 | ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data); |
| 3377 | LY_CHECK_GOTO(ret, finish); |
| 3378 | } |
| 3379 | |
| 3380 | /* insert as the last attribute */ |
| 3381 | a->parent = opaq; |
| 3382 | if (opaq->attr) { |
| 3383 | for (last = opaq->attr; last->next; last = last->next) {} |
| 3384 | last->next = a; |
| 3385 | } else { |
| 3386 | opaq->attr = a; |
| 3387 | } |
| 3388 | |
| 3389 | finish: |
| 3390 | if (ret) { |
| 3391 | lyd_free_attr_single(LYD_CTX(node), a); |
| 3392 | } else if (dup) { |
| 3393 | *dup = a; |
| 3394 | } |
| 3395 | return LY_SUCCESS; |
| 3396 | } |
| 3397 | |
| 3398 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3399 | * @brief Duplicate a single node and connect it into @p parent (if present) or last of @p first siblings. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3400 | * |
| 3401 | * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup(). |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 3402 | * |
| 3403 | * @param[in] node Original node to duplicate |
| 3404 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3405 | * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for |
| 3406 | * recursive duplication as an optimization. |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 3407 | * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set. |
| 3408 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
| 3409 | * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling). |
| 3410 | * @return LY_ERR value |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3411 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3412 | static LY_ERR |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3413 | lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, ly_bool insert_last, struct lyd_node **first, |
| 3414 | uint32_t options, struct lyd_node **dup_p) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3415 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3416 | LY_ERR ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3417 | struct lyd_node *dup = NULL; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 3418 | struct lyd_meta *meta; |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3419 | struct lyd_attr *attr; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 3420 | struct lyd_node_any *any; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3421 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3422 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3423 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3424 | if (!node->schema) { |
| 3425 | dup = calloc(1, sizeof(struct lyd_node_opaq)); |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3426 | ((struct lyd_node_opaq *)dup)->ctx = LYD_CTX(node); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3427 | } else { |
| 3428 | switch (node->schema->nodetype) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3429 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3430 | case LYS_ACTION: |
| 3431 | case LYS_NOTIF: |
| 3432 | case LYS_CONTAINER: |
| 3433 | case LYS_LIST: |
| 3434 | dup = calloc(1, sizeof(struct lyd_node_inner)); |
| 3435 | break; |
| 3436 | case LYS_LEAF: |
| 3437 | case LYS_LEAFLIST: |
| 3438 | dup = calloc(1, sizeof(struct lyd_node_term)); |
| 3439 | break; |
| 3440 | case LYS_ANYDATA: |
| 3441 | case LYS_ANYXML: |
| 3442 | dup = calloc(1, sizeof(struct lyd_node_any)); |
| 3443 | break; |
| 3444 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3445 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3446 | ret = LY_EINT; |
| 3447 | goto error; |
| 3448 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3449 | } |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3450 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3451 | |
Michal Vasko | f6df0a0 | 2020-06-16 13:08:34 +0200 | [diff] [blame] | 3452 | if (options & LYD_DUP_WITH_FLAGS) { |
| 3453 | dup->flags = node->flags; |
| 3454 | } else { |
| 3455 | dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW; |
| 3456 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3457 | dup->schema = node->schema; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3458 | dup->prev = dup; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3459 | |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3460 | /* duplicate metadata/attributes */ |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3461 | if (!(options & LYD_DUP_NO_META)) { |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3462 | if (!node->schema) { |
| 3463 | LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) { |
| 3464 | LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error); |
| 3465 | } |
| 3466 | } else { |
| 3467 | LY_LIST_FOR(node->meta, meta) { |
| 3468 | LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error); |
| 3469 | } |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3470 | } |
| 3471 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3472 | |
| 3473 | /* nodetype-specific work */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3474 | if (!dup->schema) { |
| 3475 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup; |
| 3476 | struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node; |
| 3477 | struct lyd_node *child; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3478 | |
| 3479 | if (options & LYD_DUP_RECURSIVE) { |
| 3480 | /* duplicate all the children */ |
| 3481 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3482 | LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, 1, NULL, options, NULL), error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3483 | } |
| 3484 | } |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 3485 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error); |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3486 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error); |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 3487 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.module_ns, 0, &opaq->name.module_ns), error); |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3488 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error); |
| 3489 | opaq->hints = orig->hints; |
| 3490 | opaq->format = orig->format; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 3491 | if (orig->val_prefix_data) { |
| 3492 | ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data); |
| 3493 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3494 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3495 | } else if (dup->schema->nodetype & LYD_NODE_TERM) { |
| 3496 | struct lyd_node_term *term = (struct lyd_node_term *)dup; |
| 3497 | struct lyd_node_term *orig = (struct lyd_node_term *)node; |
| 3498 | |
| 3499 | term->hash = orig->hash; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3500 | LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3501 | LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3502 | } else if (dup->schema->nodetype & LYD_NODE_INNER) { |
| 3503 | struct lyd_node_inner *orig = (struct lyd_node_inner *)node; |
| 3504 | struct lyd_node *child; |
| 3505 | |
| 3506 | if (options & LYD_DUP_RECURSIVE) { |
| 3507 | /* duplicate all the children */ |
| 3508 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3509 | LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, 1, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3510 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3511 | } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3512 | /* always duplicate keys of a list */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3513 | child = orig->child; |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 3514 | for (const struct lysc_node *key = lysc_node_child(dup->schema); |
Michal Vasko | c57d9a9 | 2020-06-23 13:28:27 +0200 | [diff] [blame] | 3515 | key && (key->flags & LYS_KEY); |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3516 | key = key->next) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3517 | if (!child) { |
| 3518 | /* possibly not keys are present in filtered tree */ |
| 3519 | break; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3520 | } else if (child->schema != key) { |
| 3521 | /* possibly not all keys are present in filtered tree, |
| 3522 | * but there can be also some non-key nodes */ |
| 3523 | continue; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3524 | } |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3525 | LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, 1, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3526 | child = child->next; |
| 3527 | } |
| 3528 | } |
| 3529 | lyd_hash(dup); |
| 3530 | } else if (dup->schema->nodetype & LYD_NODE_ANY) { |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 3531 | dup->hash = node->hash; |
| 3532 | any = (struct lyd_node_any *)node; |
| 3533 | LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3536 | /* insert */ |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3537 | lyd_insert_node(parent, first, dup, insert_last); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3538 | |
| 3539 | if (dup_p) { |
| 3540 | *dup_p = dup; |
| 3541 | } |
| 3542 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3543 | |
| 3544 | error: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3545 | lyd_free_tree(dup); |
| 3546 | return ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3547 | } |
| 3548 | |
Michal Vasko | 29d674b | 2021-08-25 11:18:35 +0200 | [diff] [blame] | 3549 | /** |
| 3550 | * @brief Get a parent node to connect duplicated subtree to. |
| 3551 | * |
| 3552 | * @param[in] node Node (subtree) to duplicate. |
| 3553 | * @param[in] parent Initial parent to connect to. |
| 3554 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
| 3555 | * @param[out] dup_parent First duplicated parent node, if any. |
| 3556 | * @param[out] local_parent Correct parent to directly connect duplicated @p node to. |
| 3557 | * @return LY_ERR value. |
| 3558 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3559 | static LY_ERR |
Michal Vasko | 2090975 | 2021-05-18 16:13:38 +0200 | [diff] [blame] | 3560 | lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, uint32_t options, |
| 3561 | struct lyd_node **dup_parent, struct lyd_node_inner **local_parent) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3562 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3563 | const struct lyd_node_inner *orig_parent, *iter; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3564 | ly_bool repeat = 1; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3565 | |
| 3566 | *dup_parent = NULL; |
| 3567 | *local_parent = NULL; |
| 3568 | |
| 3569 | for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) { |
| 3570 | if (parent && (parent->schema == orig_parent->schema)) { |
| 3571 | /* stop creating parents, connect what we have into the provided parent */ |
| 3572 | iter = parent; |
| 3573 | repeat = 0; |
| 3574 | } else { |
| 3575 | iter = NULL; |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3576 | LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, 0, (struct lyd_node **)&iter, options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3577 | (struct lyd_node **)&iter)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3578 | } |
| 3579 | if (!*local_parent) { |
| 3580 | *local_parent = (struct lyd_node_inner *)iter; |
| 3581 | } |
| 3582 | if (iter->child) { |
| 3583 | /* 1) list - add after keys |
| 3584 | * 2) provided parent with some children */ |
| 3585 | iter->child->prev->next = *dup_parent; |
| 3586 | if (*dup_parent) { |
| 3587 | (*dup_parent)->prev = iter->child->prev; |
| 3588 | iter->child->prev = *dup_parent; |
| 3589 | } |
| 3590 | } else { |
| 3591 | ((struct lyd_node_inner *)iter)->child = *dup_parent; |
| 3592 | } |
| 3593 | if (*dup_parent) { |
| 3594 | (*dup_parent)->parent = (struct lyd_node_inner *)iter; |
| 3595 | } |
| 3596 | *dup_parent = (struct lyd_node *)iter; |
| 3597 | } |
| 3598 | |
| 3599 | if (repeat && parent) { |
| 3600 | /* given parent and created parents chain actually do not interconnect */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3601 | LOGERR(LYD_CTX(node), LY_EINVAL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3602 | "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3603 | return LY_EINVAL; |
| 3604 | } |
| 3605 | |
| 3606 | return LY_SUCCESS; |
| 3607 | } |
| 3608 | |
| 3609 | static LY_ERR |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3610 | lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, ly_bool nosiblings, |
| 3611 | struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3612 | { |
| 3613 | LY_ERR rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3614 | const struct lyd_node *orig; /* original node to be duplicated */ |
| 3615 | struct lyd_node *first = NULL; /* the first duplicated node, this is returned */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3616 | struct lyd_node *top = NULL; /* the most higher created node */ |
| 3617 | struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3618 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3619 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3620 | |
| 3621 | if (options & LYD_DUP_WITH_PARENTS) { |
Michal Vasko | 2090975 | 2021-05-18 16:13:38 +0200 | [diff] [blame] | 3622 | LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, options & (LYD_DUP_WITH_FLAGS | LYD_DUP_NO_META), |
| 3623 | &top, &local_parent), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3624 | } else { |
| 3625 | local_parent = parent; |
| 3626 | } |
| 3627 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3628 | LY_LIST_FOR(node, orig) { |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 3629 | if (lysc_is_key(orig->schema)) { |
| 3630 | if (local_parent) { |
| 3631 | /* the key must already exist in the parent */ |
| 3632 | rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first); |
| 3633 | LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error); |
| 3634 | } else { |
| 3635 | assert(!(options & LYD_DUP_WITH_PARENTS)); |
| 3636 | /* duplicating a single key, okay, I suppose... */ |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3637 | rc = lyd_dup_r(orig, NULL, 0, &first, options, first ? NULL : &first); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 3638 | LY_CHECK_GOTO(rc, error); |
| 3639 | } |
| 3640 | } else { |
| 3641 | /* if there is no local parent, it will be inserted into first */ |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3642 | rc = lyd_dup_r(orig, local_parent ? &local_parent->node : NULL, 0, &first, options, first ? NULL : &first); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 3643 | LY_CHECK_GOTO(rc, error); |
| 3644 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3645 | if (nosiblings) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3646 | break; |
| 3647 | } |
| 3648 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3649 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3650 | if (dup) { |
| 3651 | *dup = first; |
| 3652 | } |
| 3653 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3654 | |
| 3655 | error: |
| 3656 | if (top) { |
| 3657 | lyd_free_tree(top); |
| 3658 | } else { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 3659 | lyd_free_siblings(first); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3660 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3661 | return rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3662 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3663 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3664 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3665 | lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3666 | { |
| 3667 | return lyd_dup(node, parent, options, 1, dup); |
| 3668 | } |
| 3669 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3670 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3671 | lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3672 | { |
| 3673 | return lyd_dup(node, parent, options, 0, dup); |
| 3674 | } |
| 3675 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3676 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3677 | lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup) |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3678 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3679 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3680 | struct lyd_meta *mt, *last; |
| 3681 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3682 | LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3683 | |
| 3684 | /* create a copy */ |
| 3685 | mt = calloc(1, sizeof *mt); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3686 | LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3687 | mt->annotation = meta->annotation; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3688 | ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3689 | LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish); |
| 3690 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3691 | |
| 3692 | /* insert as the last attribute */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3693 | mt->parent = node; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3694 | if (node->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 3695 | for (last = node->meta; last->next; last = last->next) {} |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3696 | last->next = mt; |
| 3697 | } else { |
| 3698 | node->meta = mt; |
| 3699 | } |
| 3700 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3701 | finish: |
| 3702 | if (ret) { |
| 3703 | lyd_free_meta_single(mt); |
| 3704 | } else if (dup) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3705 | *dup = mt; |
| 3706 | } |
| 3707 | return LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3708 | } |
| 3709 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3710 | /** |
| 3711 | * @brief Merge a source sibling into target siblings. |
| 3712 | * |
| 3713 | * @param[in,out] first_trg First target sibling, is updated if top-level. |
| 3714 | * @param[in] parent_trg Target parent. |
| 3715 | * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent. |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3716 | * @param[in] merge_cb Optional merge callback. |
| 3717 | * @param[in] cb_data Arbitrary callback data. |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3718 | * @param[in] options Merge options. |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3719 | * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings. |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3720 | * @return LY_ERR value. |
| 3721 | */ |
| 3722 | static LY_ERR |
| 3723 | lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p, |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3724 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3725 | { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3726 | const struct lyd_node *child_src, *tmp, *sibling_src; |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 3727 | struct lyd_node *match_trg, *dup_src, *elem; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3728 | struct lysc_type *type; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3729 | struct lyd_dup_inst *child_dup_inst = NULL; |
| 3730 | LY_ERR ret; |
| 3731 | ly_bool first_inst = 0; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3732 | |
| 3733 | sibling_src = *sibling_src_p; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3734 | if (!sibling_src->schema) { |
| 3735 | /* try to find the same opaque node */ |
| 3736 | lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg); |
| 3737 | } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3738 | /* try to find the exact instance */ |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3739 | lyd_find_sibling_first(*first_trg, sibling_src, &match_trg); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3740 | } else { |
| 3741 | /* try to simply find the node, there cannot be more instances */ |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3742 | lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3743 | } |
| 3744 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3745 | if (match_trg) { |
| 3746 | /* update match as needed */ |
| 3747 | LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst)); |
| 3748 | } else { |
| 3749 | /* first instance of this node */ |
| 3750 | first_inst = 1; |
| 3751 | } |
| 3752 | |
| 3753 | if (match_trg) { |
| 3754 | /* call callback */ |
| 3755 | if (merge_cb) { |
| 3756 | LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data)); |
| 3757 | } |
| 3758 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3759 | /* node found, make sure even value matches for all node types */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3760 | if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3761 | /* since they are different, they cannot both be default */ |
| 3762 | assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT)); |
| 3763 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3764 | /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */ |
| 3765 | if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3766 | type = ((struct lysc_node_leaf *)match_trg->schema)->type; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3767 | type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value); |
| 3768 | LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3769 | &((struct lyd_node_term *)match_trg)->value)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3770 | |
| 3771 | /* copy flags and add LYD_NEW */ |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 3772 | match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3773 | } |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3774 | } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) { |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 3775 | /* update value */ |
| 3776 | LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3777 | ((struct lyd_node_any *)sibling_src)->value_type)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3778 | |
| 3779 | /* copy flags and add LYD_NEW */ |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 3780 | match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW); |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | /* check descendants, recursively */ |
| 3784 | ret = LY_SUCCESS; |
| 3785 | LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) { |
| 3786 | ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options, |
| 3787 | &child_dup_inst); |
| 3788 | if (ret) { |
| 3789 | break; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3790 | } |
| 3791 | } |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3792 | lyd_dup_inst_free(child_dup_inst); |
| 3793 | LY_CHECK_RET(ret); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3794 | } else { |
| 3795 | /* node not found, merge it */ |
| 3796 | if (options & LYD_MERGE_DESTRUCT) { |
| 3797 | dup_src = (struct lyd_node *)sibling_src; |
| 3798 | lyd_unlink_tree(dup_src); |
| 3799 | /* spend it */ |
| 3800 | *sibling_src_p = NULL; |
| 3801 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3802 | LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &dup_src)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3803 | } |
| 3804 | |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 3805 | if (!(options & LYD_MERGE_WITH_FLAGS)) { |
| 3806 | /* set LYD_NEW for all the new nodes, required for validation */ |
| 3807 | LYD_TREE_DFS_BEGIN(dup_src, elem) { |
| 3808 | elem->flags |= LYD_NEW; |
| 3809 | LYD_TREE_DFS_END(dup_src, elem); |
| 3810 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3811 | } |
| 3812 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3813 | /* insert */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 3814 | lyd_insert_node(parent_trg, first_trg, dup_src, 0); |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3815 | |
| 3816 | if (first_inst) { |
| 3817 | /* remember not to find this instance next time */ |
| 3818 | LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst)); |
| 3819 | } |
| 3820 | |
| 3821 | /* call callback, no source node */ |
| 3822 | if (merge_cb) { |
| 3823 | LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data)); |
| 3824 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | return LY_SUCCESS; |
| 3828 | } |
| 3829 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3830 | static LY_ERR |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3831 | lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod, |
| 3832 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3833 | { |
| 3834 | const struct lyd_node *sibling_src, *tmp; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3835 | struct lyd_dup_inst *dup_inst = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3836 | ly_bool first; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3837 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3838 | |
| 3839 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 3840 | LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL, |
| 3841 | LY_EINVAL); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3842 | |
| 3843 | if (!source) { |
| 3844 | /* nothing to merge */ |
| 3845 | return LY_SUCCESS; |
| 3846 | } |
| 3847 | |
Michal Vasko | 831422b | 2020-11-24 11:20:51 +0100 | [diff] [blame] | 3848 | if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3849 | LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3850 | return LY_EINVAL; |
| 3851 | } |
| 3852 | |
| 3853 | LY_LIST_FOR_SAFE(source, tmp, sibling_src) { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3854 | if (mod && (lyd_owner_module(sibling_src) != mod)) { |
| 3855 | /* skip data nodes from different modules */ |
| 3856 | continue; |
| 3857 | } |
| 3858 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3859 | first = (sibling_src == source) ? 1 : 0; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3860 | ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst); |
| 3861 | if (ret) { |
| 3862 | break; |
| 3863 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3864 | if (first && !sibling_src) { |
| 3865 | /* source was spent (unlinked), move to the next node */ |
| 3866 | source = tmp; |
| 3867 | } |
| 3868 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3869 | if (nosiblings) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3870 | break; |
| 3871 | } |
| 3872 | } |
| 3873 | |
| 3874 | if (options & LYD_MERGE_DESTRUCT) { |
| 3875 | /* free any leftover source data that were not merged */ |
| 3876 | lyd_free_siblings((struct lyd_node *)source); |
| 3877 | } |
| 3878 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3879 | lyd_dup_inst_free(dup_inst); |
| 3880 | return ret; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3883 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3884 | lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3885 | { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3886 | return lyd_merge(target, source, NULL, NULL, NULL, options, 1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3887 | } |
| 3888 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3889 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3890 | lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3891 | { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3892 | return lyd_merge(target, source, NULL, NULL, NULL, options, 0); |
| 3893 | } |
| 3894 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3895 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3896 | lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod, |
| 3897 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options) |
| 3898 | { |
| 3899 | return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3900 | } |
| 3901 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3902 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3903 | lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3904 | { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3905 | /* ending \0 */ |
| 3906 | ++reqlen; |
| 3907 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3908 | if (reqlen > *buflen) { |
| 3909 | if (is_static) { |
| 3910 | return LY_EINCOMPLETE; |
| 3911 | } |
| 3912 | |
| 3913 | *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer); |
| 3914 | if (!*buffer) { |
| 3915 | return LY_EMEM; |
| 3916 | } |
| 3917 | |
| 3918 | *buflen = reqlen; |
| 3919 | } |
| 3920 | |
| 3921 | return LY_SUCCESS; |
| 3922 | } |
| 3923 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 3924 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3925 | lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3926 | { |
| 3927 | const struct lyd_node *key; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3928 | size_t len; |
| 3929 | const char *val; |
| 3930 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3931 | |
Michal Vasko | 824d083 | 2021-11-04 15:36:51 +0100 | [diff] [blame] | 3932 | for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 3933 | val = lyd_get_value(key); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3934 | len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3935 | LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3936 | |
| 3937 | quot = '\''; |
| 3938 | if (strchr(val, '\'')) { |
| 3939 | quot = '"'; |
| 3940 | } |
| 3941 | *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3942 | } |
| 3943 | |
| 3944 | return LY_SUCCESS; |
| 3945 | } |
| 3946 | |
| 3947 | /** |
| 3948 | * @brief Append leaf-list value predicate to path. |
| 3949 | * |
| 3950 | * @param[in] node Node to print. |
| 3951 | * @param[in,out] buffer Buffer to print to. |
| 3952 | * @param[in,out] buflen Current buffer length. |
| 3953 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 3954 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 3955 | * @return LY_ERR |
| 3956 | */ |
| 3957 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3958 | lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3959 | { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3960 | size_t len; |
| 3961 | const char *val; |
| 3962 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3963 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 3964 | val = lyd_get_value(node); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3965 | len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3966 | LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3967 | |
| 3968 | quot = '\''; |
| 3969 | if (strchr(val, '\'')) { |
| 3970 | quot = '"'; |
| 3971 | } |
| 3972 | *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot); |
| 3973 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3974 | return LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | /** |
| 3978 | * @brief Append node position (relative to its other instances) predicate to path. |
| 3979 | * |
| 3980 | * @param[in] node Node to print. |
| 3981 | * @param[in,out] buffer Buffer to print to. |
| 3982 | * @param[in,out] buflen Current buffer length. |
| 3983 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 3984 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 3985 | * @return LY_ERR |
| 3986 | */ |
| 3987 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3988 | lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3989 | { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3990 | size_t len; |
Michal Vasko | 50cc056 | 2021-05-18 16:15:43 +0200 | [diff] [blame] | 3991 | uint32_t pos; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3992 | char *val = NULL; |
| 3993 | LY_ERR rc; |
| 3994 | |
Michal Vasko | 50cc056 | 2021-05-18 16:15:43 +0200 | [diff] [blame] | 3995 | pos = lyd_list_pos(node); |
| 3996 | if (asprintf(&val, "%" PRIu32, pos) == -1) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3997 | return LY_EMEM; |
| 3998 | } |
| 3999 | |
| 4000 | len = 1 + strlen(val) + 1; |
| 4001 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 4002 | if (rc != LY_SUCCESS) { |
| 4003 | goto cleanup; |
| 4004 | } |
| 4005 | |
| 4006 | *bufused += sprintf(*buffer + *bufused, "[%s]", val); |
| 4007 | |
| 4008 | cleanup: |
| 4009 | free(val); |
| 4010 | return rc; |
| 4011 | } |
| 4012 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4013 | LIBYANG_API_DEF char * |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4014 | lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 4015 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4016 | ly_bool is_static = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4017 | uint32_t i, depth; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4018 | size_t bufused = 0, len; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4019 | const struct lyd_node *iter; |
| 4020 | const struct lys_module *mod; |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 4021 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4022 | |
| 4023 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 4024 | if (buffer) { |
Michal Vasko | 16385f4 | 2021-05-18 16:16:09 +0200 | [diff] [blame] | 4025 | LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4026 | is_static = 1; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4027 | } else { |
| 4028 | buflen = 0; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | switch (pathtype) { |
Radek Krejci | 635d2b8 | 2021-01-04 11:26:51 +0100 | [diff] [blame] | 4032 | case LYD_PATH_STD: |
| 4033 | case LYD_PATH_STD_NO_LAST_PRED: |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4034 | depth = 1; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4035 | for (iter = node; iter->parent; iter = lyd_parent(iter)) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4036 | ++depth; |
| 4037 | } |
| 4038 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4039 | goto iter_print; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4040 | while (depth) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4041 | /* find the right node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4042 | for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {} |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4043 | iter_print: |
| 4044 | /* print prefix and name */ |
| 4045 | mod = NULL; |
Michal Vasko | 9f1b447 | 2022-01-06 13:35:08 +0100 | [diff] [blame] | 4046 | if (iter->schema && (!iter->parent || !iter->parent->schema || |
| 4047 | (iter->schema->module != iter->parent->schema->module))) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4048 | mod = iter->schema->module; |
| 4049 | } |
| 4050 | |
| 4051 | /* realloc string */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 4052 | len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) : |
| 4053 | strlen(((struct lyd_node_opaq *)iter)->name.name)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4054 | rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static); |
| 4055 | if (rc != LY_SUCCESS) { |
| 4056 | break; |
| 4057 | } |
| 4058 | |
| 4059 | /* print next node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 4060 | bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 4061 | iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4062 | |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 4063 | /* do not always print the last (first) predicate */ |
Radek Krejci | 635d2b8 | 2021-01-04 11:26:51 +0100 | [diff] [blame] | 4064 | if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) { |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 4065 | switch (iter->schema->nodetype) { |
| 4066 | case LYS_LIST: |
| 4067 | if (iter->schema->flags & LYS_KEYLESS) { |
| 4068 | /* print its position */ |
| 4069 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 4070 | } else { |
| 4071 | /* print all list keys in predicates */ |
| 4072 | rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 4073 | } |
| 4074 | break; |
| 4075 | case LYS_LEAFLIST: |
| 4076 | if (iter->schema->flags & LYS_CONFIG_W) { |
| 4077 | /* print leaf-list value */ |
| 4078 | rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 4079 | } else { |
| 4080 | /* print its position */ |
| 4081 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 4082 | } |
| 4083 | break; |
| 4084 | default: |
| 4085 | /* nothing to print more */ |
| 4086 | break; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4087 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4088 | } |
| 4089 | if (rc != LY_SUCCESS) { |
| 4090 | break; |
| 4091 | } |
| 4092 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4093 | --depth; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4094 | } |
| 4095 | break; |
| 4096 | } |
| 4097 | |
| 4098 | return buffer; |
| 4099 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4100 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4101 | LIBYANG_API_DEF struct lyd_meta * |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 4102 | lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name) |
| 4103 | { |
| 4104 | struct lyd_meta *ret = NULL; |
| 4105 | const struct ly_ctx *ctx; |
| 4106 | const char *prefix, *tmp; |
| 4107 | char *str; |
| 4108 | size_t pref_len, name_len; |
| 4109 | |
| 4110 | LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 4111 | LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 4112 | |
| 4113 | if (!first) { |
| 4114 | return NULL; |
| 4115 | } |
| 4116 | |
| 4117 | ctx = first->annotation->module->ctx; |
| 4118 | |
| 4119 | /* parse the name */ |
| 4120 | tmp = name; |
| 4121 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 4122 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
| 4123 | return NULL; |
| 4124 | } |
| 4125 | |
| 4126 | /* find the module */ |
| 4127 | if (prefix) { |
| 4128 | str = strndup(prefix, pref_len); |
| 4129 | module = ly_ctx_get_module_latest(ctx, str); |
| 4130 | free(str); |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 4131 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), NULL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | /* find the metadata */ |
| 4135 | LY_LIST_FOR(first, first) { |
| 4136 | if ((first->annotation->module == module) && !strcmp(first->name, name)) { |
| 4137 | ret = (struct lyd_meta *)first; |
| 4138 | break; |
| 4139 | } |
| 4140 | } |
| 4141 | |
| 4142 | return ret; |
| 4143 | } |
| 4144 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4145 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4146 | lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match) |
| 4147 | { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4148 | struct lyd_node **match_p, *iter; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4149 | struct lyd_node_inner *parent; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4150 | ly_bool found; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4151 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 4152 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 4153 | LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4154 | |
Michal Vasko | 6da1e95 | 2020-12-09 18:14:38 +0100 | [diff] [blame] | 4155 | if (!siblings || (siblings->schema && target->schema && |
| 4156 | (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) { |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 4157 | /* no data or schema mismatch */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4158 | if (match) { |
| 4159 | *match = NULL; |
| 4160 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4161 | return LY_ENOTFOUND; |
| 4162 | } |
| 4163 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4164 | /* get first sibling */ |
| 4165 | siblings = lyd_first_sibling(siblings); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4166 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4167 | parent = siblings->parent; |
Michal Vasko | 6da1e95 | 2020-12-09 18:14:38 +0100 | [diff] [blame] | 4168 | if (parent && parent->schema && parent->children_ht) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4169 | assert(target->hash); |
| 4170 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4171 | if (lysc_is_dup_inst_list(target->schema)) { |
| 4172 | /* we must search the instances from beginning to find the first matching one */ |
| 4173 | found = 0; |
| 4174 | LYD_LIST_FOR_INST(siblings, target->schema, iter) { |
| 4175 | if (!lyd_compare_single(target, iter, 0)) { |
| 4176 | found = 1; |
| 4177 | break; |
| 4178 | } |
| 4179 | } |
| 4180 | if (found) { |
| 4181 | siblings = iter; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 4182 | } else { |
| 4183 | siblings = NULL; |
| 4184 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4185 | } else { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4186 | /* find by hash */ |
| 4187 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 4188 | siblings = *match_p; |
| 4189 | } else { |
| 4190 | /* not found */ |
| 4191 | siblings = NULL; |
| 4192 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4193 | } |
| 4194 | } else { |
| 4195 | /* no children hash table */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4196 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 4197 | if (!lyd_compare_single(siblings, target, 0)) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4198 | break; |
| 4199 | } |
| 4200 | } |
| 4201 | } |
| 4202 | |
| 4203 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4204 | if (match) { |
| 4205 | *match = NULL; |
| 4206 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4207 | return LY_ENOTFOUND; |
| 4208 | } |
| 4209 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4210 | if (match) { |
| 4211 | *match = (struct lyd_node *)siblings; |
| 4212 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4213 | return LY_SUCCESS; |
| 4214 | } |
| 4215 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4216 | /** |
| 4217 | * @brief Comparison callback to match schema node with a schema of a data node. |
| 4218 | * |
| 4219 | * @param[in] val1_p Pointer to the schema node |
| 4220 | * @param[in] val2_p Pointer to the data node |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 4221 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4222 | */ |
| 4223 | static ly_bool |
| 4224 | lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4225 | { |
| 4226 | struct lysc_node *val1; |
| 4227 | struct lyd_node *val2; |
| 4228 | |
| 4229 | val1 = *((struct lysc_node **)val1_p); |
| 4230 | val2 = *((struct lyd_node **)val2_p); |
| 4231 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4232 | if (val1 == val2->schema) { |
| 4233 | /* schema match is enough */ |
| 4234 | return 1; |
| 4235 | } else { |
| 4236 | return 0; |
| 4237 | } |
| 4238 | } |
| 4239 | |
Michal Vasko | 92239c7 | 2020-11-18 18:17:25 +0100 | [diff] [blame] | 4240 | /** |
| 4241 | * @brief Search in the given siblings (NOT recursively) for the first schema node data instance. |
| 4242 | * Uses hashes - should be used whenever possible for best performance. |
| 4243 | * |
| 4244 | * @param[in] siblings Siblings to search in including preceding and succeeding nodes. |
| 4245 | * @param[in] schema Target data node schema to find. |
| 4246 | * @param[out] match Can be NULL, otherwise the found data node. |
| 4247 | * @return LY_SUCCESS on success, @p match set. |
| 4248 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 4249 | * @return LY_ERR value if another error occurred. |
| 4250 | */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4251 | static LY_ERR |
| 4252 | lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match) |
| 4253 | { |
| 4254 | struct lyd_node **match_p; |
| 4255 | struct lyd_node_inner *parent; |
| 4256 | uint32_t hash; |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 4257 | lyht_value_equal_cb ht_cb; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4258 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 4259 | assert(siblings && schema); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4260 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4261 | parent = siblings->parent; |
Michal Vasko | 08fd613 | 2020-11-18 18:17:45 +0100 | [diff] [blame] | 4262 | if (parent && parent->schema && parent->children_ht) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4263 | /* calculate our hash */ |
| 4264 | hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name)); |
| 4265 | hash = dict_hash_multi(hash, schema->name, strlen(schema->name)); |
| 4266 | hash = dict_hash_multi(hash, NULL, 0); |
| 4267 | |
| 4268 | /* use special hash table function */ |
| 4269 | ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal); |
| 4270 | |
| 4271 | /* find by hash */ |
| 4272 | if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) { |
| 4273 | siblings = *match_p; |
| 4274 | } else { |
| 4275 | /* not found */ |
| 4276 | siblings = NULL; |
| 4277 | } |
| 4278 | |
| 4279 | /* set the original hash table compare function back */ |
| 4280 | lyht_set_cb(parent->children_ht, ht_cb); |
| 4281 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 4282 | /* find first sibling */ |
| 4283 | if (siblings->parent) { |
| 4284 | siblings = siblings->parent->child; |
| 4285 | } else { |
| 4286 | while (siblings->prev->next) { |
| 4287 | siblings = siblings->prev; |
| 4288 | } |
| 4289 | } |
| 4290 | |
| 4291 | /* search manually without hashes */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4292 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4293 | if (siblings->schema == schema) { |
| 4294 | /* schema match is enough */ |
| 4295 | break; |
| 4296 | } |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4301 | if (match) { |
| 4302 | *match = NULL; |
| 4303 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4304 | return LY_ENOTFOUND; |
| 4305 | } |
| 4306 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4307 | if (match) { |
| 4308 | *match = (struct lyd_node *)siblings; |
| 4309 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4310 | return LY_SUCCESS; |
| 4311 | } |
| 4312 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4313 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4314 | lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4315 | size_t val_len, struct lyd_node **match) |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4316 | { |
| 4317 | LY_ERR rc; |
| 4318 | struct lyd_node *target = NULL; |
| 4319 | |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 4320 | LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 4321 | LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, schema->module->ctx, LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4322 | |
Michal Vasko | 08fd613 | 2020-11-18 18:17:45 +0100 | [diff] [blame] | 4323 | if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) { |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 4324 | /* no data or schema mismatch */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4325 | if (match) { |
| 4326 | *match = NULL; |
| 4327 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4328 | return LY_ENOTFOUND; |
| 4329 | } |
| 4330 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 4331 | if (key_or_value && !val_len) { |
| 4332 | val_len = strlen(key_or_value); |
| 4333 | } |
| 4334 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 4335 | if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) { |
| 4336 | /* create a data node and find the instance */ |
| 4337 | if (schema->nodetype == LYS_LEAFLIST) { |
| 4338 | /* target used attributes: schema, hash, value */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 4339 | rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &target); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 4340 | LY_CHECK_RET(rc); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 4341 | } else { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4342 | /* target used attributes: schema, hash, child (all keys) */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4343 | LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | /* find it */ |
| 4347 | rc = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 4348 | } else { |
| 4349 | /* find the first schema node instance */ |
| 4350 | rc = lyd_find_sibling_schema(siblings, schema, match); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4351 | } |
| 4352 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4353 | lyd_free_tree(target); |
| 4354 | return rc; |
| 4355 | } |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4356 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4357 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4358 | lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set) |
| 4359 | { |
| 4360 | struct lyd_node **match_p, *first, *iter; |
| 4361 | struct lyd_node_inner *parent; |
| 4362 | |
| 4363 | LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL); |
Michal Vasko | 1d80a53 | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 4364 | LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4365 | |
| 4366 | LY_CHECK_RET(ly_set_new(set)); |
| 4367 | |
| 4368 | if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) { |
| 4369 | /* no data or schema mismatch */ |
| 4370 | return LY_ENOTFOUND; |
| 4371 | } |
| 4372 | |
| 4373 | /* get first sibling */ |
| 4374 | siblings = lyd_first_sibling(siblings); |
| 4375 | |
| 4376 | parent = siblings->parent; |
| 4377 | if (parent && parent->schema && parent->children_ht) { |
| 4378 | assert(target->hash); |
| 4379 | |
| 4380 | /* find the first instance */ |
| 4381 | lyd_find_sibling_first(siblings, target, &first); |
| 4382 | if (first) { |
| 4383 | /* add it so that it is the first in the set */ |
| 4384 | if (ly_set_add(*set, first, 1, NULL)) { |
| 4385 | goto error; |
| 4386 | } |
| 4387 | |
| 4388 | /* find by hash */ |
| 4389 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 4390 | iter = *match_p; |
| 4391 | } else { |
| 4392 | /* not found */ |
| 4393 | iter = NULL; |
| 4394 | } |
| 4395 | while (iter) { |
| 4396 | /* add all found nodes into the set */ |
| 4397 | if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) { |
| 4398 | goto error; |
| 4399 | } |
| 4400 | |
| 4401 | /* find next instance */ |
| 4402 | if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) { |
| 4403 | iter = NULL; |
| 4404 | } else { |
| 4405 | iter = *match_p; |
| 4406 | } |
| 4407 | } |
| 4408 | } |
| 4409 | } else { |
| 4410 | /* no children hash table */ |
| 4411 | LY_LIST_FOR(siblings, siblings) { |
| 4412 | if (!lyd_compare_single(target, siblings, 0)) { |
| 4413 | ly_set_add(*set, (void *)siblings, 1, NULL); |
| 4414 | } |
| 4415 | } |
| 4416 | } |
| 4417 | |
| 4418 | if (!(*set)->count) { |
| 4419 | return LY_ENOTFOUND; |
| 4420 | } |
| 4421 | return LY_SUCCESS; |
| 4422 | |
| 4423 | error: |
| 4424 | ly_set_free(*set, NULL); |
| 4425 | *set = NULL; |
| 4426 | return LY_EMEM; |
| 4427 | } |
| 4428 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4429 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 1d4af6c | 2021-02-22 13:31:26 +0100 | [diff] [blame] | 4430 | lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match) |
| 4431 | { |
| 4432 | LY_CHECK_ARG_RET(NULL, name, LY_EINVAL); |
| 4433 | |
| 4434 | for ( ; first; first = first->next) { |
| 4435 | if (!first->schema && !strcmp(LYD_NAME(first), name)) { |
| 4436 | break; |
| 4437 | } |
| 4438 | } |
| 4439 | |
| 4440 | if (match) { |
| 4441 | *match = (struct lyd_node *)first; |
| 4442 | } |
| 4443 | return first ? LY_SUCCESS : LY_ENOTFOUND; |
| 4444 | } |
| 4445 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4446 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 279539d | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4447 | lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, |
| 4448 | const struct lyxp_var *vars, struct ly_set **set) |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4449 | { |
| 4450 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 8bd9cc7 | 2021-07-16 14:06:10 +0200 | [diff] [blame] | 4451 | struct lyxp_set xp_set = {0}; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 4452 | struct lyxp_expr *exp = NULL; |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4453 | uint32_t i; |
| 4454 | |
Michal Vasko | 279539d | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4455 | LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4456 | |
Michal Vasko | 37c6943 | 2021-04-12 14:48:24 +0200 | [diff] [blame] | 4457 | *set = NULL; |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4458 | |
| 4459 | /* compile expression */ |
Michal Vasko | 279539d | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4460 | ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 4461 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4462 | |
| 4463 | /* evaluate expression */ |
Michal Vasko | 279539d | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4464 | ret = lyxp_eval(LYD_CTX(tree), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, tree, vars, &xp_set, LYXP_IGNORE_WHEN); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4465 | LY_CHECK_GOTO(ret, cleanup); |
| 4466 | |
| 4467 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4468 | ret = ly_set_new(set); |
| 4469 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4470 | |
| 4471 | /* transform into ly_set */ |
| 4472 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 4473 | /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */ |
| 4474 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
Michal Vasko | 279539d | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4475 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4476 | (*set)->size = xp_set.used; |
| 4477 | |
| 4478 | for (i = 0; i < xp_set.used; ++i) { |
| 4479 | if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 4480 | ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4481 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4482 | } |
| 4483 | } |
| 4484 | } |
| 4485 | |
| 4486 | cleanup: |
Michal Vasko | 0691d52 | 2020-05-21 13:21:47 +0200 | [diff] [blame] | 4487 | lyxp_set_free_content(&xp_set); |
Michal Vasko | 279539d | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4488 | lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp); |
Radek Krejci | 8f45abc | 2020-11-26 12:15:13 +0100 | [diff] [blame] | 4489 | if (ret) { |
| 4490 | ly_set_free(*set, NULL); |
| 4491 | *set = NULL; |
| 4492 | } |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4493 | return ret; |
| 4494 | } |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 4495 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4496 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 279539d | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4497 | lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set) |
| 4498 | { |
| 4499 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 4500 | |
| 4501 | return lyd_find_xpath3(ctx_node, ctx_node, xpath, vars, set); |
| 4502 | } |
| 4503 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4504 | LIBYANG_API_DEF LY_ERR |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 4505 | lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set) |
| 4506 | { |
Michal Vasko | 279539d | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4507 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 4508 | |
| 4509 | return lyd_find_xpath3(ctx_node, ctx_node, xpath, NULL, set); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 4510 | } |
| 4511 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4512 | LIBYANG_API_DEF LY_ERR |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 4513 | lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result) |
Michal Vasko | c9eb3ca | 2021-07-16 10:20:37 +0200 | [diff] [blame] | 4514 | { |
| 4515 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 8bd9cc7 | 2021-07-16 14:06:10 +0200 | [diff] [blame] | 4516 | struct lyxp_set xp_set = {0}; |
Michal Vasko | c9eb3ca | 2021-07-16 10:20:37 +0200 | [diff] [blame] | 4517 | struct lyxp_expr *exp = NULL; |
| 4518 | |
| 4519 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL); |
| 4520 | |
| 4521 | /* compile expression */ |
| 4522 | ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp); |
| 4523 | LY_CHECK_GOTO(ret, cleanup); |
| 4524 | |
| 4525 | /* evaluate expression */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 4526 | ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, vars, &xp_set, LYXP_IGNORE_WHEN); |
Michal Vasko | c9eb3ca | 2021-07-16 10:20:37 +0200 | [diff] [blame] | 4527 | LY_CHECK_GOTO(ret, cleanup); |
| 4528 | |
| 4529 | /* transform into boolean */ |
| 4530 | ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN); |
| 4531 | LY_CHECK_GOTO(ret, cleanup); |
| 4532 | |
| 4533 | /* set result */ |
| 4534 | *result = xp_set.val.bln; |
| 4535 | |
| 4536 | cleanup: |
| 4537 | lyxp_set_free_content(&xp_set); |
| 4538 | lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp); |
| 4539 | return ret; |
| 4540 | } |
| 4541 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4542 | LIBYANG_API_DEF LY_ERR |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 4543 | lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result) |
| 4544 | { |
| 4545 | return lyd_eval_xpath2(ctx_node, xpath, NULL, result); |
| 4546 | } |
| 4547 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4548 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 4549 | lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match) |
| 4550 | { |
| 4551 | LY_ERR ret = LY_SUCCESS; |
| 4552 | struct lyxp_expr *expr = NULL; |
| 4553 | struct ly_path *lypath = NULL; |
| 4554 | |
| 4555 | LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL); |
| 4556 | |
| 4557 | /* parse the path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 4558 | ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 4559 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr); |
| 4560 | LY_CHECK_GOTO(ret, cleanup); |
| 4561 | |
| 4562 | /* compile the path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 4563 | ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr, |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 4564 | output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, 0, LY_VALUE_JSON, NULL, &lypath); |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 4565 | LY_CHECK_GOTO(ret, cleanup); |
| 4566 | |
| 4567 | /* evaluate the path */ |
| 4568 | ret = ly_path_eval_partial(lypath, ctx_node, NULL, match); |
| 4569 | |
| 4570 | cleanup: |
| 4571 | lyxp_expr_free(LYD_CTX(ctx_node), expr); |
| 4572 | ly_path_free(LYD_CTX(ctx_node), lypath); |
| 4573 | return ret; |
| 4574 | } |
| 4575 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4576 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | bb22b18 | 2021-06-14 08:14:21 +0200 | [diff] [blame] | 4577 | lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match) |
| 4578 | { |
| 4579 | LY_ERR ret; |
| 4580 | struct lyd_node *m; |
| 4581 | |
| 4582 | LY_CHECK_ARG_RET(NULL, path, LY_EINVAL); |
| 4583 | |
| 4584 | ret = ly_path_eval(path, tree, &m); |
| 4585 | if (ret) { |
| 4586 | if (match) { |
| 4587 | *match = NULL; |
| 4588 | } |
| 4589 | return LY_ENOTFOUND; |
| 4590 | } |
| 4591 | |
| 4592 | if (match) { |
| 4593 | *match = m; |
| 4594 | } |
| 4595 | return LY_SUCCESS; |
| 4596 | } |
| 4597 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4598 | LIBYANG_API_DEF uint32_t |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 4599 | lyd_list_pos(const struct lyd_node *instance) |
| 4600 | { |
| 4601 | const struct lyd_node *iter = NULL; |
| 4602 | uint32_t pos = 0; |
| 4603 | |
| 4604 | if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 4605 | return 0; |
| 4606 | } |
| 4607 | |
| 4608 | /* data instances are ordered, so we can stop when we found instance of other schema node */ |
| 4609 | for (iter = instance; iter->schema == instance->schema; iter = iter->prev) { |
Radek Krejci | bb27df3 | 2020-11-13 15:39:16 +0100 | [diff] [blame] | 4610 | if (pos && (iter->next == NULL)) { |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 4611 | /* overrun to the end of the siblings list */ |
| 4612 | break; |
| 4613 | } |
| 4614 | ++pos; |
| 4615 | } |
| 4616 | |
| 4617 | return pos; |
| 4618 | } |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 4619 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4620 | LIBYANG_API_DEF struct lyd_node * |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 4621 | lyd_first_sibling(const struct lyd_node *node) |
| 4622 | { |
| 4623 | struct lyd_node *start; |
| 4624 | |
| 4625 | if (!node) { |
| 4626 | return NULL; |
| 4627 | } |
| 4628 | |
| 4629 | /* get the first sibling */ |
| 4630 | if (node->parent) { |
| 4631 | start = node->parent->child; |
| 4632 | } else { |
Radek Krejci | bb27df3 | 2020-11-13 15:39:16 +0100 | [diff] [blame] | 4633 | for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {} |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | return start; |
| 4637 | } |