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