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