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