blob: e4cbce3c54c75d6351365a84ca697c1e22e7d46b [file] [log] [blame]
Michal Vaskod59035b2020-07-08 12:00:06 +02001/**
2 * @file diff.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief diff functions
5 *
Michal Vaskoe78faec2021-04-08 17:24:43 +02006 * Copyright (c) 2020 - 2021 CESNET, z.s.p.o.
Michal Vaskod59035b2020-07-08 12:00:06 +02007 *
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 Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Michal Vaskod59035b2020-07-08 12:00:06 +020015
16#include "diff.h"
17
18#include <assert.h>
19#include <stddef.h>
Michal Vaskoe78faec2021-04-08 17:24:43 +020020#include <stdint.h>
21#include <stdio.h>
Radek Krejci47fab892020-11-05 17:02:41 +010022#include <stdlib.h>
Michal Vaskod59035b2020-07-08 12:00:06 +020023#include <string.h>
24
25#include "common.h"
Michal Vaskoe78faec2021-04-08 17:24:43 +020026#include "compat.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "context.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020028#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "plugins_types.h"
30#include "set.h"
31#include "tree.h"
32#include "tree_data.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020033#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010034#include "tree_edit.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020035#include "tree_schema.h"
36#include "tree_schema_internal.h"
37
Michal Vasko10765282022-01-18 14:08:34 +010038#define LOGERR_META(ctx, meta_name, node) \
39 { \
40 char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
41 LOGERR(ctx, LY_EINVAL, "Failed to find metadata \"%s\" for node \"%s\".", meta_name, __path); \
42 free(__path); \
43 }
44
45#define LOGERR_NOINST(ctx, node) \
46 { \
47 char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
48 LOGERR(ctx, LY_EINVAL, "Failed to find node \"%s\" instance in data.", __path); \
49 free(__path); \
50 }
51
52#define LOGERR_UNEXPVAL(ctx, node, data_source) \
53 { \
54 char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
55 LOGERR(ctx, LY_EINVAL, "Unexpected value of node \"%s\" in %s.", __path, data_source); \
56 free(__path); \
57 }
58
59#define LOGERR_MERGEOP(ctx, node, src_op, trg_op) \
60 { \
61 char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
62 LOGERR(ctx, LY_EINVAL, "Unable to merge operation \"%s\" with \"%s\" for node \"%s\".", \
63 lyd_diff_op2str(trg_op), lyd_diff_op2str(src_op), __path); \
64 free(__path); \
65 }
66
Michal Vaskod59035b2020-07-08 12:00:06 +020067static const char *
68lyd_diff_op2str(enum lyd_diff_op op)
69{
70 switch (op) {
71 case LYD_DIFF_OP_CREATE:
72 return "create";
73 case LYD_DIFF_OP_DELETE:
74 return "delete";
75 case LYD_DIFF_OP_REPLACE:
76 return "replace";
77 case LYD_DIFF_OP_NONE:
78 return "none";
79 }
80
81 LOGINT(NULL);
82 return NULL;
83}
84
Michal Vaskoe6323f62020-07-09 15:49:02 +020085static enum lyd_diff_op
86lyd_diff_str2op(const char *str)
87{
88 switch (str[0]) {
89 case 'c':
90 assert(!strcmp(str, "create"));
91 return LYD_DIFF_OP_CREATE;
92 case 'd':
93 assert(!strcmp(str, "delete"));
94 return LYD_DIFF_OP_DELETE;
95 case 'r':
96 assert(!strcmp(str, "replace"));
97 return LYD_DIFF_OP_REPLACE;
98 case 'n':
99 assert(!strcmp(str, "none"));
100 return LYD_DIFF_OP_NONE;
101 }
102
103 LOGINT(NULL);
104 return 0;
105}
106
Michal Vaskod59035b2020-07-08 12:00:06 +0200107LY_ERR
108lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200109 const char *key, const char *value, const char *position, const char *orig_key, const char *orig_position,
110 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200111{
112 struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL;
113 const struct lyd_node *parent = NULL;
114 const struct lys_module *yang_mod;
115
116 assert(diff);
117
Michal Vasko53d48422020-11-13 18:02:29 +0100118 /* replace leaf always needs orig-default and orig-value */
119 assert((node->schema->nodetype != LYS_LEAF) || (op != LYD_DIFF_OP_REPLACE) || (orig_default && orig_value));
120
121 /* create on userord needs key/value */
122 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_CREATE) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200123 (lysc_is_dup_inst_list(node->schema) && position) || key);
Michal Vasko53d48422020-11-13 18:02:29 +0100124 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200125 (op != LYD_DIFF_OP_CREATE) || (lysc_is_dup_inst_list(node->schema) && position) || value);
Michal Vasko53d48422020-11-13 18:02:29 +0100126
127 /* move on userord needs both key and orig-key/value and orig-value */
128 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_REPLACE) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200129 (lysc_is_dup_inst_list(node->schema) && position && orig_position) || (key && orig_key));
Michal Vasko53d48422020-11-13 18:02:29 +0100130 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200131 (op != LYD_DIFF_OP_REPLACE) || (lysc_is_dup_inst_list(node->schema) && position && orig_position) ||
132 (value && orig_value));
Michal Vasko53d48422020-11-13 18:02:29 +0100133
Michal Vaskod59035b2020-07-08 12:00:06 +0200134 /* find the first existing parent */
135 siblings = *diff;
136 while (1) {
137 /* find next node parent */
138 parent = node;
139 while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
Michal Vasko9e685082021-01-29 14:49:09 +0100140 parent = lyd_parent(parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200141 }
142 if (parent == node) {
143 /* no more parents to find */
144 break;
145 }
146
147 /* check whether it exists in the diff */
148 if (lyd_find_sibling_first(siblings, parent, &match)) {
149 break;
150 }
151
152 /* another parent found */
153 diff_parent = match;
154
155 /* move down in the diff */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200156 siblings = lyd_child_no_keys(match);
Michal Vaskod59035b2020-07-08 12:00:06 +0200157 }
158
159 /* duplicate the subtree (and connect to the diff if possible) */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200160 LY_CHECK_RET(lyd_dup_single(node, (struct lyd_node_inner *)diff_parent,
Michal Vasko871a0252020-11-11 18:35:24 +0100161 LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS, &dup));
Michal Vaskod59035b2020-07-08 12:00:06 +0200162
163 /* find the first duplicated parent */
164 if (!diff_parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100165 diff_parent = lyd_parent(dup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200166 while (diff_parent && diff_parent->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100167 diff_parent = lyd_parent(diff_parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200168 }
169 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100170 diff_parent = dup;
Michal Vaskod59035b2020-07-08 12:00:06 +0200171 while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100172 diff_parent = lyd_parent(diff_parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200173 }
174 }
175
176 /* no parent existed, must be manually connected */
177 if (!diff_parent) {
178 /* there actually was no parent to duplicate */
Michal Vaskob104f112020-07-17 09:54:54 +0200179 lyd_insert_sibling(*diff, dup, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200180 } else if (!diff_parent->parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200181 lyd_insert_sibling(*diff, diff_parent, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200182 }
183
184 /* get module with the operation metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200185 yang_mod = LYD_CTX(node)->list.objs[1];
Michal Vaskod59035b2020-07-08 12:00:06 +0200186 assert(!strcmp(yang_mod->name, "yang"));
187
188 /* add parent operation, if any */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200189 if (diff_parent && (diff_parent != dup)) {
Michal Vasko871a0252020-11-11 18:35:24 +0100190 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), diff_parent, yang_mod, "operation", "none", 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200191 }
192
193 /* add subtree operation */
Michal Vasko871a0252020-11-11 18:35:24 +0100194 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "operation", lyd_diff_op2str(op), 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200195
196 /* orig-default */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200197 if (orig_default) {
Michal Vasko871a0252020-11-11 18:35:24 +0100198 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "orig-default", orig_default, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200199 }
200
201 /* orig-value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200202 if (orig_value) {
Michal Vasko871a0252020-11-11 18:35:24 +0100203 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "orig-value", orig_value, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200204 }
205
206 /* key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200207 if (key) {
Michal Vasko871a0252020-11-11 18:35:24 +0100208 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "key", key, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200209 }
210
211 /* value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200212 if (value) {
Michal Vasko871a0252020-11-11 18:35:24 +0100213 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "value", value, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200214 }
215
Michal Vaskoe78faec2021-04-08 17:24:43 +0200216 /* position */
217 if (position) {
218 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "position", position, 0, NULL));
219 }
220
Michal Vaskod59035b2020-07-08 12:00:06 +0200221 /* orig-key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200222 if (orig_key) {
Michal Vasko871a0252020-11-11 18:35:24 +0100223 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "orig-key", orig_key, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200224 }
225
Michal Vaskoe78faec2021-04-08 17:24:43 +0200226 /* orig-position */
227 if (orig_position) {
228 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "orig-position", orig_position, 0, NULL));
229 }
230
Michal Vaskod59035b2020-07-08 12:00:06 +0200231 return LY_SUCCESS;
232}
233
234/**
235 * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
236 *
Michal Vasko1dcd73b2020-12-08 10:04:33 +0100237 * @param[in] first Node from the first tree, can be NULL (on create).
Michal Vaskod59035b2020-07-08 12:00:06 +0200238 * @param[in] schema Schema node of the list/leaf-list.
239 * @param[in,out] userord Sized array of userord items.
240 * @return Userord item for all the user-ordered list/leaf-list instances.
241 */
242static struct lyd_diff_userord *
243lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
244{
245 struct lyd_diff_userord *item;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200246 struct lyd_node *iter;
247 const struct lyd_node **node;
Michal Vaskod59035b2020-07-08 12:00:06 +0200248 LY_ARRAY_COUNT_TYPE u;
249
250 LY_ARRAY_FOR(*userord, u) {
251 if ((*userord)[u].schema == schema) {
252 return &(*userord)[u];
253 }
254 }
255
256 /* it was not added yet, add it now */
257 LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
258
259 item->schema = schema;
260 item->pos = 0;
261 item->inst = NULL;
262
263 /* store all the instance pointers in the current order */
264 if (first) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200265 LYD_LIST_FOR_INST(lyd_first_sibling(first), first->schema, iter) {
266 LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
267 *node = iter;
Michal Vaskod59035b2020-07-08 12:00:06 +0200268 }
269 }
270
271 return item;
272}
273
274/**
275 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
276 * lists/leaf-lists.
277 *
278 * @param[in] first Node from the first tree, can be NULL (on create).
279 * @param[in] second Node from the second tree, can be NULL (on delete).
280 * @param[in] options Diff options.
281 * @param[in,out] userord Sized array of userord items for keeping the current node order.
282 * @param[out] op Operation.
283 * @param[out] orig_default Original default metadata.
284 * @param[out] value Value metadata.
285 * @param[out] orig_value Original value metadata
286 * @param[out] key Key metadata.
287 * @param[out] orig_key Original key metadata.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200288 * @param[out] position Position metadata.
289 * @param[out] orig_position Original position metadata.
Michal Vaskod59035b2020-07-08 12:00:06 +0200290 * @return LY_SUCCESS on success,
291 * @return LY_ENOT if there is no change to be added into diff,
292 * @return LY_ERR value on other errors.
293 */
294static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200295lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options,
Radek Krejci0f969882020-08-21 16:56:47 +0200296 struct lyd_diff_userord **userord, enum lyd_diff_op *op, const char **orig_default, char **value,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200297 char **orig_value, char **key, char **orig_key, char **position, char **orig_position)
Michal Vaskod59035b2020-07-08 12:00:06 +0200298{
299 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200300 size_t buflen, bufused;
301 uint32_t first_pos, second_pos;
Michal Vaskod59035b2020-07-08 12:00:06 +0200302 struct lyd_diff_userord *userord_item;
303
304 assert(first || second);
305
306 *orig_default = NULL;
307 *value = NULL;
308 *orig_value = NULL;
309 *key = NULL;
310 *orig_key = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200311 *position = NULL;
312 *orig_position = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200313
314 schema = first ? first->schema : second->schema;
315 assert(lysc_is_userordered(schema));
316
317 /* get userord entry */
318 userord_item = lyd_diff_userord_get(first, schema, userord);
319 LY_CHECK_RET(!userord_item, LY_EMEM);
320
Michal Vaskod59035b2020-07-08 12:00:06 +0200321 /* find user-ordered first position */
322 if (first) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200323 for (first_pos = 0; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200324 if (userord_item->inst[first_pos] == first) {
325 break;
326 }
327 }
328 assert(first_pos < LY_ARRAY_COUNT(userord_item->inst));
329 } else {
330 first_pos = 0;
331 }
332
Michal Vaskoe78faec2021-04-08 17:24:43 +0200333 /* prepare position of the next instance */
334 second_pos = userord_item->pos++;
335
Michal Vaskod59035b2020-07-08 12:00:06 +0200336 /* learn operation first */
337 if (!second) {
338 *op = LYD_DIFF_OP_DELETE;
339 } else if (!first) {
340 *op = LYD_DIFF_OP_CREATE;
341 } else {
Michal Vasko8f359bf2020-07-28 10:41:15 +0200342 if (lyd_compare_single(second, userord_item->inst[second_pos], 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200343 /* in first, there is a different instance on the second position, we are going to move 'first' node */
344 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200345 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200346 /* default flag change */
347 *op = LYD_DIFF_OP_NONE;
348 } else {
349 /* no changes */
350 return LY_ENOT;
351 }
352 }
353
354 /*
355 * set each attribute correctly based on the operation and node type
356 */
357
358 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100359 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200360 if (first->flags & LYD_DEFAULT) {
361 *orig_default = "true";
362 } else {
363 *orig_default = "false";
364 }
365 }
366
367 /* value */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200368 if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
369 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200370 if (second_pos) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200371 *value = strdup(lyd_get_value(userord_item->inst[second_pos - 1]));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200372 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200373 } else {
374 *value = strdup("");
375 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
376 }
377 }
378
379 /* orig-value */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200380 if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
381 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200382 if (first_pos) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200383 *orig_value = strdup(lyd_get_value(userord_item->inst[first_pos - 1]));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200384 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200385 } else {
386 *orig_value = strdup("");
387 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
388 }
389 }
390
391 /* key */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200392 if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
393 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200394 if (second_pos) {
395 buflen = bufused = 0;
396 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0));
397 } else {
398 *key = strdup("");
399 LY_CHECK_ERR_RET(!*key, LOGMEM(schema->module->ctx), LY_EMEM);
400 }
401 }
402
403 /* orig-key */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200404 if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
405 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200406 if (first_pos) {
407 buflen = bufused = 0;
408 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0));
409 } else {
410 *orig_key = strdup("");
411 LY_CHECK_ERR_RET(!*orig_key, LOGMEM(schema->module->ctx), LY_EMEM);
412 }
413 }
414
Michal Vaskoe78faec2021-04-08 17:24:43 +0200415 /* position */
416 if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
417 if (second_pos) {
418 if (asprintf(position, "%" PRIu32, second_pos) == -1) {
419 LOGMEM(schema->module->ctx);
420 return LY_EMEM;
421 }
422 } else {
423 *position = strdup("");
424 LY_CHECK_ERR_RET(!*position, LOGMEM(schema->module->ctx), LY_EMEM);
425 }
426 }
427
428 /* orig-position */
429 if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
430 if (first_pos) {
431 if (asprintf(orig_position, "%" PRIu32, first_pos) == -1) {
432 LOGMEM(schema->module->ctx);
433 return LY_EMEM;
434 }
435 } else {
436 *orig_position = strdup("");
437 LY_CHECK_ERR_RET(!*orig_position, LOGMEM(schema->module->ctx), LY_EMEM);
438 }
439 }
440
Michal Vaskod59035b2020-07-08 12:00:06 +0200441 /*
442 * update our instances - apply the change
443 */
444 if (*op == LYD_DIFF_OP_CREATE) {
445 /* insert the instance */
Michal Vasko5cde11b2020-12-08 10:04:48 +0100446 LY_ARRAY_CREATE_RET(schema->module->ctx, userord_item->inst, 1, LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200447 if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) {
448 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
449 (LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
450 }
451 LY_ARRAY_INCREMENT(userord_item->inst);
452 userord_item->inst[second_pos] = second;
453
454 } else if (*op == LYD_DIFF_OP_DELETE) {
455 /* remove the instance */
456 if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) {
457 memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
458 (LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
459 }
460 LY_ARRAY_DECREMENT(userord_item->inst);
461
462 } else if (*op == LYD_DIFF_OP_REPLACE) {
463 /* move the instances */
464 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
465 (first_pos - second_pos) * sizeof *userord_item->inst);
466 userord_item->inst[second_pos] = first;
467 }
468
469 return LY_SUCCESS;
470}
471
472/**
473 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
474 * lists/leaf-lists.
475 *
476 * @param[in] first Node from the first tree, can be NULL (on create).
477 * @param[in] second Node from the second tree, can be NULL (on delete).
478 * @param[in] options Diff options.
479 * @param[out] op Operation.
480 * @param[out] orig_default Original default metadata.
481 * @param[out] orig_value Original value metadata.
482 * @return LY_SUCCESS on success,
483 * @return LY_ENOT if there is no change to be added into diff,
484 * @return LY_ERR value on other errors.
485 */
486static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200487lyd_diff_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, enum lyd_diff_op *op,
Radek Krejci0f969882020-08-21 16:56:47 +0200488 const char **orig_default, char **orig_value)
Michal Vaskod59035b2020-07-08 12:00:06 +0200489{
490 const struct lysc_node *schema;
Michal Vasko6ea6fe22021-10-08 09:57:01 +0200491 const char *str_val;
Michal Vaskod59035b2020-07-08 12:00:06 +0200492
493 assert(first || second);
494
495 *orig_default = NULL;
496 *orig_value = NULL;
497
498 schema = first ? first->schema : second->schema;
499 assert(!lysc_is_userordered(schema));
500
501 /* learn operation first */
502 if (!second) {
503 *op = LYD_DIFF_OP_DELETE;
504 } else if (!first) {
505 *op = LYD_DIFF_OP_CREATE;
506 } else {
507 switch (schema->nodetype) {
508 case LYS_CONTAINER:
509 case LYS_RPC:
510 case LYS_ACTION:
511 case LYS_NOTIF:
512 /* no changes */
513 return LY_ENOT;
514 case LYS_LIST:
515 case LYS_LEAFLIST:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200516 if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200517 /* default flag change */
518 *op = LYD_DIFF_OP_NONE;
519 } else {
520 /* no changes */
521 return LY_ENOT;
522 }
523 break;
524 case LYS_LEAF:
525 case LYS_ANYXML:
526 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +0200527 if (lyd_compare_single(first, second, 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200528 /* different values */
529 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200530 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200531 /* default flag change */
532 *op = LYD_DIFF_OP_NONE;
533 } else {
534 /* no changes */
535 return LY_ENOT;
536 }
537 break;
538 default:
539 LOGINT_RET(schema->module->ctx);
540 }
541 }
542
543 /*
544 * set each attribute correctly based on the operation and node type
545 */
546
547 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100548 if ((schema->nodetype & LYD_NODE_TERM) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200549 if (first->flags & LYD_DEFAULT) {
550 *orig_default = "true";
551 } else {
552 *orig_default = "false";
553 }
554 }
555
556 /* orig-value */
Michal Vaskobaba84e2021-02-05 16:33:30 +0100557 if ((schema->nodetype & (LYS_LEAF | LYS_ANYDATA)) && (*op == LYD_DIFF_OP_REPLACE)) {
558 if (schema->nodetype == LYS_LEAF) {
Michal Vasko6ea6fe22021-10-08 09:57:01 +0200559 str_val = lyd_get_value(first);
560 *orig_value = strdup(str_val ? str_val : "");
Michal Vaskobaba84e2021-02-05 16:33:30 +0100561 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
562 } else {
563 LY_CHECK_RET(lyd_any_value_str(first, orig_value));
564 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200565 }
566
567 return LY_SUCCESS;
568}
569
570/**
Michal Vaskoe78faec2021-04-08 17:24:43 +0200571 * @brief Find a matching instance of a node in a data tree.
572 *
573 * @param[in] siblings Siblings to search in.
574 * @param[in] target Target node to search for.
575 * @param[in] defaults Whether to consider (or ignore) default values.
576 * @param[in,out] dup_inst_cache Duplicate instance cache.
577 * @param[out] match Found match, NULL if no matching node found.
578 * @return LY_ERR value.
579 */
580static LY_ERR
581lyd_diff_find_match(const struct lyd_node *siblings, const struct lyd_node *target, ly_bool defaults,
Michal Vaskod7c048c2021-05-18 16:12:55 +0200582 struct lyd_dup_inst **dup_inst_cache, struct lyd_node **match)
Michal Vaskoe78faec2021-04-08 17:24:43 +0200583{
Michal Vaskoe78faec2021-04-08 17:24:43 +0200584 if (target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
585 /* try to find the exact instance */
586 lyd_find_sibling_first(siblings, target, match);
587 } else {
588 /* try to simply find the node, there cannot be more instances */
589 lyd_find_sibling_val(siblings, target->schema, NULL, 0, match);
590 }
591
Michal Vaskod7c048c2021-05-18 16:12:55 +0200592 /* update match as needed */
593 LY_CHECK_RET(lyd_dup_inst_next(match, siblings, dup_inst_cache));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200594
595 if (*match && ((*match)->flags & LYD_DEFAULT) && !defaults) {
596 /* ignore default nodes */
597 *match = NULL;
598 }
599 return LY_SUCCESS;
600}
601
602/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200603 * @brief Perform diff for all siblings at certain depth, recursively.
604 *
605 * For user-ordered lists/leaf-lists a specific structure is used for storing
606 * the current order. The idea is to apply all the generated diff changes
607 * virtually on the first tree so that we can continue to generate correct
608 * changes after some were already generated.
609 *
610 * The algorithm then uses second tree position-based changes with a before
611 * (preceding) item anchor.
612 *
613 * Example:
614 *
615 * Virtual first tree leaf-list order:
616 * 1 2 [3] 4 5
617 *
618 * Second tree leaf-list order:
619 * 1 2 [5] 3 4
620 *
621 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
622 * match - they do not - move nodes so that the 3rd position node is final ->
623 * -> move node 5 to the 3rd position -> move node 5 after node 2.
624 *
625 * Required properties:
626 * Stored operations (move) should not be affected by later operations -
627 * - would cause a redundantly long list of operations, possibly inifinite.
628 *
629 * Implemenation justification:
630 * First, all delete operations and only then move/create operations are stored.
631 * Also, preceding anchor is used and after each iteration another node is
632 * at its final position. That results in the invariant that all preceding
633 * nodes are final and will not be changed by the later operations, meaning
634 * they can safely be used as anchors for the later operations.
635 *
636 * @param[in] first First tree first sibling.
637 * @param[in] second Second tree first sibling.
638 * @param[in] options Diff options.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200639 * @param[in] nosiblings Whether to skip following siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +0200640 * @param[in,out] diff Diff to append to.
641 * @return LY_ERR value.
642 */
643static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200644lyd_diff_siblings_r(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings,
Radek Krejci0f969882020-08-21 16:56:47 +0200645 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200646{
647 LY_ERR ret = LY_SUCCESS;
648 const struct lyd_node *iter_first, *iter_second;
649 struct lyd_node *match_second, *match_first;
Michal Vaskod59035b2020-07-08 12:00:06 +0200650 struct lyd_diff_userord *userord = NULL;
Michal Vaskod7c048c2021-05-18 16:12:55 +0200651 struct lyd_dup_inst *dup_inst_first = NULL, *dup_inst_second = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200652 LY_ARRAY_COUNT_TYPE u;
653 enum lyd_diff_op op;
654 const char *orig_default;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200655 char *orig_value, *key, *value, *position, *orig_key, *orig_position;
Michal Vaskod59035b2020-07-08 12:00:06 +0200656
Michal Vaskod59035b2020-07-08 12:00:06 +0200657 /* compare first tree to the second tree - delete, replace, none */
658 LY_LIST_FOR(first, iter_first) {
Michal Vaskoc825ed72021-07-21 16:05:59 +0200659 if (!iter_first->schema) {
660 continue;
661 }
662
Michal Vaskod59035b2020-07-08 12:00:06 +0200663 assert(!(iter_first->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200664 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200665 /* skip default nodes */
666 continue;
667 }
668
Michal Vaskoe78faec2021-04-08 17:24:43 +0200669 /* find a match in the second tree */
670 LY_CHECK_GOTO(ret = lyd_diff_find_match(second, iter_first, options & LYD_DIFF_DEFAULTS, &dup_inst_second,
671 &match_second), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200672
673 if (lysc_is_userordered(iter_first->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200674 /* we are handling only user-ordered node delete now */
675 if (!match_second) {
676 /* get all the attributes */
677 LY_CHECK_GOTO(ret = lyd_diff_userord_attrs(iter_first, match_second, options, &userord, &op, &orig_default,
678 &value, &orig_value, &key, &orig_key, &position, &orig_position), cleanup);
679
680 /* there must be changes, it is deleted */
681 assert(op == LYD_DIFF_OP_DELETE);
682 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, position, orig_key, orig_position, diff);
683
684 free(orig_value);
685 free(key);
686 free(value);
687 free(position);
688 free(orig_key);
689 free(orig_position);
690 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200691 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200692 } else {
693 /* get all the attributes */
694 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
695
696 /* add into diff if there are any changes */
697 if (!ret) {
698 if (op == LYD_DIFF_OP_DELETE) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200699 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200700 } else {
Michal Vasko3c2dd6c2020-11-06 17:38:55 +0100701 assert(match_second);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200702 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200703 }
704
705 free(orig_value);
706 LY_CHECK_GOTO(ret, cleanup);
707 } else if (ret == LY_ENOT) {
708 ret = LY_SUCCESS;
709 } else {
710 goto cleanup;
711 }
712 }
713
714 /* check descendants, if any, recursively */
715 if (match_second) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200716 LY_CHECK_GOTO(ret = lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second),
717 options, 0, diff), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200718 }
719
720 if (nosiblings) {
721 break;
722 }
723 }
724
725 /* reset all cached positions */
726 LY_ARRAY_FOR(userord, u) {
727 userord[u].pos = 0;
728 }
729
730 /* compare second tree to the first tree - create, user-ordered move */
731 LY_LIST_FOR(second, iter_second) {
Michal Vaskoc825ed72021-07-21 16:05:59 +0200732 if (!iter_second->schema) {
733 continue;
734 }
735
Michal Vaskod59035b2020-07-08 12:00:06 +0200736 assert(!(iter_second->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200737 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200738 /* skip default nodes */
739 continue;
740 }
741
Michal Vaskoe78faec2021-04-08 17:24:43 +0200742 /* find a match in the first tree */
743 LY_CHECK_GOTO(ret = lyd_diff_find_match(first, iter_second, options & LYD_DIFF_DEFAULTS, &dup_inst_first,
744 &match_first), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200745
746 if (lysc_is_userordered(iter_second->schema)) {
747 /* get all the attributes */
748 ret = lyd_diff_userord_attrs(match_first, iter_second, options, &userord, &op, &orig_default,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200749 &value, &orig_value, &key, &orig_key, &position, &orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200750
751 /* add into diff if there are any changes */
752 if (!ret) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200753 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, position, orig_key,
754 orig_position, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200755
756 free(orig_value);
757 free(key);
758 free(value);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200759 free(position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200760 free(orig_key);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200761 free(orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200762 LY_CHECK_GOTO(ret, cleanup);
763 } else if (ret == LY_ENOT) {
764 ret = LY_SUCCESS;
765 } else {
766 goto cleanup;
767 }
768 } else if (!match_first) {
769 /* get all the attributes */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200770 LY_CHECK_GOTO(ret = lyd_diff_attrs(match_first, iter_second, options, &op, &orig_default, &orig_value), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200771
772 /* there must be changes, it is created */
773 assert(op == LYD_DIFF_OP_CREATE);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200774 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200775
776 free(orig_value);
777 LY_CHECK_GOTO(ret, cleanup);
778 } /* else was handled */
779
780 if (nosiblings) {
781 break;
782 }
783 }
784
785cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +0200786 lyd_dup_inst_free(dup_inst_first);
787 lyd_dup_inst_free(dup_inst_second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200788 LY_ARRAY_FOR(userord, u) {
789 LY_ARRAY_FREE(userord[u].inst);
790 }
791 LY_ARRAY_FREE(userord);
792 return ret;
793}
794
Michal Vasko3a41dff2020-07-15 14:30:28 +0200795static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200796lyd_diff(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings, struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200797{
798 const struct ly_ctx *ctx;
799
800 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
801
802 if (first) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200803 ctx = LYD_CTX(first);
Michal Vaskod59035b2020-07-08 12:00:06 +0200804 } else if (second) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200805 ctx = LYD_CTX(second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200806 } else {
807 ctx = NULL;
808 }
809
810 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
811 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
812 return LY_EINVAL;
813 }
814
815 *diff = NULL;
816
Michal Vasko3a41dff2020-07-15 14:30:28 +0200817 return lyd_diff_siblings_r(first, second, options, nosiblings, diff);
818}
819
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100820LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200821lyd_diff_tree(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff)
Michal Vasko3a41dff2020-07-15 14:30:28 +0200822{
823 return lyd_diff(first, second, options, 1, diff);
824}
825
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100826LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200827lyd_diff_siblings(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff)
Michal Vasko3a41dff2020-07-15 14:30:28 +0200828{
829 return lyd_diff(first, second, options, 0, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200830}
831
832/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200833 * @brief Learn operation of a diff node.
834 *
835 * @param[in] diff_node Diff node.
836 * @param[out] op Operation.
Michal Vaskod59035b2020-07-08 12:00:06 +0200837 * @return LY_ERR value.
838 */
839static LY_ERR
Michal Vaskoe6323f62020-07-09 15:49:02 +0200840lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op)
Michal Vaskod59035b2020-07-08 12:00:06 +0200841{
842 struct lyd_meta *meta = NULL;
843 const struct lyd_node *diff_parent;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200844 const char *str;
Michal Vasko10765282022-01-18 14:08:34 +0100845 char *path;
Michal Vaskod59035b2020-07-08 12:00:06 +0200846
Michal Vasko9e685082021-01-29 14:49:09 +0100847 for (diff_parent = diff_node; diff_parent; diff_parent = lyd_parent(diff_parent)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200848 LY_LIST_FOR(diff_parent->meta, meta) {
849 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200850 str = lyd_get_meta_value(meta);
Michal Vaskod59035b2020-07-08 12:00:06 +0200851 if ((str[0] == 'r') && (diff_parent != diff_node)) {
852 /* we do not care about this operation if it's in our parent */
853 continue;
854 }
Michal Vaskoe6323f62020-07-09 15:49:02 +0200855 *op = lyd_diff_str2op(str);
Michal Vaskod59035b2020-07-08 12:00:06 +0200856 break;
857 }
858 }
859 if (meta) {
860 break;
861 }
862 }
Michal Vasko10765282022-01-18 14:08:34 +0100863
864 if (!meta) {
865 path = lyd_path(diff_node, LYD_PATH_STD, NULL, 0);
866 LOGERR(LYD_CTX(diff_node), LY_EINVAL, "Node \"%s\" without an operation.", path);
867 free(path);
868 return LY_EINT;
869 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200870
Michal Vaskod59035b2020-07-08 12:00:06 +0200871 return LY_SUCCESS;
872}
873
874/**
875 * @brief Insert a diff node into a data tree.
876 *
877 * @param[in,out] first_node First sibling of the data tree.
878 * @param[in] parent_node Data tree sibling parent node.
879 * @param[in] new_node Node to insert.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200880 * @param[in] userord_anchor Optional anchor (key, value, or position) of relative (leaf-)list instance. If not set,
881 * the user-ordered instance will be inserted at the first position.
Michal Vaskod59035b2020-07-08 12:00:06 +0200882 * @return err_info, NULL on success.
883 */
884static LY_ERR
885lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200886 const char *userord_anchor)
Michal Vaskod59035b2020-07-08 12:00:06 +0200887{
888 LY_ERR ret;
889 struct lyd_node *anchor;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200890 uint32_t pos, anchor_pos;
891 int found;
Michal Vaskod59035b2020-07-08 12:00:06 +0200892
893 assert(new_node);
894
895 if (!*first_node) {
896 if (!parent_node) {
897 /* no parent or siblings */
898 *first_node = new_node;
899 return LY_SUCCESS;
900 }
901
902 /* simply insert into parent, no other children */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200903 if (userord_anchor) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200904 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
Michal Vasko69730152020-10-09 16:30:07 +0200905 new_node->schema->name);
Michal Vaskod59035b2020-07-08 12:00:06 +0200906 return LY_EINVAL;
907 }
Michal Vaskob104f112020-07-17 09:54:54 +0200908 return lyd_insert_child(parent_node, new_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200909 }
910
Michal Vasko9e685082021-01-29 14:49:09 +0100911 assert(!(*first_node)->parent || (lyd_parent(*first_node) == parent_node));
Michal Vaskod59035b2020-07-08 12:00:06 +0200912
Michal Vaskod59035b2020-07-08 12:00:06 +0200913 if (!lysc_is_userordered(new_node->schema)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200914 /* simple insert */
915 return lyd_insert_sibling(*first_node, new_node, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200916 }
917
Michal Vaskoe78faec2021-04-08 17:24:43 +0200918 if (userord_anchor) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200919 /* find the anchor sibling */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200920 if (lysc_is_dup_inst_list(new_node->schema)) {
921 anchor_pos = atoi(userord_anchor);
Michal Vaskoca6832d2022-01-18 16:35:41 +0100922 if (!anchor_pos) {
923 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Invalid user-ordered anchor value \"%s\".", userord_anchor);
924 return LY_EINVAL;
925 }
Michal Vaskoe78faec2021-04-08 17:24:43 +0200926
927 found = 0;
928 pos = 1;
929 LYD_LIST_FOR_INST(*first_node, new_node->schema, anchor) {
930 if (pos == anchor_pos) {
931 found = 1;
932 break;
933 }
934 ++pos;
935 }
936 if (!found) {
937 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
938 new_node->schema->name);
939 return LY_EINVAL;
940 }
941 } else {
942 ret = lyd_find_sibling_val(*first_node, new_node->schema, userord_anchor, 0, &anchor);
943 if (ret == LY_ENOTFOUND) {
944 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
945 new_node->schema->name);
946 return LY_EINVAL;
947 } else if (ret) {
948 return ret;
949 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200950 }
951
952 /* insert after */
953 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
954 assert(new_node->prev == anchor);
955 if (*first_node == new_node) {
956 *first_node = anchor;
957 }
958 } else {
959 if ((*first_node)->schema->flags & LYS_KEY) {
960 assert(parent_node && (parent_node->schema->nodetype == LYS_LIST));
961
962 /* find last key */
963 anchor = *first_node;
964 while (anchor->next && (anchor->next->schema->flags & LYS_KEY)) {
965 anchor = anchor->next;
966 }
967 /* insert after the last key */
968 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
969 } else {
970 /* insert at the beginning */
971 LY_CHECK_RET(lyd_insert_before(*first_node, new_node));
972 *first_node = new_node;
973 }
974 }
975
976 return LY_SUCCESS;
977}
978
979/**
980 * @brief Apply diff subtree on data tree nodes, recursively.
981 *
982 * @param[in,out] first_node First sibling of the data tree.
983 * @param[in] parent_node Parent of the first sibling.
984 * @param[in] diff_node Current diff node.
Michal Vaskoe6323f62020-07-09 15:49:02 +0200985 * @param[in] diff_cb Optional diff callback.
986 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200987 * @param[in,out] dup_inst Duplicate instance cache for all @p diff_node siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +0200988 * @return LY_ERR value.
989 */
990static LY_ERR
991lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node,
Michal Vaskod7c048c2021-05-18 16:12:55 +0200992 lyd_diff_cb diff_cb, void *cb_data, struct lyd_dup_inst **dup_inst)
Michal Vaskod59035b2020-07-08 12:00:06 +0200993{
994 LY_ERR ret;
995 struct lyd_node *match, *diff_child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200996 const char *str_val, *meta_str;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200997 enum lyd_diff_op op;
998 struct lyd_meta *meta;
Michal Vaskod7c048c2021-05-18 16:12:55 +0200999 struct lyd_dup_inst *child_dup_inst = NULL;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001000 const struct ly_ctx *ctx = LYD_CTX(diff_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001001
1002 /* read all the valid attributes */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001003 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op));
Michal Vaskod59035b2020-07-08 12:00:06 +02001004
Michal Vaskoe6323f62020-07-09 15:49:02 +02001005 /* handle specific user-ordered (leaf-)lists operations separately */
1006 if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) {
1007 if (op == LYD_DIFF_OP_REPLACE) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001008 /* find the node (we must have some siblings because the node was only moved) */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001009 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko10765282022-01-18 14:08:34 +01001010 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001011 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001012 /* duplicate the node */
1013 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001014 }
1015
Michal Vaskoe78faec2021-04-08 17:24:43 +02001016 /* get "key", "value", or "position" metadata string value */
1017 if (lysc_is_dup_inst_list(diff_node->schema)) {
1018 meta_str = "yang:position";
1019 } else if (diff_node->schema->nodetype == LYS_LIST) {
1020 meta_str = "yang:key";
1021 } else {
1022 meta_str = "yang:value";
1023 }
1024 meta = lyd_find_meta(diff_node->meta, NULL, meta_str);
Michal Vasko10765282022-01-18 14:08:34 +01001025 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_str, diff_node), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001026 str_val = lyd_get_meta_value(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001027
Michal Vaskod59035b2020-07-08 12:00:06 +02001028 /* insert/move the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001029 if (str_val[0]) {
1030 ret = lyd_diff_insert(first_node, parent_node, match, str_val);
Michal Vaskod59035b2020-07-08 12:00:06 +02001031 } else {
1032 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
1033 }
1034 if (ret) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001035 if (op == LYD_DIFF_OP_CREATE) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001036 lyd_free_tree(match);
1037 }
1038 return ret;
1039 }
1040
1041 goto next_iter_r;
1042 }
1043
1044 /* apply operation */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001045 switch (op) {
1046 case LYD_DIFF_OP_NONE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001047 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001048 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko10765282022-01-18 14:08:34 +01001049 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001050
1051 if (match->schema->nodetype & LYD_NODE_TERM) {
1052 /* special case of only dflt flag change */
1053 if (diff_node->flags & LYD_DEFAULT) {
1054 match->flags |= LYD_DEFAULT;
1055 } else {
1056 match->flags &= ~LYD_DEFAULT;
1057 }
1058 } else {
1059 /* none operation on nodes without children is redundant and hence forbidden */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001060 if (!lyd_child_no_keys(diff_node)) {
Michal Vaskoca6832d2022-01-18 16:35:41 +01001061 LOGERR(ctx, LY_EINVAL, "Operation \"none\" is invalid for node \"%s\" without children.",
1062 LYD_NAME(diff_node));
1063 return LY_EINVAL;
Michal Vaskod59035b2020-07-08 12:00:06 +02001064 }
1065 }
1066 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001067 case LYD_DIFF_OP_CREATE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001068 /* duplicate the node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001069 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001070
1071 /* insert it at the end */
1072 ret = 0;
Michal Vaskob104f112020-07-17 09:54:54 +02001073 if (parent_node) {
1074 ret = lyd_insert_child(parent_node, match);
Michal Vaskod59035b2020-07-08 12:00:06 +02001075 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001076 ret = lyd_insert_sibling(*first_node, match, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001077 }
1078 if (ret) {
1079 lyd_free_tree(match);
1080 return ret;
1081 }
1082
1083 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001084 case LYD_DIFF_OP_DELETE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001085 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001086 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko10765282022-01-18 14:08:34 +01001087 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001088
1089 /* remove it */
1090 if ((match == *first_node) && !match->parent) {
1091 assert(!parent_node);
1092 /* we have removed the top-level node */
1093 *first_node = (*first_node)->next;
1094 }
1095 lyd_free_tree(match);
1096
1097 /* we are not going recursively in this case, the whole subtree was already deleted */
1098 return LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001099 case LYD_DIFF_OP_REPLACE:
Michal Vaskoca6832d2022-01-18 16:35:41 +01001100 if (!(diff_node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA))) {
1101 LOGERR(ctx, LY_EINVAL, "Operation \"replace\" is invalid for %s node \"%s\".",
1102 lys_nodetype2str(diff_node->schema->nodetype), LYD_NAME(diff_node));
1103 return LY_EINVAL;
1104 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001105
1106 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001107 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko10765282022-01-18 14:08:34 +01001108 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001109
Michal Vaskobaba84e2021-02-05 16:33:30 +01001110 /* update the value */
1111 if (diff_node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001112 ret = lyd_change_term(match, lyd_get_value(diff_node));
Michal Vasko10765282022-01-18 14:08:34 +01001113 LY_CHECK_ERR_RET(ret && (ret != LY_EEXIST), LOGERR_UNEXPVAL(ctx, match, "data"), LY_EINVAL);
Michal Vaskobaba84e2021-02-05 16:33:30 +01001114 } else {
1115 struct lyd_node_any *any = (struct lyd_node_any *)diff_node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001116 LY_CHECK_RET(lyd_any_copy_value(match, &any->value, any->value_type));
Michal Vaskod59035b2020-07-08 12:00:06 +02001117 }
1118
1119 /* with flags */
1120 match->flags = diff_node->flags;
1121 break;
1122 default:
1123 LOGINT_RET(ctx);
1124 }
1125
1126next_iter_r:
1127 if (diff_cb) {
1128 /* call callback */
1129 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
1130 }
1131
1132 /* apply diff recursively */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001133 ret = LY_SUCCESS;
Radek Krejcia1c1e542020-09-29 16:06:52 +02001134 LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001135 ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst);
1136 if (ret) {
1137 break;
1138 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001139 }
1140
Michal Vaskod7c048c2021-05-18 16:12:55 +02001141 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001142 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001143}
1144
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001145LIBYANG_API_DEF LY_ERR
Michal Vaskod59035b2020-07-08 12:00:06 +02001146lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +02001147 lyd_diff_cb diff_cb, void *cb_data)
Michal Vaskod59035b2020-07-08 12:00:06 +02001148{
1149 const struct lyd_node *root;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001150 struct lyd_dup_inst *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001151 LY_ERR ret = LY_SUCCESS;
Michal Vaskod59035b2020-07-08 12:00:06 +02001152
1153 LY_LIST_FOR(diff, root) {
1154 if (mod && (lyd_owner_module(root) != mod)) {
1155 /* skip data nodes from different modules */
1156 continue;
1157 }
1158
1159 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001160 ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst);
1161 if (ret) {
1162 break;
1163 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001164 }
1165
Michal Vaskod7c048c2021-05-18 16:12:55 +02001166 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001167 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001168}
1169
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001170LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001171lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff)
Michal Vaskod59035b2020-07-08 12:00:06 +02001172{
1173 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
1174}
Michal Vaskoe6323f62020-07-09 15:49:02 +02001175
1176/**
1177 * @brief Update operations on a diff node when the new operation is NONE.
1178 *
1179 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001180 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001181 * @param[in] src_diff Current source diff node.
1182 * @return LY_ERR value.
1183 */
1184static LY_ERR
1185lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1186{
1187 switch (cur_op) {
1188 case LYD_DIFF_OP_NONE:
1189 case LYD_DIFF_OP_CREATE:
1190 case LYD_DIFF_OP_REPLACE:
1191 if (src_diff->schema->nodetype & LYD_NODE_TERM) {
1192 /* NONE on a term means only its dflt flag was changed */
1193 diff_match->flags &= ~LYD_DEFAULT;
1194 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1195 }
1196 break;
1197 default:
1198 /* delete operation is not valid */
Michal Vasko10765282022-01-18 14:08:34 +01001199 LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_NONE);
1200 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001201 }
1202
1203 return LY_SUCCESS;
1204}
1205
1206/**
1207 * @brief Remove an attribute from a node.
1208 *
1209 * @param[in] node Node with the metadata.
1210 * @param[in] name Metadata name.
1211 */
1212static void
1213lyd_diff_del_meta(struct lyd_node *node, const char *name)
1214{
1215 struct lyd_meta *meta;
1216
1217 LY_LIST_FOR(node->meta, meta) {
1218 if (!strcmp(meta->name, name) && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001219 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001220 return;
1221 }
1222 }
1223
1224 assert(0);
1225}
1226
1227/**
1228 * @brief Set a specific operation of a node. Delete the previous operation, if any.
Michal Vasko871a0252020-11-11 18:35:24 +01001229 * Does not change the default flag.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001230 *
1231 * @param[in] node Node to change.
1232 * @param[in] op Operation to set.
1233 * @return LY_ERR value.
1234 */
1235static LY_ERR
1236lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op)
1237{
1238 struct lyd_meta *meta;
1239
1240 LY_LIST_FOR(node->meta, meta) {
1241 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001242 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001243 break;
1244 }
1245 }
1246
Michal Vasko871a0252020-11-11 18:35:24 +01001247 return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001248}
1249
1250/**
1251 * @brief Update operations on a diff node when the new operation is REPLACE.
1252 *
1253 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001254 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001255 * @param[in] src_diff Current source diff node.
1256 * @return LY_ERR value.
1257 */
1258static LY_ERR
1259lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1260{
1261 LY_ERR ret;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001262 const char *str_val, *meta_name, *orig_meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001263 struct lyd_meta *meta;
1264 const struct lys_module *mod;
1265 const struct lyd_node_any *any;
Michal Vasko10765282022-01-18 14:08:34 +01001266 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001267
1268 /* get "yang" module for the metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001269 mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001270 assert(mod);
1271
1272 switch (cur_op) {
1273 case LYD_DIFF_OP_REPLACE:
1274 case LYD_DIFF_OP_CREATE:
1275 switch (diff_match->schema->nodetype) {
1276 case LYS_LIST:
1277 case LYS_LEAFLIST:
Michal Vasko4231fb62020-07-13 13:54:47 +02001278 /* it was created/moved somewhere, but now it will be created/moved somewhere else,
Michal Vaskoe6323f62020-07-09 15:49:02 +02001279 * keep orig_key/orig_value (only replace oper) and replace key/value */
1280 assert(lysc_is_userordered(diff_match->schema));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001281 if (lysc_is_dup_inst_list(diff_match->schema)) {
1282 meta_name = "position";
1283 } else if (diff_match->schema->nodetype == LYS_LIST) {
1284 meta_name = "key";
1285 } else {
1286 meta_name = "value";
1287 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001288
1289 lyd_diff_del_meta(diff_match, meta_name);
1290 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vasko10765282022-01-18 14:08:34 +01001291 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001292 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001293 break;
1294 case LYS_LEAF:
1295 /* replaced with the exact same value, impossible */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001296 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vasko10765282022-01-18 14:08:34 +01001297 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1298 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001299 }
1300
Michal Vaskoe6323f62020-07-09 15:49:02 +02001301 /* modify the node value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001302 if (lyd_change_term(diff_match, lyd_get_value(src_diff))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001303 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001304 }
1305
Michal Vasko8caadab2020-11-05 17:38:15 +01001306 if (cur_op == LYD_DIFF_OP_REPLACE) {
1307 /* compare values whether there is any change at all */
1308 meta = lyd_find_meta(diff_match->meta, mod, "orig-value");
Michal Vasko10765282022-01-18 14:08:34 +01001309 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "orig-value", diff_match), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001310 str_val = lyd_get_meta_value(meta);
Michal Vasko8caadab2020-11-05 17:38:15 +01001311 ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val));
1312 if (!ret) {
1313 /* values are the same, remove orig-value meta and set oper to NONE */
1314 lyd_free_meta_single(meta);
1315 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1316 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001317 }
1318
1319 /* modify the default flag */
1320 diff_match->flags &= ~LYD_DEFAULT;
1321 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1322 break;
1323 case LYS_ANYXML:
1324 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +02001325 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vasko10765282022-01-18 14:08:34 +01001326 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1327 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001328 }
1329
1330 /* modify the node value */
1331 any = (struct lyd_node_any *)src_diff;
1332 LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type));
1333 break;
1334 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001335 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001336 }
1337 break;
1338 case LYD_DIFF_OP_NONE:
1339 /* it is moved now */
1340 assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST));
1341
1342 /* change the operation */
1343 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1344
Michal Vaskoe78faec2021-04-08 17:24:43 +02001345 /* set orig-meta and meta */
1346 if (lysc_is_dup_inst_list(diff_match->schema)) {
1347 meta_name = "position";
1348 orig_meta_name = "orig-position";
1349 } else {
1350 meta_name = "key";
1351 orig_meta_name = "orig-key";
1352 }
1353
1354 meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name);
Michal Vasko10765282022-01-18 14:08:34 +01001355 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, orig_meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001356 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001357
Michal Vaskoe78faec2021-04-08 17:24:43 +02001358 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vasko10765282022-01-18 14:08:34 +01001359 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001360 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001361 break;
1362 default:
1363 /* delete operation is not valid */
Michal Vasko10765282022-01-18 14:08:34 +01001364 LOGERR_MERGEOP(ctx, diff_match, cur_op, LYD_DIFF_OP_REPLACE);
1365 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001366 }
1367
1368 return LY_SUCCESS;
1369}
1370
1371/**
1372 * @brief Update operations in a diff node when the new operation is CREATE.
1373 *
1374 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001375 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001376 * @param[in] src_diff Current source diff node.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001377 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001378 * @return LY_ERR value.
1379 */
1380static LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001381lyd_diff_merge_create(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001382{
1383 struct lyd_node *child;
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001384 const struct lysc_node_leaf *sleaf = NULL;
Michal Vasko871a0252020-11-11 18:35:24 +01001385 uint32_t trg_flags;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001386 const char *meta_name, *orig_meta_name;
1387 struct lyd_meta *meta, *orig_meta;
Michal Vasko10765282022-01-18 14:08:34 +01001388 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001389
1390 switch (cur_op) {
1391 case LYD_DIFF_OP_DELETE:
Michal Vasko871a0252020-11-11 18:35:24 +01001392 /* remember current flags */
1393 trg_flags = diff_match->flags;
1394
Michal Vaskoe78faec2021-04-08 17:24:43 +02001395 if (lysc_is_userordered(diff_match->schema)) {
1396 /* get anchor metadata */
1397 if (lysc_is_dup_inst_list(diff_match->schema)) {
1398 meta_name = "yang:position";
1399 orig_meta_name = "yang:orig-position";
1400 } else if (diff_match->schema->nodetype == LYS_LIST) {
1401 meta_name = "yang:key";
1402 orig_meta_name = "yang:orig-key";
1403 } else {
1404 meta_name = "yang:value";
1405 orig_meta_name = "yang:orig-value";
1406 }
1407 meta = lyd_find_meta(src_diff->meta, NULL, meta_name);
Michal Vasko10765282022-01-18 14:08:34 +01001408 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001409 orig_meta = lyd_find_meta(diff_match->meta, NULL, orig_meta_name);
Michal Vasko10765282022-01-18 14:08:34 +01001410 LY_CHECK_ERR_RET(!orig_meta, LOGERR_META(ctx, orig_meta_name, diff_match), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001411
1412 /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect
1413 * the anchors stored in the metadata */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001414 if (strcmp(lyd_get_meta_value(meta), lyd_get_meta_value(orig_meta))) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001415 /* deleted + created at another position -> operation REPLACE */
1416 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1417
1418 /* add anchor metadata */
1419 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
1420 } else {
1421 /* deleted + created at the same position -> operation NONE */
1422 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1423
1424 /* delete anchor metadata */
1425 lyd_free_meta_single(orig_meta);
1426 }
1427 } else if (diff_match->schema->nodetype == LYS_LEAF) {
1428 if (options & LYD_DIFF_MERGE_DEFAULTS) {
1429 /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */
1430 sleaf = (struct lysc_node_leaf *)diff_match->schema;
1431 }
1432
Radek Krejci55c4bd22021-04-26 08:09:04 +02001433 if (sleaf && sleaf->dflt && !sleaf->dflt->realtype->plugin->compare(sleaf->dflt,
1434 &((struct lyd_node_term *)src_diff)->value)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001435 /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */
1436 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1437 } else if (!lyd_compare_single(diff_match, src_diff, 0)) {
1438 /* deleted + created -> operation NONE */
1439 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1440 } else {
1441 /* we deleted it, but it was created with a different value -> operation REPLACE */
1442 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1443
1444 /* current value is the previous one (meta) */
1445 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-value",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001446 lyd_get_value(diff_match), 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001447
1448 /* update the value itself */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001449 LY_CHECK_RET(lyd_change_term(diff_match, lyd_get_value(src_diff)));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001450 }
1451 } else {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001452 /* deleted + created -> operation NONE */
1453 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001454 }
1455
1456 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
Michal Vasko4b715ca2020-11-11 18:39:57 +01001457 /* add orig-dflt metadata */
1458 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1459 trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
1460
Michal Vaskoe6323f62020-07-09 15:49:02 +02001461 /* update dflt flag itself */
1462 diff_match->flags &= ~LYD_DEFAULT;
1463 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001464 }
1465
1466 /* but the operation of its children should remain DELETE */
1467 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
1468 LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001469 }
1470 break;
1471 default:
1472 /* create and replace operations are not valid */
Michal Vasko10765282022-01-18 14:08:34 +01001473 LOGERR_MERGEOP(LYD_CTX(src_diff), diff_match, cur_op, LYD_DIFF_OP_CREATE);
1474 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001475 }
1476
1477 return LY_SUCCESS;
1478}
1479
1480/**
1481 * @brief Update operations on a diff node when the new operation is DELETE.
1482 *
1483 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001484 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001485 * @param[in] src_diff Current source diff node.
1486 * @return LY_ERR value.
1487 */
1488static LY_ERR
1489lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1490{
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001491 struct lyd_node *child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001492 struct lyd_meta *meta;
1493 const char *meta_name;
Michal Vasko10765282022-01-18 14:08:34 +01001494 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001495
1496 /* we can delete only exact existing nodes */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001497 LY_CHECK_ERR_RET(lyd_compare_single(diff_match, src_diff, 0), LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001498
1499 switch (cur_op) {
1500 case LYD_DIFF_OP_CREATE:
1501 /* it was created, but then deleted -> set NONE operation */
1502 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1503
1504 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
1505 /* add orig-default meta because it is expected */
Michal Vasko871a0252020-11-11 18:35:24 +01001506 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1507 diff_match->flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001508 } else if (!lysc_is_dup_inst_list(diff_match->schema)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001509 /* keep operation for all descendants (for now) */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001510 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001511 LY_CHECK_RET(lyd_diff_change_op(child, cur_op));
1512 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001513 } /* else key-less list, for which all the descendants act as keys */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001514 break;
1515 case LYD_DIFF_OP_REPLACE:
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001516 /* remove the redundant metadata */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001517 if (lysc_is_userordered(diff_match->schema)) {
1518 if (lysc_is_dup_inst_list(diff_match->schema)) {
1519 meta_name = "position";
1520 } else if (diff_match->schema->nodetype == LYS_LIST) {
1521 meta_name = "key";
1522 } else {
1523 meta_name = "value";
1524 }
1525 } else {
1526 assert(diff_match->schema->nodetype == LYS_LEAF);
1527
1528 /* switch value for the original one */
1529 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value");
Michal Vasko10765282022-01-18 14:08:34 +01001530 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-value", diff_match), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001531 if (lyd_change_term(diff_match, lyd_get_meta_value(meta))) {
Michal Vasko10765282022-01-18 14:08:34 +01001532 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1533 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001534 }
1535
1536 /* switch default for the original one, then remove the meta */
1537 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default");
Michal Vasko10765282022-01-18 14:08:34 +01001538 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-default", diff_match), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001539 diff_match->flags &= ~LYD_DEFAULT;
1540 if (meta->value.boolean) {
1541 diff_match->flags |= LYD_DEFAULT;
1542 }
1543 lyd_free_meta_single(meta);
1544
1545 meta_name = "orig-value";
1546 }
1547 lyd_diff_del_meta(diff_match, meta_name);
1548
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001549 /* it was being changed, but should be deleted instead -> set DELETE operation */
1550 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
1551 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001552 case LYD_DIFF_OP_NONE:
1553 /* it was not modified, but should be deleted -> set DELETE operation */
1554 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001555 break;
1556 default:
1557 /* delete operation is not valid */
Michal Vasko10765282022-01-18 14:08:34 +01001558 LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_DELETE);
1559 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001560 }
1561
1562 return LY_SUCCESS;
1563}
1564
1565/**
1566 * @brief Check whether this diff node is redundant (does not change data).
1567 *
1568 * @param[in] diff Diff node.
1569 * @return 0 if not, non-zero if it is.
1570 */
1571static int
1572lyd_diff_is_redundant(struct lyd_node *diff)
1573{
1574 enum lyd_diff_op op;
1575 struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL;
1576 struct lyd_node *child;
1577 const struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001578 const char *str, *orig_meta_name, *meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001579
1580 assert(diff);
1581
Michal Vaskoe78faec2021-04-08 17:24:43 +02001582 if (lysc_is_dup_inst_list(diff->schema)) {
1583 /* all descendants are keys */
1584 child = NULL;
1585 } else {
1586 child = lyd_child_no_keys(diff);
1587 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02001588 mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001589 assert(mod);
1590
1591 /* get node operation */
Michal Vasko53bf6f22020-07-14 08:23:40 +02001592 LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001593
1594 if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001595 /* get metadata names */
1596 if (lysc_is_dup_inst_list(diff->schema)) {
1597 meta_name = "position";
1598 orig_meta_name = "orig-position";
1599 } else if (diff->schema->nodetype == LYS_LIST) {
1600 meta_name = "key";
1601 orig_meta_name = "orig-key";
1602 } else {
1603 meta_name = "value";
1604 orig_meta_name = "orig-value";
1605 }
1606
Michal Vaskoe6323f62020-07-09 15:49:02 +02001607 /* check for redundant move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001608 orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name);
1609 val_meta = lyd_find_meta(diff->meta, mod, meta_name);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001610 assert(orig_val_meta && val_meta);
1611
1612 if (!lyd_compare_meta(orig_val_meta, val_meta)) {
1613 /* there is actually no move */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001614 lyd_free_meta_single(orig_val_meta);
1615 lyd_free_meta_single(val_meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001616 if (child) {
1617 /* change operation to NONE, we have siblings */
1618 lyd_diff_change_op(diff, LYD_DIFF_OP_NONE);
1619 return 0;
1620 }
1621
1622 /* redundant node, BUT !!
1623 * In diff the move operation is always converted to be INSERT_AFTER, which is fine
1624 * because the data that this is applied on should not change for the diff lifetime.
1625 * However, when we are merging 2 diffs, this conversion is actually lossy because
1626 * if the data change, the move operation can also change its meaning. In this specific
1627 * case the move operation will be lost. But it can be considered a feature, it is not supported.
1628 */
1629 return 1;
1630 }
1631 } else if ((op == LYD_DIFF_OP_NONE) && (diff->schema->nodetype & LYD_NODE_TERM)) {
1632 /* check whether at least the default flags are different */
1633 meta = lyd_find_meta(diff->meta, mod, "orig-default");
1634 assert(meta);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001635 str = lyd_get_meta_value(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001636
1637 /* if previous and current dflt flags are the same, this node is redundant */
1638 if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) {
1639 return 1;
1640 }
1641 return 0;
1642 }
1643
1644 if (!child && (op == LYD_DIFF_OP_NONE)) {
1645 return 1;
1646 }
1647
1648 return 0;
1649}
1650
1651/**
Michal Vaskoe78faec2021-04-08 17:24:43 +02001652 * @brief Merge sysrepo diff subtree with another diff, recursively.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001653 *
1654 * @param[in] src_diff Source diff node.
1655 * @param[in] diff_parent Current sysrepo diff parent.
1656 * @param[in] diff_cb Optional diff callback.
1657 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001658 * @param[in,out] dup_inst Duplicate instance cache for all @p src_diff siblings.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001659 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001660 * @param[in,out] diff Diff root node.
1661 * @return LY_ERR value.
1662 */
1663static LY_ERR
1664lyd_diff_merge_r(const struct lyd_node *src_diff, struct lyd_node *diff_parent, lyd_diff_cb diff_cb, void *cb_data,
Michal Vaskod7c048c2021-05-18 16:12:55 +02001665 struct lyd_dup_inst **dup_inst, uint16_t options, struct lyd_node **diff)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001666{
1667 LY_ERR ret = LY_SUCCESS;
1668 struct lyd_node *child, *diff_node = NULL;
1669 enum lyd_diff_op src_op, cur_op;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001670 struct lyd_dup_inst *child_dup_inst = NULL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001671
1672 /* get source node operation */
1673 LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op));
1674
1675 /* find an equal node in the current diff */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001676 LY_CHECK_RET(lyd_diff_find_match(diff_parent ? lyd_child_no_keys(diff_parent) : *diff, src_diff, 1, dup_inst, &diff_node));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001677
1678 if (diff_node) {
1679 /* get target (current) operation */
1680 LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op));
1681
1682 /* merge operations */
1683 switch (src_op) {
1684 case LYD_DIFF_OP_REPLACE:
1685 ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff);
1686 break;
1687 case LYD_DIFF_OP_CREATE:
Michal Vasko1dc0a842021-02-04 11:04:57 +01001688 if ((cur_op == LYD_DIFF_OP_CREATE) && lysc_is_dup_inst_list(diff_node->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001689 /* special case of creating duplicate (leaf-)list instances */
Michal Vasko1dc0a842021-02-04 11:04:57 +01001690 goto add_diff;
1691 }
1692
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001693 ret = lyd_diff_merge_create(diff_node, cur_op, src_diff, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001694 break;
1695 case LYD_DIFF_OP_DELETE:
1696 ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff);
1697 break;
1698 case LYD_DIFF_OP_NONE:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001699 /* key-less list can never have "none" operation since all its descendants are acting as "keys" */
1700 assert((src_diff->schema->nodetype != LYS_LIST) || !lysc_is_dup_inst_list(src_diff->schema));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001701 ret = lyd_diff_merge_none(diff_node, cur_op, src_diff);
1702 break;
1703 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001704 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001705 }
1706 if (ret) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001707 LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001708 return ret;
1709 }
1710
1711 if (diff_cb) {
1712 /* call callback */
Michal Vaskobc5fba92020-08-07 12:14:39 +02001713 LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001714 }
1715
1716 /* update diff parent */
1717 diff_parent = diff_node;
1718
Michal Vaskoe78faec2021-04-08 17:24:43 +02001719 /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances),
1720 * so there is nothing to merge for these "keys" */
1721 if (!lysc_is_dup_inst_list(src_diff->schema)) {
1722 /* merge src_diff recursively */
1723 LY_LIST_FOR(lyd_child_no_keys(src_diff), child) {
1724 ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff);
1725 if (ret) {
1726 break;
1727 }
1728 }
Michal Vaskod7c048c2021-05-18 16:12:55 +02001729 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001730 LY_CHECK_RET(ret);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001731 }
1732 } else {
Michal Vasko1dc0a842021-02-04 11:04:57 +01001733add_diff:
Michal Vaskoe6323f62020-07-09 15:49:02 +02001734 /* add new diff node with all descendants */
Michal Vasko871a0252020-11-11 18:35:24 +01001735 LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS,
1736 &diff_node));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001737
1738 /* insert node into diff if not already */
1739 if (!diff_parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001740 lyd_insert_sibling(*diff, diff_node, diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001741 }
1742
1743 /* update operation */
1744 LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op));
1745
1746 if (diff_cb) {
Michal Vaskoe2af8412020-12-03 14:11:38 +01001747 /* call callback with no source diff node since it was duplicated and just added */
1748 LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001749 }
1750
1751 /* update diff parent */
1752 diff_parent = diff_node;
1753 }
1754
1755 /* remove any redundant nodes */
Michal Vaskob98d7082020-07-15 16:38:36 +02001756 if (lyd_diff_is_redundant(diff_parent)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001757 if (diff_parent == *diff) {
1758 *diff = (*diff)->next;
1759 }
1760 lyd_free_tree(diff_parent);
1761 }
1762
1763 return LY_SUCCESS;
1764}
1765
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001766LIBYANG_API_DEF LY_ERR
Michal Vaskofb737aa2020-08-06 13:53:53 +02001767lyd_diff_merge_module(struct lyd_node **diff, const struct lyd_node *src_diff, const struct lys_module *mod,
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001768 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001769{
1770 const struct lyd_node *src_root;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001771 struct lyd_dup_inst *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001772 LY_ERR ret = LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001773
1774 LY_LIST_FOR(src_diff, src_root) {
1775 if (mod && (lyd_owner_module(src_root) != mod)) {
1776 /* skip data nodes from different modules */
1777 continue;
1778 }
1779
1780 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001781 LY_CHECK_GOTO(ret = lyd_diff_merge_r(src_root, NULL, diff_cb, cb_data, &dup_inst, options, diff), cleanup);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001782 }
1783
Michal Vaskoe78faec2021-04-08 17:24:43 +02001784cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +02001785 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001786 return ret;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001787}
1788
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001789LIBYANG_API_DEF LY_ERR
Michal Vasko04f85912020-08-07 12:14:58 +02001790lyd_diff_merge_tree(struct lyd_node **diff_first, struct lyd_node *diff_parent, const struct lyd_node *src_sibling,
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001791 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vasko04f85912020-08-07 12:14:58 +02001792{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001793 LY_ERR ret;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001794 struct lyd_dup_inst *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001795
Michal Vasko04f85912020-08-07 12:14:58 +02001796 if (!src_sibling) {
1797 return LY_SUCCESS;
1798 }
1799
Michal Vaskoe78faec2021-04-08 17:24:43 +02001800 ret = lyd_diff_merge_r(src_sibling, diff_parent, diff_cb, cb_data, &dup_inst, options, diff_first);
Michal Vaskod7c048c2021-05-18 16:12:55 +02001801 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001802 return ret;
Michal Vasko04f85912020-08-07 12:14:58 +02001803}
1804
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001805LIBYANG_API_DEF LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001806lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001807{
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001808 return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001809}
Michal Vasko4231fb62020-07-13 13:54:47 +02001810
1811static LY_ERR
Michal Vaskobaba84e2021-02-05 16:33:30 +01001812lyd_diff_reverse_value(struct lyd_node *node, const struct lys_module *mod)
Michal Vasko4231fb62020-07-13 13:54:47 +02001813{
1814 LY_ERR ret = LY_SUCCESS;
1815 struct lyd_meta *meta;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001816 const char *val1 = NULL;
1817 char *val2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001818 uint32_t flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001819
Michal Vaskobaba84e2021-02-05 16:33:30 +01001820 assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA));
1821
1822 meta = lyd_find_meta(node->meta, mod, "orig-value");
Michal Vasko10765282022-01-18 14:08:34 +01001823 LY_CHECK_ERR_RET(!meta, LOGERR_META(LYD_CTX(node), "orig-value", node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02001824
1825 /* orig-value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001826 val1 = lyd_get_meta_value(meta);
Michal Vasko4231fb62020-07-13 13:54:47 +02001827
1828 /* current value */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001829 if (node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001830 val2 = strdup(lyd_get_value(node));
Michal Vaskobaba84e2021-02-05 16:33:30 +01001831 } else {
1832 LY_CHECK_RET(lyd_any_value_str(node, &val2));
1833 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001834
1835 /* switch values, keep default flag */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001836 flags = node->flags;
1837 if (node->schema->nodetype == LYS_LEAF) {
1838 LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup);
1839 } else {
1840 union lyd_any_value anyval = {.str = val1};
1841 LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup);
1842 }
1843 node->flags = flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001844 LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup);
1845
1846cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001847 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001848 return ret;
1849}
1850
1851static LY_ERR
1852lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod)
1853{
1854 struct lyd_meta *meta;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001855 uint32_t flag1, flag2;
Michal Vasko4231fb62020-07-13 13:54:47 +02001856
1857 meta = lyd_find_meta(node->meta, mod, "orig-default");
Michal Vasko610e93b2020-11-09 20:58:32 +01001858 LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001859
1860 /* orig-default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001861 if (meta->value.boolean) {
Michal Vasko4231fb62020-07-13 13:54:47 +02001862 flag1 = LYD_DEFAULT;
1863 } else {
1864 flag1 = 0;
1865 }
1866
1867 /* current default */
1868 flag2 = node->flags & LYD_DEFAULT;
1869
Michal Vasko610e93b2020-11-09 20:58:32 +01001870 if (flag1 == flag2) {
1871 /* no default state change so nothing to reverse */
1872 return LY_SUCCESS;
1873 }
1874
Michal Vasko4231fb62020-07-13 13:54:47 +02001875 /* switch defaults */
1876 node->flags &= ~LYD_DEFAULT;
1877 node->flags |= flag1;
1878 LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false"));
1879
1880 return LY_SUCCESS;
1881}
1882
1883static LY_ERR
1884lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2)
1885{
1886 LY_ERR ret = LY_SUCCESS;
1887 struct lyd_meta *meta1, *meta2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001888 const char *val1 = NULL;
1889 char *val2 = NULL;
Michal Vasko4231fb62020-07-13 13:54:47 +02001890
1891 meta1 = lyd_find_meta(node->meta, mod, name1);
Michal Vasko10765282022-01-18 14:08:34 +01001892 LY_CHECK_ERR_RET(!meta1, LOGERR_META(LYD_CTX(node), name1, node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02001893
1894 meta2 = lyd_find_meta(node->meta, mod, name2);
Michal Vasko10765282022-01-18 14:08:34 +01001895 LY_CHECK_ERR_RET(!meta2, LOGERR_META(LYD_CTX(node), name2, node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02001896
1897 /* value1 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001898 val1 = lyd_get_meta_value(meta1);
Michal Vasko4231fb62020-07-13 13:54:47 +02001899
1900 /* value2 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001901 val2 = strdup(lyd_get_meta_value(meta2));
Michal Vasko4231fb62020-07-13 13:54:47 +02001902
1903 /* switch values */
1904 LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup);
1905 LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup);
1906
1907cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001908 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001909 return ret;
1910}
1911
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001912/**
1913 * @brief Remove specific operation from all the nodes in a subtree.
1914 *
1915 * @param[in] diff Diff subtree to process.
1916 * @param[in] op Only expected operation.
1917 * @return LY_ERR value.
1918 */
1919static LY_ERR
1920lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op)
1921{
1922 struct lyd_node *elem;
1923 struct lyd_meta *meta;
1924
1925 LYD_TREE_DFS_BEGIN(diff, elem) {
1926 meta = lyd_find_meta(elem->meta, NULL, "yang:operation");
1927 if (meta) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001928 LY_CHECK_ERR_RET(lyd_diff_str2op(lyd_get_meta_value(meta)) != op, LOGINT(LYD_CTX(diff)), LY_EINT);
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001929 lyd_free_meta_single(meta);
1930 }
1931
1932 LYD_TREE_DFS_END(diff, elem);
1933 }
1934
1935 return LY_SUCCESS;
1936}
1937
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001938LIBYANG_API_DEF LY_ERR
Michal Vasko66535812020-08-11 08:44:22 +02001939lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff)
Michal Vasko4231fb62020-07-13 13:54:47 +02001940{
1941 LY_ERR ret = LY_SUCCESS;
1942 const struct lys_module *mod;
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001943 struct lyd_node *root, *elem, *iter;
Michal Vasko4231fb62020-07-13 13:54:47 +02001944 enum lyd_diff_op op;
1945
1946 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
1947
1948 if (!src_diff) {
1949 *diff = NULL;
1950 return LY_SUCCESS;
1951 }
1952
1953 /* duplicate diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001954 LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff));
Michal Vasko4231fb62020-07-13 13:54:47 +02001955
1956 /* find module with metadata needed for later */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001957 mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang");
1958 LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001959
1960 LY_LIST_FOR(*diff, root) {
Michal Vasko56daf732020-08-10 10:57:18 +02001961 LYD_TREE_DFS_BEGIN(root, elem) {
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001962 /* skip all keys */
1963 if (!lysc_is_key(elem->schema)) {
1964 /* find operation attribute, if any */
1965 LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001966
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001967 switch (op) {
1968 case LYD_DIFF_OP_CREATE:
1969 /* reverse create to delete */
1970 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01001971
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001972 /* check all the children for the same operation, nothing else is expected */
1973 LY_LIST_FOR(lyd_child(elem), iter) {
1974 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE);
1975 }
1976
Michal Vasko9e070522021-03-05 14:00:14 +01001977 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02001978 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001979 case LYD_DIFF_OP_DELETE:
1980 /* reverse delete to create */
1981 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01001982
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001983 /* check all the children for the same operation, nothing else is expected */
1984 LY_LIST_FOR(lyd_child(elem), iter) {
1985 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE);
1986 }
1987
Michal Vasko9e070522021-03-05 14:00:14 +01001988 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02001989 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001990 case LYD_DIFF_OP_REPLACE:
1991 switch (elem->schema->nodetype) {
1992 case LYS_LEAF:
1993 /* leaf value change */
1994 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
1995 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
1996 break;
Michal Vaskobaba84e2021-02-05 16:33:30 +01001997 case LYS_ANYXML:
1998 case LYS_ANYDATA:
1999 /* any value change */
2000 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
2001 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002002 case LYS_LEAFLIST:
2003 /* leaf-list move */
2004 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002005 if (lysc_is_dup_inst_list(elem->schema)) {
2006 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
2007 } else {
2008 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup);
2009 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002010 break;
2011 case LYS_LIST:
2012 /* list move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02002013 if (lysc_is_dup_inst_list(elem->schema)) {
2014 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
2015 } else {
2016 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup);
2017 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002018 break;
2019 default:
2020 LOGINT(LYD_CTX(src_diff));
2021 ret = LY_EINT;
2022 goto cleanup;
2023 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002024 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002025 case LYD_DIFF_OP_NONE:
2026 switch (elem->schema->nodetype) {
2027 case LYS_LEAF:
2028 case LYS_LEAFLIST:
2029 /* default flag change */
2030 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
2031 break;
2032 default:
2033 /* nothing to do */
2034 break;
2035 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002036 break;
2037 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002038 }
2039
Michal Vasko56daf732020-08-10 10:57:18 +02002040 LYD_TREE_DFS_END(root, elem);
Michal Vasko4231fb62020-07-13 13:54:47 +02002041 }
2042 }
2043
2044cleanup:
2045 if (ret) {
2046 lyd_free_siblings(*diff);
2047 *diff = NULL;
2048 }
2049 return ret;
2050}