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