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