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> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 21 | #include <inttypes.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdarg.h> |
| 23 | #include <stdint.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include <string.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 29 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 30 | #include "context.h" |
| 31 | #include "dict.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 32 | #include "diff.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 33 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 34 | #include "in.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 35 | #include "in_internal.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 | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 40 | #include "plugins.h" |
Radek Krejci | f1ca0ac | 2021-04-12 16:00:06 +0200 | [diff] [blame] | 41 | #include "plugins_exts/metadata.h" |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 42 | #include "plugins_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" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 47 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 48 | #include "tree_schema.h" |
| 49 | #include "tree_schema_internal.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 50 | #include "validation.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 51 | #include "xml.h" |
| 52 | #include "xpath.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 53 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 54 | static LYD_FORMAT |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 55 | lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 56 | { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 57 | if (!format && (in->type == LY_IN_FILEPATH)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 58 | /* unknown format - try to detect it from filename's suffix */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 59 | const char *path = in->method.fpath.filepath; |
| 60 | size_t len = strlen(path); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 61 | |
| 62 | /* ignore trailing whitespaces */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 63 | for ( ; len > 0 && isspace(path[len - 1]); len--) {} |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 64 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 65 | if ((len >= LY_XML_SUFFIX_LEN + 1) && |
| 66 | !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 67 | format = LYD_XML; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 68 | } else if ((len >= LY_JSON_SUFFIX_LEN + 1) && |
| 69 | !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 70 | format = LYD_JSON; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 71 | } else if ((len >= LY_LYB_SUFFIX_LEN + 1) && |
| 72 | !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 73 | format = LYD_LYB; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 74 | } /* else still unknown */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 77 | return format; |
| 78 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 79 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 80 | /** |
| 81 | * @brief Parse YANG data into a data tree. |
| 82 | * |
| 83 | * @param[in] ctx libyang context. |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 84 | * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 85 | * @param[in] parent Parent to connect the parsed nodes to, if any. |
| 86 | * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL. |
| 87 | * @param[in] in Input handle to read the input from. |
| 88 | * @param[in] format Expected format of the data in @p in. |
| 89 | * @param[in] parse_opts Options for parser. |
| 90 | * @param[in] val_opts Options for validation. |
| 91 | * @param[out] op Optional pointer to the parsed operation, if any. |
| 92 | * @return LY_ERR value. |
| 93 | */ |
| 94 | static LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 95 | lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p, |
| 96 | struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 97 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 98 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 99 | struct lyd_ctx *lydctx = NULL; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 100 | struct ly_set parsed = {0}; |
| 101 | struct lyd_node *first; |
| 102 | uint32_t i; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 103 | ly_bool subtree_sibling = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 104 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 105 | assert(ctx && (parent || first_p)); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 106 | |
| 107 | format = lyd_parse_get_format(in, format); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 108 | if (first_p) { |
| 109 | *first_p = NULL; |
| 110 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 111 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 112 | /* remember input position */ |
| 113 | in->func_start = in->current; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 114 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 115 | /* parse the data */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 116 | switch (format) { |
| 117 | case LYD_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 118 | rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, NULL, &parsed, |
| 119 | &subtree_sibling, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 120 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 121 | case LYD_JSON: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 122 | rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, |
| 123 | &subtree_sibling, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 124 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 125 | case LYD_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 126 | rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, |
| 127 | &subtree_sibling, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 128 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 129 | case LYD_UNKNOWN: |
Michal Vasko | d74978f | 2021-02-12 11:59:36 +0100 | [diff] [blame] | 130 | LOGARG(ctx, format); |
| 131 | rc = LY_EINVAL; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | LY_CHECK_GOTO(rc, cleanup); |
| 135 | |
| 136 | if (parent) { |
| 137 | /* get first top-level sibling */ |
| 138 | for (first = parent; first->parent; first = lyd_parent(first)) {} |
| 139 | first = lyd_first_sibling(first); |
| 140 | first_p = &first; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 143 | if (!(parse_opts & LYD_PARSE_ONLY)) { |
| 144 | /* validate data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 145 | rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types, |
| 146 | &lydctx->ext_val, NULL); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 147 | LY_CHECK_GOTO(rc, cleanup); |
| 148 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 149 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 150 | /* set the operation node */ |
| 151 | if (op) { |
| 152 | *op = lydctx->op_node; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 156 | if (lydctx) { |
| 157 | lydctx->free(lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 158 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 159 | if (rc) { |
| 160 | if (parent) { |
| 161 | /* free all the parsed subtrees */ |
| 162 | for (i = 0; i < parsed.count; ++i) { |
| 163 | lyd_free_tree(parsed.dnodes[i]); |
| 164 | } |
| 165 | } else { |
| 166 | /* free everything */ |
| 167 | lyd_free_all(*first_p); |
| 168 | *first_p = NULL; |
| 169 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 170 | } else if (subtree_sibling) { |
| 171 | rc = LY_ENOT; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 172 | } |
| 173 | ly_set_erase(&parsed, NULL); |
| 174 | return rc; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 177 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 178 | lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 179 | uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree) |
| 180 | { |
| 181 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 182 | |
| 183 | LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL); |
| 184 | LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL); |
| 185 | LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL); |
| 186 | |
| 187 | return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL); |
| 188 | } |
| 189 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 190 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 191 | lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 192 | uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree) |
| 193 | { |
| 194 | LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL); |
| 195 | LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL); |
| 196 | LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL); |
| 197 | |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 198 | return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 201 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 202 | lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options, |
| 203 | uint32_t validate_options, struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 204 | { |
| 205 | LY_ERR ret; |
| 206 | struct ly_in *in; |
| 207 | |
| 208 | LY_CHECK_RET(ly_in_new_memory(data, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 209 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 210 | |
| 211 | ly_in_free(in, 0); |
| 212 | return ret; |
| 213 | } |
| 214 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 215 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 216 | lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 217 | struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 218 | { |
| 219 | LY_ERR ret; |
| 220 | struct ly_in *in; |
| 221 | |
| 222 | LY_CHECK_RET(ly_in_new_fd(fd, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 223 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 224 | |
| 225 | ly_in_free(in, 0); |
| 226 | return ret; |
| 227 | } |
| 228 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 229 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 230 | lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options, |
| 231 | uint32_t validate_options, struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 232 | { |
| 233 | LY_ERR ret; |
| 234 | struct ly_in *in; |
| 235 | |
| 236 | LY_CHECK_RET(ly_in_new_filepath(path, 0, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 237 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 238 | |
| 239 | ly_in_free(in, 0); |
| 240 | return ret; |
| 241 | } |
| 242 | |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 243 | /** |
| 244 | * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching |
| 245 | * for schema nodes locked inside the extension instance. |
| 246 | * |
| 247 | * At least one of @p parent, @p tree, or @p op must always be set. |
| 248 | * |
| 249 | * Specific @p data_type values have different parameter meaning as follows: |
| 250 | * - ::LYD_TYPE_RPC_NETCONF: |
| 251 | * - @p parent - must be NULL, the whole RPC is expected; |
| 252 | * - @p format - must be ::LYD_XML, NETCONF supports only this format; |
| 253 | * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as |
| 254 | * a separate opaque data tree, even if the function fails, this may be returned; |
| 255 | * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation; |
| 256 | * |
| 257 | * - ::LYD_TYPE_NOTIF_NETCONF: |
| 258 | * - @p parent - must be NULL, the whole notification is expected; |
| 259 | * - @p format - must be ::LYD_XML, NETCONF supports only this format; |
| 260 | * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as |
| 261 | * a separate opaque data tree, even if the function fails, this may be returned; |
| 262 | * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation; |
| 263 | * |
| 264 | * - ::LYD_TYPE_REPLY_NETCONF: |
| 265 | * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node; |
| 266 | * - @p format - must be ::LYD_XML, NETCONF supports only this format; |
| 267 | * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as |
| 268 | * a separate opaque data tree, even if the function fails, this may be returned; |
| 269 | * - @p op - must be NULL, the reply is appended to the RPC; |
| 270 | * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC. |
| 271 | * |
| 272 | * @param[in] ctx libyang context. |
| 273 | * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed. |
| 274 | * @param[in] parent Optional parent to connect the parsed nodes to. |
| 275 | * @param[in] in Input handle to read the input from. |
| 276 | * @param[in] format Expected format of the data in @p in. |
| 277 | * @param[in] data_type Expected operation to parse (@ref datatype). |
| 278 | * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL. |
| 279 | * @param[out] op Optional parsed operation node. |
| 280 | * @return LY_ERR value. |
| 281 | * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one. |
| 282 | */ |
| 283 | static LY_ERR |
| 284 | lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 285 | struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 286 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 287 | LY_ERR rc = LY_SUCCESS; |
| 288 | struct lyd_ctx *lydctx = NULL; |
| 289 | struct ly_set parsed = {0}; |
| 290 | struct lyd_node *first = NULL, *envp = NULL; |
| 291 | uint32_t i, parse_opts, val_opts; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 292 | |
Michal Vasko | 27fb026 | 2021-02-23 09:42:01 +0100 | [diff] [blame] | 293 | if (!ctx) { |
| 294 | ctx = LYD_CTX(parent); |
| 295 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 296 | if (tree) { |
| 297 | *tree = NULL; |
| 298 | } |
| 299 | if (op) { |
| 300 | *op = NULL; |
| 301 | } |
| 302 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 303 | format = lyd_parse_get_format(in, format); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 304 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 305 | /* remember input position */ |
| 306 | in->func_start = in->current; |
| 307 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 308 | /* check params based on the data type */ |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 309 | if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 310 | LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL); |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 311 | } else if (data_type == LYD_TYPE_REPLY_NETCONF) { |
| 312 | LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, |
| 313 | LY_EINVAL); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 314 | } |
Michal Vasko | 140ede9 | 2022-05-10 09:27:30 +0200 | [diff] [blame] | 315 | parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 316 | val_opts = 0; |
| 317 | |
| 318 | /* parse the data */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 319 | switch (format) { |
| 320 | case LYD_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 321 | rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, NULL, &lydctx); |
Michal Vasko | 299d5d1 | 2021-02-16 16:36:37 +0100 | [diff] [blame] | 322 | if (rc && envp) { |
| 323 | /* special situation when the envelopes were parsed successfully */ |
| 324 | if (tree) { |
| 325 | *tree = envp; |
Michal Vasko | 73792a1 | 2022-05-10 09:28:45 +0200 | [diff] [blame] | 326 | } else { |
| 327 | lyd_free_all(envp); |
Michal Vasko | 299d5d1 | 2021-02-16 16:36:37 +0100 | [diff] [blame] | 328 | } |
Michal Vasko | 73792a1 | 2022-05-10 09:28:45 +0200 | [diff] [blame] | 329 | goto cleanup; |
Michal Vasko | 299d5d1 | 2021-02-16 16:36:37 +0100 | [diff] [blame] | 330 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 331 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 332 | case LYD_JSON: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 333 | rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 334 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 335 | case LYD_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 336 | rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 337 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 338 | case LYD_UNKNOWN: |
Michal Vasko | d74978f | 2021-02-12 11:59:36 +0100 | [diff] [blame] | 339 | LOGARG(ctx, format); |
| 340 | rc = LY_EINVAL; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 341 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 342 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 343 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 344 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 345 | /* set out params correctly */ |
| 346 | if (tree) { |
| 347 | if (envp) { |
| 348 | /* special out param meaning */ |
| 349 | *tree = envp; |
| 350 | } else { |
| 351 | *tree = parent ? NULL : first; |
| 352 | } |
| 353 | } |
| 354 | if (op) { |
| 355 | *op = lydctx->op_node; |
| 356 | } |
| 357 | |
| 358 | cleanup: |
| 359 | if (lydctx) { |
| 360 | lydctx->free(lydctx); |
| 361 | } |
| 362 | if (rc) { |
Michal Vasko | 73792a1 | 2022-05-10 09:28:45 +0200 | [diff] [blame] | 363 | /* free all the parsed nodes */ |
| 364 | if (parsed.count) { |
| 365 | i = parsed.count; |
| 366 | do { |
| 367 | --i; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 368 | lyd_free_tree(parsed.dnodes[i]); |
Michal Vasko | 73792a1 | 2022-05-10 09:28:45 +0200 | [diff] [blame] | 369 | } while (i); |
| 370 | } |
| 371 | if (tree && ((format != LYD_XML) || !envp)) { |
| 372 | *tree = NULL; |
| 373 | } |
| 374 | if (op) { |
| 375 | *op = NULL; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | ly_set_erase(&parsed, NULL); |
| 379 | return rc; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 380 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 381 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 382 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 383 | lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 384 | enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
| 385 | { |
| 386 | LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL); |
| 387 | |
| 388 | return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op); |
| 389 | } |
| 390 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 391 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 392 | lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 393 | enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
| 394 | { |
| 395 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 396 | |
| 397 | LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL); |
| 398 | |
| 399 | return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op); |
| 400 | } |
| 401 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 402 | struct lyd_node * |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 403 | 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] | 404 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 405 | const struct lysc_node *schema, *sparent; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 406 | struct lyd_node *match = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 407 | ly_bool found; |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 408 | uint32_t getnext_opts; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 409 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 410 | assert(new_node); |
| 411 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 412 | if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 413 | /* insert at the end, no next anchor */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 414 | return NULL; |
| 415 | } |
| 416 | |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 417 | getnext_opts = 0; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 418 | if (new_node->schema->flags & LYS_IS_OUTPUT) { |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 419 | getnext_opts = LYS_GETNEXT_OUTPUT; |
| 420 | } |
| 421 | |
Michal Vasko | a2756f0 | 2021-07-09 13:20:28 +0200 | [diff] [blame] | 422 | if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 423 | /* find the anchor using hashes */ |
| 424 | sparent = first_sibling->parent->schema; |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 425 | schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 426 | while (schema) { |
| 427 | /* keep trying to find the first existing instance of the closest following schema sibling, |
| 428 | * otherwise return NULL - inserting at the end */ |
| 429 | if (!lyd_find_sibling_schema(first_sibling, schema, &match)) { |
| 430 | break; |
| 431 | } |
| 432 | |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 433 | schema = lys_getnext(schema, sparent, NULL, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 434 | } |
| 435 | } else { |
| 436 | /* find the anchor without hashes */ |
| 437 | match = (struct lyd_node *)first_sibling; |
Michal Vasko | 3a70862 | 2021-07-15 14:20:10 +0200 | [diff] [blame] | 438 | sparent = lysc_data_parent(new_node->schema); |
| 439 | if (!sparent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 440 | /* we are in top-level, skip all the data from preceding modules */ |
| 441 | LY_LIST_FOR(match, match) { |
| 442 | if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) { |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /* get the first schema sibling */ |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 449 | schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 450 | |
| 451 | found = 0; |
| 452 | LY_LIST_FOR(match, match) { |
| 453 | if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) { |
| 454 | /* we have found an opaque node, which must be at the end, so use it OR |
| 455 | * modules do not match, so we must have traversed all the data from new_node module (if any), |
| 456 | * we have found the first node of the next module, that is what we want */ |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | /* skip schema nodes until we find the instantiated one */ |
| 461 | while (!found) { |
| 462 | if (new_node->schema == schema) { |
| 463 | /* we have found the schema of the new node, continue search to find the first |
| 464 | * data node with a different schema (after our schema) */ |
| 465 | found = 1; |
| 466 | break; |
| 467 | } |
| 468 | if (match->schema == schema) { |
| 469 | /* current node (match) is a data node still before the new node, continue search in data */ |
| 470 | break; |
| 471 | } |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 472 | schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 473 | assert(schema); |
| 474 | } |
| 475 | |
| 476 | if (found && (match->schema != new_node->schema)) { |
| 477 | /* find the next node after we have found our node schema data instance */ |
| 478 | break; |
| 479 | } |
| 480 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | return match; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * @brief Insert node after a sibling. |
| 488 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 489 | * Handles inserting into NP containers and key-less lists. |
| 490 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 491 | * @param[in] sibling Sibling to insert after. |
| 492 | * @param[in] node Node to insert. |
| 493 | */ |
| 494 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 495 | lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 496 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 497 | struct lyd_node_inner *par; |
| 498 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 499 | assert(!node->next && (node->prev == node)); |
| 500 | |
| 501 | node->next = sibling->next; |
| 502 | node->prev = sibling; |
| 503 | sibling->next = node; |
| 504 | if (node->next) { |
| 505 | /* sibling had a succeeding node */ |
| 506 | node->next->prev = node; |
| 507 | } else { |
| 508 | /* sibling was last, find first sibling and change its prev */ |
| 509 | if (sibling->parent) { |
| 510 | sibling = sibling->parent->child; |
| 511 | } else { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 512 | for ( ; sibling->prev->next != node; sibling = sibling->prev) {} |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 513 | } |
| 514 | sibling->prev = node; |
| 515 | } |
| 516 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 517 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 518 | for (par = node->parent; par; par = par->parent) { |
| 519 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 520 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 521 | par->flags &= ~LYD_DEFAULT; |
| 522 | } |
| 523 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /** |
| 527 | * @brief Insert node before a sibling. |
| 528 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 529 | * Handles inserting into NP containers and key-less lists. |
| 530 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 531 | * @param[in] sibling Sibling to insert before. |
| 532 | * @param[in] node Node to insert. |
| 533 | */ |
| 534 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 535 | lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 536 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 537 | struct lyd_node_inner *par; |
| 538 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 539 | assert(!node->next && (node->prev == node)); |
| 540 | |
| 541 | node->next = sibling; |
| 542 | /* covers situation of sibling being first */ |
| 543 | node->prev = sibling->prev; |
| 544 | sibling->prev = node; |
| 545 | if (node->prev->next) { |
| 546 | /* sibling had a preceding node */ |
| 547 | node->prev->next = node; |
| 548 | } else if (sibling->parent) { |
| 549 | /* sibling was first and we must also change parent child pointer */ |
| 550 | sibling->parent->child = node; |
| 551 | } |
| 552 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 553 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 554 | for (par = node->parent; par; par = par->parent) { |
| 555 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 556 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 557 | par->flags &= ~LYD_DEFAULT; |
| 558 | } |
| 559 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 563 | * @brief Insert node as the first and only child of a parent. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 564 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 565 | * Handles inserting into NP containers and key-less lists. |
| 566 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 567 | * @param[in] parent Parent to insert into. |
| 568 | * @param[in] node Node to insert. |
| 569 | */ |
| 570 | static void |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 571 | lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 572 | { |
| 573 | struct lyd_node_inner *par; |
| 574 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 575 | assert(parent && !lyd_child(parent) && !node->next && (node->prev == node)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 576 | assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 577 | |
| 578 | par = (struct lyd_node_inner *)parent; |
| 579 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 580 | par->child = node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 581 | node->parent = par; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 582 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 583 | for ( ; par; par = par->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 584 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 585 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 586 | par->flags &= ~LYD_DEFAULT; |
| 587 | } |
| 588 | } |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 589 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 590 | |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 591 | /** |
| 592 | * @brief Learn whether a list instance has all the keys. |
| 593 | * |
| 594 | * @param[in] list List instance to check. |
| 595 | * @return non-zero if all the keys were found, |
| 596 | * @return 0 otherwise. |
| 597 | */ |
| 598 | static int |
| 599 | lyd_insert_has_keys(const struct lyd_node *list) |
| 600 | { |
| 601 | const struct lyd_node *key; |
| 602 | const struct lysc_node *skey = NULL; |
| 603 | |
| 604 | assert(list->schema->nodetype == LYS_LIST); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 605 | key = lyd_child(list); |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 606 | while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 607 | if (!key || (key->schema != skey)) { |
| 608 | /* key missing */ |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | key = key->next; |
| 613 | } |
| 614 | |
| 615 | /* all keys found */ |
| 616 | return 1; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | void |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 620 | lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, ly_bool last) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 621 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 622 | struct lyd_node *anchor, *first_sibling; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 623 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 624 | /* inserting list without its keys is not supported */ |
| 625 | assert((parent || first_sibling_p) && node && (node->hash || !node->schema)); |
Michal Vasko | f756c94 | 2020-11-06 17:21:37 +0100 | [diff] [blame] | 626 | assert(!parent || !parent->schema || |
| 627 | (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF))); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 628 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 629 | if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 630 | parent = lyd_parent(*first_sibling_p); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 631 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 632 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 633 | /* get first sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 634 | first_sibling = parent ? lyd_child(parent) : *first_sibling_p; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 635 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 636 | if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 637 | /* no next anchor */ |
| 638 | anchor = NULL; |
| 639 | } else { |
| 640 | /* find the anchor, our next node, so we can insert before it */ |
| 641 | anchor = lyd_insert_get_next_anchor(first_sibling, node); |
| 642 | } |
| 643 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 644 | if (anchor) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 645 | /* insert before the anchor */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 646 | lyd_insert_before_node(anchor, node); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 647 | if (!parent && (*first_sibling_p == anchor)) { |
| 648 | /* move first sibling */ |
| 649 | *first_sibling_p = node; |
| 650 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 651 | } else if (first_sibling) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 652 | /* insert as the last node */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 653 | lyd_insert_after_node(first_sibling->prev, node); |
| 654 | } else if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 655 | /* insert as the only child */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 656 | lyd_insert_only_child(parent, node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 657 | } else { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 658 | /* insert as the only sibling */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 659 | *first_sibling_p = node; |
| 660 | } |
| 661 | |
| 662 | /* insert into parent HT */ |
| 663 | lyd_insert_hash(node); |
| 664 | |
| 665 | /* finish hashes for our parent, if needed and possible */ |
Michal Vasko | 9e8de2d | 2020-09-01 08:17:10 +0200 | [diff] [blame] | 666 | if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 667 | lyd_hash(parent); |
| 668 | |
| 669 | /* now we can insert even the list into its parent HT */ |
| 670 | lyd_insert_hash(parent); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 671 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 672 | } |
| 673 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 674 | /** |
| 675 | * @brief Check schema place of a node to be inserted. |
| 676 | * |
| 677 | * @param[in] parent Schema node of the parent data node. |
| 678 | * @param[in] sibling Schema node of a sibling data node. |
| 679 | * @param[in] schema Schema node if the data node to be inserted. |
| 680 | * @return LY_SUCCESS on success. |
| 681 | * @return LY_EINVAL if the place is invalid. |
| 682 | */ |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 683 | static LY_ERR |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 684 | lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *sibling, const struct lysc_node *schema) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 685 | { |
| 686 | const struct lysc_node *par2; |
| 687 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 688 | assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 689 | assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 690 | assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 691 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 692 | if (!schema || (!parent && !sibling)) { |
Michal Vasko | 71e795e | 2020-12-04 16:27:37 +0100 | [diff] [blame] | 693 | /* opaque nodes can be inserted wherever */ |
| 694 | return LY_SUCCESS; |
| 695 | } |
| 696 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 697 | if (!parent) { |
| 698 | parent = lysc_data_parent(sibling); |
| 699 | } |
| 700 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 701 | /* find schema parent */ |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 702 | par2 = lysc_data_parent(schema); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 703 | |
| 704 | if (parent) { |
| 705 | /* inner node */ |
| 706 | if (par2 != parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 707 | LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 708 | parent->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 709 | return LY_EINVAL; |
| 710 | } |
| 711 | } else { |
| 712 | /* top-level node */ |
| 713 | if (par2) { |
Radek Krejci | f6d14cb | 2020-07-02 16:11:45 +0200 | [diff] [blame] | 714 | 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] | 715 | return LY_EINVAL; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | return LY_SUCCESS; |
| 720 | } |
| 721 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 722 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 723 | lyd_insert_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 724 | { |
| 725 | struct lyd_node *iter; |
| 726 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 727 | LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 728 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 729 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 730 | LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 731 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 732 | if (node->schema && (node->schema->flags & LYS_KEY)) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 733 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 734 | return LY_EINVAL; |
| 735 | } |
| 736 | |
| 737 | if (node->parent || node->prev->next) { |
| 738 | lyd_unlink_tree(node); |
| 739 | } |
| 740 | |
| 741 | while (node) { |
| 742 | iter = node->next; |
| 743 | lyd_unlink_tree(node); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 744 | lyd_insert_node(parent, NULL, node, 0); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 745 | node = iter; |
| 746 | } |
| 747 | return LY_SUCCESS; |
| 748 | } |
| 749 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 750 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 751 | lyd_insert_ext(struct lyd_node *parent, struct lyd_node *first) |
| 752 | { |
| 753 | struct lyd_node *iter; |
| 754 | |
| 755 | LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next, |
| 756 | !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL); |
| 757 | |
| 758 | if (first->schema && (first->schema->flags & LYS_KEY)) { |
| 759 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name); |
| 760 | return LY_EINVAL; |
| 761 | } |
| 762 | |
| 763 | while (first) { |
| 764 | iter = first->next; |
| 765 | lyd_unlink_tree(first); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 766 | lyd_insert_node(parent, NULL, first, 1); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 767 | first = iter; |
| 768 | } |
| 769 | return LY_SUCCESS; |
| 770 | } |
| 771 | |
| 772 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 773 | 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] | 774 | { |
| 775 | struct lyd_node *iter; |
| 776 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 777 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 778 | LY_CHECK_CTX_EQUAL_RET(sibling ? LYD_CTX(sibling) : NULL, LYD_CTX(node), LY_EINVAL); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 779 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 780 | if (sibling) { |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 781 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 782 | } |
| 783 | |
Michal Vasko | 553d0b5 | 2020-12-04 16:27:52 +0100 | [diff] [blame] | 784 | if (sibling == node) { |
| 785 | /* we need to keep the connection to siblings so we can insert into them */ |
| 786 | sibling = sibling->prev; |
| 787 | } |
| 788 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 789 | if (node->parent || node->prev->next) { |
| 790 | lyd_unlink_tree(node); |
| 791 | } |
| 792 | |
| 793 | while (node) { |
Michal Vasko | 71e795e | 2020-12-04 16:27:37 +0100 | [diff] [blame] | 794 | if (lysc_is_key(node->schema)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 795 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 796 | return LY_EINVAL; |
| 797 | } |
| 798 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 799 | iter = node->next; |
| 800 | lyd_unlink_tree(node); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 801 | lyd_insert_node(NULL, &sibling, node, 0); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 802 | node = iter; |
| 803 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 804 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 805 | if (first) { |
| 806 | /* find the first sibling */ |
| 807 | *first = sibling; |
| 808 | while ((*first)->prev->next) { |
| 809 | *first = (*first)->prev; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | return LY_SUCCESS; |
| 814 | } |
| 815 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 816 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 817 | lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node) |
| 818 | { |
Michal Vasko | bce230b | 2022-04-19 09:55:31 +0200 | [diff] [blame] | 819 | LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 820 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 821 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 822 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 823 | |
Michal Vasko | ab7a2bf | 2022-04-01 14:37:54 +0200 | [diff] [blame] | 824 | if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 825 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes."); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 826 | return LY_EINVAL; |
| 827 | } |
Michal Vasko | 5c0b27a | 2022-04-19 09:56:02 +0200 | [diff] [blame] | 828 | if (node->schema && sibling->schema && (node->schema != sibling->schema)) { |
| 829 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance."); |
Michal Vasko | 7508ef2 | 2022-02-22 14:13:10 +0100 | [diff] [blame] | 830 | return LY_EINVAL; |
| 831 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 832 | |
Michal Vasko | 4bc2ce3 | 2020-12-08 10:06:16 +0100 | [diff] [blame] | 833 | lyd_unlink_tree(node); |
| 834 | lyd_insert_before_node(sibling, node); |
| 835 | lyd_insert_hash(node); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 836 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 837 | return LY_SUCCESS; |
| 838 | } |
| 839 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 840 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 841 | lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node) |
| 842 | { |
Michal Vasko | bce230b | 2022-04-19 09:55:31 +0200 | [diff] [blame] | 843 | LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 844 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 845 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 846 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 847 | |
Michal Vasko | ab7a2bf | 2022-04-01 14:37:54 +0200 | [diff] [blame] | 848 | if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 849 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes."); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 850 | return LY_EINVAL; |
| 851 | } |
Michal Vasko | 5c0b27a | 2022-04-19 09:56:02 +0200 | [diff] [blame] | 852 | if (node->schema && sibling->schema && (node->schema != sibling->schema)) { |
| 853 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance."); |
Michal Vasko | 7508ef2 | 2022-02-22 14:13:10 +0100 | [diff] [blame] | 854 | return LY_EINVAL; |
| 855 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 856 | |
Michal Vasko | 4bc2ce3 | 2020-12-08 10:06:16 +0100 | [diff] [blame] | 857 | lyd_unlink_tree(node); |
| 858 | lyd_insert_after_node(sibling, node); |
| 859 | lyd_insert_hash(node); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 860 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 861 | return LY_SUCCESS; |
| 862 | } |
| 863 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 864 | LIBYANG_API_DEF void |
Michal Vasko | 66d508c | 2021-07-21 16:07:09 +0200 | [diff] [blame] | 865 | lyd_unlink_siblings(struct lyd_node *node) |
| 866 | { |
| 867 | struct lyd_node *next, *elem, *first = NULL; |
| 868 | |
| 869 | LY_LIST_FOR_SAFE(node, next, elem) { |
| 870 | lyd_unlink_tree(elem); |
| 871 | lyd_insert_node(NULL, &first, elem, 1); |
| 872 | } |
| 873 | } |
| 874 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 875 | LIBYANG_API_DEF void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 876 | lyd_unlink_tree(struct lyd_node *node) |
| 877 | { |
| 878 | struct lyd_node *iter; |
| 879 | |
| 880 | if (!node) { |
| 881 | return; |
| 882 | } |
| 883 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 884 | /* update hashes while still linked into the tree */ |
| 885 | lyd_unlink_hash(node); |
| 886 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 887 | /* unlink from siblings */ |
| 888 | if (node->prev->next) { |
| 889 | node->prev->next = node->next; |
| 890 | } |
| 891 | if (node->next) { |
| 892 | node->next->prev = node->prev; |
| 893 | } else { |
| 894 | /* unlinking the last node */ |
| 895 | if (node->parent) { |
| 896 | iter = node->parent->child; |
| 897 | } else { |
| 898 | iter = node->prev; |
| 899 | while (iter->prev != node) { |
| 900 | iter = iter->prev; |
| 901 | } |
| 902 | } |
| 903 | /* update the "last" pointer from the first node */ |
| 904 | iter->prev = node->prev; |
| 905 | } |
| 906 | |
| 907 | /* unlink from parent */ |
| 908 | if (node->parent) { |
| 909 | if (node->parent->child == node) { |
| 910 | /* the node is the first child */ |
| 911 | node->parent->child = node->next; |
| 912 | } |
| 913 | |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 914 | /* check for NP container whether its last non-default node is not being unlinked */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 915 | if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) && |
| 916 | !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) { |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 917 | LY_LIST_FOR(node->parent->child, iter) { |
| 918 | if ((iter != node) && !(iter->flags & LYD_DEFAULT)) { |
| 919 | break; |
| 920 | } |
| 921 | } |
| 922 | if (!iter) { |
| 923 | node->parent->flags |= LYD_DEFAULT; |
| 924 | } |
| 925 | } |
| 926 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 927 | node->parent = NULL; |
| 928 | } |
| 929 | |
| 930 | node->next = NULL; |
| 931 | node->prev = node; |
| 932 | } |
| 933 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 934 | void |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 935 | lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 936 | { |
| 937 | struct lyd_meta *last, *iter; |
| 938 | |
| 939 | assert(parent); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 940 | |
| 941 | if (!meta) { |
| 942 | return; |
| 943 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 944 | |
| 945 | for (iter = meta; iter; iter = iter->next) { |
| 946 | iter->parent = parent; |
| 947 | } |
| 948 | |
| 949 | /* insert as the last attribute */ |
| 950 | if (parent->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 951 | for (last = parent->meta; last->next; last = last->next) {} |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 952 | last->next = meta; |
| 953 | } else { |
| 954 | parent->meta = meta; |
| 955 | } |
| 956 | |
| 957 | /* remove default flags from NP containers */ |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 958 | while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 959 | parent->flags &= ~LYD_DEFAULT; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 960 | parent = lyd_parent(parent); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 961 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | LY_ERR |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 965 | lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 966 | size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 967 | void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, ly_bool clear_dflt, ly_bool *incomplete) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 968 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 969 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 970 | struct lysc_ext_instance *ant = NULL; |
Michal Vasko | b0534f9 | 2021-07-01 13:24:03 +0200 | [diff] [blame] | 971 | const struct lysc_type **ant_type; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 972 | struct lyd_meta *mt, *last; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 973 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 974 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 975 | assert((parent || meta) && mod); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 976 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 977 | LOG_LOCSET(ctx_node, parent, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 978 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 979 | LY_ARRAY_FOR(mod->compiled->exts, u) { |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 980 | if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) && |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 981 | !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 982 | /* we have the annotation definition */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 983 | ant = &mod->compiled->exts[u]; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 984 | break; |
| 985 | } |
| 986 | } |
| 987 | if (!ant) { |
| 988 | /* attribute is not defined as a metadata annotation (RFC 7952) */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 989 | LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 990 | mod->name, (int)name_len, name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 991 | ret = LY_EINVAL; |
| 992 | goto cleanup; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 993 | } |
| 994 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 995 | mt = calloc(1, sizeof *mt); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 996 | LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 997 | mt->parent = parent; |
| 998 | mt->annotation = ant; |
Michal Vasko | b0534f9 | 2021-07-01 13:24:03 +0200 | [diff] [blame] | 999 | ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage; |
| 1000 | ret = lyd_value_store(mod->ctx, &mt->value, *ant_type, value, value_len, dynamic, format, prefix_data, hints, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1001 | ctx_node, incomplete); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1002 | LY_CHECK_ERR_GOTO(ret, free(mt), cleanup); |
| 1003 | ret = lydict_insert(mod->ctx, name, name_len, &mt->name); |
| 1004 | LY_CHECK_ERR_GOTO(ret, free(mt), cleanup); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1005 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1006 | /* insert as the last attribute */ |
| 1007 | if (parent) { |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1008 | lyd_insert_meta(parent, mt, clear_dflt); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1009 | } else if (*meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1010 | for (last = *meta; last->next; last = last->next) {} |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1011 | last->next = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1014 | if (meta) { |
| 1015 | *meta = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1016 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1017 | |
| 1018 | cleanup: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1019 | LOG_LOCBACK(ctx_node ? 1 : 0, parent ? 1 : 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1020 | return ret; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1021 | } |
| 1022 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1023 | void |
| 1024 | lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr) |
| 1025 | { |
| 1026 | struct lyd_attr *last, *iter; |
| 1027 | struct lyd_node_opaq *opaq; |
| 1028 | |
| 1029 | assert(parent && !parent->schema); |
| 1030 | |
| 1031 | if (!attr) { |
| 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | opaq = (struct lyd_node_opaq *)parent; |
| 1036 | for (iter = attr; iter; iter = iter->next) { |
| 1037 | iter->parent = opaq; |
| 1038 | } |
| 1039 | |
| 1040 | /* insert as the last attribute */ |
| 1041 | if (opaq->attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1042 | for (last = opaq->attr; last->next; last = last->next) {} |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1043 | last->next = attr; |
| 1044 | } else { |
| 1045 | opaq->attr = attr; |
| 1046 | } |
| 1047 | } |
| 1048 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1049 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1050 | lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len, |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 1051 | const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1052 | size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1053 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1054 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1055 | struct lyd_attr *at, *last; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1056 | |
| 1057 | assert(ctx && (parent || attr) && (!parent || !parent->schema)); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1058 | assert(name && name_len && format); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1059 | |
Michal Vasko | 2a3722d | 2021-06-16 11:52:39 +0200 | [diff] [blame] | 1060 | if (!value_len && (!dynamic || !*dynamic)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1061 | value = ""; |
| 1062 | } |
| 1063 | |
| 1064 | at = calloc(1, sizeof *at); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1065 | LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM); |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 1066 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1067 | LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 1068 | if (prefix_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1069 | LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 1070 | } |
| 1071 | if (module_key_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1072 | LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 1073 | } |
| 1074 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1075 | if (dynamic && *dynamic) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1076 | ret = lydict_insert_zc(ctx, (char *)value, &at->value); |
| 1077 | LY_CHECK_GOTO(ret, finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1078 | *dynamic = 0; |
| 1079 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1080 | LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1081 | } |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 1082 | at->format = format; |
| 1083 | at->val_prefix_data = val_prefix_data; |
| 1084 | at->hints = hints; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1085 | |
| 1086 | /* insert as the last attribute */ |
| 1087 | if (parent) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1088 | lyd_insert_attr(parent, at); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1089 | } else if (*attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1090 | for (last = *attr; last->next; last = last->next) {} |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1091 | last->next = at; |
| 1092 | } |
| 1093 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1094 | finish: |
| 1095 | if (ret) { |
| 1096 | lyd_free_attr_single(ctx, at); |
| 1097 | } else if (attr) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1098 | *attr = at; |
| 1099 | } |
| 1100 | return LY_SUCCESS; |
| 1101 | } |
| 1102 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1103 | LIBYANG_API_DEF const struct lyd_node_term * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1104 | lyd_target(const struct ly_path *path, const struct lyd_node *tree) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1105 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1106 | struct lyd_node *target; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1107 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1108 | if (ly_path_eval(path, tree, &target)) { |
| 1109 | return NULL; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1112 | return (struct lyd_node_term *)target; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1115 | /** |
| 1116 | * @brief Check the equality of the two schemas from different contexts. |
| 1117 | * |
| 1118 | * @param schema1 of first node. |
| 1119 | * @param schema2 of second node. |
| 1120 | * @return 1 if the schemas are equal otherwise 0. |
| 1121 | */ |
| 1122 | static ly_bool |
| 1123 | lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2) |
| 1124 | { |
| 1125 | if (!schema1 && !schema2) { |
| 1126 | return 1; |
| 1127 | } else if (!schema1 || !schema2) { |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | assert(schema1->module->ctx != schema2->module->ctx); |
| 1132 | |
| 1133 | if (schema1->nodetype != schema2->nodetype) { |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | if (strcmp(schema1->name, schema2->name)) { |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | if (strcmp(schema1->module->name, schema2->module->name)) { |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | if (schema1->module->revision || schema2->module->revision) { |
| 1146 | if (!schema1->module->revision || !schema2->module->revision) { |
| 1147 | return 0; |
| 1148 | } |
| 1149 | if (strcmp(schema1->module->revision, schema2->module->revision)) { |
| 1150 | return 0; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | return 1; |
| 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * @brief Check the equality of the schemas for all parent nodes. |
| 1159 | * |
| 1160 | * Both nodes must be from different contexts. |
| 1161 | * |
| 1162 | * @param node1 Data of first node. |
| 1163 | * @param node2 Data of second node. |
| 1164 | * @return 1 if the all related parental schemas are equal otherwise 0. |
| 1165 | */ |
| 1166 | static ly_bool |
| 1167 | lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2) |
| 1168 | { |
| 1169 | const struct lysc_node *parent1, *parent2; |
| 1170 | |
| 1171 | assert(node1 && node2); |
| 1172 | |
| 1173 | for (parent1 = node1->schema->parent, parent2 = node2->schema->parent; |
| 1174 | parent1 && parent2; |
| 1175 | parent1 = parent1->parent, parent2 = parent2->parent) { |
| 1176 | if (!lyd_compare_schema_equal(parent1, parent2)) { |
| 1177 | return 0; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | if (parent1 || parent2) { |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | return 1; |
| 1186 | } |
| 1187 | |
| 1188 | /** |
| 1189 | * @brief Internal implementation of @ref lyd_compare_single. |
| 1190 | * @copydoc lyd_compare_single |
| 1191 | * @param[in] parental_schemas_checked Flag used for optimization. |
| 1192 | * When this function is called for the first time, the flag must be set to 0. |
| 1193 | * The @ref lyd_compare_schema_parents_equal should be called only once during |
| 1194 | * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body. |
| 1195 | */ |
| 1196 | static LY_ERR |
| 1197 | lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2, |
| 1198 | uint32_t options, ly_bool parental_schemas_checked) |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1199 | { |
| 1200 | const struct lyd_node *iter1, *iter2; |
| 1201 | struct lyd_node_term *term1, *term2; |
| 1202 | struct lyd_node_any *any1, *any2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1203 | struct lyd_node_opaq *opaq1, *opaq2; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1204 | size_t len1, len2; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1205 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1206 | if (!node1 || !node2) { |
| 1207 | if (node1 == node2) { |
| 1208 | return LY_SUCCESS; |
| 1209 | } else { |
| 1210 | return LY_ENOT; |
| 1211 | } |
| 1212 | } |
| 1213 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1214 | if (LYD_CTX(node1) == LYD_CTX(node2)) { |
| 1215 | /* same contexts */ |
| 1216 | if (node1->schema != node2->schema) { |
| 1217 | return LY_ENOT; |
| 1218 | } |
| 1219 | } else { |
| 1220 | /* different contexts */ |
| 1221 | if (!lyd_compare_schema_equal(node1->schema, node2->schema)) { |
| 1222 | return LY_ENOT; |
| 1223 | } |
| 1224 | if (!parental_schemas_checked) { |
| 1225 | if (!lyd_compare_schema_parents_equal(node1, node2)) { |
| 1226 | return LY_ENOT; |
| 1227 | } |
| 1228 | parental_schemas_checked = 1; |
| 1229 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | if (node1->hash != node2->hash) { |
| 1233 | return LY_ENOT; |
| 1234 | } |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1235 | /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1236 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1237 | if (!node1->schema) { |
| 1238 | opaq1 = (struct lyd_node_opaq *)node1; |
| 1239 | opaq2 = (struct lyd_node_opaq *)node2; |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1240 | if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) || |
| 1241 | (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1242 | return LY_ENOT; |
| 1243 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1244 | switch (opaq1->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1245 | case LY_VALUE_XML: |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1246 | if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value, |
| 1247 | opaq2->val_prefix_data)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1248 | return LY_ENOT; |
| 1249 | } |
| 1250 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1251 | case LY_VALUE_JSON: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1252 | /* prefixes in JSON are unique, so it is not necessary to canonize the values */ |
| 1253 | if (strcmp(opaq1->value, opaq2->value)) { |
| 1254 | return LY_ENOT; |
| 1255 | } |
| 1256 | break; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1257 | default: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1258 | /* not allowed */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1259 | LOGINT(LYD_CTX(node1)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1260 | return LY_EINT; |
| 1261 | } |
| 1262 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 1263 | iter1 = opaq1->child; |
| 1264 | iter2 = opaq2->child; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1265 | goto all_children_compare; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1266 | } |
| 1267 | return LY_SUCCESS; |
| 1268 | } else { |
| 1269 | switch (node1->schema->nodetype) { |
| 1270 | case LYS_LEAF: |
| 1271 | case LYS_LEAFLIST: |
| 1272 | if (options & LYD_COMPARE_DEFAULTS) { |
| 1273 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 1274 | return LY_ENOT; |
| 1275 | } |
| 1276 | } |
| 1277 | |
Michal Vasko | 1a2bf2e | 2020-06-16 13:07:48 +0200 | [diff] [blame] | 1278 | term1 = (struct lyd_node_term *)node1; |
| 1279 | term2 = (struct lyd_node_term *)node2; |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1280 | |
| 1281 | /* same contexts */ |
| 1282 | if (LYD_CTX(node1) == LYD_CTX(node2)) { |
| 1283 | return term1->value.realtype->plugin->compare(&term1->value, &term2->value); |
| 1284 | } |
| 1285 | |
| 1286 | /* different contexts */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1287 | if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1288 | return LY_ENOT; |
| 1289 | } |
| 1290 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1291 | case LYS_CONTAINER: |
Michal Vasko | c8187dc | 2022-05-13 12:26:40 +0200 | [diff] [blame] | 1292 | case LYS_RPC: |
| 1293 | case LYS_ACTION: |
| 1294 | case LYS_NOTIF: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1295 | if (options & LYD_COMPARE_DEFAULTS) { |
| 1296 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 1297 | return LY_ENOT; |
| 1298 | } |
| 1299 | } |
| 1300 | if (options & LYD_COMPARE_FULL_RECURSION) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1301 | iter1 = lyd_child(node1); |
| 1302 | iter2 = lyd_child(node2); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1303 | goto all_children_compare; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1304 | } |
| 1305 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1306 | case LYS_LIST: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1307 | iter1 = lyd_child(node1); |
| 1308 | iter2 = lyd_child(node2); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1309 | |
| 1310 | if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) { |
| 1311 | /* lists with keys, their equivalence is based on their keys */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 1312 | for (const struct lysc_node *key = lysc_node_child(node1->schema); |
Michal Vasko | c57d9a9 | 2020-06-23 13:28:27 +0200 | [diff] [blame] | 1313 | key && (key->flags & LYS_KEY); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1314 | key = key->next) { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1315 | if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1316 | return LY_ENOT; |
| 1317 | } |
| 1318 | iter1 = iter1->next; |
| 1319 | iter2 = iter2->next; |
| 1320 | } |
| 1321 | } else { |
| 1322 | /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */ |
| 1323 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1324 | all_children_compare: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1325 | if (!iter1 && !iter2) { |
| 1326 | /* no children, nothing to compare */ |
| 1327 | return LY_SUCCESS; |
| 1328 | } |
| 1329 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1330 | for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1331 | if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1332 | return LY_ENOT; |
| 1333 | } |
| 1334 | } |
| 1335 | if (iter1 || iter2) { |
| 1336 | return LY_ENOT; |
| 1337 | } |
| 1338 | } |
| 1339 | return LY_SUCCESS; |
| 1340 | case LYS_ANYXML: |
| 1341 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 1342 | any1 = (struct lyd_node_any *)node1; |
| 1343 | any2 = (struct lyd_node_any *)node2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1344 | |
| 1345 | if (any1->value_type != any2->value_type) { |
| 1346 | return LY_ENOT; |
| 1347 | } |
| 1348 | switch (any1->value_type) { |
| 1349 | case LYD_ANYDATA_DATATREE: |
| 1350 | iter1 = any1->value.tree; |
| 1351 | iter2 = any2->value.tree; |
| 1352 | goto all_children_compare; |
| 1353 | case LYD_ANYDATA_STRING: |
| 1354 | case LYD_ANYDATA_XML: |
| 1355 | case LYD_ANYDATA_JSON: |
| 1356 | len1 = strlen(any1->value.str); |
| 1357 | len2 = strlen(any2->value.str); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1358 | if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1359 | return LY_ENOT; |
| 1360 | } |
| 1361 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1362 | case LYD_ANYDATA_LYB: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1363 | len1 = lyd_lyb_data_length(any1->value.mem); |
| 1364 | len2 = lyd_lyb_data_length(any2->value.mem); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1365 | if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1366 | return LY_ENOT; |
| 1367 | } |
| 1368 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1369 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1373 | LOGINT(LYD_CTX(node1)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 1374 | return LY_EINT; |
| 1375 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1376 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1377 | LIBYANG_API_DEF LY_ERR |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 1378 | lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
| 1379 | { |
| 1380 | return lyd_compare_single_(node1, node2, options, 0); |
| 1381 | } |
| 1382 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1383 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1384 | lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1385 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1386 | for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1387 | LY_CHECK_RET(lyd_compare_single(node1, node2, options)); |
| 1388 | } |
| 1389 | |
Michal Vasko | 11a8143 | 2020-07-28 16:31:29 +0200 | [diff] [blame] | 1390 | if (node1 == node2) { |
| 1391 | return LY_SUCCESS; |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1392 | } |
Michal Vasko | 11a8143 | 2020-07-28 16:31:29 +0200 | [diff] [blame] | 1393 | return LY_ENOT; |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1396 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 1397 | lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2) |
| 1398 | { |
| 1399 | if (!meta1 || !meta2) { |
| 1400 | if (meta1 == meta2) { |
| 1401 | return LY_SUCCESS; |
| 1402 | } else { |
| 1403 | return LY_ENOT; |
| 1404 | } |
| 1405 | } |
| 1406 | |
Michal Vasko | a808306 | 2020-11-06 17:22:10 +0100 | [diff] [blame] | 1407 | if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) { |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 1408 | return LY_ENOT; |
| 1409 | } |
| 1410 | |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 1411 | return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value); |
| 1412 | } |
| 1413 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1414 | /** |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 1415 | * @brief Create a copy of the attribute. |
| 1416 | * |
| 1417 | * @param[in] attr Attribute to copy. |
| 1418 | * @param[in] node Opaque where to append the new attribute. |
| 1419 | * @param[out] dup Optional created attribute copy. |
| 1420 | * @return LY_ERR value. |
| 1421 | */ |
| 1422 | static LY_ERR |
| 1423 | lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup) |
| 1424 | { |
| 1425 | LY_ERR ret = LY_SUCCESS; |
| 1426 | struct lyd_attr *a, *last; |
| 1427 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node; |
| 1428 | |
| 1429 | LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL); |
| 1430 | |
| 1431 | /* create a copy */ |
| 1432 | a = calloc(1, sizeof *attr); |
| 1433 | LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM); |
| 1434 | |
| 1435 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish); |
| 1436 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish); |
| 1437 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish); |
| 1438 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish); |
| 1439 | a->hints = attr->hints; |
| 1440 | a->format = attr->format; |
| 1441 | if (attr->val_prefix_data) { |
| 1442 | ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data); |
| 1443 | LY_CHECK_GOTO(ret, finish); |
| 1444 | } |
| 1445 | |
| 1446 | /* insert as the last attribute */ |
| 1447 | a->parent = opaq; |
| 1448 | if (opaq->attr) { |
| 1449 | for (last = opaq->attr; last->next; last = last->next) {} |
| 1450 | last->next = a; |
| 1451 | } else { |
| 1452 | opaq->attr = a; |
| 1453 | } |
| 1454 | |
| 1455 | finish: |
| 1456 | if (ret) { |
| 1457 | lyd_free_attr_single(LYD_CTX(node), a); |
| 1458 | } else if (dup) { |
| 1459 | *dup = a; |
| 1460 | } |
| 1461 | return LY_SUCCESS; |
| 1462 | } |
| 1463 | |
| 1464 | /** |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1465 | * @brief Find @p schema equivalent in @p trg_ctx. |
| 1466 | * |
| 1467 | * @param[in] schema Schema node to find. |
| 1468 | * @param[in] trg_ctx Target context to search in. |
| 1469 | * @param[in] parent Data parent of @p schema, if any. |
| 1470 | * @param[out] trg_schema Found schema from @p trg_ctx to use. |
| 1471 | * @return LY_RRR value. |
| 1472 | */ |
| 1473 | static LY_ERR |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1474 | lyd_dup_find_schema(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1475 | const struct lysc_node **trg_schema) |
| 1476 | { |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1477 | const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp; |
| 1478 | const struct lys_module *trg_mod = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1479 | char *path; |
| 1480 | |
| 1481 | if (!schema) { |
| 1482 | /* opaque node */ |
| 1483 | *trg_schema = NULL; |
| 1484 | return LY_SUCCESS; |
| 1485 | } |
| 1486 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1487 | if (lysc_data_parent(schema) && parent && parent->schema) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1488 | /* start from schema parent */ |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1489 | trg_parent = parent->schema; |
| 1490 | src_parent = lysc_data_parent(schema); |
| 1491 | } |
| 1492 | |
| 1493 | do { |
| 1494 | /* find the next parent */ |
| 1495 | sp = schema; |
| 1496 | while (sp->parent != src_parent) { |
| 1497 | sp = sp->parent; |
| 1498 | } |
| 1499 | src_parent = sp; |
| 1500 | |
| 1501 | if (!src_parent->parent) { |
| 1502 | /* find the module first */ |
| 1503 | trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name); |
| 1504 | if (!trg_mod) { |
| 1505 | LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.", |
| 1506 | src_parent->module->name); |
| 1507 | return LY_ENOTFOUND; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | /* find the next parent */ |
| 1512 | assert(trg_parent || trg_mod); |
| 1513 | tp = NULL; |
| 1514 | while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) { |
| 1515 | if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) { |
| 1516 | break; |
| 1517 | } |
| 1518 | } |
| 1519 | if (!tp) { |
| 1520 | /* schema node not found */ |
| 1521 | path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0); |
| 1522 | LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path); |
| 1523 | free(path); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1524 | return LY_ENOTFOUND; |
| 1525 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1526 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1527 | trg_parent = tp; |
| 1528 | } while (schema != src_parent); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1529 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1530 | /* success */ |
| 1531 | *trg_schema = trg_parent; |
| 1532 | return LY_SUCCESS; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1536 | * @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] | 1537 | * |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1538 | * 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] | 1539 | * |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1540 | * @param[in] node Node to duplicate. |
| 1541 | * @param[in] trg_ctx Target context for duplicated nodes. |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 1542 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 1543 | * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for |
| 1544 | * recursive duplication as an optimization. |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 1545 | * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set. |
| 1546 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1547 | * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first). |
| 1548 | * @return LY_ERR value. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1549 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1550 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1551 | lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last, |
| 1552 | struct lyd_node **first, uint32_t options, struct lyd_node **dup_p) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1553 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1554 | LY_ERR ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1555 | struct lyd_node *dup = NULL; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 1556 | struct lyd_meta *meta; |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 1557 | struct lyd_attr *attr; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 1558 | struct lyd_node_any *any; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1559 | const struct lysc_type *type; |
| 1560 | const char *val_can; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1561 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1562 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1563 | |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 1564 | if (node->flags & LYD_EXT) { |
| 1565 | /* we need to use the same context */ |
| 1566 | trg_ctx = LYD_CTX(node); |
| 1567 | } |
| 1568 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1569 | if (!node->schema) { |
| 1570 | dup = calloc(1, sizeof(struct lyd_node_opaq)); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1571 | ((struct lyd_node_opaq *)dup)->ctx = trg_ctx; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1572 | } else { |
| 1573 | switch (node->schema->nodetype) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1574 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1575 | case LYS_ACTION: |
| 1576 | case LYS_NOTIF: |
| 1577 | case LYS_CONTAINER: |
| 1578 | case LYS_LIST: |
| 1579 | dup = calloc(1, sizeof(struct lyd_node_inner)); |
| 1580 | break; |
| 1581 | case LYS_LEAF: |
| 1582 | case LYS_LEAFLIST: |
| 1583 | dup = calloc(1, sizeof(struct lyd_node_term)); |
| 1584 | break; |
| 1585 | case LYS_ANYDATA: |
| 1586 | case LYS_ANYXML: |
| 1587 | dup = calloc(1, sizeof(struct lyd_node_any)); |
| 1588 | break; |
| 1589 | default: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1590 | LOGINT(trg_ctx); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1591 | ret = LY_EINT; |
| 1592 | goto error; |
| 1593 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1594 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1595 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1596 | |
Michal Vasko | f6df0a0 | 2020-06-16 13:08:34 +0200 | [diff] [blame] | 1597 | if (options & LYD_DUP_WITH_FLAGS) { |
| 1598 | dup->flags = node->flags; |
| 1599 | } else { |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 1600 | dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW; |
Michal Vasko | f6df0a0 | 2020-06-16 13:08:34 +0200 | [diff] [blame] | 1601 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1602 | if (trg_ctx == LYD_CTX(node)) { |
| 1603 | dup->schema = node->schema; |
| 1604 | } else { |
Michal Vasko | 27f536f | 2022-04-01 09:17:30 +0200 | [diff] [blame] | 1605 | ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema); |
| 1606 | if (ret) { |
| 1607 | /* has no schema but is not an opaque node */ |
| 1608 | free(dup); |
| 1609 | dup = NULL; |
| 1610 | goto error; |
| 1611 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1612 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1613 | dup->prev = dup; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1614 | |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 1615 | /* duplicate metadata/attributes */ |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 1616 | if (!(options & LYD_DUP_NO_META)) { |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 1617 | if (!node->schema) { |
| 1618 | LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) { |
| 1619 | LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error); |
| 1620 | } |
| 1621 | } else { |
| 1622 | LY_LIST_FOR(node->meta, meta) { |
| 1623 | LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error); |
| 1624 | } |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 1625 | } |
| 1626 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1627 | |
| 1628 | /* nodetype-specific work */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1629 | if (!dup->schema) { |
| 1630 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup; |
| 1631 | struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node; |
| 1632 | struct lyd_node *child; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1633 | |
| 1634 | if (options & LYD_DUP_RECURSIVE) { |
| 1635 | /* duplicate all the children */ |
| 1636 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1637 | LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1638 | } |
| 1639 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1640 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error); |
| 1641 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error); |
| 1642 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error); |
| 1643 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error); |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 1644 | opaq->hints = orig->hints; |
| 1645 | opaq->format = orig->format; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1646 | if (orig->val_prefix_data) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1647 | ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1648 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1649 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1650 | } else if (dup->schema->nodetype & LYD_NODE_TERM) { |
| 1651 | struct lyd_node_term *term = (struct lyd_node_term *)dup; |
| 1652 | struct lyd_node_term *orig = (struct lyd_node_term *)node; |
| 1653 | |
| 1654 | term->hash = orig->hash; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1655 | if (trg_ctx == LYD_CTX(node)) { |
| 1656 | ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value); |
| 1657 | LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error); |
| 1658 | } else { |
| 1659 | /* store canonical value in the target context */ |
| 1660 | val_can = lyd_get_value(node); |
| 1661 | type = ((struct lysc_node_leaf *)term->schema)->type; |
| 1662 | ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL, |
| 1663 | LYD_HINT_DATA, term->schema, NULL); |
| 1664 | LY_CHECK_GOTO(ret, error); |
| 1665 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1666 | } else if (dup->schema->nodetype & LYD_NODE_INNER) { |
| 1667 | struct lyd_node_inner *orig = (struct lyd_node_inner *)node; |
| 1668 | struct lyd_node *child; |
| 1669 | |
| 1670 | if (options & LYD_DUP_RECURSIVE) { |
| 1671 | /* duplicate all the children */ |
| 1672 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1673 | LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1674 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1675 | } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1676 | /* always duplicate keys of a list */ |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 1677 | for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1678 | LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | lyd_hash(dup); |
| 1682 | } else if (dup->schema->nodetype & LYD_NODE_ANY) { |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 1683 | dup->hash = node->hash; |
| 1684 | any = (struct lyd_node_any *)node; |
| 1685 | 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] | 1686 | } |
| 1687 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1688 | /* insert */ |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 1689 | lyd_insert_node(parent, first, dup, insert_last); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1690 | |
| 1691 | if (dup_p) { |
| 1692 | *dup_p = dup; |
| 1693 | } |
| 1694 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1695 | |
| 1696 | error: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1697 | lyd_free_tree(dup); |
| 1698 | return ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1699 | } |
| 1700 | |
Michal Vasko | 29d674b | 2021-08-25 11:18:35 +0200 | [diff] [blame] | 1701 | /** |
| 1702 | * @brief Get a parent node to connect duplicated subtree to. |
| 1703 | * |
| 1704 | * @param[in] node Node (subtree) to duplicate. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1705 | * @param[in] trg_ctx Target context for duplicated nodes. |
Michal Vasko | 29d674b | 2021-08-25 11:18:35 +0200 | [diff] [blame] | 1706 | * @param[in] parent Initial parent to connect to. |
| 1707 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
| 1708 | * @param[out] dup_parent First duplicated parent node, if any. |
| 1709 | * @param[out] local_parent Correct parent to directly connect duplicated @p node to. |
| 1710 | * @return LY_ERR value. |
| 1711 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1712 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1713 | lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent, |
| 1714 | uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1715 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1716 | const struct lyd_node_inner *orig_parent, *iter; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1717 | ly_bool repeat = 1; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1718 | |
| 1719 | *dup_parent = NULL; |
| 1720 | *local_parent = NULL; |
| 1721 | |
| 1722 | for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) { |
| 1723 | if (parent && (parent->schema == orig_parent->schema)) { |
| 1724 | /* stop creating parents, connect what we have into the provided parent */ |
| 1725 | iter = parent; |
| 1726 | repeat = 0; |
| 1727 | } else { |
| 1728 | iter = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1729 | LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, trg_ctx, NULL, 0, (struct lyd_node **)&iter, options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1730 | (struct lyd_node **)&iter)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1731 | } |
| 1732 | if (!*local_parent) { |
| 1733 | *local_parent = (struct lyd_node_inner *)iter; |
| 1734 | } |
| 1735 | if (iter->child) { |
| 1736 | /* 1) list - add after keys |
| 1737 | * 2) provided parent with some children */ |
| 1738 | iter->child->prev->next = *dup_parent; |
| 1739 | if (*dup_parent) { |
| 1740 | (*dup_parent)->prev = iter->child->prev; |
| 1741 | iter->child->prev = *dup_parent; |
| 1742 | } |
| 1743 | } else { |
| 1744 | ((struct lyd_node_inner *)iter)->child = *dup_parent; |
| 1745 | } |
| 1746 | if (*dup_parent) { |
| 1747 | (*dup_parent)->parent = (struct lyd_node_inner *)iter; |
| 1748 | } |
| 1749 | *dup_parent = (struct lyd_node *)iter; |
| 1750 | } |
| 1751 | |
| 1752 | if (repeat && parent) { |
| 1753 | /* given parent and created parents chain actually do not interconnect */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1754 | LOGERR(trg_ctx, LY_EINVAL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1755 | "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1756 | return LY_EINVAL; |
| 1757 | } |
| 1758 | |
| 1759 | return LY_SUCCESS; |
| 1760 | } |
| 1761 | |
| 1762 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1763 | lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options, |
| 1764 | ly_bool nosiblings, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1765 | { |
| 1766 | LY_ERR rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1767 | const struct lyd_node *orig; /* original node to be duplicated */ |
| 1768 | struct lyd_node *first = NULL; /* the first duplicated node, this is returned */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1769 | struct lyd_node *top = NULL; /* the most higher created node */ |
| 1770 | 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] | 1771 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1772 | assert(node && trg_ctx && (!parent || (LYD_CTX(parent) == trg_ctx))); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1773 | |
| 1774 | if (options & LYD_DUP_WITH_PARENTS) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1775 | LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, trg_ctx, parent, options & (LYD_DUP_WITH_FLAGS | LYD_DUP_NO_META), |
Michal Vasko | 2090975 | 2021-05-18 16:13:38 +0200 | [diff] [blame] | 1776 | &top, &local_parent), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1777 | } else { |
| 1778 | local_parent = parent; |
| 1779 | } |
| 1780 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1781 | LY_LIST_FOR(node, orig) { |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 1782 | if (lysc_is_key(orig->schema)) { |
| 1783 | if (local_parent) { |
| 1784 | /* the key must already exist in the parent */ |
| 1785 | rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1786 | LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 1787 | } else { |
| 1788 | assert(!(options & LYD_DUP_WITH_PARENTS)); |
| 1789 | /* duplicating a single key, okay, I suppose... */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1790 | rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 1791 | LY_CHECK_GOTO(rc, error); |
| 1792 | } |
| 1793 | } else { |
| 1794 | /* if there is no local parent, it will be inserted into first */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1795 | rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options, |
| 1796 | first ? NULL : &first); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 1797 | LY_CHECK_GOTO(rc, error); |
| 1798 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1799 | if (nosiblings) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1800 | break; |
| 1801 | } |
| 1802 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1803 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1804 | if (dup) { |
| 1805 | *dup = first; |
| 1806 | } |
| 1807 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1808 | |
| 1809 | error: |
| 1810 | if (top) { |
| 1811 | lyd_free_tree(top); |
| 1812 | } else { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1813 | lyd_free_siblings(first); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1814 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1815 | return rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 1816 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1817 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1818 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1819 | lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1820 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1821 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
| 1822 | if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) { |
| 1823 | LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication."); |
| 1824 | return LY_EINVAL; |
| 1825 | } |
| 1826 | |
| 1827 | return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup); |
| 1828 | } |
| 1829 | |
| 1830 | LIBYANG_API_DEF LY_ERR |
| 1831 | lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, |
| 1832 | uint32_t options, struct lyd_node **dup) |
| 1833 | { |
| 1834 | LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL); |
| 1835 | |
| 1836 | return lyd_dup(node, trg_ctx, parent, options, 1, dup); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1837 | } |
| 1838 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1839 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1840 | lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1841 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1842 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
| 1843 | if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) { |
| 1844 | LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication."); |
| 1845 | return LY_EINVAL; |
| 1846 | } |
| 1847 | |
| 1848 | return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup); |
| 1849 | } |
| 1850 | |
| 1851 | LIBYANG_API_DEF LY_ERR |
| 1852 | lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, |
| 1853 | uint32_t options, struct lyd_node **dup) |
| 1854 | { |
| 1855 | LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL); |
| 1856 | |
| 1857 | return lyd_dup(node, trg_ctx, parent, options, 0, dup); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1858 | } |
| 1859 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1860 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1861 | 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] | 1862 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1863 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 1864 | struct lyd_meta *mt, *last; |
| 1865 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1866 | LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 1867 | |
| 1868 | /* create a copy */ |
| 1869 | mt = calloc(1, sizeof *mt); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1870 | LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 1871 | mt->annotation = meta->annotation; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1872 | ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1873 | LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish); |
| 1874 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 1875 | |
| 1876 | /* insert as the last attribute */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1877 | mt->parent = node; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 1878 | if (node->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1879 | for (last = node->meta; last->next; last = last->next) {} |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 1880 | last->next = mt; |
| 1881 | } else { |
| 1882 | node->meta = mt; |
| 1883 | } |
| 1884 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1885 | finish: |
| 1886 | if (ret) { |
| 1887 | lyd_free_meta_single(mt); |
| 1888 | } else if (dup) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1889 | *dup = mt; |
| 1890 | } |
| 1891 | return LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 1892 | } |
| 1893 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1894 | /** |
| 1895 | * @brief Merge a source sibling into target siblings. |
| 1896 | * |
| 1897 | * @param[in,out] first_trg First target sibling, is updated if top-level. |
| 1898 | * @param[in] parent_trg Target parent. |
| 1899 | * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent. |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 1900 | * @param[in] merge_cb Optional merge callback. |
| 1901 | * @param[in] cb_data Arbitrary callback data. |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1902 | * @param[in] options Merge options. |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 1903 | * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings. |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1904 | * @return LY_ERR value. |
| 1905 | */ |
| 1906 | static LY_ERR |
| 1907 | lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p, |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 1908 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1909 | { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1910 | const struct lyd_node *child_src, *tmp, *sibling_src; |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1911 | struct lyd_node *match_trg, *dup_src, *elem; |
Michal Vasko | d1afa50 | 2022-01-27 16:18:12 +0100 | [diff] [blame] | 1912 | struct lyd_node_opaq *opaq_trg, *opaq_src; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1913 | struct lysc_type *type; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 1914 | struct lyd_dup_inst *child_dup_inst = NULL; |
| 1915 | LY_ERR ret; |
| 1916 | ly_bool first_inst = 0; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1917 | |
| 1918 | sibling_src = *sibling_src_p; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 1919 | if (!sibling_src->schema) { |
| 1920 | /* try to find the same opaque node */ |
| 1921 | lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg); |
| 1922 | } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1923 | /* try to find the exact instance */ |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 1924 | lyd_find_sibling_first(*first_trg, sibling_src, &match_trg); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1925 | } else { |
| 1926 | /* try to simply find the node, there cannot be more instances */ |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 1927 | lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1928 | } |
| 1929 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 1930 | if (match_trg) { |
| 1931 | /* update match as needed */ |
| 1932 | LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst)); |
| 1933 | } else { |
| 1934 | /* first instance of this node */ |
| 1935 | first_inst = 1; |
| 1936 | } |
| 1937 | |
| 1938 | if (match_trg) { |
| 1939 | /* call callback */ |
| 1940 | if (merge_cb) { |
| 1941 | LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data)); |
| 1942 | } |
| 1943 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1944 | /* node found, make sure even value matches for all node types */ |
Michal Vasko | d1afa50 | 2022-01-27 16:18:12 +0100 | [diff] [blame] | 1945 | if (!match_trg->schema) { |
| 1946 | if (lyd_compare_single(sibling_src, match_trg, 0)) { |
| 1947 | /* update value */ |
| 1948 | opaq_trg = (struct lyd_node_opaq *)match_trg; |
| 1949 | opaq_src = (struct lyd_node_opaq *)sibling_src; |
| 1950 | |
| 1951 | lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value); |
| 1952 | lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value); |
| 1953 | opaq_trg->hints = opaq_src->hints; |
| 1954 | |
| 1955 | ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data); |
| 1956 | opaq_trg->format = opaq_src->format; |
| 1957 | ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data, |
| 1958 | &opaq_trg->val_prefix_data); |
| 1959 | } |
| 1960 | } else if ((match_trg->schema->nodetype == LYS_LEAF) && |
| 1961 | lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1962 | /* since they are different, they cannot both be default */ |
| 1963 | assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT)); |
| 1964 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1965 | /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */ |
| 1966 | if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1967 | type = ((struct lysc_node_leaf *)match_trg->schema)->type; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1968 | type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value); |
| 1969 | LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1970 | &((struct lyd_node_term *)match_trg)->value)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1971 | |
| 1972 | /* copy flags and add LYD_NEW */ |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 1973 | match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1974 | } |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1975 | } 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] | 1976 | /* update value */ |
| 1977 | LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1978 | ((struct lyd_node_any *)sibling_src)->value_type)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1979 | |
| 1980 | /* copy flags and add LYD_NEW */ |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 1981 | match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW); |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /* check descendants, recursively */ |
| 1985 | ret = LY_SUCCESS; |
| 1986 | LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) { |
| 1987 | ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options, |
| 1988 | &child_dup_inst); |
| 1989 | if (ret) { |
| 1990 | break; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1991 | } |
| 1992 | } |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 1993 | lyd_dup_inst_free(child_dup_inst); |
| 1994 | LY_CHECK_RET(ret); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1995 | } else { |
| 1996 | /* node not found, merge it */ |
| 1997 | if (options & LYD_MERGE_DESTRUCT) { |
| 1998 | dup_src = (struct lyd_node *)sibling_src; |
| 1999 | lyd_unlink_tree(dup_src); |
| 2000 | /* spend it */ |
| 2001 | *sibling_src_p = NULL; |
| 2002 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2003 | 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] | 2004 | } |
| 2005 | |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 2006 | if (!(options & LYD_MERGE_WITH_FLAGS)) { |
| 2007 | /* set LYD_NEW for all the new nodes, required for validation */ |
| 2008 | LYD_TREE_DFS_BEGIN(dup_src, elem) { |
| 2009 | elem->flags |= LYD_NEW; |
| 2010 | LYD_TREE_DFS_END(dup_src, elem); |
| 2011 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2012 | } |
| 2013 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2014 | /* insert */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2015 | lyd_insert_node(parent_trg, first_trg, dup_src, 0); |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2016 | |
| 2017 | if (first_inst) { |
| 2018 | /* remember not to find this instance next time */ |
| 2019 | LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst)); |
| 2020 | } |
| 2021 | |
| 2022 | /* call callback, no source node */ |
| 2023 | if (merge_cb) { |
| 2024 | LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data)); |
| 2025 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | return LY_SUCCESS; |
| 2029 | } |
| 2030 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2031 | static LY_ERR |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2032 | lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod, |
| 2033 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2034 | { |
| 2035 | const struct lyd_node *sibling_src, *tmp; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2036 | struct lyd_dup_inst *dup_inst = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2037 | ly_bool first; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2038 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2039 | |
| 2040 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2041 | LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL, |
| 2042 | LY_EINVAL); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2043 | |
| 2044 | if (!source) { |
| 2045 | /* nothing to merge */ |
| 2046 | return LY_SUCCESS; |
| 2047 | } |
| 2048 | |
Michal Vasko | 831422b | 2020-11-24 11:20:51 +0100 | [diff] [blame] | 2049 | if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2050 | LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2051 | return LY_EINVAL; |
| 2052 | } |
| 2053 | |
| 2054 | LY_LIST_FOR_SAFE(source, tmp, sibling_src) { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2055 | if (mod && (lyd_owner_module(sibling_src) != mod)) { |
| 2056 | /* skip data nodes from different modules */ |
| 2057 | continue; |
| 2058 | } |
| 2059 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2060 | first = (sibling_src == source) ? 1 : 0; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2061 | ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst); |
| 2062 | if (ret) { |
| 2063 | break; |
| 2064 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2065 | if (first && !sibling_src) { |
| 2066 | /* source was spent (unlinked), move to the next node */ |
| 2067 | source = tmp; |
| 2068 | } |
| 2069 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2070 | if (nosiblings) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2071 | break; |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | if (options & LYD_MERGE_DESTRUCT) { |
| 2076 | /* free any leftover source data that were not merged */ |
| 2077 | lyd_free_siblings((struct lyd_node *)source); |
| 2078 | } |
| 2079 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2080 | lyd_dup_inst_free(dup_inst); |
| 2081 | return ret; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2082 | } |
| 2083 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2084 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2085 | lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2086 | { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2087 | return lyd_merge(target, source, NULL, NULL, NULL, options, 1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2088 | } |
| 2089 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2090 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2091 | lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2092 | { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2093 | return lyd_merge(target, source, NULL, NULL, NULL, options, 0); |
| 2094 | } |
| 2095 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2096 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 2097 | lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod, |
| 2098 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options) |
| 2099 | { |
| 2100 | return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2101 | } |
| 2102 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2103 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2104 | lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2105 | { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2106 | /* ending \0 */ |
| 2107 | ++reqlen; |
| 2108 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2109 | if (reqlen > *buflen) { |
| 2110 | if (is_static) { |
| 2111 | return LY_EINCOMPLETE; |
| 2112 | } |
| 2113 | |
| 2114 | *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer); |
| 2115 | if (!*buffer) { |
| 2116 | return LY_EMEM; |
| 2117 | } |
| 2118 | |
| 2119 | *buflen = reqlen; |
| 2120 | } |
| 2121 | |
| 2122 | return LY_SUCCESS; |
| 2123 | } |
| 2124 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 2125 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2126 | lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2127 | { |
| 2128 | const struct lyd_node *key; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2129 | size_t len; |
| 2130 | const char *val; |
| 2131 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2132 | |
Michal Vasko | 824d083 | 2021-11-04 15:36:51 +0100 | [diff] [blame] | 2133 | for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2134 | val = lyd_get_value(key); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2135 | len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2136 | LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2137 | |
| 2138 | quot = '\''; |
| 2139 | if (strchr(val, '\'')) { |
| 2140 | quot = '"'; |
| 2141 | } |
| 2142 | *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | return LY_SUCCESS; |
| 2146 | } |
| 2147 | |
| 2148 | /** |
| 2149 | * @brief Append leaf-list value predicate to path. |
| 2150 | * |
| 2151 | * @param[in] node Node to print. |
| 2152 | * @param[in,out] buffer Buffer to print to. |
| 2153 | * @param[in,out] buflen Current buffer length. |
| 2154 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 2155 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 2156 | * @return LY_ERR |
| 2157 | */ |
| 2158 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2159 | lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2160 | { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2161 | size_t len; |
| 2162 | const char *val; |
| 2163 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2164 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2165 | val = lyd_get_value(node); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 2166 | len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2167 | LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2168 | |
| 2169 | quot = '\''; |
| 2170 | if (strchr(val, '\'')) { |
| 2171 | quot = '"'; |
| 2172 | } |
| 2173 | *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot); |
| 2174 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2175 | return LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | /** |
| 2179 | * @brief Append node position (relative to its other instances) predicate to path. |
| 2180 | * |
| 2181 | * @param[in] node Node to print. |
| 2182 | * @param[in,out] buffer Buffer to print to. |
| 2183 | * @param[in,out] buflen Current buffer length. |
| 2184 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 2185 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 2186 | * @return LY_ERR |
| 2187 | */ |
| 2188 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2189 | lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2190 | { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2191 | size_t len; |
Michal Vasko | 50cc056 | 2021-05-18 16:15:43 +0200 | [diff] [blame] | 2192 | uint32_t pos; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2193 | char *val = NULL; |
| 2194 | LY_ERR rc; |
| 2195 | |
Michal Vasko | 50cc056 | 2021-05-18 16:15:43 +0200 | [diff] [blame] | 2196 | pos = lyd_list_pos(node); |
| 2197 | if (asprintf(&val, "%" PRIu32, pos) == -1) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2198 | return LY_EMEM; |
| 2199 | } |
| 2200 | |
| 2201 | len = 1 + strlen(val) + 1; |
| 2202 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 2203 | if (rc != LY_SUCCESS) { |
| 2204 | goto cleanup; |
| 2205 | } |
| 2206 | |
| 2207 | *bufused += sprintf(*buffer + *bufused, "[%s]", val); |
| 2208 | |
| 2209 | cleanup: |
| 2210 | free(val); |
| 2211 | return rc; |
| 2212 | } |
| 2213 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2214 | LIBYANG_API_DEF char * |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2215 | lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 2216 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2217 | ly_bool is_static = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2218 | uint32_t i, depth; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2219 | size_t bufused = 0, len; |
Michal Vasko | 12eb622 | 2022-03-18 13:35:49 +0100 | [diff] [blame] | 2220 | const struct lyd_node *iter, *parent; |
| 2221 | const struct lys_module *mod, *prev_mod; |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 2222 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2223 | |
| 2224 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 2225 | if (buffer) { |
Michal Vasko | 16385f4 | 2021-05-18 16:16:09 +0200 | [diff] [blame] | 2226 | LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2227 | is_static = 1; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2228 | } else { |
| 2229 | buflen = 0; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | switch (pathtype) { |
Radek Krejci | 635d2b8 | 2021-01-04 11:26:51 +0100 | [diff] [blame] | 2233 | case LYD_PATH_STD: |
| 2234 | case LYD_PATH_STD_NO_LAST_PRED: |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2235 | depth = 1; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2236 | for (iter = node; iter->parent; iter = lyd_parent(iter)) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2237 | ++depth; |
| 2238 | } |
| 2239 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2240 | goto iter_print; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2241 | while (depth) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2242 | /* find the right node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2243 | for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {} |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2244 | iter_print: |
Michal Vasko | 12eb622 | 2022-03-18 13:35:49 +0100 | [diff] [blame] | 2245 | /* get the module */ |
| 2246 | mod = iter->schema ? iter->schema->module : lyd_owner_module(iter); |
| 2247 | parent = lyd_parent(iter); |
| 2248 | prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent); |
| 2249 | if (prev_mod == mod) { |
| 2250 | mod = NULL; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | /* realloc string */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 2254 | len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) : |
| 2255 | strlen(((struct lyd_node_opaq *)iter)->name.name)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2256 | rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static); |
| 2257 | if (rc != LY_SUCCESS) { |
| 2258 | break; |
| 2259 | } |
| 2260 | |
| 2261 | /* print next node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2262 | bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 2263 | iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2264 | |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 2265 | /* do not always print the last (first) predicate */ |
Radek Krejci | 635d2b8 | 2021-01-04 11:26:51 +0100 | [diff] [blame] | 2266 | if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) { |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 2267 | switch (iter->schema->nodetype) { |
| 2268 | case LYS_LIST: |
| 2269 | if (iter->schema->flags & LYS_KEYLESS) { |
| 2270 | /* print its position */ |
| 2271 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2272 | } else { |
| 2273 | /* print all list keys in predicates */ |
| 2274 | rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2275 | } |
| 2276 | break; |
| 2277 | case LYS_LEAFLIST: |
| 2278 | if (iter->schema->flags & LYS_CONFIG_W) { |
| 2279 | /* print leaf-list value */ |
| 2280 | rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2281 | } else { |
| 2282 | /* print its position */ |
| 2283 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 2284 | } |
| 2285 | break; |
| 2286 | default: |
| 2287 | /* nothing to print more */ |
| 2288 | break; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2289 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2290 | } |
| 2291 | if (rc != LY_SUCCESS) { |
| 2292 | break; |
| 2293 | } |
| 2294 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2295 | --depth; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2296 | } |
| 2297 | break; |
| 2298 | } |
| 2299 | |
| 2300 | return buffer; |
| 2301 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2302 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2303 | LIBYANG_API_DEF struct lyd_meta * |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2304 | lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name) |
| 2305 | { |
| 2306 | struct lyd_meta *ret = NULL; |
| 2307 | const struct ly_ctx *ctx; |
| 2308 | const char *prefix, *tmp; |
| 2309 | char *str; |
| 2310 | size_t pref_len, name_len; |
| 2311 | |
| 2312 | LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2313 | LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2314 | |
| 2315 | if (!first) { |
| 2316 | return NULL; |
| 2317 | } |
| 2318 | |
| 2319 | ctx = first->annotation->module->ctx; |
| 2320 | |
| 2321 | /* parse the name */ |
| 2322 | tmp = name; |
| 2323 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 2324 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
| 2325 | return NULL; |
| 2326 | } |
| 2327 | |
| 2328 | /* find the module */ |
| 2329 | if (prefix) { |
| 2330 | str = strndup(prefix, pref_len); |
| 2331 | module = ly_ctx_get_module_latest(ctx, str); |
| 2332 | free(str); |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 2333 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), NULL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | /* find the metadata */ |
| 2337 | LY_LIST_FOR(first, first) { |
| 2338 | if ((first->annotation->module == module) && !strcmp(first->name, name)) { |
| 2339 | ret = (struct lyd_meta *)first; |
| 2340 | break; |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | return ret; |
| 2345 | } |
| 2346 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2347 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2348 | lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match) |
| 2349 | { |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 2350 | struct lyd_node **match_p, *iter, *dup = NULL; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2351 | struct lyd_node_inner *parent; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2352 | ly_bool found; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2353 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2354 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 2355 | if (!siblings) { |
| 2356 | /* no data */ |
| 2357 | if (match) { |
| 2358 | *match = NULL; |
| 2359 | } |
| 2360 | return LY_ENOTFOUND; |
| 2361 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2362 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 2363 | if (LYD_CTX(siblings) != LYD_CTX(target)) { |
| 2364 | /* create a duplicate in this context */ |
| 2365 | LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup)); |
| 2366 | target = dup; |
| 2367 | } |
| 2368 | |
| 2369 | if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) { |
| 2370 | /* schema mismatch */ |
| 2371 | lyd_free_tree(dup); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2372 | if (match) { |
| 2373 | *match = NULL; |
| 2374 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2375 | return LY_ENOTFOUND; |
| 2376 | } |
| 2377 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2378 | /* get first sibling */ |
| 2379 | siblings = lyd_first_sibling(siblings); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2380 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2381 | parent = siblings->parent; |
Michal Vasko | 6da1e95 | 2020-12-09 18:14:38 +0100 | [diff] [blame] | 2382 | if (parent && parent->schema && parent->children_ht) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2383 | assert(target->hash); |
| 2384 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2385 | if (lysc_is_dup_inst_list(target->schema)) { |
| 2386 | /* we must search the instances from beginning to find the first matching one */ |
| 2387 | found = 0; |
| 2388 | LYD_LIST_FOR_INST(siblings, target->schema, iter) { |
| 2389 | if (!lyd_compare_single(target, iter, 0)) { |
| 2390 | found = 1; |
| 2391 | break; |
| 2392 | } |
| 2393 | } |
| 2394 | if (found) { |
| 2395 | siblings = iter; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 2396 | } else { |
| 2397 | siblings = NULL; |
| 2398 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2399 | } else { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2400 | /* find by hash */ |
| 2401 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 2402 | siblings = *match_p; |
| 2403 | } else { |
| 2404 | /* not found */ |
| 2405 | siblings = NULL; |
| 2406 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2407 | } |
| 2408 | } else { |
| 2409 | /* no children hash table */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2410 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2411 | if (!lyd_compare_single(siblings, target, 0)) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2412 | break; |
| 2413 | } |
| 2414 | } |
| 2415 | } |
| 2416 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 2417 | lyd_free_tree(dup); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2418 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2419 | if (match) { |
| 2420 | *match = NULL; |
| 2421 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2422 | return LY_ENOTFOUND; |
| 2423 | } |
| 2424 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2425 | if (match) { |
| 2426 | *match = (struct lyd_node *)siblings; |
| 2427 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2428 | return LY_SUCCESS; |
| 2429 | } |
| 2430 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2431 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2432 | lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2433 | size_t val_len, struct lyd_node **match) |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2434 | { |
| 2435 | LY_ERR rc; |
| 2436 | struct lyd_node *target = NULL; |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 2437 | const struct lyd_node *parent; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2438 | |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 2439 | LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL); |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 2440 | if (!siblings) { |
| 2441 | /* no data */ |
| 2442 | if (match) { |
| 2443 | *match = NULL; |
| 2444 | } |
| 2445 | return LY_ENOTFOUND; |
| 2446 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2447 | |
Michal Vasko | 9beceb8 | 2022-04-05 12:14:15 +0200 | [diff] [blame] | 2448 | if ((LYD_CTX(siblings) != schema->module->ctx)) { |
| 2449 | /* parent of ext nodes is useless */ |
| 2450 | parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings); |
| 2451 | LY_CHECK_RET(lyd_dup_find_schema(schema, LYD_CTX(siblings), parent, &schema)); |
| 2452 | } |
| 2453 | |
| 2454 | if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) { |
| 2455 | /* schema mismatch */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2456 | if (match) { |
| 2457 | *match = NULL; |
| 2458 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2459 | return LY_ENOTFOUND; |
| 2460 | } |
| 2461 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2462 | if (key_or_value && !val_len) { |
| 2463 | val_len = strlen(key_or_value); |
| 2464 | } |
| 2465 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2466 | if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) { |
| 2467 | /* create a data node and find the instance */ |
| 2468 | if (schema->nodetype == LYS_LEAFLIST) { |
| 2469 | /* target used attributes: schema, hash, value */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 2470 | rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &target); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2471 | LY_CHECK_RET(rc); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2472 | } else { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2473 | /* target used attributes: schema, hash, child (all keys) */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2474 | 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] | 2475 | } |
| 2476 | |
| 2477 | /* find it */ |
| 2478 | rc = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2479 | } else { |
| 2480 | /* find the first schema node instance */ |
| 2481 | rc = lyd_find_sibling_schema(siblings, schema, match); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2482 | } |
| 2483 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 2484 | lyd_free_tree(target); |
| 2485 | return rc; |
| 2486 | } |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2487 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2488 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2489 | lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set) |
| 2490 | { |
| 2491 | struct lyd_node **match_p, *first, *iter; |
| 2492 | struct lyd_node_inner *parent; |
| 2493 | |
| 2494 | LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2495 | LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2496 | |
| 2497 | LY_CHECK_RET(ly_set_new(set)); |
| 2498 | |
| 2499 | if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) { |
| 2500 | /* no data or schema mismatch */ |
| 2501 | return LY_ENOTFOUND; |
| 2502 | } |
| 2503 | |
| 2504 | /* get first sibling */ |
| 2505 | siblings = lyd_first_sibling(siblings); |
| 2506 | |
| 2507 | parent = siblings->parent; |
| 2508 | if (parent && parent->schema && parent->children_ht) { |
| 2509 | assert(target->hash); |
| 2510 | |
| 2511 | /* find the first instance */ |
| 2512 | lyd_find_sibling_first(siblings, target, &first); |
| 2513 | if (first) { |
| 2514 | /* add it so that it is the first in the set */ |
| 2515 | if (ly_set_add(*set, first, 1, NULL)) { |
| 2516 | goto error; |
| 2517 | } |
| 2518 | |
| 2519 | /* find by hash */ |
| 2520 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 2521 | iter = *match_p; |
| 2522 | } else { |
| 2523 | /* not found */ |
| 2524 | iter = NULL; |
| 2525 | } |
| 2526 | while (iter) { |
| 2527 | /* add all found nodes into the set */ |
| 2528 | if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) { |
| 2529 | goto error; |
| 2530 | } |
| 2531 | |
| 2532 | /* find next instance */ |
| 2533 | if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) { |
| 2534 | iter = NULL; |
| 2535 | } else { |
| 2536 | iter = *match_p; |
| 2537 | } |
| 2538 | } |
| 2539 | } |
| 2540 | } else { |
| 2541 | /* no children hash table */ |
| 2542 | LY_LIST_FOR(siblings, siblings) { |
| 2543 | if (!lyd_compare_single(target, siblings, 0)) { |
| 2544 | ly_set_add(*set, (void *)siblings, 1, NULL); |
| 2545 | } |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | if (!(*set)->count) { |
| 2550 | return LY_ENOTFOUND; |
| 2551 | } |
| 2552 | return LY_SUCCESS; |
| 2553 | |
| 2554 | error: |
| 2555 | ly_set_free(*set, NULL); |
| 2556 | *set = NULL; |
| 2557 | return LY_EMEM; |
| 2558 | } |
| 2559 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2560 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 1d4af6c | 2021-02-22 13:31:26 +0100 | [diff] [blame] | 2561 | lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match) |
| 2562 | { |
| 2563 | LY_CHECK_ARG_RET(NULL, name, LY_EINVAL); |
| 2564 | |
| 2565 | for ( ; first; first = first->next) { |
| 2566 | if (!first->schema && !strcmp(LYD_NAME(first), name)) { |
| 2567 | break; |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | if (match) { |
| 2572 | *match = (struct lyd_node *)first; |
| 2573 | } |
| 2574 | return first ? LY_SUCCESS : LY_ENOTFOUND; |
| 2575 | } |
| 2576 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2577 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2578 | lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format, |
| 2579 | void *prefix_data, const struct lyxp_var *vars, struct ly_set **set) |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2580 | { |
| 2581 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 8bd9cc7 | 2021-07-16 14:06:10 +0200 | [diff] [blame] | 2582 | struct lyxp_set xp_set = {0}; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2583 | struct lyxp_expr *exp = NULL; |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2584 | uint32_t i; |
| 2585 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2586 | LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2587 | |
Michal Vasko | 37c6943 | 2021-04-12 14:48:24 +0200 | [diff] [blame] | 2588 | *set = NULL; |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2589 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2590 | /* parse expression */ |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 2591 | ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2592 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2593 | |
| 2594 | /* evaluate expression */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2595 | ret = lyxp_eval(LYD_CTX(tree), exp, NULL, format, prefix_data, ctx_node, tree, vars, &xp_set, LYXP_IGNORE_WHEN); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2596 | LY_CHECK_GOTO(ret, cleanup); |
| 2597 | |
| 2598 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2599 | ret = ly_set_new(set); |
| 2600 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2601 | |
| 2602 | /* transform into ly_set */ |
| 2603 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 2604 | /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */ |
| 2605 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 2606 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2607 | (*set)->size = xp_set.used; |
| 2608 | |
| 2609 | for (i = 0; i < xp_set.used; ++i) { |
| 2610 | if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 2611 | ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2612 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | cleanup: |
Michal Vasko | 0691d52 | 2020-05-21 13:21:47 +0200 | [diff] [blame] | 2618 | lyxp_set_free_content(&xp_set); |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 2619 | lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp); |
Radek Krejci | 8f45abc | 2020-11-26 12:15:13 +0100 | [diff] [blame] | 2620 | if (ret) { |
| 2621 | ly_set_free(*set, NULL); |
| 2622 | *set = NULL; |
| 2623 | } |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 2624 | return ret; |
| 2625 | } |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 2626 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2627 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2628 | lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, |
| 2629 | const struct lyxp_var *vars, struct ly_set **set) |
| 2630 | { |
| 2631 | LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL); |
| 2632 | |
| 2633 | return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set); |
| 2634 | } |
| 2635 | |
| 2636 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 2637 | lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set) |
| 2638 | { |
| 2639 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 2640 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2641 | return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set); |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 2642 | } |
| 2643 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2644 | LIBYANG_API_DEF LY_ERR |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2645 | lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set) |
| 2646 | { |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 2647 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 2648 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2649 | return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2652 | LIBYANG_API_DEF LY_ERR |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2653 | lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result) |
Michal Vasko | c9eb3ca | 2021-07-16 10:20:37 +0200 | [diff] [blame] | 2654 | { |
| 2655 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 8bd9cc7 | 2021-07-16 14:06:10 +0200 | [diff] [blame] | 2656 | struct lyxp_set xp_set = {0}; |
Michal Vasko | c9eb3ca | 2021-07-16 10:20:37 +0200 | [diff] [blame] | 2657 | struct lyxp_expr *exp = NULL; |
| 2658 | |
| 2659 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL); |
| 2660 | |
| 2661 | /* compile expression */ |
| 2662 | ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp); |
| 2663 | LY_CHECK_GOTO(ret, cleanup); |
| 2664 | |
| 2665 | /* evaluate expression */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2666 | ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, vars, &xp_set, LYXP_IGNORE_WHEN); |
Michal Vasko | c9eb3ca | 2021-07-16 10:20:37 +0200 | [diff] [blame] | 2667 | LY_CHECK_GOTO(ret, cleanup); |
| 2668 | |
| 2669 | /* transform into boolean */ |
| 2670 | ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN); |
| 2671 | LY_CHECK_GOTO(ret, cleanup); |
| 2672 | |
| 2673 | /* set result */ |
| 2674 | *result = xp_set.val.bln; |
| 2675 | |
| 2676 | cleanup: |
| 2677 | lyxp_set_free_content(&xp_set); |
| 2678 | lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp); |
| 2679 | return ret; |
| 2680 | } |
| 2681 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2682 | LIBYANG_API_DEF LY_ERR |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2683 | lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result) |
| 2684 | { |
| 2685 | return lyd_eval_xpath2(ctx_node, xpath, NULL, result); |
| 2686 | } |
| 2687 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2688 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 2689 | lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match) |
| 2690 | { |
| 2691 | LY_ERR ret = LY_SUCCESS; |
| 2692 | struct lyxp_expr *expr = NULL; |
| 2693 | struct ly_path *lypath = NULL; |
| 2694 | |
| 2695 | LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL); |
| 2696 | |
| 2697 | /* parse the path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 2698 | ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 2699 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr); |
| 2700 | LY_CHECK_GOTO(ret, cleanup); |
| 2701 | |
| 2702 | /* compile the path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 2703 | ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr, |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 2704 | output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, 0, LY_VALUE_JSON, NULL, &lypath); |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 2705 | LY_CHECK_GOTO(ret, cleanup); |
| 2706 | |
| 2707 | /* evaluate the path */ |
| 2708 | ret = ly_path_eval_partial(lypath, ctx_node, NULL, match); |
| 2709 | |
| 2710 | cleanup: |
| 2711 | lyxp_expr_free(LYD_CTX(ctx_node), expr); |
| 2712 | ly_path_free(LYD_CTX(ctx_node), lypath); |
| 2713 | return ret; |
| 2714 | } |
| 2715 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2716 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | bb22b18 | 2021-06-14 08:14:21 +0200 | [diff] [blame] | 2717 | lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match) |
| 2718 | { |
| 2719 | LY_ERR ret; |
| 2720 | struct lyd_node *m; |
| 2721 | |
| 2722 | LY_CHECK_ARG_RET(NULL, path, LY_EINVAL); |
| 2723 | |
| 2724 | ret = ly_path_eval(path, tree, &m); |
| 2725 | if (ret) { |
| 2726 | if (match) { |
| 2727 | *match = NULL; |
| 2728 | } |
| 2729 | return LY_ENOTFOUND; |
| 2730 | } |
| 2731 | |
| 2732 | if (match) { |
| 2733 | *match = m; |
| 2734 | } |
| 2735 | return LY_SUCCESS; |
| 2736 | } |