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