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