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