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