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