Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file diff.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief diff functions |
| 5 | * |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 6 | * Copyright (c) 2020 - 2021 CESNET, z.s.p.o. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +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 | */ |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 14 | #define _GNU_SOURCE /* asprintf, strdup */ |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 15 | |
| 16 | #include "diff.h" |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <stddef.h> |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 22 | #include <stdlib.h> |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 25 | #include "compat.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 26 | #include "context.h" |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 27 | #include "log.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 28 | #include "ly_common.h" |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 29 | #include "plugins_exts.h" |
Michal Vasko | b475096 | 2022-10-06 15:33:35 +0200 | [diff] [blame] | 30 | #include "plugins_exts/metadata.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 31 | #include "plugins_types.h" |
| 32 | #include "set.h" |
| 33 | #include "tree.h" |
| 34 | #include "tree_data.h" |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 35 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 36 | #include "tree_edit.h" |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 37 | #include "tree_schema.h" |
| 38 | #include "tree_schema_internal.h" |
| 39 | |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 40 | #define LOGERR_META(ctx, meta_name, node) \ |
| 41 | { \ |
| 42 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 43 | LOGERR(ctx, LY_EINVAL, "Failed to find metadata \"%s\" for node \"%s\".", meta_name, __path); \ |
| 44 | free(__path); \ |
| 45 | } |
| 46 | |
| 47 | #define LOGERR_NOINST(ctx, node) \ |
| 48 | { \ |
| 49 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 50 | LOGERR(ctx, LY_EINVAL, "Failed to find node \"%s\" instance in data.", __path); \ |
| 51 | free(__path); \ |
| 52 | } |
| 53 | |
| 54 | #define LOGERR_UNEXPVAL(ctx, node, data_source) \ |
| 55 | { \ |
| 56 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 57 | LOGERR(ctx, LY_EINVAL, "Unexpected value of node \"%s\" in %s.", __path, data_source); \ |
| 58 | free(__path); \ |
| 59 | } |
| 60 | |
| 61 | #define LOGERR_MERGEOP(ctx, node, src_op, trg_op) \ |
| 62 | { \ |
| 63 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 64 | LOGERR(ctx, LY_EINVAL, "Unable to merge operation \"%s\" with \"%s\" for node \"%s\".", \ |
| 65 | lyd_diff_op2str(trg_op), lyd_diff_op2str(src_op), __path); \ |
| 66 | free(__path); \ |
| 67 | } |
| 68 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 69 | static const char * |
| 70 | lyd_diff_op2str(enum lyd_diff_op op) |
| 71 | { |
| 72 | switch (op) { |
| 73 | case LYD_DIFF_OP_CREATE: |
| 74 | return "create"; |
| 75 | case LYD_DIFF_OP_DELETE: |
| 76 | return "delete"; |
| 77 | case LYD_DIFF_OP_REPLACE: |
| 78 | return "replace"; |
| 79 | case LYD_DIFF_OP_NONE: |
| 80 | return "none"; |
| 81 | } |
| 82 | |
| 83 | LOGINT(NULL); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 87 | static enum lyd_diff_op |
| 88 | lyd_diff_str2op(const char *str) |
| 89 | { |
| 90 | switch (str[0]) { |
| 91 | case 'c': |
| 92 | assert(!strcmp(str, "create")); |
| 93 | return LYD_DIFF_OP_CREATE; |
| 94 | case 'd': |
| 95 | assert(!strcmp(str, "delete")); |
| 96 | return LYD_DIFF_OP_DELETE; |
| 97 | case 'r': |
| 98 | assert(!strcmp(str, "replace")); |
| 99 | return LYD_DIFF_OP_REPLACE; |
| 100 | case 'n': |
| 101 | assert(!strcmp(str, "none")); |
| 102 | return LYD_DIFF_OP_NONE; |
| 103 | } |
| 104 | |
| 105 | LOGINT(NULL); |
| 106 | return 0; |
| 107 | } |
| 108 | |
Michal Vasko | cffc3f9 | 2022-06-15 07:57:24 +0200 | [diff] [blame] | 109 | /** |
| 110 | * @brief Create diff metadata for a nested user-ordered node with the effective operation "create". |
| 111 | * |
| 112 | * @param[in] node User-rodered node to update. |
| 113 | * @return LY_ERR value. |
| 114 | */ |
| 115 | static LY_ERR |
| 116 | lyd_diff_add_create_nested_userord(struct lyd_node *node) |
| 117 | { |
| 118 | LY_ERR rc = LY_SUCCESS; |
| 119 | const char *meta_name, *meta_val; |
| 120 | size_t buflen = 0, bufused = 0; |
| 121 | uint32_t pos; |
| 122 | char *dyn = NULL; |
| 123 | |
| 124 | assert(lysc_is_userordered(node->schema)); |
| 125 | |
| 126 | /* get correct metadata name and value */ |
| 127 | if (lysc_is_dup_inst_list(node->schema)) { |
| 128 | meta_name = "yang:position"; |
| 129 | |
| 130 | pos = lyd_list_pos(node); |
| 131 | if (asprintf(&dyn, "%" PRIu32, pos) == -1) { |
| 132 | LOGMEM(LYD_CTX(node)); |
| 133 | rc = LY_EMEM; |
| 134 | goto cleanup; |
| 135 | } |
| 136 | meta_val = dyn; |
| 137 | } else if (node->schema->nodetype == LYS_LIST) { |
| 138 | meta_name = "yang:key"; |
| 139 | |
| 140 | if (node->prev->next && (node->prev->schema == node->schema)) { |
| 141 | LY_CHECK_GOTO(rc = lyd_path_list_predicate(node->prev, &dyn, &buflen, &bufused, 0), cleanup); |
| 142 | meta_val = dyn; |
| 143 | } else { |
| 144 | meta_val = ""; |
| 145 | } |
| 146 | } else { |
| 147 | meta_name = "yang:value"; |
| 148 | |
| 149 | if (node->prev->next && (node->prev->schema == node->schema)) { |
| 150 | meta_val = lyd_get_value(node->prev); |
| 151 | } else { |
| 152 | meta_val = ""; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* create the metadata */ |
| 157 | LY_CHECK_GOTO(rc = lyd_new_meta(NULL, node, NULL, meta_name, meta_val, 0, NULL), cleanup); |
| 158 | |
| 159 | cleanup: |
| 160 | free(dyn); |
| 161 | return rc; |
| 162 | } |
| 163 | |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 164 | /** |
| 165 | * @brief Find metadata/an attribute of a node. |
| 166 | * |
| 167 | * @param[in] node Node to search. |
| 168 | * @param[in] name Metadata/attribute name. |
| 169 | * @param[out] meta Metadata found, NULL if not found. |
| 170 | * @param[out] attr Attribute found, NULL if not found. |
| 171 | */ |
| 172 | static void |
| 173 | lyd_diff_find_meta(const struct lyd_node *node, const char *name, struct lyd_meta **meta, struct lyd_attr **attr) |
| 174 | { |
| 175 | struct lyd_meta *m; |
| 176 | struct lyd_attr *a; |
| 177 | |
| 178 | if (meta) { |
| 179 | *meta = NULL; |
| 180 | } |
| 181 | if (attr) { |
| 182 | *attr = NULL; |
| 183 | } |
| 184 | |
| 185 | if (node->schema) { |
| 186 | assert(meta); |
| 187 | |
| 188 | LY_LIST_FOR(node->meta, m) { |
| 189 | if (!strcmp(m->name, name) && !strcmp(m->annotation->module->name, "yang")) { |
| 190 | *meta = m; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } else { |
| 195 | assert(attr); |
| 196 | |
| 197 | LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, a) { |
| 198 | /* name */ |
| 199 | if (strcmp(a->name.name, name)) { |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | /* module */ |
| 204 | switch (a->format) { |
| 205 | case LY_VALUE_JSON: |
| 206 | if (strcmp(a->name.module_name, "yang")) { |
| 207 | continue; |
| 208 | } |
| 209 | break; |
| 210 | case LY_VALUE_XML: |
| 211 | if (strcmp(a->name.module_ns, "urn:ietf:params:xml:ns:yang:1")) { |
| 212 | continue; |
| 213 | } |
| 214 | break; |
| 215 | default: |
| 216 | LOGINT(LYD_CTX(node)); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | *attr = a; |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @brief Learn operation of a diff node. |
| 228 | * |
| 229 | * @param[in] diff_node Diff node. |
| 230 | * @param[out] op Operation. |
| 231 | * @return LY_ERR value. |
| 232 | */ |
| 233 | static LY_ERR |
| 234 | lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op) |
| 235 | { |
| 236 | struct lyd_meta *meta = NULL; |
| 237 | struct lyd_attr *attr = NULL; |
| 238 | const struct lyd_node *diff_parent; |
| 239 | const char *str; |
| 240 | char *path; |
| 241 | |
| 242 | for (diff_parent = diff_node; diff_parent; diff_parent = lyd_parent(diff_parent)) { |
| 243 | lyd_diff_find_meta(diff_parent, "operation", &meta, &attr); |
| 244 | if (!meta && !attr) { |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | str = meta ? lyd_get_meta_value(meta) : attr->value; |
| 249 | if ((str[0] == 'r') && (diff_parent != diff_node)) { |
| 250 | /* we do not care about this operation if it's in our parent */ |
| 251 | continue; |
| 252 | } |
| 253 | *op = lyd_diff_str2op(str); |
| 254 | return LY_SUCCESS; |
| 255 | } |
| 256 | |
| 257 | /* operation not found */ |
| 258 | path = lyd_path(diff_node, LYD_PATH_STD, NULL, 0); |
| 259 | LOGERR(LYD_CTX(diff_node), LY_EINVAL, "Node \"%s\" without an operation.", path); |
| 260 | free(path); |
| 261 | return LY_EINT; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @brief Remove metadata/an attribute from a node. |
| 266 | * |
| 267 | * @param[in] node Node to update. |
| 268 | * @param[in] name Metadata/attribute name. |
| 269 | */ |
| 270 | static void |
| 271 | lyd_diff_del_meta(struct lyd_node *node, const char *name) |
| 272 | { |
| 273 | struct lyd_meta *meta; |
| 274 | struct lyd_attr *attr; |
| 275 | |
| 276 | lyd_diff_find_meta(node, name, &meta, &attr); |
| 277 | |
| 278 | if (meta) { |
| 279 | lyd_free_meta_single(meta); |
| 280 | } else if (attr) { |
| 281 | lyd_free_attr_single(LYD_CTX(node), attr); |
| 282 | } |
| 283 | } |
| 284 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 285 | LY_ERR |
| 286 | lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 287 | const char *key, const char *value, const char *position, const char *orig_key, const char *orig_position, |
| 288 | struct lyd_node **diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 289 | { |
Michal Vasko | cffc3f9 | 2022-06-15 07:57:24 +0200 | [diff] [blame] | 290 | struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL, *elem; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 291 | const struct lyd_node *parent = NULL; |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 292 | enum lyd_diff_op cur_op; |
Michal Vasko | d452567 | 2023-06-20 09:15:42 +0200 | [diff] [blame] | 293 | struct lyd_meta *meta; |
Michal Vasko | 9af4091 | 2023-06-21 08:03:43 +0200 | [diff] [blame] | 294 | uint32_t diff_opts; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 295 | |
| 296 | assert(diff); |
| 297 | |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 298 | /* replace leaf always needs orig-default and orig-value */ |
| 299 | assert((node->schema->nodetype != LYS_LEAF) || (op != LYD_DIFF_OP_REPLACE) || (orig_default && orig_value)); |
| 300 | |
| 301 | /* create on userord needs key/value */ |
| 302 | assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_CREATE) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 303 | (lysc_is_dup_inst_list(node->schema) && position) || key); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 304 | assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 305 | (op != LYD_DIFF_OP_CREATE) || (lysc_is_dup_inst_list(node->schema) && position) || value); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 306 | |
| 307 | /* move on userord needs both key and orig-key/value and orig-value */ |
| 308 | assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_REPLACE) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 309 | (lysc_is_dup_inst_list(node->schema) && position && orig_position) || (key && orig_key)); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 310 | assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 311 | (op != LYD_DIFF_OP_REPLACE) || (lysc_is_dup_inst_list(node->schema) && position && orig_position) || |
| 312 | (value && orig_value)); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 313 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 314 | /* find the first existing parent */ |
| 315 | siblings = *diff; |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 316 | do { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 317 | /* find next node parent */ |
| 318 | parent = node; |
| 319 | while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 320 | parent = lyd_parent(parent); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 321 | } |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 322 | |
| 323 | if (lysc_is_dup_inst_list(parent->schema)) { |
| 324 | /* assume it never exists, we are not able to distinguish whether it does or not */ |
| 325 | match = NULL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 326 | break; |
| 327 | } |
| 328 | |
| 329 | /* check whether it exists in the diff */ |
| 330 | if (lyd_find_sibling_first(siblings, parent, &match)) { |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | /* another parent found */ |
| 335 | diff_parent = match; |
| 336 | |
| 337 | /* move down in the diff */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 338 | siblings = lyd_child_no_keys(match); |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 339 | } while (parent != node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 340 | |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 341 | if (match && (parent == node)) { |
| 342 | /* special case when there is already an operation on our descendant */ |
| 343 | assert(!lyd_diff_get_op(diff_parent, &cur_op) && (cur_op == LYD_DIFF_OP_NONE)); |
| 344 | (void)cur_op; |
| 345 | |
Michal Vasko | 9af4091 | 2023-06-21 08:03:43 +0200 | [diff] [blame] | 346 | /* move it to the end where it is expected (matters for user-ordered lists) */ |
| 347 | if (lysc_is_userordered(diff_parent->schema)) { |
| 348 | for (elem = diff_parent; elem->next && (elem->next->schema == elem->schema); elem = elem->next) {} |
| 349 | if (elem != diff_parent) { |
| 350 | LY_CHECK_RET(lyd_insert_after(elem, diff_parent)); |
| 351 | } |
| 352 | } |
| 353 | |
Michal Vasko | d452567 | 2023-06-20 09:15:42 +0200 | [diff] [blame] | 354 | /* will be replaced by the new operation but keep the current op for descendants */ |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 355 | lyd_diff_del_meta(diff_parent, "operation"); |
Michal Vasko | d452567 | 2023-06-20 09:15:42 +0200 | [diff] [blame] | 356 | LY_LIST_FOR(lyd_child_no_keys(diff_parent), elem) { |
| 357 | lyd_diff_find_meta(elem, "operation", &meta, NULL); |
| 358 | if (meta) { |
| 359 | /* explicit operation, fine */ |
| 360 | continue; |
| 361 | } |
| 362 | |
| 363 | /* set the none operation */ |
| 364 | LY_CHECK_RET(lyd_new_meta(NULL, elem, NULL, "yang:operation", "none", 0, NULL)); |
| 365 | } |
| 366 | |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 367 | dup = diff_parent; |
Michal Vasko | 695a7f2 | 2023-02-14 10:02:10 +0100 | [diff] [blame] | 368 | } else { |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame^] | 369 | diff_opts = LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS | LYD_DUP_NO_LYDS; |
Michal Vasko | 2be7c8a | 2023-06-21 08:04:21 +0200 | [diff] [blame] | 370 | if ((op != LYD_DIFF_OP_REPLACE) || !lysc_is_userordered(node->schema) || (node->schema->flags & LYS_CONFIG_R)) { |
| 371 | /* move applies only to the user-ordered list, no descendants */ |
| 372 | diff_opts |= LYD_DUP_RECURSIVE; |
| 373 | } |
| 374 | |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 375 | /* duplicate the subtree (and connect to the diff if possible) */ |
| 376 | if (diff_parent) { |
| 377 | LY_CHECK_RET(lyd_dup_single_to_ctx(node, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent, |
Michal Vasko | 2be7c8a | 2023-06-21 08:04:21 +0200 | [diff] [blame] | 378 | diff_opts, &dup)); |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 379 | } else { |
Michal Vasko | 2be7c8a | 2023-06-21 08:04:21 +0200 | [diff] [blame] | 380 | LY_CHECK_RET(lyd_dup_single(node, NULL, diff_opts, &dup)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 381 | } |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 382 | |
| 383 | /* find the first duplicated parent */ |
| 384 | if (!diff_parent) { |
| 385 | diff_parent = lyd_parent(dup); |
| 386 | while (diff_parent && diff_parent->parent) { |
| 387 | diff_parent = lyd_parent(diff_parent); |
| 388 | } |
| 389 | } else { |
| 390 | diff_parent = dup; |
| 391 | while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) { |
| 392 | diff_parent = lyd_parent(diff_parent); |
| 393 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 394 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 395 | |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 396 | /* no parent existed, must be manually connected */ |
| 397 | if (!diff_parent) { |
| 398 | /* there actually was no parent to duplicate */ |
| 399 | lyd_insert_sibling(*diff, dup, diff); |
| 400 | } else if (!diff_parent->parent) { |
| 401 | lyd_insert_sibling(*diff, diff_parent, diff); |
| 402 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 403 | |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame] | 404 | /* add parent operation, if any */ |
| 405 | if (diff_parent && (diff_parent != dup)) { |
| 406 | LY_CHECK_RET(lyd_new_meta(NULL, diff_parent, NULL, "yang:operation", "none", 0, NULL)); |
| 407 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /* add subtree operation */ |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 411 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 412 | |
Michal Vasko | cffc3f9 | 2022-06-15 07:57:24 +0200 | [diff] [blame] | 413 | if (op == LYD_DIFF_OP_CREATE) { |
| 414 | /* all nested user-ordered (leaf-)lists need special metadata for create op */ |
| 415 | LYD_TREE_DFS_BEGIN(dup, elem) { |
| 416 | if ((elem != dup) && lysc_is_userordered(elem->schema)) { |
| 417 | LY_CHECK_RET(lyd_diff_add_create_nested_userord(elem)); |
| 418 | } |
| 419 | LYD_TREE_DFS_END(dup, elem); |
| 420 | } |
| 421 | } |
| 422 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 423 | /* orig-default */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 424 | if (orig_default) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 425 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-default", orig_default, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* orig-value */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 429 | if (orig_value) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 430 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-value", orig_value, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* key */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 434 | if (key) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 435 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:key", key, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* value */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 439 | if (value) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 440 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:value", value, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 443 | /* position */ |
| 444 | if (position) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 445 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:position", position, 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 448 | /* orig-key */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 449 | if (orig_key) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 450 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-key", orig_key, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 453 | /* orig-position */ |
| 454 | if (orig_position) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 455 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-position", orig_position, 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 458 | return LY_SUCCESS; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet. |
| 463 | * |
Michal Vasko | 1dcd73b | 2020-12-08 10:04:33 +0100 | [diff] [blame] | 464 | * @param[in] first Node from the first tree, can be NULL (on create). |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 465 | * @param[in] schema Schema node of the list/leaf-list. |
| 466 | * @param[in,out] userord Sized array of userord items. |
| 467 | * @return Userord item for all the user-ordered list/leaf-list instances. |
| 468 | */ |
| 469 | static struct lyd_diff_userord * |
| 470 | lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord) |
| 471 | { |
| 472 | struct lyd_diff_userord *item; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 473 | struct lyd_node *iter; |
| 474 | const struct lyd_node **node; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 475 | LY_ARRAY_COUNT_TYPE u; |
| 476 | |
| 477 | LY_ARRAY_FOR(*userord, u) { |
| 478 | if ((*userord)[u].schema == schema) { |
| 479 | return &(*userord)[u]; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* it was not added yet, add it now */ |
| 484 | LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL); |
| 485 | |
| 486 | item->schema = schema; |
| 487 | item->pos = 0; |
| 488 | item->inst = NULL; |
| 489 | |
| 490 | /* store all the instance pointers in the current order */ |
| 491 | if (first) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 492 | LYD_LIST_FOR_INST(lyd_first_sibling(first), first->schema, iter) { |
| 493 | LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL); |
| 494 | *node = iter; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | return item; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered |
| 503 | * lists/leaf-lists. |
| 504 | * |
| 505 | * @param[in] first Node from the first tree, can be NULL (on create). |
| 506 | * @param[in] second Node from the second tree, can be NULL (on delete). |
| 507 | * @param[in] options Diff options. |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 508 | * @param[in] userord_item Userord item of @p first and/or @p second node. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 509 | * @param[out] op Operation. |
| 510 | * @param[out] orig_default Original default metadata. |
| 511 | * @param[out] value Value metadata. |
| 512 | * @param[out] orig_value Original value metadata |
| 513 | * @param[out] key Key metadata. |
| 514 | * @param[out] orig_key Original key metadata. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 515 | * @param[out] position Position metadata. |
| 516 | * @param[out] orig_position Original position metadata. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 517 | * @return LY_SUCCESS on success, |
| 518 | * @return LY_ENOT if there is no change to be added into diff, |
| 519 | * @return LY_ERR value on other errors. |
| 520 | */ |
| 521 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 522 | lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 523 | struct lyd_diff_userord *userord_item, enum lyd_diff_op *op, const char **orig_default, char **value, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 524 | char **orig_value, char **key, char **orig_key, char **position, char **orig_position) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 525 | { |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 526 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 527 | const struct lysc_node *schema; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 528 | size_t buflen, bufused; |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 529 | uint32_t first_pos, second_pos, comp_opts; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 530 | |
| 531 | assert(first || second); |
| 532 | |
| 533 | *orig_default = NULL; |
| 534 | *value = NULL; |
| 535 | *orig_value = NULL; |
| 536 | *key = NULL; |
| 537 | *orig_key = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 538 | *position = NULL; |
| 539 | *orig_position = NULL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 540 | |
| 541 | schema = first ? first->schema : second->schema; |
| 542 | assert(lysc_is_userordered(schema)); |
| 543 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 544 | /* find user-ordered first position */ |
| 545 | if (first) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 546 | for (first_pos = 0; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 547 | if (userord_item->inst[first_pos] == first) { |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | assert(first_pos < LY_ARRAY_COUNT(userord_item->inst)); |
| 552 | } else { |
| 553 | first_pos = 0; |
| 554 | } |
| 555 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 556 | /* prepare position of the next instance */ |
| 557 | second_pos = userord_item->pos++; |
| 558 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 559 | /* learn operation first */ |
| 560 | if (!second) { |
| 561 | *op = LYD_DIFF_OP_DELETE; |
| 562 | } else if (!first) { |
| 563 | *op = LYD_DIFF_OP_CREATE; |
| 564 | } else { |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 565 | comp_opts = lysc_is_dup_inst_list(second->schema) ? LYD_COMPARE_FULL_RECURSION : 0; |
| 566 | if (lyd_compare_single(second, userord_item->inst[second_pos], comp_opts)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 567 | /* in first, there is a different instance on the second position, we are going to move 'first' node */ |
| 568 | *op = LYD_DIFF_OP_REPLACE; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 569 | } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 570 | /* default flag change */ |
| 571 | *op = LYD_DIFF_OP_NONE; |
| 572 | } else { |
| 573 | /* no changes */ |
| 574 | return LY_ENOT; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * set each attribute correctly based on the operation and node type |
| 580 | */ |
| 581 | |
| 582 | /* orig-default */ |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 583 | if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 584 | if (first->flags & LYD_DEFAULT) { |
| 585 | *orig_default = "true"; |
| 586 | } else { |
| 587 | *orig_default = "false"; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /* value */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 592 | if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) && |
| 593 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 594 | if (second_pos) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 595 | *value = strdup(lyd_get_value(userord_item->inst[second_pos - 1])); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 596 | LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 597 | } else { |
| 598 | *value = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 599 | LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | /* orig-value */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 604 | if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) && |
| 605 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 606 | if (first_pos) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 607 | *orig_value = strdup(lyd_get_value(userord_item->inst[first_pos - 1])); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 608 | LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 609 | } else { |
| 610 | *orig_value = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 611 | LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
| 615 | /* key */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 616 | if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) && |
| 617 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 618 | if (second_pos) { |
| 619 | buflen = bufused = 0; |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 620 | LY_CHECK_GOTO(rc = lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 621 | } else { |
| 622 | *key = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 623 | LY_CHECK_ERR_GOTO(!*key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | /* orig-key */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 628 | if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) && |
| 629 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 630 | if (first_pos) { |
| 631 | buflen = bufused = 0; |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 632 | LY_CHECK_GOTO(rc = lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 633 | } else { |
| 634 | *orig_key = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 635 | LY_CHECK_ERR_GOTO(!*orig_key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 639 | /* position */ |
| 640 | if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) { |
| 641 | if (second_pos) { |
| 642 | if (asprintf(position, "%" PRIu32, second_pos) == -1) { |
| 643 | LOGMEM(schema->module->ctx); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 644 | rc = LY_EMEM; |
| 645 | goto cleanup; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 646 | } |
| 647 | } else { |
| 648 | *position = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 649 | LY_CHECK_ERR_GOTO(!*position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
| 653 | /* orig-position */ |
| 654 | if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) { |
| 655 | if (first_pos) { |
| 656 | if (asprintf(orig_position, "%" PRIu32, first_pos) == -1) { |
| 657 | LOGMEM(schema->module->ctx); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 658 | rc = LY_EMEM; |
| 659 | goto cleanup; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 660 | } |
| 661 | } else { |
| 662 | *orig_position = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 663 | LY_CHECK_ERR_GOTO(!*orig_position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 667 | /* |
| 668 | * update our instances - apply the change |
| 669 | */ |
| 670 | if (*op == LYD_DIFF_OP_CREATE) { |
| 671 | /* insert the instance */ |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 672 | LY_ARRAY_CREATE_GOTO(schema->module->ctx, userord_item->inst, 1, rc, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 673 | if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) { |
| 674 | memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos, |
| 675 | (LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst); |
| 676 | } |
| 677 | LY_ARRAY_INCREMENT(userord_item->inst); |
| 678 | userord_item->inst[second_pos] = second; |
| 679 | |
| 680 | } else if (*op == LYD_DIFF_OP_DELETE) { |
| 681 | /* remove the instance */ |
| 682 | if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) { |
| 683 | memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1, |
| 684 | (LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst); |
| 685 | } |
| 686 | LY_ARRAY_DECREMENT(userord_item->inst); |
| 687 | |
| 688 | } else if (*op == LYD_DIFF_OP_REPLACE) { |
| 689 | /* move the instances */ |
| 690 | memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos, |
| 691 | (first_pos - second_pos) * sizeof *userord_item->inst); |
| 692 | userord_item->inst[second_pos] = first; |
| 693 | } |
| 694 | |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 695 | cleanup: |
| 696 | if (rc) { |
| 697 | free(*value); |
| 698 | *value = NULL; |
| 699 | free(*orig_value); |
| 700 | *orig_value = NULL; |
| 701 | free(*key); |
| 702 | *key = NULL; |
| 703 | free(*orig_key); |
| 704 | *orig_key = NULL; |
| 705 | free(*position); |
| 706 | *position = NULL; |
| 707 | free(*orig_position); |
| 708 | *orig_position = NULL; |
| 709 | } |
| 710 | return rc; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /** |
| 714 | * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered |
| 715 | * lists/leaf-lists. |
| 716 | * |
| 717 | * @param[in] first Node from the first tree, can be NULL (on create). |
| 718 | * @param[in] second Node from the second tree, can be NULL (on delete). |
| 719 | * @param[in] options Diff options. |
| 720 | * @param[out] op Operation. |
| 721 | * @param[out] orig_default Original default metadata. |
| 722 | * @param[out] orig_value Original value metadata. |
| 723 | * @return LY_SUCCESS on success, |
| 724 | * @return LY_ENOT if there is no change to be added into diff, |
| 725 | * @return LY_ERR value on other errors. |
| 726 | */ |
| 727 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 728 | lyd_diff_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, enum lyd_diff_op *op, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 729 | const char **orig_default, char **orig_value) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 730 | { |
| 731 | const struct lysc_node *schema; |
Michal Vasko | 6ea6fe2 | 2021-10-08 09:57:01 +0200 | [diff] [blame] | 732 | const char *str_val; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 733 | |
| 734 | assert(first || second); |
| 735 | |
| 736 | *orig_default = NULL; |
| 737 | *orig_value = NULL; |
| 738 | |
| 739 | schema = first ? first->schema : second->schema; |
| 740 | assert(!lysc_is_userordered(schema)); |
| 741 | |
| 742 | /* learn operation first */ |
| 743 | if (!second) { |
| 744 | *op = LYD_DIFF_OP_DELETE; |
| 745 | } else if (!first) { |
| 746 | *op = LYD_DIFF_OP_CREATE; |
| 747 | } else { |
| 748 | switch (schema->nodetype) { |
| 749 | case LYS_CONTAINER: |
| 750 | case LYS_RPC: |
| 751 | case LYS_ACTION: |
| 752 | case LYS_NOTIF: |
| 753 | /* no changes */ |
| 754 | return LY_ENOT; |
| 755 | case LYS_LIST: |
| 756 | case LYS_LEAFLIST: |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 757 | if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 758 | /* default flag change */ |
| 759 | *op = LYD_DIFF_OP_NONE; |
| 760 | } else { |
| 761 | /* no changes */ |
| 762 | return LY_ENOT; |
| 763 | } |
| 764 | break; |
| 765 | case LYS_LEAF: |
| 766 | case LYS_ANYXML: |
| 767 | case LYS_ANYDATA: |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 768 | if (lyd_compare_single(first, second, 0)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 769 | /* different values */ |
| 770 | *op = LYD_DIFF_OP_REPLACE; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 771 | } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 772 | /* default flag change */ |
| 773 | *op = LYD_DIFF_OP_NONE; |
| 774 | } else { |
| 775 | /* no changes */ |
| 776 | return LY_ENOT; |
| 777 | } |
| 778 | break; |
| 779 | default: |
| 780 | LOGINT_RET(schema->module->ctx); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * set each attribute correctly based on the operation and node type |
| 786 | */ |
| 787 | |
| 788 | /* orig-default */ |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 789 | if ((schema->nodetype & LYD_NODE_TERM) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 790 | if (first->flags & LYD_DEFAULT) { |
| 791 | *orig_default = "true"; |
| 792 | } else { |
| 793 | *orig_default = "false"; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | /* orig-value */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 798 | if ((schema->nodetype & (LYS_LEAF | LYS_ANYDATA)) && (*op == LYD_DIFF_OP_REPLACE)) { |
| 799 | if (schema->nodetype == LYS_LEAF) { |
Michal Vasko | 6ea6fe2 | 2021-10-08 09:57:01 +0200 | [diff] [blame] | 800 | str_val = lyd_get_value(first); |
| 801 | *orig_value = strdup(str_val ? str_val : ""); |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 802 | LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM); |
| 803 | } else { |
| 804 | LY_CHECK_RET(lyd_any_value_str(first, orig_value)); |
| 805 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | return LY_SUCCESS; |
| 809 | } |
| 810 | |
| 811 | /** |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 812 | * @brief Find a matching instance of a node in a data tree. |
| 813 | * |
| 814 | * @param[in] siblings Siblings to search in. |
| 815 | * @param[in] target Target node to search for. |
| 816 | * @param[in] defaults Whether to consider (or ignore) default values. |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 817 | * @param[in,out] dup_inst_ht Duplicate instance cache. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 818 | * @param[out] match Found match, NULL if no matching node found. |
| 819 | * @return LY_ERR value. |
| 820 | */ |
| 821 | static LY_ERR |
| 822 | lyd_diff_find_match(const struct lyd_node *siblings, const struct lyd_node *target, ly_bool defaults, |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 823 | struct ly_ht **dup_inst_ht, struct lyd_node **match) |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 824 | { |
Michal Vasko | 2bd856f | 2022-12-02 14:03:29 +0100 | [diff] [blame] | 825 | LY_ERR r; |
| 826 | |
Michal Vasko | db91fc3 | 2023-05-02 14:39:40 +0200 | [diff] [blame] | 827 | if (!target->schema) { |
| 828 | /* try to find the same opaque node */ |
| 829 | r = lyd_find_sibling_opaq_next(siblings, LYD_NAME(target), match); |
| 830 | } else if (target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 831 | /* try to find the exact instance */ |
Michal Vasko | 2bd856f | 2022-12-02 14:03:29 +0100 | [diff] [blame] | 832 | r = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 833 | } else { |
| 834 | /* try to simply find the node, there cannot be more instances */ |
Michal Vasko | 2bd856f | 2022-12-02 14:03:29 +0100 | [diff] [blame] | 835 | r = lyd_find_sibling_val(siblings, target->schema, NULL, 0, match); |
| 836 | } |
| 837 | if (r && (r != LY_ENOTFOUND)) { |
| 838 | return r; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 841 | /* update match as needed */ |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 842 | LY_CHECK_RET(lyd_dup_inst_next(match, siblings, dup_inst_ht)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 843 | |
| 844 | if (*match && ((*match)->flags & LYD_DEFAULT) && !defaults) { |
| 845 | /* ignore default nodes */ |
| 846 | *match = NULL; |
| 847 | } |
| 848 | return LY_SUCCESS; |
| 849 | } |
| 850 | |
| 851 | /** |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 852 | * @brief Perform diff for all siblings at certain depth, recursively. |
| 853 | * |
| 854 | * For user-ordered lists/leaf-lists a specific structure is used for storing |
| 855 | * the current order. The idea is to apply all the generated diff changes |
| 856 | * virtually on the first tree so that we can continue to generate correct |
| 857 | * changes after some were already generated. |
| 858 | * |
| 859 | * The algorithm then uses second tree position-based changes with a before |
| 860 | * (preceding) item anchor. |
| 861 | * |
| 862 | * Example: |
| 863 | * |
| 864 | * Virtual first tree leaf-list order: |
| 865 | * 1 2 [3] 4 5 |
| 866 | * |
| 867 | * Second tree leaf-list order: |
| 868 | * 1 2 [5] 3 4 |
| 869 | * |
| 870 | * We are at the 3rd node now. We look at whether the nodes on the 3rd position |
| 871 | * match - they do not - move nodes so that the 3rd position node is final -> |
| 872 | * -> move node 5 to the 3rd position -> move node 5 after node 2. |
| 873 | * |
| 874 | * Required properties: |
| 875 | * Stored operations (move) should not be affected by later operations - |
| 876 | * - would cause a redundantly long list of operations, possibly inifinite. |
| 877 | * |
| 878 | * Implemenation justification: |
| 879 | * First, all delete operations and only then move/create operations are stored. |
| 880 | * Also, preceding anchor is used and after each iteration another node is |
| 881 | * at its final position. That results in the invariant that all preceding |
| 882 | * nodes are final and will not be changed by the later operations, meaning |
| 883 | * they can safely be used as anchors for the later operations. |
| 884 | * |
| 885 | * @param[in] first First tree first sibling. |
| 886 | * @param[in] second Second tree first sibling. |
| 887 | * @param[in] options Diff options. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 888 | * @param[in] nosiblings Whether to skip following siblings. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 889 | * @param[in,out] diff Diff to append to. |
| 890 | * @return LY_ERR value. |
| 891 | */ |
| 892 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 893 | lyd_diff_siblings_r(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 894 | struct lyd_node **diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 895 | { |
| 896 | LY_ERR ret = LY_SUCCESS; |
| 897 | const struct lyd_node *iter_first, *iter_second; |
| 898 | struct lyd_node *match_second, *match_first; |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 899 | struct lyd_diff_userord *userord = NULL, *userord_item; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 900 | struct ly_ht *dup_inst_first = NULL, *dup_inst_second = NULL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 901 | LY_ARRAY_COUNT_TYPE u; |
| 902 | enum lyd_diff_op op; |
| 903 | const char *orig_default; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 904 | char *orig_value, *key, *value, *position, *orig_key, *orig_position; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 905 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 906 | /* compare first tree to the second tree - delete, replace, none */ |
| 907 | LY_LIST_FOR(first, iter_first) { |
Michal Vasko | c825ed7 | 2021-07-21 16:05:59 +0200 | [diff] [blame] | 908 | if (!iter_first->schema) { |
| 909 | continue; |
| 910 | } |
| 911 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 912 | assert(!(iter_first->schema->flags & LYS_KEY)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 913 | if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 914 | /* skip default nodes */ |
| 915 | continue; |
| 916 | } |
| 917 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 918 | /* find a match in the second tree */ |
| 919 | LY_CHECK_GOTO(ret = lyd_diff_find_match(second, iter_first, options & LYD_DIFF_DEFAULTS, &dup_inst_second, |
| 920 | &match_second), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 921 | |
| 922 | if (lysc_is_userordered(iter_first->schema)) { |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 923 | /* get (create) userord entry */ |
| 924 | userord_item = lyd_diff_userord_get(iter_first, iter_first->schema, &userord); |
| 925 | LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_first)); ret = LY_EMEM, cleanup); |
| 926 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 927 | /* we are handling only user-ordered node delete now */ |
| 928 | if (!match_second) { |
| 929 | /* get all the attributes */ |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 930 | LY_CHECK_GOTO(ret = lyd_diff_userord_attrs(iter_first, match_second, options, userord_item, &op, |
| 931 | &orig_default, &value, &orig_value, &key, &orig_key, &position, &orig_position), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 932 | |
| 933 | /* there must be changes, it is deleted */ |
| 934 | assert(op == LYD_DIFF_OP_DELETE); |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 935 | ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, position, orig_key, |
| 936 | orig_position, diff); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 937 | |
| 938 | free(orig_value); |
| 939 | free(key); |
| 940 | free(value); |
| 941 | free(position); |
| 942 | free(orig_key); |
| 943 | free(orig_position); |
| 944 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 945 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 946 | } else { |
| 947 | /* get all the attributes */ |
| 948 | ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value); |
| 949 | |
| 950 | /* add into diff if there are any changes */ |
| 951 | if (!ret) { |
| 952 | if (op == LYD_DIFF_OP_DELETE) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 953 | ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 954 | } else { |
Michal Vasko | 3c2dd6c | 2020-11-06 17:38:55 +0100 | [diff] [blame] | 955 | assert(match_second); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 956 | ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | free(orig_value); |
| 960 | LY_CHECK_GOTO(ret, cleanup); |
| 961 | } else if (ret == LY_ENOT) { |
| 962 | ret = LY_SUCCESS; |
| 963 | } else { |
| 964 | goto cleanup; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | /* check descendants, if any, recursively */ |
| 969 | if (match_second) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 970 | LY_CHECK_GOTO(ret = lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second), |
| 971 | options, 0, diff), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | if (nosiblings) { |
| 975 | break; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | /* reset all cached positions */ |
| 980 | LY_ARRAY_FOR(userord, u) { |
| 981 | userord[u].pos = 0; |
| 982 | } |
| 983 | |
| 984 | /* compare second tree to the first tree - create, user-ordered move */ |
| 985 | LY_LIST_FOR(second, iter_second) { |
Michal Vasko | c825ed7 | 2021-07-21 16:05:59 +0200 | [diff] [blame] | 986 | if (!iter_second->schema) { |
| 987 | continue; |
| 988 | } |
| 989 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 990 | assert(!(iter_second->schema->flags & LYS_KEY)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 991 | if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 992 | /* skip default nodes */ |
| 993 | continue; |
| 994 | } |
| 995 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 996 | /* find a match in the first tree */ |
| 997 | LY_CHECK_GOTO(ret = lyd_diff_find_match(first, iter_second, options & LYD_DIFF_DEFAULTS, &dup_inst_first, |
| 998 | &match_first), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 999 | |
| 1000 | if (lysc_is_userordered(iter_second->schema)) { |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 1001 | /* get userord entry */ |
Michal Vasko | aa51cc5 | 2022-12-06 09:57:11 +0100 | [diff] [blame] | 1002 | userord_item = lyd_diff_userord_get(match_first, iter_second->schema, &userord); |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 1003 | LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_second)); ret = LY_EMEM, cleanup); |
| 1004 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1005 | /* get all the attributes */ |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 1006 | ret = lyd_diff_userord_attrs(match_first, iter_second, options, userord_item, &op, &orig_default, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1007 | &value, &orig_value, &key, &orig_key, &position, &orig_position); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1008 | |
| 1009 | /* add into diff if there are any changes */ |
| 1010 | if (!ret) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1011 | ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, position, orig_key, |
| 1012 | orig_position, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1013 | |
| 1014 | free(orig_value); |
| 1015 | free(key); |
| 1016 | free(value); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1017 | free(position); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1018 | free(orig_key); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1019 | free(orig_position); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1020 | LY_CHECK_GOTO(ret, cleanup); |
| 1021 | } else if (ret == LY_ENOT) { |
| 1022 | ret = LY_SUCCESS; |
| 1023 | } else { |
| 1024 | goto cleanup; |
| 1025 | } |
| 1026 | } else if (!match_first) { |
| 1027 | /* get all the attributes */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1028 | LY_CHECK_GOTO(ret = lyd_diff_attrs(match_first, iter_second, options, &op, &orig_default, &orig_value), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1029 | |
| 1030 | /* there must be changes, it is created */ |
| 1031 | assert(op == LYD_DIFF_OP_CREATE); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1032 | ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1033 | |
| 1034 | free(orig_value); |
| 1035 | LY_CHECK_GOTO(ret, cleanup); |
| 1036 | } /* else was handled */ |
| 1037 | |
| 1038 | if (nosiblings) { |
| 1039 | break; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | cleanup: |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1044 | lyd_dup_inst_free(dup_inst_first); |
| 1045 | lyd_dup_inst_free(dup_inst_second); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1046 | LY_ARRAY_FOR(userord, u) { |
| 1047 | LY_ARRAY_FREE(userord[u].inst); |
| 1048 | } |
| 1049 | LY_ARRAY_FREE(userord); |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1053 | static LY_ERR |
Michal Vasko | 5589617 | 2022-02-17 10:47:21 +0100 | [diff] [blame] | 1054 | lyd_diff(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings, |
| 1055 | struct lyd_node **diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1056 | { |
| 1057 | const struct ly_ctx *ctx; |
| 1058 | |
| 1059 | LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL); |
| 1060 | |
| 1061 | if (first) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1062 | ctx = LYD_CTX(first); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1063 | } else if (second) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1064 | ctx = LYD_CTX(second); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1065 | } else { |
| 1066 | ctx = NULL; |
| 1067 | } |
| 1068 | |
| 1069 | if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) { |
| 1070 | LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__); |
| 1071 | return LY_EINVAL; |
| 1072 | } |
| 1073 | |
| 1074 | *diff = NULL; |
| 1075 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1076 | return lyd_diff_siblings_r(first, second, options, nosiblings, diff); |
| 1077 | } |
| 1078 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1079 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1080 | lyd_diff_tree(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1081 | { |
| 1082 | return lyd_diff(first, second, options, 1, diff); |
| 1083 | } |
| 1084 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1085 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1086 | lyd_diff_siblings(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1087 | { |
| 1088 | return lyd_diff(first, second, options, 0, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | /** |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1092 | * @brief Insert a diff node into a data tree. |
| 1093 | * |
| 1094 | * @param[in,out] first_node First sibling of the data tree. |
| 1095 | * @param[in] parent_node Data tree sibling parent node. |
| 1096 | * @param[in] new_node Node to insert. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1097 | * @param[in] userord_anchor Optional anchor (key, value, or position) of relative (leaf-)list instance. If not set, |
| 1098 | * the user-ordered instance will be inserted at the first position. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1099 | * @return err_info, NULL on success. |
| 1100 | */ |
| 1101 | static LY_ERR |
| 1102 | lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1103 | const char *userord_anchor) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1104 | { |
| 1105 | LY_ERR ret; |
| 1106 | struct lyd_node *anchor; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1107 | uint32_t pos, anchor_pos; |
| 1108 | int found; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1109 | |
| 1110 | assert(new_node); |
| 1111 | |
| 1112 | if (!*first_node) { |
| 1113 | if (!parent_node) { |
| 1114 | /* no parent or siblings */ |
| 1115 | *first_node = new_node; |
| 1116 | return LY_SUCCESS; |
| 1117 | } |
| 1118 | |
| 1119 | /* simply insert into parent, no other children */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1120 | if (userord_anchor) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1121 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1122 | new_node->schema->name); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1123 | return LY_EINVAL; |
| 1124 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1125 | return lyd_insert_child(parent_node, new_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1126 | } |
| 1127 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1128 | assert(!(*first_node)->parent || (lyd_parent(*first_node) == parent_node)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1129 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1130 | if (!lysc_is_userordered(new_node->schema)) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1131 | /* simple insert */ |
| 1132 | return lyd_insert_sibling(*first_node, new_node, first_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1135 | if (userord_anchor) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1136 | /* find the anchor sibling */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1137 | if (lysc_is_dup_inst_list(new_node->schema)) { |
| 1138 | anchor_pos = atoi(userord_anchor); |
Michal Vasko | 0ff9775 | 2022-01-18 16:35:41 +0100 | [diff] [blame] | 1139 | if (!anchor_pos) { |
| 1140 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Invalid user-ordered anchor value \"%s\".", userord_anchor); |
| 1141 | return LY_EINVAL; |
| 1142 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1143 | |
| 1144 | found = 0; |
| 1145 | pos = 1; |
| 1146 | LYD_LIST_FOR_INST(*first_node, new_node->schema, anchor) { |
| 1147 | if (pos == anchor_pos) { |
| 1148 | found = 1; |
| 1149 | break; |
| 1150 | } |
| 1151 | ++pos; |
| 1152 | } |
| 1153 | if (!found) { |
| 1154 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.", |
| 1155 | new_node->schema->name); |
| 1156 | return LY_EINVAL; |
| 1157 | } |
| 1158 | } else { |
| 1159 | ret = lyd_find_sibling_val(*first_node, new_node->schema, userord_anchor, 0, &anchor); |
| 1160 | if (ret == LY_ENOTFOUND) { |
| 1161 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.", |
| 1162 | new_node->schema->name); |
| 1163 | return LY_EINVAL; |
| 1164 | } else if (ret) { |
| 1165 | return ret; |
| 1166 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /* insert after */ |
| 1170 | LY_CHECK_RET(lyd_insert_after(anchor, new_node)); |
| 1171 | assert(new_node->prev == anchor); |
| 1172 | if (*first_node == new_node) { |
| 1173 | *first_node = anchor; |
| 1174 | } |
| 1175 | } else { |
Michal Vasko | ea7d323 | 2022-04-19 12:01:36 +0200 | [diff] [blame] | 1176 | /* find the first instance */ |
| 1177 | ret = lyd_find_sibling_val(*first_node, new_node->schema, NULL, 0, &anchor); |
| 1178 | LY_CHECK_RET(ret && (ret != LY_ENOTFOUND), ret); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1179 | |
Michal Vasko | ea7d323 | 2022-04-19 12:01:36 +0200 | [diff] [blame] | 1180 | if (anchor) { |
| 1181 | /* insert before the first instance */ |
| 1182 | LY_CHECK_RET(lyd_insert_before(anchor, new_node)); |
| 1183 | if ((*first_node)->prev->next) { |
| 1184 | assert(!new_node->prev->next); |
| 1185 | *first_node = new_node; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1186 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1187 | } else { |
Michal Vasko | ea7d323 | 2022-04-19 12:01:36 +0200 | [diff] [blame] | 1188 | /* insert anywhere */ |
| 1189 | LY_CHECK_RET(lyd_insert_sibling(*first_node, new_node, first_node)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | return LY_SUCCESS; |
| 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * @brief Apply diff subtree on data tree nodes, recursively. |
| 1198 | * |
| 1199 | * @param[in,out] first_node First sibling of the data tree. |
| 1200 | * @param[in] parent_node Parent of the first sibling. |
| 1201 | * @param[in] diff_node Current diff node. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1202 | * @param[in] diff_cb Optional diff callback. |
| 1203 | * @param[in] cb_data User data for @p diff_cb. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1204 | * @param[in,out] dup_inst Duplicate instance cache for all @p diff_node siblings. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1205 | * @return LY_ERR value. |
| 1206 | */ |
| 1207 | static LY_ERR |
| 1208 | lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node, |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1209 | lyd_diff_cb diff_cb, void *cb_data, struct ly_ht **dup_inst) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1210 | { |
| 1211 | LY_ERR ret; |
| 1212 | struct lyd_node *match, *diff_child; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1213 | const char *str_val, *meta_str; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1214 | enum lyd_diff_op op; |
| 1215 | struct lyd_meta *meta; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1216 | struct ly_ht *child_dup_inst = NULL; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1217 | const struct ly_ctx *ctx = LYD_CTX(diff_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1218 | |
| 1219 | /* read all the valid attributes */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1220 | LY_CHECK_RET(lyd_diff_get_op(diff_node, &op)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1221 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1222 | /* handle specific user-ordered (leaf-)lists operations separately */ |
| 1223 | if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) { |
| 1224 | if (op == LYD_DIFF_OP_REPLACE) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1225 | /* find the node (we must have some siblings because the node was only moved) */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1226 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1227 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1228 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1229 | /* duplicate the node */ |
| 1230 | LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1231 | } |
| 1232 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1233 | /* get "key", "value", or "position" metadata string value */ |
| 1234 | if (lysc_is_dup_inst_list(diff_node->schema)) { |
| 1235 | meta_str = "yang:position"; |
| 1236 | } else if (diff_node->schema->nodetype == LYS_LIST) { |
| 1237 | meta_str = "yang:key"; |
| 1238 | } else { |
| 1239 | meta_str = "yang:value"; |
| 1240 | } |
| 1241 | meta = lyd_find_meta(diff_node->meta, NULL, meta_str); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1242 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_str, diff_node), LY_EINVAL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1243 | str_val = lyd_get_meta_value(meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1244 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1245 | /* insert/move the node */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1246 | if (str_val[0]) { |
| 1247 | ret = lyd_diff_insert(first_node, parent_node, match, str_val); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1248 | } else { |
| 1249 | ret = lyd_diff_insert(first_node, parent_node, match, NULL); |
| 1250 | } |
| 1251 | if (ret) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1252 | if (op == LYD_DIFF_OP_CREATE) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1253 | lyd_free_tree(match); |
| 1254 | } |
| 1255 | return ret; |
| 1256 | } |
| 1257 | |
| 1258 | goto next_iter_r; |
| 1259 | } |
| 1260 | |
| 1261 | /* apply operation */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1262 | switch (op) { |
| 1263 | case LYD_DIFF_OP_NONE: |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1264 | /* find the node */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1265 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1266 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1267 | |
| 1268 | if (match->schema->nodetype & LYD_NODE_TERM) { |
| 1269 | /* special case of only dflt flag change */ |
| 1270 | if (diff_node->flags & LYD_DEFAULT) { |
| 1271 | match->flags |= LYD_DEFAULT; |
| 1272 | } else { |
| 1273 | match->flags &= ~LYD_DEFAULT; |
| 1274 | } |
| 1275 | } else { |
| 1276 | /* none operation on nodes without children is redundant and hence forbidden */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1277 | if (!lyd_child_no_keys(diff_node)) { |
Michal Vasko | 0ff9775 | 2022-01-18 16:35:41 +0100 | [diff] [blame] | 1278 | LOGERR(ctx, LY_EINVAL, "Operation \"none\" is invalid for node \"%s\" without children.", |
| 1279 | LYD_NAME(diff_node)); |
| 1280 | return LY_EINVAL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | break; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1284 | case LYD_DIFF_OP_CREATE: |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1285 | /* duplicate the node */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1286 | LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1287 | |
| 1288 | /* insert it at the end */ |
| 1289 | ret = 0; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1290 | if (parent_node) { |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 1291 | if (match->flags & LYD_EXT) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1292 | ret = lyplg_ext_insert(parent_node, match); |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 1293 | } else { |
| 1294 | ret = lyd_insert_child(parent_node, match); |
| 1295 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1296 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1297 | ret = lyd_insert_sibling(*first_node, match, first_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1298 | } |
| 1299 | if (ret) { |
| 1300 | lyd_free_tree(match); |
| 1301 | return ret; |
| 1302 | } |
| 1303 | |
| 1304 | break; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1305 | case LYD_DIFF_OP_DELETE: |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1306 | /* find the node */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1307 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1308 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1309 | |
| 1310 | /* remove it */ |
| 1311 | if ((match == *first_node) && !match->parent) { |
| 1312 | assert(!parent_node); |
| 1313 | /* we have removed the top-level node */ |
| 1314 | *first_node = (*first_node)->next; |
| 1315 | } |
| 1316 | lyd_free_tree(match); |
| 1317 | |
| 1318 | /* we are not going recursively in this case, the whole subtree was already deleted */ |
| 1319 | return LY_SUCCESS; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1320 | case LYD_DIFF_OP_REPLACE: |
Michal Vasko | 0ff9775 | 2022-01-18 16:35:41 +0100 | [diff] [blame] | 1321 | if (!(diff_node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA))) { |
| 1322 | LOGERR(ctx, LY_EINVAL, "Operation \"replace\" is invalid for %s node \"%s\".", |
| 1323 | lys_nodetype2str(diff_node->schema->nodetype), LYD_NAME(diff_node)); |
| 1324 | return LY_EINVAL; |
| 1325 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1326 | |
| 1327 | /* find the node */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1328 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1329 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1330 | |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1331 | /* update the value */ |
| 1332 | if (diff_node->schema->nodetype == LYS_LEAF) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1333 | ret = lyd_change_term(match, lyd_get_value(diff_node)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1334 | LY_CHECK_ERR_RET(ret && (ret != LY_EEXIST), LOGERR_UNEXPVAL(ctx, match, "data"), LY_EINVAL); |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1335 | } else { |
| 1336 | struct lyd_node_any *any = (struct lyd_node_any *)diff_node; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1337 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1338 | LY_CHECK_RET(lyd_any_copy_value(match, &any->value, any->value_type)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /* with flags */ |
| 1342 | match->flags = diff_node->flags; |
| 1343 | break; |
| 1344 | default: |
| 1345 | LOGINT_RET(ctx); |
| 1346 | } |
| 1347 | |
| 1348 | next_iter_r: |
| 1349 | if (diff_cb) { |
| 1350 | /* call callback */ |
| 1351 | LY_CHECK_RET(diff_cb(diff_node, match, cb_data)); |
| 1352 | } |
| 1353 | |
| 1354 | /* apply diff recursively */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1355 | ret = LY_SUCCESS; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1356 | LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1357 | ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst); |
| 1358 | if (ret) { |
| 1359 | break; |
| 1360 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1361 | } |
| 1362 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1363 | lyd_dup_inst_free(child_dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1364 | return ret; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1365 | } |
| 1366 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1367 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1368 | lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1369 | lyd_diff_cb diff_cb, void *cb_data) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1370 | { |
| 1371 | const struct lyd_node *root; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1372 | struct ly_ht *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1373 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1374 | |
| 1375 | LY_LIST_FOR(diff, root) { |
| 1376 | if (mod && (lyd_owner_module(root) != mod)) { |
| 1377 | /* skip data nodes from different modules */ |
| 1378 | continue; |
| 1379 | } |
| 1380 | |
| 1381 | /* apply relevant nodes from the diff datatree */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1382 | ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst); |
| 1383 | if (ret) { |
| 1384 | break; |
| 1385 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1386 | } |
| 1387 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1388 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1389 | return ret; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1392 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1393 | lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1394 | { |
| 1395 | return lyd_diff_apply_module(data, diff, NULL, NULL, NULL); |
| 1396 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1397 | |
| 1398 | /** |
| 1399 | * @brief Update operations on a diff node when the new operation is NONE. |
| 1400 | * |
| 1401 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1402 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1403 | * @param[in] src_diff Current source diff node. |
| 1404 | * @return LY_ERR value. |
| 1405 | */ |
| 1406 | static LY_ERR |
| 1407 | lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1408 | { |
| 1409 | switch (cur_op) { |
| 1410 | case LYD_DIFF_OP_NONE: |
| 1411 | case LYD_DIFF_OP_CREATE: |
| 1412 | case LYD_DIFF_OP_REPLACE: |
| 1413 | if (src_diff->schema->nodetype & LYD_NODE_TERM) { |
| 1414 | /* NONE on a term means only its dflt flag was changed */ |
| 1415 | diff_match->flags &= ~LYD_DEFAULT; |
| 1416 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
| 1417 | } |
| 1418 | break; |
| 1419 | default: |
| 1420 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1421 | LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_NONE); |
| 1422 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | return LY_SUCCESS; |
| 1426 | } |
| 1427 | |
| 1428 | /** |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1429 | * @brief Set a specific operation of a node. Delete the previous operation, if any. |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1430 | * Does not change the default flag. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1431 | * |
| 1432 | * @param[in] node Node to change. |
| 1433 | * @param[in] op Operation to set. |
| 1434 | * @return LY_ERR value. |
| 1435 | */ |
| 1436 | static LY_ERR |
| 1437 | lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op) |
| 1438 | { |
Michal Vasko | db91fc3 | 2023-05-02 14:39:40 +0200 | [diff] [blame] | 1439 | lyd_diff_del_meta(node, "operation"); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1440 | |
Michal Vasko | db91fc3 | 2023-05-02 14:39:40 +0200 | [diff] [blame] | 1441 | if (node->schema) { |
| 1442 | return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL); |
| 1443 | } else { |
| 1444 | return lyd_new_attr(node, "yang", "operation", lyd_diff_op2str(op), NULL); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1445 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | /** |
| 1449 | * @brief Update operations on a diff node when the new operation is REPLACE. |
| 1450 | * |
| 1451 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1452 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1453 | * @param[in] src_diff Current source diff node. |
| 1454 | * @return LY_ERR value. |
| 1455 | */ |
| 1456 | static LY_ERR |
| 1457 | lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1458 | { |
| 1459 | LY_ERR ret; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1460 | const char *str_val, *meta_name, *orig_meta_name; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1461 | struct lyd_meta *meta; |
| 1462 | const struct lys_module *mod; |
| 1463 | const struct lyd_node_any *any; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1464 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1465 | |
| 1466 | /* get "yang" module for the metadata */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1467 | mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang"); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1468 | assert(mod); |
| 1469 | |
| 1470 | switch (cur_op) { |
| 1471 | case LYD_DIFF_OP_REPLACE: |
| 1472 | case LYD_DIFF_OP_CREATE: |
| 1473 | switch (diff_match->schema->nodetype) { |
| 1474 | case LYS_LIST: |
| 1475 | case LYS_LEAFLIST: |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1476 | /* it was created/moved somewhere, but now it will be created/moved somewhere else, |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1477 | * keep orig_key/orig_value (only replace oper) and replace key/value */ |
| 1478 | assert(lysc_is_userordered(diff_match->schema)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1479 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1480 | meta_name = "position"; |
| 1481 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1482 | meta_name = "key"; |
| 1483 | } else { |
| 1484 | meta_name = "value"; |
| 1485 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1486 | |
| 1487 | lyd_diff_del_meta(diff_match, meta_name); |
| 1488 | meta = lyd_find_meta(src_diff->meta, mod, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1489 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1490 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1491 | break; |
| 1492 | case LYS_LEAF: |
| 1493 | /* replaced with the exact same value, impossible */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1494 | if (!lyd_compare_single(diff_match, src_diff, 0)) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1495 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1496 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1499 | /* modify the node value */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1500 | if (lyd_change_term(diff_match, lyd_get_value(src_diff))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1501 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1502 | } |
| 1503 | |
Michal Vasko | 8caadab | 2020-11-05 17:38:15 +0100 | [diff] [blame] | 1504 | if (cur_op == LYD_DIFF_OP_REPLACE) { |
| 1505 | /* compare values whether there is any change at all */ |
| 1506 | meta = lyd_find_meta(diff_match->meta, mod, "orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1507 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "orig-value", diff_match), LY_EINVAL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1508 | str_val = lyd_get_meta_value(meta); |
Michal Vasko | 8caadab | 2020-11-05 17:38:15 +0100 | [diff] [blame] | 1509 | ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val)); |
| 1510 | if (!ret) { |
| 1511 | /* values are the same, remove orig-value meta and set oper to NONE */ |
| 1512 | lyd_free_meta_single(meta); |
| 1513 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1514 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /* modify the default flag */ |
| 1518 | diff_match->flags &= ~LYD_DEFAULT; |
| 1519 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
| 1520 | break; |
| 1521 | case LYS_ANYXML: |
| 1522 | case LYS_ANYDATA: |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1523 | if (!lyd_compare_single(diff_match, src_diff, 0)) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1524 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1525 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | /* modify the node value */ |
| 1529 | any = (struct lyd_node_any *)src_diff; |
| 1530 | LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type)); |
| 1531 | break; |
| 1532 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1533 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1534 | } |
| 1535 | break; |
| 1536 | case LYD_DIFF_OP_NONE: |
Michal Vasko | 2a141b3 | 2023-12-14 13:58:14 +0100 | [diff] [blame] | 1537 | switch (diff_match->schema->nodetype) { |
| 1538 | case LYS_LIST: |
| 1539 | /* it is moved now */ |
| 1540 | assert(lysc_is_userordered(diff_match->schema)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1541 | |
Michal Vasko | 2a141b3 | 2023-12-14 13:58:14 +0100 | [diff] [blame] | 1542 | /* change the operation */ |
| 1543 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1544 | |
Michal Vasko | 2a141b3 | 2023-12-14 13:58:14 +0100 | [diff] [blame] | 1545 | /* set orig-meta and meta */ |
| 1546 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1547 | meta_name = "position"; |
| 1548 | orig_meta_name = "orig-position"; |
| 1549 | } else { |
| 1550 | meta_name = "key"; |
| 1551 | orig_meta_name = "orig-key"; |
| 1552 | } |
| 1553 | |
| 1554 | meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name); |
| 1555 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, orig_meta_name, src_diff), LY_EINVAL); |
| 1556 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
| 1557 | |
| 1558 | meta = lyd_find_meta(src_diff->meta, mod, meta_name); |
| 1559 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL); |
| 1560 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
| 1561 | break; |
| 1562 | case LYS_LEAF: |
| 1563 | /* only dflt flag changed, now value changed as well, update the operation */ |
| 1564 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
| 1565 | |
| 1566 | /* modify the node value */ |
| 1567 | if (lyd_change_term(diff_match, lyd_get_value(src_diff))) { |
| 1568 | LOGINT_RET(LYD_CTX(src_diff)); |
| 1569 | } |
| 1570 | break; |
| 1571 | default: |
| 1572 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1573 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1574 | break; |
| 1575 | default: |
| 1576 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1577 | LOGERR_MERGEOP(ctx, diff_match, cur_op, LYD_DIFF_OP_REPLACE); |
| 1578 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | return LY_SUCCESS; |
| 1582 | } |
| 1583 | |
| 1584 | /** |
| 1585 | * @brief Update operations in a diff node when the new operation is CREATE. |
| 1586 | * |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1587 | * @param[in,out] diff_match Node from the diff, may be replaced. |
| 1588 | * @param[in,out] diff Diff root node, may be updated. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1589 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1590 | * @param[in] src_diff Current source diff node. |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1591 | * @param[in] options Diff merge options. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1592 | * @return LY_ERR value. |
| 1593 | */ |
| 1594 | static LY_ERR |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1595 | lyd_diff_merge_create(struct lyd_node **diff_match, struct lyd_node **diff, enum lyd_diff_op cur_op, |
| 1596 | const struct lyd_node *src_diff, uint16_t options) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1597 | { |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1598 | struct lyd_node *child, *src_dup, *to_free = NULL; |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1599 | const struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1600 | uint32_t trg_flags; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1601 | const char *meta_name, *orig_meta_name; |
| 1602 | struct lyd_meta *meta, *orig_meta; |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1603 | const struct ly_ctx *ctx = LYD_CTX(*diff_match); |
| 1604 | LY_ERR r; |
| 1605 | |
| 1606 | /* create operation is valid only for data nodes */ |
| 1607 | LY_CHECK_ERR_RET(!src_diff->schema, LOGINT(ctx), LY_EINT); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1608 | |
| 1609 | switch (cur_op) { |
| 1610 | case LYD_DIFF_OP_DELETE: |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1611 | /* remember current flags */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1612 | trg_flags = (*diff_match)->flags; |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1613 | |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1614 | if (lysc_is_userordered(src_diff->schema)) { |
| 1615 | assert((*diff_match)->schema); |
| 1616 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1617 | /* get anchor metadata */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1618 | if (lysc_is_dup_inst_list((*diff_match)->schema)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1619 | meta_name = "yang:position"; |
| 1620 | orig_meta_name = "yang:orig-position"; |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1621 | } else if ((*diff_match)->schema->nodetype == LYS_LIST) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1622 | meta_name = "yang:key"; |
| 1623 | orig_meta_name = "yang:orig-key"; |
| 1624 | } else { |
| 1625 | meta_name = "yang:value"; |
| 1626 | orig_meta_name = "yang:orig-value"; |
| 1627 | } |
| 1628 | meta = lyd_find_meta(src_diff->meta, NULL, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1629 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL); |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1630 | orig_meta = lyd_find_meta((*diff_match)->meta, NULL, orig_meta_name); |
| 1631 | LY_CHECK_ERR_RET(!orig_meta, LOGERR_META(ctx, orig_meta_name, *diff_match), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1632 | |
| 1633 | /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect |
| 1634 | * the anchors stored in the metadata */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1635 | if (strcmp(lyd_get_meta_value(meta), lyd_get_meta_value(orig_meta))) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1636 | /* deleted + created at another position -> operation REPLACE */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1637 | LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_REPLACE)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1638 | |
| 1639 | /* add anchor metadata */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1640 | LY_CHECK_RET(lyd_dup_meta_single(meta, *diff_match, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1641 | } else { |
| 1642 | /* deleted + created at the same position -> operation NONE */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1643 | LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_NONE)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1644 | |
| 1645 | /* delete anchor metadata */ |
| 1646 | lyd_free_meta_single(orig_meta); |
| 1647 | } |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1648 | } else if (src_diff->schema->nodetype == LYS_LEAF) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1649 | if (options & LYD_DIFF_MERGE_DEFAULTS) { |
| 1650 | /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1651 | sleaf = (struct lysc_node_leaf *)src_diff->schema; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1652 | } |
| 1653 | |
aPiecek | 0a6705b | 2023-11-14 14:20:58 +0100 | [diff] [blame] | 1654 | if (sleaf && sleaf->dflt && !sleaf->dflt->realtype->plugin->compare(ctx, sleaf->dflt, |
Radek Krejci | 55c4bd2 | 2021-04-26 08:09:04 +0200 | [diff] [blame] | 1655 | &((struct lyd_node_term *)src_diff)->value)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1656 | /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1657 | LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_NONE)); |
| 1658 | } else if (!lyd_compare_single(*diff_match, src_diff, 0)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1659 | /* deleted + created -> operation NONE */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1660 | LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_NONE)); |
| 1661 | } else if ((*diff_match)->schema) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1662 | /* we deleted it, but it was created with a different value -> operation REPLACE */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1663 | LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_REPLACE)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1664 | |
| 1665 | /* current value is the previous one (meta) */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1666 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), *diff_match, NULL, "yang:orig-value", |
| 1667 | lyd_get_value(*diff_match), 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1668 | |
| 1669 | /* update the value itself */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1670 | LY_CHECK_RET(lyd_change_term(*diff_match, lyd_get_value(src_diff))); |
| 1671 | } else { |
| 1672 | /* also operation REPLACE but we need to change an opaque node into a data node */ |
| 1673 | LY_CHECK_RET(lyd_dup_single(src_diff, (*diff_match)->parent, LYD_DUP_NO_META | LYD_DUP_WITH_FLAGS, &src_dup)); |
| 1674 | if (!(*diff_match)->parent) { |
| 1675 | /* will always be inserted before diff_match, which is opaque */ |
| 1676 | LY_CHECK_RET(lyd_insert_sibling(*diff_match, src_dup, diff)); |
| 1677 | } |
| 1678 | to_free = *diff_match; |
| 1679 | *diff_match = src_dup; |
| 1680 | |
| 1681 | r = lyd_new_meta(ctx, src_dup, NULL, "yang:orig-value", lyd_get_value(to_free), 0, NULL); |
| 1682 | lyd_free_tree(to_free); |
| 1683 | LY_CHECK_RET(r); |
| 1684 | LY_CHECK_RET(lyd_new_meta(ctx, src_dup, NULL, "yang:operation", lyd_diff_op2str(LYD_DIFF_OP_REPLACE), 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1685 | } |
| 1686 | } else { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1687 | /* deleted + created -> operation NONE */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1688 | LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_NONE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1689 | } |
| 1690 | |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1691 | assert((*diff_match)->schema); |
| 1692 | if ((*diff_match)->schema->nodetype & LYD_NODE_TERM) { |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 1693 | /* add orig-dflt metadata */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1694 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), *diff_match, NULL, "yang:orig-default", |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 1695 | trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL)); |
| 1696 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1697 | /* update dflt flag itself */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1698 | (*diff_match)->flags &= ~LYD_DEFAULT; |
| 1699 | (*diff_match)->flags |= src_diff->flags & LYD_DEFAULT; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | /* but the operation of its children should remain DELETE */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1703 | LY_LIST_FOR(lyd_child_no_keys(*diff_match), child) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1704 | LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1705 | } |
| 1706 | break; |
| 1707 | default: |
| 1708 | /* create and replace operations are not valid */ |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1709 | LOGERR_MERGEOP(LYD_CTX(src_diff), *diff_match, cur_op, LYD_DIFF_OP_CREATE); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1710 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | return LY_SUCCESS; |
| 1714 | } |
| 1715 | |
| 1716 | /** |
| 1717 | * @brief Update operations on a diff node when the new operation is DELETE. |
| 1718 | * |
| 1719 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1720 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1721 | * @param[in] src_diff Current source diff node. |
| 1722 | * @return LY_ERR value. |
| 1723 | */ |
| 1724 | static LY_ERR |
| 1725 | lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1726 | { |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1727 | struct lyd_node *child; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1728 | struct lyd_meta *meta; |
Michal Vasko | a24a55f | 2024-01-17 16:07:36 +0100 | [diff] [blame] | 1729 | struct lyd_attr *attr; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1730 | const char *meta_name; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1731 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | a24a55f | 2024-01-17 16:07:36 +0100 | [diff] [blame] | 1732 | LY_ERR r; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1733 | |
| 1734 | /* we can delete only exact existing nodes */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1735 | LY_CHECK_ERR_RET(lyd_compare_single(diff_match, src_diff, 0), LOGINT(LYD_CTX(src_diff)), LY_EINT); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1736 | |
| 1737 | switch (cur_op) { |
| 1738 | case LYD_DIFF_OP_CREATE: |
| 1739 | /* it was created, but then deleted -> set NONE operation */ |
| 1740 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1741 | |
| 1742 | if (diff_match->schema->nodetype & LYD_NODE_TERM) { |
| 1743 | /* add orig-default meta because it is expected */ |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1744 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default", |
Michal Vasko | f933e66 | 2023-12-05 09:56:30 +0100 | [diff] [blame] | 1745 | src_diff->flags & LYD_DEFAULT ? "true" : "false", 0, NULL)); |
Michal Vasko | a24a55f | 2024-01-17 16:07:36 +0100 | [diff] [blame] | 1746 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1747 | break; |
| 1748 | case LYD_DIFF_OP_REPLACE: |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1749 | /* remove the redundant metadata */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1750 | if (lysc_is_userordered(diff_match->schema)) { |
| 1751 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1752 | meta_name = "position"; |
| 1753 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1754 | meta_name = "key"; |
| 1755 | } else { |
| 1756 | meta_name = "value"; |
| 1757 | } |
| 1758 | } else { |
| 1759 | assert(diff_match->schema->nodetype == LYS_LEAF); |
| 1760 | |
| 1761 | /* switch value for the original one */ |
| 1762 | meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1763 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-value", diff_match), LY_EINVAL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1764 | if (lyd_change_term(diff_match, lyd_get_meta_value(meta))) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1765 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1766 | return LY_EINVAL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | /* switch default for the original one, then remove the meta */ |
| 1770 | meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1771 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-default", diff_match), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1772 | diff_match->flags &= ~LYD_DEFAULT; |
| 1773 | if (meta->value.boolean) { |
| 1774 | diff_match->flags |= LYD_DEFAULT; |
| 1775 | } |
| 1776 | lyd_free_meta_single(meta); |
| 1777 | |
| 1778 | meta_name = "orig-value"; |
| 1779 | } |
| 1780 | lyd_diff_del_meta(diff_match, meta_name); |
| 1781 | |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1782 | /* it was being changed, but should be deleted instead -> set DELETE operation */ |
| 1783 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE)); |
| 1784 | break; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1785 | case LYD_DIFF_OP_NONE: |
| 1786 | /* it was not modified, but should be deleted -> set DELETE operation */ |
| 1787 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1788 | break; |
| 1789 | default: |
| 1790 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1791 | LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_DELETE); |
| 1792 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1793 | } |
| 1794 | |
Michal Vasko | a24a55f | 2024-01-17 16:07:36 +0100 | [diff] [blame] | 1795 | if (!lysc_is_dup_inst_list(diff_match->schema)) { |
| 1796 | /* keep operation without one for descendants that are yet to be merged */ |
| 1797 | LY_LIST_FOR(lyd_child_no_keys(diff_match), child) { |
| 1798 | lyd_diff_find_meta(child, "operation", &meta, &attr); |
| 1799 | if (meta || attr) { |
| 1800 | continue; |
| 1801 | } |
| 1802 | |
| 1803 | if (!child->schema) { |
| 1804 | r = lyd_find_sibling_opaq_next(lyd_child(src_diff), LYD_NAME(child), NULL); |
| 1805 | } else if (child->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 1806 | r = lyd_find_sibling_first(lyd_child(src_diff), child, NULL); |
| 1807 | } else { |
| 1808 | r = lyd_find_sibling_val(lyd_child(src_diff), child->schema, NULL, 0, NULL); |
| 1809 | } |
| 1810 | if (!r) { |
| 1811 | LY_CHECK_RET(lyd_diff_change_op(child, cur_op)); |
| 1812 | } else if (r != LY_ENOTFOUND) { |
| 1813 | return r; |
| 1814 | } |
| 1815 | } |
| 1816 | } /* else key-less list, for which all the descendants act as keys */ |
| 1817 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1818 | return LY_SUCCESS; |
| 1819 | } |
| 1820 | |
| 1821 | /** |
| 1822 | * @brief Check whether this diff node is redundant (does not change data). |
| 1823 | * |
| 1824 | * @param[in] diff Diff node. |
| 1825 | * @return 0 if not, non-zero if it is. |
| 1826 | */ |
| 1827 | static int |
| 1828 | lyd_diff_is_redundant(struct lyd_node *diff) |
| 1829 | { |
| 1830 | enum lyd_diff_op op; |
| 1831 | struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL; |
| 1832 | struct lyd_node *child; |
| 1833 | const struct lys_module *mod; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1834 | const char *str, *orig_meta_name, *meta_name; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1835 | |
| 1836 | assert(diff); |
| 1837 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1838 | if (lysc_is_dup_inst_list(diff->schema)) { |
| 1839 | /* all descendants are keys */ |
| 1840 | child = NULL; |
| 1841 | } else { |
| 1842 | child = lyd_child_no_keys(diff); |
| 1843 | } |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1844 | mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang"); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1845 | assert(mod); |
| 1846 | |
| 1847 | /* get node operation */ |
Michal Vasko | 53bf6f2 | 2020-07-14 08:23:40 +0200 | [diff] [blame] | 1848 | LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1849 | |
| 1850 | if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1851 | /* get metadata names */ |
| 1852 | if (lysc_is_dup_inst_list(diff->schema)) { |
| 1853 | meta_name = "position"; |
| 1854 | orig_meta_name = "orig-position"; |
| 1855 | } else if (diff->schema->nodetype == LYS_LIST) { |
| 1856 | meta_name = "key"; |
| 1857 | orig_meta_name = "orig-key"; |
| 1858 | } else { |
| 1859 | meta_name = "value"; |
| 1860 | orig_meta_name = "orig-value"; |
| 1861 | } |
| 1862 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1863 | /* check for redundant move */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1864 | orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name); |
| 1865 | val_meta = lyd_find_meta(diff->meta, mod, meta_name); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1866 | assert(orig_val_meta && val_meta); |
| 1867 | |
| 1868 | if (!lyd_compare_meta(orig_val_meta, val_meta)) { |
| 1869 | /* there is actually no move */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1870 | lyd_free_meta_single(orig_val_meta); |
| 1871 | lyd_free_meta_single(val_meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1872 | if (child) { |
| 1873 | /* change operation to NONE, we have siblings */ |
| 1874 | lyd_diff_change_op(diff, LYD_DIFF_OP_NONE); |
| 1875 | return 0; |
| 1876 | } |
| 1877 | |
| 1878 | /* redundant node, BUT !! |
| 1879 | * In diff the move operation is always converted to be INSERT_AFTER, which is fine |
| 1880 | * because the data that this is applied on should not change for the diff lifetime. |
| 1881 | * However, when we are merging 2 diffs, this conversion is actually lossy because |
| 1882 | * if the data change, the move operation can also change its meaning. In this specific |
| 1883 | * case the move operation will be lost. But it can be considered a feature, it is not supported. |
| 1884 | */ |
| 1885 | return 1; |
| 1886 | } |
Michal Vasko | db91fc3 | 2023-05-02 14:39:40 +0200 | [diff] [blame] | 1887 | } else if (op == LYD_DIFF_OP_NONE) { |
| 1888 | if (!diff->schema) { |
| 1889 | /* opaque node with none must be redundant */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1890 | return 1; |
| 1891 | } |
Michal Vasko | db91fc3 | 2023-05-02 14:39:40 +0200 | [diff] [blame] | 1892 | |
| 1893 | if (diff->schema->nodetype & LYD_NODE_TERM) { |
| 1894 | /* check whether at least the default flags are different */ |
| 1895 | meta = lyd_find_meta(diff->meta, mod, "orig-default"); |
| 1896 | assert(meta); |
| 1897 | str = lyd_get_meta_value(meta); |
| 1898 | |
| 1899 | /* if previous and current dflt flags are the same, this node is redundant */ |
| 1900 | if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) { |
| 1901 | return 1; |
| 1902 | } |
| 1903 | return 0; |
| 1904 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | if (!child && (op == LYD_DIFF_OP_NONE)) { |
| 1908 | return 1; |
| 1909 | } |
| 1910 | |
| 1911 | return 0; |
| 1912 | } |
| 1913 | |
| 1914 | /** |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1915 | * @brief Merge sysrepo diff subtree with another diff, recursively. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1916 | * |
| 1917 | * @param[in] src_diff Source diff node. |
| 1918 | * @param[in] diff_parent Current sysrepo diff parent. |
| 1919 | * @param[in] diff_cb Optional diff callback. |
| 1920 | * @param[in] cb_data User data for @p diff_cb. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1921 | * @param[in,out] dup_inst Duplicate instance cache for all @p src_diff siblings. |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1922 | * @param[in] options Diff merge options. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1923 | * @param[in,out] diff Diff root node. |
| 1924 | * @return LY_ERR value. |
| 1925 | */ |
| 1926 | static LY_ERR |
| 1927 | lyd_diff_merge_r(const struct lyd_node *src_diff, struct lyd_node *diff_parent, lyd_diff_cb diff_cb, void *cb_data, |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1928 | struct ly_ht **dup_inst, uint16_t options, struct lyd_node **diff) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1929 | { |
| 1930 | LY_ERR ret = LY_SUCCESS; |
| 1931 | struct lyd_node *child, *diff_node = NULL; |
| 1932 | enum lyd_diff_op src_op, cur_op; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1933 | struct ly_ht *child_dup_inst = NULL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1934 | |
| 1935 | /* get source node operation */ |
| 1936 | LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op)); |
| 1937 | |
| 1938 | /* find an equal node in the current diff */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1939 | LY_CHECK_RET(lyd_diff_find_match(diff_parent ? lyd_child_no_keys(diff_parent) : *diff, src_diff, 1, dup_inst, &diff_node)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1940 | |
| 1941 | if (diff_node) { |
| 1942 | /* get target (current) operation */ |
| 1943 | LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op)); |
| 1944 | |
| 1945 | /* merge operations */ |
| 1946 | switch (src_op) { |
| 1947 | case LYD_DIFF_OP_REPLACE: |
| 1948 | ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff); |
| 1949 | break; |
| 1950 | case LYD_DIFF_OP_CREATE: |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1951 | if ((cur_op == LYD_DIFF_OP_CREATE) && lysc_is_dup_inst_list(diff_node->schema)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1952 | /* special case of creating duplicate (leaf-)list instances */ |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1953 | goto add_diff; |
| 1954 | } |
| 1955 | |
Michal Vasko | 4f33116 | 2023-08-15 10:07:44 +0200 | [diff] [blame] | 1956 | ret = lyd_diff_merge_create(&diff_node, diff, cur_op, src_diff, options); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1957 | break; |
| 1958 | case LYD_DIFF_OP_DELETE: |
| 1959 | ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff); |
| 1960 | break; |
| 1961 | case LYD_DIFF_OP_NONE: |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1962 | /* key-less list can never have "none" operation since all its descendants are acting as "keys" */ |
| 1963 | assert((src_diff->schema->nodetype != LYS_LIST) || !lysc_is_dup_inst_list(src_diff->schema)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1964 | ret = lyd_diff_merge_none(diff_node, cur_op, src_diff); |
| 1965 | break; |
| 1966 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1967 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1968 | } |
| 1969 | if (ret) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1970 | LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1971 | return ret; |
| 1972 | } |
| 1973 | |
| 1974 | if (diff_cb) { |
| 1975 | /* call callback */ |
Michal Vasko | bc5fba9 | 2020-08-07 12:14:39 +0200 | [diff] [blame] | 1976 | LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | /* update diff parent */ |
| 1980 | diff_parent = diff_node; |
| 1981 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1982 | /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances), |
| 1983 | * so there is nothing to merge for these "keys" */ |
| 1984 | if (!lysc_is_dup_inst_list(src_diff->schema)) { |
| 1985 | /* merge src_diff recursively */ |
| 1986 | LY_LIST_FOR(lyd_child_no_keys(src_diff), child) { |
| 1987 | ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff); |
| 1988 | if (ret) { |
| 1989 | break; |
| 1990 | } |
| 1991 | } |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1992 | lyd_dup_inst_free(child_dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1993 | LY_CHECK_RET(ret); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1994 | } |
| 1995 | } else { |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1996 | add_diff: |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1997 | /* add new diff node with all descendants */ |
Michal Vasko | 9ad7685 | 2022-07-12 10:18:03 +0200 | [diff] [blame] | 1998 | if ((src_diff->flags & LYD_EXT) && diff_parent) { |
| 1999 | LY_CHECK_RET(lyd_dup_single_to_ctx(src_diff, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent, |
| 2000 | LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node)); |
| 2001 | } else { |
| 2002 | LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent, |
| 2003 | LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node)); |
| 2004 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 2005 | |
| 2006 | /* insert node into diff if not already */ |
| 2007 | if (!diff_parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2008 | lyd_insert_sibling(*diff, diff_node, diff); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | /* update operation */ |
| 2012 | LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op)); |
| 2013 | |
| 2014 | if (diff_cb) { |
Michal Vasko | e2af841 | 2020-12-03 14:11:38 +0100 | [diff] [blame] | 2015 | /* call callback with no source diff node since it was duplicated and just added */ |
| 2016 | LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | /* update diff parent */ |
| 2020 | diff_parent = diff_node; |
| 2021 | } |
| 2022 | |
| 2023 | /* remove any redundant nodes */ |
Michal Vasko | b98d708 | 2020-07-15 16:38:36 +0200 | [diff] [blame] | 2024 | if (lyd_diff_is_redundant(diff_parent)) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 2025 | if (diff_parent == *diff) { |
| 2026 | *diff = (*diff)->next; |
| 2027 | } |
| 2028 | lyd_free_tree(diff_parent); |
| 2029 | } |
| 2030 | |
| 2031 | return LY_SUCCESS; |
| 2032 | } |
| 2033 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2034 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | fb737aa | 2020-08-06 13:53:53 +0200 | [diff] [blame] | 2035 | lyd_diff_merge_module(struct lyd_node **diff, const struct lyd_node *src_diff, const struct lys_module *mod, |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 2036 | lyd_diff_cb diff_cb, void *cb_data, uint16_t options) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 2037 | { |
| 2038 | const struct lyd_node *src_root; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 2039 | struct ly_ht *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2040 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 2041 | |
| 2042 | LY_LIST_FOR(src_diff, src_root) { |
| 2043 | if (mod && (lyd_owner_module(src_root) != mod)) { |
| 2044 | /* skip data nodes from different modules */ |
| 2045 | continue; |
| 2046 | } |
| 2047 | |
| 2048 | /* apply relevant nodes from the diff datatree */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2049 | LY_CHECK_GOTO(ret = lyd_diff_merge_r(src_root, NULL, diff_cb, cb_data, &dup_inst, options, diff), cleanup); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 2050 | } |
| 2051 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2052 | cleanup: |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 2053 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2054 | return ret; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 2055 | } |
| 2056 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2057 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 2058 | lyd_diff_merge_tree(struct lyd_node **diff_first, struct lyd_node *diff_parent, const struct lyd_node *src_sibling, |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 2059 | lyd_diff_cb diff_cb, void *cb_data, uint16_t options) |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 2060 | { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2061 | LY_ERR ret; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 2062 | struct ly_ht *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2063 | |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 2064 | if (!src_sibling) { |
| 2065 | return LY_SUCCESS; |
| 2066 | } |
| 2067 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2068 | ret = lyd_diff_merge_r(src_sibling, diff_parent, diff_cb, cb_data, &dup_inst, options, diff_first); |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 2069 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2070 | return ret; |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 2071 | } |
| 2072 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2073 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 2074 | lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 2075 | { |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 2076 | return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 2077 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2078 | |
| 2079 | static LY_ERR |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2080 | lyd_diff_reverse_value(struct lyd_node *node, const struct lys_module *mod) |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2081 | { |
| 2082 | LY_ERR ret = LY_SUCCESS; |
| 2083 | struct lyd_meta *meta; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2084 | const char *val1 = NULL; |
| 2085 | char *val2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2086 | uint32_t flags; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2087 | |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2088 | assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA)); |
| 2089 | |
| 2090 | meta = lyd_find_meta(node->meta, mod, "orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 2091 | LY_CHECK_ERR_RET(!meta, LOGERR_META(LYD_CTX(node), "orig-value", node), LY_EINVAL); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2092 | |
| 2093 | /* orig-value */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2094 | val1 = lyd_get_meta_value(meta); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2095 | |
| 2096 | /* current value */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2097 | if (node->schema->nodetype == LYS_LEAF) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2098 | val2 = strdup(lyd_get_value(node)); |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2099 | } else { |
| 2100 | LY_CHECK_RET(lyd_any_value_str(node, &val2)); |
| 2101 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2102 | |
| 2103 | /* switch values, keep default flag */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2104 | flags = node->flags; |
| 2105 | if (node->schema->nodetype == LYS_LEAF) { |
| 2106 | LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup); |
| 2107 | } else { |
| 2108 | union lyd_any_value anyval = {.str = val1}; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 2109 | |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2110 | LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup); |
| 2111 | } |
| 2112 | node->flags = flags; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2113 | LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup); |
| 2114 | |
| 2115 | cleanup: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2116 | free(val2); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2117 | return ret; |
| 2118 | } |
| 2119 | |
| 2120 | static LY_ERR |
| 2121 | lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod) |
| 2122 | { |
| 2123 | struct lyd_meta *meta; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2124 | uint32_t flag1, flag2; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2125 | |
| 2126 | meta = lyd_find_meta(node->meta, mod, "orig-default"); |
Michal Vasko | 610e93b | 2020-11-09 20:58:32 +0100 | [diff] [blame] | 2127 | LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2128 | |
| 2129 | /* orig-default */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2130 | if (meta->value.boolean) { |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2131 | flag1 = LYD_DEFAULT; |
| 2132 | } else { |
| 2133 | flag1 = 0; |
| 2134 | } |
| 2135 | |
| 2136 | /* current default */ |
| 2137 | flag2 = node->flags & LYD_DEFAULT; |
| 2138 | |
Michal Vasko | 610e93b | 2020-11-09 20:58:32 +0100 | [diff] [blame] | 2139 | if (flag1 == flag2) { |
| 2140 | /* no default state change so nothing to reverse */ |
| 2141 | return LY_SUCCESS; |
| 2142 | } |
| 2143 | |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2144 | /* switch defaults */ |
| 2145 | node->flags &= ~LYD_DEFAULT; |
| 2146 | node->flags |= flag1; |
| 2147 | LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false")); |
| 2148 | |
| 2149 | return LY_SUCCESS; |
| 2150 | } |
| 2151 | |
| 2152 | static LY_ERR |
| 2153 | lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2) |
| 2154 | { |
| 2155 | LY_ERR ret = LY_SUCCESS; |
| 2156 | struct lyd_meta *meta1, *meta2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2157 | const char *val1 = NULL; |
| 2158 | char *val2 = NULL; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2159 | |
| 2160 | meta1 = lyd_find_meta(node->meta, mod, name1); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 2161 | LY_CHECK_ERR_RET(!meta1, LOGERR_META(LYD_CTX(node), name1, node), LY_EINVAL); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2162 | |
| 2163 | meta2 = lyd_find_meta(node->meta, mod, name2); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 2164 | LY_CHECK_ERR_RET(!meta2, LOGERR_META(LYD_CTX(node), name2, node), LY_EINVAL); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2165 | |
| 2166 | /* value1 */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2167 | val1 = lyd_get_meta_value(meta1); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2168 | |
| 2169 | /* value2 */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2170 | val2 = strdup(lyd_get_meta_value(meta2)); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2171 | |
| 2172 | /* switch values */ |
| 2173 | LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup); |
| 2174 | LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup); |
| 2175 | |
| 2176 | cleanup: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2177 | free(val2); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2178 | return ret; |
| 2179 | } |
| 2180 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2181 | /** |
| 2182 | * @brief Remove specific operation from all the nodes in a subtree. |
| 2183 | * |
| 2184 | * @param[in] diff Diff subtree to process. |
| 2185 | * @param[in] op Only expected operation. |
| 2186 | * @return LY_ERR value. |
| 2187 | */ |
| 2188 | static LY_ERR |
| 2189 | lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op) |
| 2190 | { |
| 2191 | struct lyd_node *elem; |
| 2192 | struct lyd_meta *meta; |
| 2193 | |
| 2194 | LYD_TREE_DFS_BEGIN(diff, elem) { |
| 2195 | meta = lyd_find_meta(elem->meta, NULL, "yang:operation"); |
| 2196 | if (meta) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2197 | LY_CHECK_ERR_RET(lyd_diff_str2op(lyd_get_meta_value(meta)) != op, LOGINT(LYD_CTX(diff)), LY_EINT); |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2198 | lyd_free_meta_single(meta); |
| 2199 | } |
| 2200 | |
| 2201 | LYD_TREE_DFS_END(diff, elem); |
| 2202 | } |
| 2203 | |
| 2204 | return LY_SUCCESS; |
| 2205 | } |
| 2206 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2207 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 6653581 | 2020-08-11 08:44:22 +0200 | [diff] [blame] | 2208 | lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff) |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2209 | { |
| 2210 | LY_ERR ret = LY_SUCCESS; |
| 2211 | const struct lys_module *mod; |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2212 | struct lyd_node *root, *elem, *iter; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2213 | enum lyd_diff_op op; |
| 2214 | |
| 2215 | LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL); |
| 2216 | |
| 2217 | if (!src_diff) { |
| 2218 | *diff = NULL; |
| 2219 | return LY_SUCCESS; |
| 2220 | } |
| 2221 | |
| 2222 | /* duplicate diff */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2223 | LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff)); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2224 | |
| 2225 | /* find module with metadata needed for later */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2226 | mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang"); |
| 2227 | LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2228 | |
| 2229 | LY_LIST_FOR(*diff, root) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2230 | LYD_TREE_DFS_BEGIN(root, elem) { |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2231 | /* skip all keys */ |
| 2232 | if (!lysc_is_key(elem->schema)) { |
| 2233 | /* find operation attribute, if any */ |
| 2234 | LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2235 | |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2236 | switch (op) { |
| 2237 | case LYD_DIFF_OP_CREATE: |
| 2238 | /* reverse create to delete */ |
| 2239 | LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup); |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2240 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2241 | /* check all the children for the same operation, nothing else is expected */ |
| 2242 | LY_LIST_FOR(lyd_child(elem), iter) { |
| 2243 | lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE); |
| 2244 | } |
| 2245 | |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2246 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2247 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2248 | case LYD_DIFF_OP_DELETE: |
| 2249 | /* reverse delete to create */ |
| 2250 | LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup); |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2251 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2252 | /* check all the children for the same operation, nothing else is expected */ |
| 2253 | LY_LIST_FOR(lyd_child(elem), iter) { |
| 2254 | lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE); |
| 2255 | } |
| 2256 | |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2257 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2258 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2259 | case LYD_DIFF_OP_REPLACE: |
| 2260 | switch (elem->schema->nodetype) { |
| 2261 | case LYS_LEAF: |
| 2262 | /* leaf value change */ |
| 2263 | LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup); |
| 2264 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
| 2265 | break; |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2266 | case LYS_ANYXML: |
| 2267 | case LYS_ANYDATA: |
| 2268 | /* any value change */ |
| 2269 | LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup); |
| 2270 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2271 | case LYS_LEAFLIST: |
| 2272 | /* leaf-list move */ |
| 2273 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2274 | if (lysc_is_dup_inst_list(elem->schema)) { |
| 2275 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup); |
| 2276 | } else { |
| 2277 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup); |
| 2278 | } |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2279 | break; |
| 2280 | case LYS_LIST: |
| 2281 | /* list move */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2282 | if (lysc_is_dup_inst_list(elem->schema)) { |
| 2283 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup); |
| 2284 | } else { |
| 2285 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup); |
| 2286 | } |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2287 | break; |
| 2288 | default: |
| 2289 | LOGINT(LYD_CTX(src_diff)); |
| 2290 | ret = LY_EINT; |
| 2291 | goto cleanup; |
| 2292 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2293 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2294 | case LYD_DIFF_OP_NONE: |
| 2295 | switch (elem->schema->nodetype) { |
| 2296 | case LYS_LEAF: |
| 2297 | case LYS_LEAFLIST: |
| 2298 | /* default flag change */ |
| 2299 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
| 2300 | break; |
| 2301 | default: |
| 2302 | /* nothing to do */ |
| 2303 | break; |
| 2304 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2305 | break; |
| 2306 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2307 | } |
| 2308 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2309 | LYD_TREE_DFS_END(root, elem); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | cleanup: |
| 2314 | if (ret) { |
| 2315 | lyd_free_siblings(*diff); |
| 2316 | *diff = NULL; |
| 2317 | } |
| 2318 | return ret; |
| 2319 | } |