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