blob: ab37b33530dfab56acc59ec6b8e61f9fda4e1fbf [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 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejcif8dc59a2020-11-25 13:47:44 +010014#define _POSIX_C_SOURCE 200809L /* strdup */
Michal Vaskod59035b2020-07-08 12:00:06 +020015
16#include "diff.h"
17
18#include <assert.h>
19#include <stddef.h>
Radek Krejci47fab892020-11-05 17:02:41 +010020#include <stdlib.h>
Michal Vaskod59035b2020-07-08 12:00:06 +020021#include <string.h>
22
23#include "common.h"
Radek Krejci47fab892020-11-05 17:02:41 +010024#include "context.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020025#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010026#include "plugins_types.h"
27#include "set.h"
28#include "tree.h"
29#include "tree_data.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020030#include "tree_data_internal.h"
31#include "tree_schema.h"
32#include "tree_schema_internal.h"
33
34static const char *
35lyd_diff_op2str(enum lyd_diff_op op)
36{
37 switch (op) {
38 case LYD_DIFF_OP_CREATE:
39 return "create";
40 case LYD_DIFF_OP_DELETE:
41 return "delete";
42 case LYD_DIFF_OP_REPLACE:
43 return "replace";
44 case LYD_DIFF_OP_NONE:
45 return "none";
46 }
47
48 LOGINT(NULL);
49 return NULL;
50}
51
Michal Vaskoe6323f62020-07-09 15:49:02 +020052static enum lyd_diff_op
53lyd_diff_str2op(const char *str)
54{
55 switch (str[0]) {
56 case 'c':
57 assert(!strcmp(str, "create"));
58 return LYD_DIFF_OP_CREATE;
59 case 'd':
60 assert(!strcmp(str, "delete"));
61 return LYD_DIFF_OP_DELETE;
62 case 'r':
63 assert(!strcmp(str, "replace"));
64 return LYD_DIFF_OP_REPLACE;
65 case 'n':
66 assert(!strcmp(str, "none"));
67 return LYD_DIFF_OP_NONE;
68 }
69
70 LOGINT(NULL);
71 return 0;
72}
73
Michal Vaskod59035b2020-07-08 12:00:06 +020074LY_ERR
75lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value,
Radek Krejci0f969882020-08-21 16:56:47 +020076 const char *key, const char *value, const char *orig_key, struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +020077{
78 struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL;
79 const struct lyd_node *parent = NULL;
80 const struct lys_module *yang_mod;
81
82 assert(diff);
83
Michal Vasko53d48422020-11-13 18:02:29 +010084 /* replace leaf always needs orig-default and orig-value */
85 assert((node->schema->nodetype != LYS_LEAF) || (op != LYD_DIFF_OP_REPLACE) || (orig_default && orig_value));
86
87 /* create on userord needs key/value */
88 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_CREATE) ||
89 key);
90 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
91 (op != LYD_DIFF_OP_CREATE) || value);
92
93 /* move on userord needs both key and orig-key/value and orig-value */
94 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_REPLACE) ||
95 (key && orig_key));
96 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
97 (op != LYD_DIFF_OP_REPLACE) || (value && orig_value));
98
Michal Vaskod59035b2020-07-08 12:00:06 +020099 /* find the first existing parent */
100 siblings = *diff;
101 while (1) {
102 /* find next node parent */
103 parent = node;
104 while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
105 parent = (struct lyd_node *)parent->parent;
106 }
107 if (parent == node) {
108 /* no more parents to find */
109 break;
110 }
111
112 /* check whether it exists in the diff */
113 if (lyd_find_sibling_first(siblings, parent, &match)) {
114 break;
115 }
116
117 /* another parent found */
118 diff_parent = match;
119
120 /* move down in the diff */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200121 siblings = lyd_child_no_keys(match);
Michal Vaskod59035b2020-07-08 12:00:06 +0200122 }
123
124 /* duplicate the subtree (and connect to the diff if possible) */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200125 LY_CHECK_RET(lyd_dup_single(node, (struct lyd_node_inner *)diff_parent,
Michal Vasko871a0252020-11-11 18:35:24 +0100126 LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS, &dup));
Michal Vaskod59035b2020-07-08 12:00:06 +0200127
128 /* find the first duplicated parent */
129 if (!diff_parent) {
130 diff_parent = (struct lyd_node *)dup->parent;
131 while (diff_parent && diff_parent->parent) {
132 diff_parent = (struct lyd_node *)diff_parent->parent;
133 }
134 } else {
135 diff_parent = (struct lyd_node *)dup;
136 while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
137 diff_parent = (struct lyd_node *)diff_parent->parent;
138 }
139 }
140
141 /* no parent existed, must be manually connected */
142 if (!diff_parent) {
143 /* there actually was no parent to duplicate */
Michal Vaskob104f112020-07-17 09:54:54 +0200144 lyd_insert_sibling(*diff, dup, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200145 } else if (!diff_parent->parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200146 lyd_insert_sibling(*diff, diff_parent, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200147 }
148
149 /* get module with the operation metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200150 yang_mod = LYD_CTX(node)->list.objs[1];
Michal Vaskod59035b2020-07-08 12:00:06 +0200151 assert(!strcmp(yang_mod->name, "yang"));
152
153 /* add parent operation, if any */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200154 if (diff_parent && (diff_parent != dup)) {
Michal Vasko871a0252020-11-11 18:35:24 +0100155 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), diff_parent, yang_mod, "operation", "none", 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200156 }
157
158 /* add subtree operation */
Michal Vasko871a0252020-11-11 18:35:24 +0100159 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 +0200160
161 /* orig-default */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200162 if (orig_default) {
Michal Vasko871a0252020-11-11 18:35:24 +0100163 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 +0200164 }
165
166 /* orig-value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200167 if (orig_value) {
Michal Vasko871a0252020-11-11 18:35:24 +0100168 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 +0200169 }
170
171 /* key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200172 if (key) {
Michal Vasko871a0252020-11-11 18:35:24 +0100173 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "key", key, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200174 }
175
176 /* value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200177 if (value) {
Michal Vasko871a0252020-11-11 18:35:24 +0100178 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "value", value, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200179 }
180
181 /* orig-key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200182 if (orig_key) {
Michal Vasko871a0252020-11-11 18:35:24 +0100183 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 +0200184 }
185
186 return LY_SUCCESS;
187}
188
189/**
190 * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
191 *
Michal Vasko1dcd73b2020-12-08 10:04:33 +0100192 * @param[in] first Node from the first tree, can be NULL (on create).
Michal Vaskod59035b2020-07-08 12:00:06 +0200193 * @param[in] schema Schema node of the list/leaf-list.
194 * @param[in,out] userord Sized array of userord items.
195 * @return Userord item for all the user-ordered list/leaf-list instances.
196 */
197static struct lyd_diff_userord *
198lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
199{
200 struct lyd_diff_userord *item;
201 const struct lyd_node *iter, **node;
202 LY_ARRAY_COUNT_TYPE u;
203
204 LY_ARRAY_FOR(*userord, u) {
205 if ((*userord)[u].schema == schema) {
206 return &(*userord)[u];
207 }
208 }
209
210 /* it was not added yet, add it now */
211 LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
212
213 item->schema = schema;
214 item->pos = 0;
215 item->inst = NULL;
216
217 /* store all the instance pointers in the current order */
218 if (first) {
219 if (first->parent) {
220 iter = first->parent->child;
221 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +0200222 for (iter = first; iter->prev->next; iter = iter->prev) {}
Michal Vaskod59035b2020-07-08 12:00:06 +0200223 }
Michal Vaskod989ba02020-08-24 10:59:24 +0200224 for ( ; iter; iter = iter->next) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200225 if (iter->schema == first->schema) {
226 LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
227 *node = iter;
228 }
229 }
230 }
231
232 return item;
233}
234
235/**
236 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
237 * lists/leaf-lists.
238 *
239 * @param[in] first Node from the first tree, can be NULL (on create).
240 * @param[in] second Node from the second tree, can be NULL (on delete).
241 * @param[in] options Diff options.
242 * @param[in,out] userord Sized array of userord items for keeping the current node order.
243 * @param[out] op Operation.
244 * @param[out] orig_default Original default metadata.
245 * @param[out] value Value metadata.
246 * @param[out] orig_value Original value metadata
247 * @param[out] key Key metadata.
248 * @param[out] orig_key Original key metadata.
249 * @return LY_SUCCESS on success,
250 * @return LY_ENOT if there is no change to be added into diff,
251 * @return LY_ERR value on other errors.
252 */
253static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200254lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options,
Radek Krejci0f969882020-08-21 16:56:47 +0200255 struct lyd_diff_userord **userord, enum lyd_diff_op *op, const char **orig_default, char **value,
256 char **orig_value, char **key, char **orig_key)
Michal Vaskod59035b2020-07-08 12:00:06 +0200257{
258 const struct lysc_node *schema;
Michal Vaskod59035b2020-07-08 12:00:06 +0200259 size_t buflen, bufused, first_pos, second_pos;
260 struct lyd_diff_userord *userord_item;
261
262 assert(first || second);
263
264 *orig_default = NULL;
265 *value = NULL;
266 *orig_value = NULL;
267 *key = NULL;
268 *orig_key = NULL;
269
270 schema = first ? first->schema : second->schema;
271 assert(lysc_is_userordered(schema));
272
273 /* get userord entry */
274 userord_item = lyd_diff_userord_get(first, schema, userord);
275 LY_CHECK_RET(!userord_item, LY_EMEM);
276
277 /* prepare position of the next instance */
278 second_pos = userord_item->pos++;
279
280 /* find user-ordered first position */
281 if (first) {
282 for (first_pos = second_pos; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) {
283 if (userord_item->inst[first_pos] == first) {
284 break;
285 }
286 }
287 assert(first_pos < LY_ARRAY_COUNT(userord_item->inst));
288 } else {
289 first_pos = 0;
290 }
291
292 /* learn operation first */
293 if (!second) {
294 *op = LYD_DIFF_OP_DELETE;
295 } else if (!first) {
296 *op = LYD_DIFF_OP_CREATE;
297 } else {
298 assert(schema->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200299 if (lyd_compare_single(second, userord_item->inst[second_pos], 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200300 /* in first, there is a different instance on the second position, we are going to move 'first' node */
301 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200302 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200303 /* default flag change */
304 *op = LYD_DIFF_OP_NONE;
305 } else {
306 /* no changes */
307 return LY_ENOT;
308 }
309 }
310
311 /*
312 * set each attribute correctly based on the operation and node type
313 */
314
315 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100316 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200317 if (first->flags & LYD_DEFAULT) {
318 *orig_default = "true";
319 } else {
320 *orig_default = "false";
321 }
322 }
323
324 /* value */
325 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
326 if (second_pos) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200327 *value = strdup(LYD_CANON_VALUE(userord_item->inst[second_pos - 1]));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200328 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200329 } else {
330 *value = strdup("");
331 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
332 }
333 }
334
335 /* orig-value */
336 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
337 if (first_pos) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200338 *orig_value = strdup(LYD_CANON_VALUE(userord_item->inst[first_pos - 1]));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200339 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200340 } else {
341 *orig_value = strdup("");
342 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
343 }
344 }
345
346 /* key */
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200347 if ((schema->nodetype == LYS_LIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200348 if (second_pos) {
349 buflen = bufused = 0;
350 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0));
351 } else {
352 *key = strdup("");
353 LY_CHECK_ERR_RET(!*key, LOGMEM(schema->module->ctx), LY_EMEM);
354 }
355 }
356
357 /* orig-key */
358 if ((schema->nodetype == LYS_LIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
359 if (first_pos) {
360 buflen = bufused = 0;
361 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0));
362 } else {
363 *orig_key = strdup("");
364 LY_CHECK_ERR_RET(!*orig_key, LOGMEM(schema->module->ctx), LY_EMEM);
365 }
366 }
367
368 /*
369 * update our instances - apply the change
370 */
371 if (*op == LYD_DIFF_OP_CREATE) {
372 /* insert the instance */
Michal Vasko5cde11b2020-12-08 10:04:48 +0100373 LY_ARRAY_CREATE_RET(schema->module->ctx, userord_item->inst, 1, LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200374 if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) {
375 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
376 (LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
377 }
378 LY_ARRAY_INCREMENT(userord_item->inst);
379 userord_item->inst[second_pos] = second;
380
381 } else if (*op == LYD_DIFF_OP_DELETE) {
382 /* remove the instance */
383 if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) {
384 memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
385 (LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
386 }
387 LY_ARRAY_DECREMENT(userord_item->inst);
388
389 } else if (*op == LYD_DIFF_OP_REPLACE) {
390 /* move the instances */
391 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
392 (first_pos - second_pos) * sizeof *userord_item->inst);
393 userord_item->inst[second_pos] = first;
394 }
395
396 return LY_SUCCESS;
397}
398
399/**
400 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
401 * lists/leaf-lists.
402 *
403 * @param[in] first Node from the first tree, can be NULL (on create).
404 * @param[in] second Node from the second tree, can be NULL (on delete).
405 * @param[in] options Diff options.
406 * @param[out] op Operation.
407 * @param[out] orig_default Original default metadata.
408 * @param[out] orig_value Original value metadata.
409 * @return LY_SUCCESS on success,
410 * @return LY_ENOT if there is no change to be added into diff,
411 * @return LY_ERR value on other errors.
412 */
413static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200414lyd_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 +0200415 const char **orig_default, char **orig_value)
Michal Vaskod59035b2020-07-08 12:00:06 +0200416{
417 const struct lysc_node *schema;
Michal Vaskod59035b2020-07-08 12:00:06 +0200418
419 assert(first || second);
420
421 *orig_default = NULL;
422 *orig_value = NULL;
423
424 schema = first ? first->schema : second->schema;
425 assert(!lysc_is_userordered(schema));
426
427 /* learn operation first */
428 if (!second) {
429 *op = LYD_DIFF_OP_DELETE;
430 } else if (!first) {
431 *op = LYD_DIFF_OP_CREATE;
432 } else {
433 switch (schema->nodetype) {
434 case LYS_CONTAINER:
435 case LYS_RPC:
436 case LYS_ACTION:
437 case LYS_NOTIF:
438 /* no changes */
439 return LY_ENOT;
440 case LYS_LIST:
441 case LYS_LEAFLIST:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200442 if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200443 /* default flag change */
444 *op = LYD_DIFF_OP_NONE;
445 } else {
446 /* no changes */
447 return LY_ENOT;
448 }
449 break;
450 case LYS_LEAF:
451 case LYS_ANYXML:
452 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +0200453 if (lyd_compare_single(first, second, 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200454 /* different values */
455 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200456 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200457 /* default flag change */
458 *op = LYD_DIFF_OP_NONE;
459 } else {
460 /* no changes */
461 return LY_ENOT;
462 }
463 break;
464 default:
465 LOGINT_RET(schema->module->ctx);
466 }
467 }
468
469 /*
470 * set each attribute correctly based on the operation and node type
471 */
472
473 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100474 if ((schema->nodetype & LYD_NODE_TERM) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200475 if (first->flags & LYD_DEFAULT) {
476 *orig_default = "true";
477 } else {
478 *orig_default = "false";
479 }
480 }
481
482 /* orig-value */
483 if ((schema->nodetype == LYS_LEAF) && (*op == LYD_DIFF_OP_REPLACE)) {
484 /* leaf */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200485 *orig_value = strdup(LYD_CANON_VALUE(first));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200486 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200487 }
488
489 return LY_SUCCESS;
490}
491
492/**
493 * @brief Perform diff for all siblings at certain depth, recursively.
494 *
495 * For user-ordered lists/leaf-lists a specific structure is used for storing
496 * the current order. The idea is to apply all the generated diff changes
497 * virtually on the first tree so that we can continue to generate correct
498 * changes after some were already generated.
499 *
500 * The algorithm then uses second tree position-based changes with a before
501 * (preceding) item anchor.
502 *
503 * Example:
504 *
505 * Virtual first tree leaf-list order:
506 * 1 2 [3] 4 5
507 *
508 * Second tree leaf-list order:
509 * 1 2 [5] 3 4
510 *
511 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
512 * match - they do not - move nodes so that the 3rd position node is final ->
513 * -> move node 5 to the 3rd position -> move node 5 after node 2.
514 *
515 * Required properties:
516 * Stored operations (move) should not be affected by later operations -
517 * - would cause a redundantly long list of operations, possibly inifinite.
518 *
519 * Implemenation justification:
520 * First, all delete operations and only then move/create operations are stored.
521 * Also, preceding anchor is used and after each iteration another node is
522 * at its final position. That results in the invariant that all preceding
523 * nodes are final and will not be changed by the later operations, meaning
524 * they can safely be used as anchors for the later operations.
525 *
526 * @param[in] first First tree first sibling.
527 * @param[in] second Second tree first sibling.
528 * @param[in] options Diff options.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200529 * @param[in] nosiblings Whether to skip following siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +0200530 * @param[in,out] diff Diff to append to.
531 * @return LY_ERR value.
532 */
533static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200534lyd_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 +0200535 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200536{
537 LY_ERR ret = LY_SUCCESS;
538 const struct lyd_node *iter_first, *iter_second;
539 struct lyd_node *match_second, *match_first;
Michal Vaskod59035b2020-07-08 12:00:06 +0200540 struct lyd_diff_userord *userord = NULL;
541 LY_ARRAY_COUNT_TYPE u;
542 enum lyd_diff_op op;
543 const char *orig_default;
544 char *orig_value, *key, *value, *orig_key;
545
Michal Vaskod59035b2020-07-08 12:00:06 +0200546 /* compare first tree to the second tree - delete, replace, none */
547 LY_LIST_FOR(first, iter_first) {
548 assert(!(iter_first->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200549 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200550 /* skip default nodes */
551 continue;
552 }
553
554 if (iter_first->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
555 /* try to find the exact instance */
556 lyd_find_sibling_first(second, iter_first, &match_second);
557 } else {
558 /* try to simply find the node, there cannot be more instances */
559 lyd_find_sibling_val(second, iter_first->schema, NULL, 0, &match_second);
560 }
561
Michal Vasko3a41dff2020-07-15 14:30:28 +0200562 if (match_second && (match_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200563 /* ignore default nodes */
564 match_second = NULL;
565 }
566
567 if (lysc_is_userordered(iter_first->schema)) {
568 if (match_second) {
569 /* we are handling only user-ordered node delete now */
570 continue;
571 }
572
573 /* get all the attributes */
574 LY_CHECK_GOTO(lyd_diff_userord_attrs(iter_first, match_second, options, &userord, &op, &orig_default,
Michal Vasko69730152020-10-09 16:30:07 +0200575 &value, &orig_value, &key, &orig_key), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200576
577 /* there must be changes, it is deleted */
578 assert(op == LYD_DIFF_OP_DELETE);
579 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, orig_key, diff);
580
581 free(orig_value);
582 free(key);
583 free(value);
584 free(orig_key);
585 LY_CHECK_GOTO(ret, cleanup);
586 } else {
587 /* get all the attributes */
588 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
589
590 /* add into diff if there are any changes */
591 if (!ret) {
592 if (op == LYD_DIFF_OP_DELETE) {
593 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, diff);
594 } else {
Michal Vasko3c2dd6c2020-11-06 17:38:55 +0100595 assert(match_second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200596 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, diff);
597 }
598
599 free(orig_value);
600 LY_CHECK_GOTO(ret, cleanup);
601 } else if (ret == LY_ENOT) {
602 ret = LY_SUCCESS;
603 } else {
604 goto cleanup;
605 }
606 }
607
608 /* check descendants, if any, recursively */
609 if (match_second) {
Radek Krejcia1c1e542020-09-29 16:06:52 +0200610 LY_CHECK_GOTO(lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second), options,
Michal Vasko69730152020-10-09 16:30:07 +0200611 0, diff), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200612 }
613
614 if (nosiblings) {
615 break;
616 }
617 }
618
619 /* reset all cached positions */
620 LY_ARRAY_FOR(userord, u) {
621 userord[u].pos = 0;
622 }
623
624 /* compare second tree to the first tree - create, user-ordered move */
625 LY_LIST_FOR(second, iter_second) {
626 assert(!(iter_second->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200627 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200628 /* skip default nodes */
629 continue;
630 }
631
632 if (iter_second->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
633 lyd_find_sibling_first(first, iter_second, &match_first);
634 } else {
635 lyd_find_sibling_val(first, iter_second->schema, NULL, 0, &match_first);
636 }
637
Michal Vasko3a41dff2020-07-15 14:30:28 +0200638 if (match_first && (match_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200639 /* ignore default nodes */
640 match_first = NULL;
641 }
642
643 if (lysc_is_userordered(iter_second->schema)) {
644 /* get all the attributes */
645 ret = lyd_diff_userord_attrs(match_first, iter_second, options, &userord, &op, &orig_default,
Michal Vasko69730152020-10-09 16:30:07 +0200646 &value, &orig_value, &key, &orig_key);
Michal Vaskod59035b2020-07-08 12:00:06 +0200647
648 /* add into diff if there are any changes */
649 if (!ret) {
650 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, orig_key, diff);
651
652 free(orig_value);
653 free(key);
654 free(value);
655 free(orig_key);
656 LY_CHECK_GOTO(ret, cleanup);
657 } else if (ret == LY_ENOT) {
658 ret = LY_SUCCESS;
659 } else {
660 goto cleanup;
661 }
662 } else if (!match_first) {
663 /* get all the attributes */
664 LY_CHECK_GOTO(lyd_diff_attrs(match_first, iter_second, options, &op, &orig_default, &orig_value), cleanup);
665
666 /* there must be changes, it is created */
667 assert(op == LYD_DIFF_OP_CREATE);
668 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, diff);
669
670 free(orig_value);
671 LY_CHECK_GOTO(ret, cleanup);
672 } /* else was handled */
673
674 if (nosiblings) {
675 break;
676 }
677 }
678
679cleanup:
680 LY_ARRAY_FOR(userord, u) {
681 LY_ARRAY_FREE(userord[u].inst);
682 }
683 LY_ARRAY_FREE(userord);
684 return ret;
685}
686
Michal Vasko3a41dff2020-07-15 14:30:28 +0200687static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200688lyd_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 +0200689{
690 const struct ly_ctx *ctx;
691
692 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
693
694 if (first) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200695 ctx = LYD_CTX(first);
Michal Vaskod59035b2020-07-08 12:00:06 +0200696 } else if (second) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200697 ctx = LYD_CTX(second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200698 } else {
699 ctx = NULL;
700 }
701
702 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
703 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
704 return LY_EINVAL;
705 }
706
707 *diff = NULL;
708
Michal Vasko3a41dff2020-07-15 14:30:28 +0200709 return lyd_diff_siblings_r(first, second, options, nosiblings, diff);
710}
711
712API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200713lyd_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 +0200714{
715 return lyd_diff(first, second, options, 1, diff);
716}
717
718API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200719lyd_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 +0200720{
721 return lyd_diff(first, second, options, 0, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200722}
723
724/**
725 * @brief Find a matching node in data tree for a diff node.
726 *
727 * @param[in] first_node First sibling in the data tree.
728 * @param[in] diff_node Diff node to match.
Michal Vaskoe6323f62020-07-09 15:49:02 +0200729 * @param[out] match_p Matching node, NULL if no found.
Michal Vaskod59035b2020-07-08 12:00:06 +0200730 */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200731static void
Michal Vaskod59035b2020-07-08 12:00:06 +0200732lyd_diff_find_node(const struct lyd_node *first_node, const struct lyd_node *diff_node, struct lyd_node **match_p)
733{
734 if (diff_node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
735 /* try to find the exact instance */
736 lyd_find_sibling_first(first_node, diff_node, match_p);
737 } else {
738 /* try to simply find the node, there cannot be more instances */
739 lyd_find_sibling_val(first_node, diff_node->schema, NULL, 0, match_p);
740 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200741}
742
743/**
744 * @brief Learn operation of a diff node.
745 *
746 * @param[in] diff_node Diff node.
747 * @param[out] op Operation.
Michal Vaskod59035b2020-07-08 12:00:06 +0200748 * @return LY_ERR value.
749 */
750static LY_ERR
Michal Vaskoe6323f62020-07-09 15:49:02 +0200751lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op)
Michal Vaskod59035b2020-07-08 12:00:06 +0200752{
753 struct lyd_meta *meta = NULL;
754 const struct lyd_node *diff_parent;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200755 const char *str;
Michal Vaskod59035b2020-07-08 12:00:06 +0200756
757 for (diff_parent = diff_node; diff_parent; diff_parent = (struct lyd_node *)diff_parent->parent) {
758 LY_LIST_FOR(diff_parent->meta, meta) {
759 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200760 str = meta->value.canonical;
Michal Vaskod59035b2020-07-08 12:00:06 +0200761 if ((str[0] == 'r') && (diff_parent != diff_node)) {
762 /* we do not care about this operation if it's in our parent */
763 continue;
764 }
Michal Vaskoe6323f62020-07-09 15:49:02 +0200765 *op = lyd_diff_str2op(str);
Michal Vaskod59035b2020-07-08 12:00:06 +0200766 break;
767 }
768 }
769 if (meta) {
770 break;
771 }
772 }
Michal Vaskob7be7a82020-08-20 09:09:04 +0200773 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(diff_node)), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +0200774
Michal Vaskod59035b2020-07-08 12:00:06 +0200775 return LY_SUCCESS;
776}
777
778/**
779 * @brief Insert a diff node into a data tree.
780 *
781 * @param[in,out] first_node First sibling of the data tree.
782 * @param[in] parent_node Data tree sibling parent node.
783 * @param[in] new_node Node to insert.
784 * @param[in] keys_or_value Optional predicate of relative (leaf-)list instance. If not set, the user-ordered
785 * instance will be inserted at the first position.
786 * @return err_info, NULL on success.
787 */
788static LY_ERR
789lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
Radek Krejci0f969882020-08-21 16:56:47 +0200790 const char *key_or_value)
Michal Vaskod59035b2020-07-08 12:00:06 +0200791{
792 LY_ERR ret;
793 struct lyd_node *anchor;
794
795 assert(new_node);
796
797 if (!*first_node) {
798 if (!parent_node) {
799 /* no parent or siblings */
800 *first_node = new_node;
801 return LY_SUCCESS;
802 }
803
804 /* simply insert into parent, no other children */
805 if (key_or_value) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200806 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
Michal Vasko69730152020-10-09 16:30:07 +0200807 new_node->schema->name);
Michal Vaskod59035b2020-07-08 12:00:06 +0200808 return LY_EINVAL;
809 }
Michal Vaskob104f112020-07-17 09:54:54 +0200810 return lyd_insert_child(parent_node, new_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200811 }
812
813 assert(!(*first_node)->parent || ((struct lyd_node *)(*first_node)->parent == parent_node));
814
Michal Vaskod59035b2020-07-08 12:00:06 +0200815 if (!lysc_is_userordered(new_node->schema)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200816 /* simple insert */
817 return lyd_insert_sibling(*first_node, new_node, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200818 }
819
820 if (key_or_value) {
821 /* find the anchor sibling */
822 ret = lyd_find_sibling_val(*first_node, new_node->schema, key_or_value, 0, &anchor);
823 if (ret == LY_ENOTFOUND) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200824 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
Michal Vasko69730152020-10-09 16:30:07 +0200825 new_node->schema->name);
Michal Vaskod59035b2020-07-08 12:00:06 +0200826 return LY_EINVAL;
827 } else if (ret) {
828 return ret;
829 }
830
831 /* insert after */
832 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
833 assert(new_node->prev == anchor);
834 if (*first_node == new_node) {
835 *first_node = anchor;
836 }
837 } else {
838 if ((*first_node)->schema->flags & LYS_KEY) {
839 assert(parent_node && (parent_node->schema->nodetype == LYS_LIST));
840
841 /* find last key */
842 anchor = *first_node;
843 while (anchor->next && (anchor->next->schema->flags & LYS_KEY)) {
844 anchor = anchor->next;
845 }
846 /* insert after the last key */
847 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
848 } else {
849 /* insert at the beginning */
850 LY_CHECK_RET(lyd_insert_before(*first_node, new_node));
851 *first_node = new_node;
852 }
853 }
854
855 return LY_SUCCESS;
856}
857
858/**
859 * @brief Apply diff subtree on data tree nodes, recursively.
860 *
861 * @param[in,out] first_node First sibling of the data tree.
862 * @param[in] parent_node Parent of the first sibling.
863 * @param[in] diff_node Current diff node.
Michal Vaskoe6323f62020-07-09 15:49:02 +0200864 * @param[in] diff_cb Optional diff callback.
865 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskod59035b2020-07-08 12:00:06 +0200866 * @return LY_ERR value.
867 */
868static LY_ERR
869lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node,
Radek Krejci0f969882020-08-21 16:56:47 +0200870 lyd_diff_cb diff_cb, void *cb_data)
Michal Vaskod59035b2020-07-08 12:00:06 +0200871{
872 LY_ERR ret;
873 struct lyd_node *match, *diff_child;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200874 const char *str_val;
875 enum lyd_diff_op op;
876 struct lyd_meta *meta;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200877 const struct ly_ctx *ctx = LYD_CTX(diff_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200878
879 /* read all the valid attributes */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200880 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op));
Michal Vaskod59035b2020-07-08 12:00:06 +0200881
Michal Vaskoe6323f62020-07-09 15:49:02 +0200882 /* handle specific user-ordered (leaf-)lists operations separately */
883 if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) {
884 if (op == LYD_DIFF_OP_REPLACE) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200885 /* find the node (we must have some siblings because the node was only moved) */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200886 lyd_diff_find_node(*first_node, diff_node, &match);
Michal Vaskoe8022002020-12-03 14:10:14 +0100887 LY_CHECK_ERR_RET(!match, LOGINT(LYD_CTX(diff_node)), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +0200888 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200889 /* duplicate the node */
890 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +0200891 }
892
Michal Vaskoe6323f62020-07-09 15:49:02 +0200893 /* get "key" or "value" metadata string value */
894 meta = lyd_find_meta(diff_node->meta, NULL, diff_node->schema->nodetype == LYS_LIST ? "yang:key" : "yang:value");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200895 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(diff_node)), LY_EINT);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200896 str_val = meta->value.canonical;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200897
Michal Vaskod59035b2020-07-08 12:00:06 +0200898 /* insert/move the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200899 if (str_val[0]) {
900 ret = lyd_diff_insert(first_node, parent_node, match, str_val);
Michal Vaskod59035b2020-07-08 12:00:06 +0200901 } else {
902 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
903 }
904 if (ret) {
Michal Vaskoe6323f62020-07-09 15:49:02 +0200905 if (op == LYD_DIFF_OP_CREATE) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200906 lyd_free_tree(match);
907 }
908 return ret;
909 }
910
911 goto next_iter_r;
912 }
913
914 /* apply operation */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200915 switch (op) {
916 case LYD_DIFF_OP_NONE:
Michal Vaskod59035b2020-07-08 12:00:06 +0200917 /* find the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200918 lyd_diff_find_node(*first_node, diff_node, &match);
Michal Vaskoe8022002020-12-03 14:10:14 +0100919 LY_CHECK_ERR_RET(!match, LOGINT(LYD_CTX(diff_node)), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +0200920
921 if (match->schema->nodetype & LYD_NODE_TERM) {
922 /* special case of only dflt flag change */
923 if (diff_node->flags & LYD_DEFAULT) {
924 match->flags |= LYD_DEFAULT;
925 } else {
926 match->flags &= ~LYD_DEFAULT;
927 }
928 } else {
929 /* none operation on nodes without children is redundant and hence forbidden */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200930 if (!lyd_child_no_keys(diff_node)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200931 LOGINT_RET(ctx);
932 }
933 }
934 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200935 case LYD_DIFF_OP_CREATE:
Michal Vaskod59035b2020-07-08 12:00:06 +0200936 /* duplicate the node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200937 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +0200938
939 /* insert it at the end */
940 ret = 0;
Michal Vaskob104f112020-07-17 09:54:54 +0200941 if (parent_node) {
942 ret = lyd_insert_child(parent_node, match);
Michal Vaskod59035b2020-07-08 12:00:06 +0200943 } else {
Michal Vaskob104f112020-07-17 09:54:54 +0200944 ret = lyd_insert_sibling(*first_node, match, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200945 }
946 if (ret) {
947 lyd_free_tree(match);
948 return ret;
949 }
950
951 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200952 case LYD_DIFF_OP_DELETE:
Michal Vaskod59035b2020-07-08 12:00:06 +0200953 /* find the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200954 lyd_diff_find_node(*first_node, diff_node, &match);
Michal Vaskoe8022002020-12-03 14:10:14 +0100955 LY_CHECK_ERR_RET(!match, LOGINT(LYD_CTX(diff_node)), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +0200956
957 /* remove it */
958 if ((match == *first_node) && !match->parent) {
959 assert(!parent_node);
960 /* we have removed the top-level node */
961 *first_node = (*first_node)->next;
962 }
963 lyd_free_tree(match);
964
965 /* we are not going recursively in this case, the whole subtree was already deleted */
966 return LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200967 case LYD_DIFF_OP_REPLACE:
Michal Vaskod59035b2020-07-08 12:00:06 +0200968 LY_CHECK_ERR_RET(diff_node->schema->nodetype != LYS_LEAF, LOGINT(ctx), LY_EINT);
969
970 /* find the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200971 lyd_diff_find_node(*first_node, diff_node, &match);
Michal Vaskoe8022002020-12-03 14:10:14 +0100972 LY_CHECK_ERR_RET(!match, LOGINT(LYD_CTX(diff_node)), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +0200973
974 /* update its value */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200975 ret = lyd_change_term(match, LYD_CANON_VALUE(diff_node));
Michal Vaskod59035b2020-07-08 12:00:06 +0200976 if (ret && (ret != LY_EEXIST)) {
977 LOGINT_RET(ctx);
978 }
979
980 /* with flags */
981 match->flags = diff_node->flags;
982 break;
983 default:
984 LOGINT_RET(ctx);
985 }
986
987next_iter_r:
988 if (diff_cb) {
989 /* call callback */
990 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
991 }
992
993 /* apply diff recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200994 LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200995 LY_CHECK_RET(lyd_diff_apply_r(lyd_node_children_p(match), match, diff_child, diff_cb, cb_data));
996 }
997
998 return LY_SUCCESS;
999}
1000
1001API LY_ERR
1002lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +02001003 lyd_diff_cb diff_cb, void *cb_data)
Michal Vaskod59035b2020-07-08 12:00:06 +02001004{
1005 const struct lyd_node *root;
1006
1007 LY_LIST_FOR(diff, root) {
1008 if (mod && (lyd_owner_module(root) != mod)) {
1009 /* skip data nodes from different modules */
1010 continue;
1011 }
1012
1013 /* apply relevant nodes from the diff datatree */
1014 LY_CHECK_RET(lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data));
1015 }
1016
1017 return LY_SUCCESS;
1018}
1019
1020API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001021lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff)
Michal Vaskod59035b2020-07-08 12:00:06 +02001022{
1023 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
1024}
Michal Vaskoe6323f62020-07-09 15:49:02 +02001025
1026/**
1027 * @brief Update operations on a diff node when the new operation is NONE.
1028 *
1029 * @param[in] diff_match Node from the diff.
1030 * @param[in] cur_op Current operation of the diff node.
1031 * @param[in] src_diff Current source diff node.
1032 * @return LY_ERR value.
1033 */
1034static LY_ERR
1035lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1036{
1037 switch (cur_op) {
1038 case LYD_DIFF_OP_NONE:
1039 case LYD_DIFF_OP_CREATE:
1040 case LYD_DIFF_OP_REPLACE:
1041 if (src_diff->schema->nodetype & LYD_NODE_TERM) {
1042 /* NONE on a term means only its dflt flag was changed */
1043 diff_match->flags &= ~LYD_DEFAULT;
1044 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1045 }
1046 break;
1047 default:
1048 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001049 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001050 }
1051
1052 return LY_SUCCESS;
1053}
1054
1055/**
1056 * @brief Remove an attribute from a node.
1057 *
1058 * @param[in] node Node with the metadata.
1059 * @param[in] name Metadata name.
1060 */
1061static void
1062lyd_diff_del_meta(struct lyd_node *node, const char *name)
1063{
1064 struct lyd_meta *meta;
1065
1066 LY_LIST_FOR(node->meta, meta) {
1067 if (!strcmp(meta->name, name) && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001068 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001069 return;
1070 }
1071 }
1072
1073 assert(0);
1074}
1075
1076/**
1077 * @brief Set a specific operation of a node. Delete the previous operation, if any.
Michal Vasko871a0252020-11-11 18:35:24 +01001078 * Does not change the default flag.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001079 *
1080 * @param[in] node Node to change.
1081 * @param[in] op Operation to set.
1082 * @return LY_ERR value.
1083 */
1084static LY_ERR
1085lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op)
1086{
1087 struct lyd_meta *meta;
1088
1089 LY_LIST_FOR(node->meta, meta) {
1090 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001091 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001092 break;
1093 }
1094 }
1095
Michal Vasko871a0252020-11-11 18:35:24 +01001096 return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001097}
1098
1099/**
1100 * @brief Update operations on a diff node when the new operation is REPLACE.
1101 *
1102 * @param[in] diff_match Node from the diff.
1103 * @param[in] cur_op Current operation of the diff node.
1104 * @param[in] src_diff Current source diff node.
1105 * @return LY_ERR value.
1106 */
1107static LY_ERR
1108lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1109{
1110 LY_ERR ret;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001111 const char *str_val, *meta_name;
1112 struct lyd_meta *meta;
1113 const struct lys_module *mod;
1114 const struct lyd_node_any *any;
1115
1116 /* get "yang" module for the metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001117 mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001118 assert(mod);
1119
1120 switch (cur_op) {
1121 case LYD_DIFF_OP_REPLACE:
1122 case LYD_DIFF_OP_CREATE:
1123 switch (diff_match->schema->nodetype) {
1124 case LYS_LIST:
1125 case LYS_LEAFLIST:
Michal Vasko4231fb62020-07-13 13:54:47 +02001126 /* it was created/moved somewhere, but now it will be created/moved somewhere else,
Michal Vaskoe6323f62020-07-09 15:49:02 +02001127 * keep orig_key/orig_value (only replace oper) and replace key/value */
1128 assert(lysc_is_userordered(diff_match->schema));
1129 meta_name = (diff_match->schema->nodetype == LYS_LIST ? "key" : "value");
1130
1131 lyd_diff_del_meta(diff_match, meta_name);
1132 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001133 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001134 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001135 break;
1136 case LYS_LEAF:
1137 /* replaced with the exact same value, impossible */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001138 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001139 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001140 }
1141
Michal Vaskoe6323f62020-07-09 15:49:02 +02001142 /* modify the node value */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001143 if (lyd_change_term(diff_match, LYD_CANON_VALUE(src_diff))) {
1144 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001145 }
1146
Michal Vasko8caadab2020-11-05 17:38:15 +01001147 if (cur_op == LYD_DIFF_OP_REPLACE) {
1148 /* compare values whether there is any change at all */
1149 meta = lyd_find_meta(diff_match->meta, mod, "orig-value");
1150 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(diff_match)), LY_EINT);
1151 str_val = meta->value.canonical;
1152 ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val));
1153 if (!ret) {
1154 /* values are the same, remove orig-value meta and set oper to NONE */
1155 lyd_free_meta_single(meta);
1156 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1157 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001158 }
1159
1160 /* modify the default flag */
1161 diff_match->flags &= ~LYD_DEFAULT;
1162 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1163 break;
1164 case LYS_ANYXML:
1165 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +02001166 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001167 /* replaced with the exact same value, impossible */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001168 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001169 }
1170
1171 /* modify the node value */
1172 any = (struct lyd_node_any *)src_diff;
1173 LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type));
1174 break;
1175 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001176 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001177 }
1178 break;
1179 case LYD_DIFF_OP_NONE:
1180 /* it is moved now */
1181 assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST));
1182
1183 /* change the operation */
1184 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1185
1186 /* set orig-key and key metadata */
1187 meta = lyd_find_meta(src_diff->meta, mod, "orig-key");
Michal Vaskob7be7a82020-08-20 09:09:04 +02001188 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001189 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001190
1191 meta = lyd_find_meta(src_diff->meta, mod, "key");
Michal Vaskob7be7a82020-08-20 09:09:04 +02001192 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001193 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001194 break;
1195 default:
1196 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001197 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001198 }
1199
1200 return LY_SUCCESS;
1201}
1202
1203/**
1204 * @brief Update operations in a diff node when the new operation is CREATE.
1205 *
1206 * @param[in] diff_match Node from the diff.
1207 * @param[in] cur_op Current operation of the diff node.
1208 * @param[in] src_diff Current source diff node.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001209 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001210 * @return LY_ERR value.
1211 */
1212static LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001213lyd_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 +02001214{
1215 struct lyd_node *child;
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001216 const struct lysc_node_leaf *sleaf = NULL;
Michal Vasko871a0252020-11-11 18:35:24 +01001217 uint32_t trg_flags;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001218
1219 switch (cur_op) {
1220 case LYD_DIFF_OP_DELETE:
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001221 if ((options & LYD_DIFF_MERGE_DEFAULTS) && (diff_match->schema->nodetype == LYS_LEAF)) {
1222 /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */
Michal Vasko5632e0d2020-07-31 14:13:37 +02001223 sleaf = (struct lysc_node_leaf *)diff_match->schema;
Michal Vasko5632e0d2020-07-31 14:13:37 +02001224 }
1225
Michal Vasko871a0252020-11-11 18:35:24 +01001226 /* remember current flags */
1227 trg_flags = diff_match->flags;
1228
Michal Vasko69730152020-10-09 16:30:07 +02001229 if (sleaf && sleaf->dflt &&
1230 !sleaf->dflt->realtype->plugin->compare(sleaf->dflt, &((struct lyd_node_term *)src_diff)->value)) {
Michal Vasko5632e0d2020-07-31 14:13:37 +02001231 /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */
1232 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
Michal Vasko5632e0d2020-07-31 14:13:37 +02001233 } else if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001234 /* deleted + created -> operation NONE */
1235 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001236 } else {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001237 /* we deleted it, but it was created with a different value -> operation REPLACE */
1238 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1239
1240 /* current value is the previous one (meta) */
Michal Vasko871a0252020-11-11 18:35:24 +01001241 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-value",
1242 LYD_CANON_VALUE(diff_match), 0, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001243
1244 /* update the value itself */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001245 LY_CHECK_RET(lyd_change_term(diff_match, LYD_CANON_VALUE(src_diff)));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001246 }
1247
1248 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
Michal Vasko4b715ca2020-11-11 18:39:57 +01001249 /* add orig-dflt metadata */
1250 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1251 trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
1252
Michal Vaskoe6323f62020-07-09 15:49:02 +02001253 /* update dflt flag itself */
1254 diff_match->flags &= ~LYD_DEFAULT;
1255 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1256 } else {
1257 /* but the operation of its children should remain DELETE */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001258 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001259 LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE));
1260 }
1261 }
1262 break;
1263 default:
1264 /* create and replace operations are not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001265 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001266 }
1267
1268 return LY_SUCCESS;
1269}
1270
1271/**
1272 * @brief Update operations on a diff node when the new operation is DELETE.
1273 *
1274 * @param[in] diff_match Node from the diff.
1275 * @param[in] cur_op Current operation of the diff node.
1276 * @param[in] src_diff Current source diff node.
1277 * @return LY_ERR value.
1278 */
1279static LY_ERR
1280lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1281{
1282 struct lyd_node *next, *child;
1283
1284 /* we can delete only exact existing nodes */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001285 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 +02001286
1287 switch (cur_op) {
1288 case LYD_DIFF_OP_CREATE:
1289 /* it was created, but then deleted -> set NONE operation */
1290 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1291
1292 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
1293 /* add orig-default meta because it is expected */
Michal Vasko871a0252020-11-11 18:35:24 +01001294 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1295 diff_match->flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001296 } else {
1297 /* keep operation for all descendants (for now) */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001298 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001299 LY_CHECK_RET(lyd_diff_change_op(child, cur_op));
1300 }
1301 }
1302 break;
1303 case LYD_DIFF_OP_REPLACE:
1304 /* similar to none operation but also remove the redundant attribute */
1305 lyd_diff_del_meta(diff_match, "orig-value");
Radek Krejcif13b87b2020-12-01 22:02:17 +01001306 /* fall through */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001307 case LYD_DIFF_OP_NONE:
1308 /* it was not modified, but should be deleted -> set DELETE operation */
1309 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
1310
Michal Vasko5632e0d2020-07-31 14:13:37 +02001311 /* all descendants not in the diff will be deleted and redundant in the diff, so remove them */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001312 LY_LIST_FOR_SAFE(lyd_child_no_keys(diff_match), next, child) {
1313 if (lyd_find_sibling_first(lyd_child(src_diff), child, NULL) == LY_ENOTFOUND) {
Michal Vasko5632e0d2020-07-31 14:13:37 +02001314 lyd_free_tree(child);
1315 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001316 }
1317 break;
1318 default:
1319 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001320 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001321 }
1322
1323 return LY_SUCCESS;
1324}
1325
1326/**
1327 * @brief Check whether this diff node is redundant (does not change data).
1328 *
1329 * @param[in] diff Diff node.
1330 * @return 0 if not, non-zero if it is.
1331 */
1332static int
1333lyd_diff_is_redundant(struct lyd_node *diff)
1334{
1335 enum lyd_diff_op op;
1336 struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL;
1337 struct lyd_node *child;
1338 const struct lys_module *mod;
1339 const char *str;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001340
1341 assert(diff);
1342
Radek Krejcia1c1e542020-09-29 16:06:52 +02001343 child = lyd_child_no_keys(diff);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001344 mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001345 assert(mod);
1346
1347 /* get node operation */
Michal Vasko53bf6f22020-07-14 08:23:40 +02001348 LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001349
1350 if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) {
1351 /* check for redundant move */
1352 orig_val_meta = lyd_find_meta(diff->meta, mod, (diff->schema->nodetype == LYS_LIST ? "orig-key" : "orig-value"));
1353 val_meta = lyd_find_meta(diff->meta, mod, (diff->schema->nodetype == LYS_LIST ? "key" : "value"));
1354 assert(orig_val_meta && val_meta);
1355
1356 if (!lyd_compare_meta(orig_val_meta, val_meta)) {
1357 /* there is actually no move */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001358 lyd_free_meta_single(orig_val_meta);
1359 lyd_free_meta_single(val_meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001360 if (child) {
1361 /* change operation to NONE, we have siblings */
1362 lyd_diff_change_op(diff, LYD_DIFF_OP_NONE);
1363 return 0;
1364 }
1365
1366 /* redundant node, BUT !!
1367 * In diff the move operation is always converted to be INSERT_AFTER, which is fine
1368 * because the data that this is applied on should not change for the diff lifetime.
1369 * However, when we are merging 2 diffs, this conversion is actually lossy because
1370 * if the data change, the move operation can also change its meaning. In this specific
1371 * case the move operation will be lost. But it can be considered a feature, it is not supported.
1372 */
1373 return 1;
1374 }
1375 } else if ((op == LYD_DIFF_OP_NONE) && (diff->schema->nodetype & LYD_NODE_TERM)) {
1376 /* check whether at least the default flags are different */
1377 meta = lyd_find_meta(diff->meta, mod, "orig-default");
1378 assert(meta);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001379 str = meta->value.canonical;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001380
1381 /* if previous and current dflt flags are the same, this node is redundant */
1382 if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) {
1383 return 1;
1384 }
1385 return 0;
1386 }
1387
1388 if (!child && (op == LYD_DIFF_OP_NONE)) {
1389 return 1;
1390 }
1391
1392 return 0;
1393}
1394
1395/**
1396 * @brief Merge sysrepo diff with another diff, recursively.
1397 *
1398 * @param[in] src_diff Source diff node.
1399 * @param[in] diff_parent Current sysrepo diff parent.
1400 * @param[in] diff_cb Optional diff callback.
1401 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001402 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001403 * @param[in,out] diff Diff root node.
1404 * @return LY_ERR value.
1405 */
1406static LY_ERR
1407lyd_diff_merge_r(const struct lyd_node *src_diff, struct lyd_node *diff_parent, lyd_diff_cb diff_cb, void *cb_data,
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001408 uint16_t options, struct lyd_node **diff)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001409{
1410 LY_ERR ret = LY_SUCCESS;
1411 struct lyd_node *child, *diff_node = NULL;
1412 enum lyd_diff_op src_op, cur_op;
1413
1414 /* get source node operation */
1415 LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op));
1416
1417 /* find an equal node in the current diff */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001418 lyd_diff_find_node(diff_parent ? lyd_child_no_keys(diff_parent) : *diff, src_diff, &diff_node);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001419
1420 if (diff_node) {
1421 /* get target (current) operation */
1422 LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op));
1423
1424 /* merge operations */
1425 switch (src_op) {
1426 case LYD_DIFF_OP_REPLACE:
1427 ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff);
1428 break;
1429 case LYD_DIFF_OP_CREATE:
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001430 ret = lyd_diff_merge_create(diff_node, cur_op, src_diff, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001431 break;
1432 case LYD_DIFF_OP_DELETE:
1433 ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff);
1434 break;
1435 case LYD_DIFF_OP_NONE:
1436 ret = lyd_diff_merge_none(diff_node, cur_op, src_diff);
1437 break;
1438 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001439 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001440 }
1441 if (ret) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001442 LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001443 return ret;
1444 }
1445
1446 if (diff_cb) {
1447 /* call callback */
Michal Vaskobc5fba92020-08-07 12:14:39 +02001448 LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001449 }
1450
1451 /* update diff parent */
1452 diff_parent = diff_node;
1453
1454 /* merge src_diff recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001455 LY_LIST_FOR(lyd_child_no_keys(src_diff), child) {
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001456 LY_CHECK_RET(lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, options, diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001457 }
1458 } else {
1459 /* add new diff node with all descendants */
Michal Vasko871a0252020-11-11 18:35:24 +01001460 LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS,
1461 &diff_node));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001462
1463 /* insert node into diff if not already */
1464 if (!diff_parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001465 lyd_insert_sibling(*diff, diff_node, diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001466 }
1467
1468 /* update operation */
1469 LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op));
1470
1471 if (diff_cb) {
Michal Vaskoe2af8412020-12-03 14:11:38 +01001472 /* call callback with no source diff node since it was duplicated and just added */
1473 LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001474 }
1475
1476 /* update diff parent */
1477 diff_parent = diff_node;
1478 }
1479
1480 /* remove any redundant nodes */
Michal Vaskob98d7082020-07-15 16:38:36 +02001481 if (lyd_diff_is_redundant(diff_parent)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001482 if (diff_parent == *diff) {
1483 *diff = (*diff)->next;
1484 }
1485 lyd_free_tree(diff_parent);
1486 }
1487
1488 return LY_SUCCESS;
1489}
1490
1491API LY_ERR
Michal Vaskofb737aa2020-08-06 13:53:53 +02001492lyd_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 +01001493 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001494{
1495 const struct lyd_node *src_root;
1496
1497 LY_LIST_FOR(src_diff, src_root) {
1498 if (mod && (lyd_owner_module(src_root) != mod)) {
1499 /* skip data nodes from different modules */
1500 continue;
1501 }
1502
1503 /* apply relevant nodes from the diff datatree */
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001504 LY_CHECK_RET(lyd_diff_merge_r(src_root, NULL, diff_cb, cb_data, options, diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001505 }
1506
1507 return LY_SUCCESS;
1508}
1509
1510API LY_ERR
Michal Vasko04f85912020-08-07 12:14:58 +02001511lyd_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 +01001512 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vasko04f85912020-08-07 12:14:58 +02001513{
1514 if (!src_sibling) {
1515 return LY_SUCCESS;
1516 }
1517
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001518 return lyd_diff_merge_r(src_sibling, diff_parent, diff_cb, cb_data, options, diff_first);
Michal Vasko04f85912020-08-07 12:14:58 +02001519}
1520
1521API LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001522lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001523{
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001524 return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001525}
Michal Vasko4231fb62020-07-13 13:54:47 +02001526
1527static LY_ERR
1528lyd_diff_reverse_value(struct lyd_node *leaf, const struct lys_module *mod)
1529{
1530 LY_ERR ret = LY_SUCCESS;
1531 struct lyd_meta *meta;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001532 const char *val1 = NULL;
1533 char *val2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001534 uint32_t flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001535
1536 meta = lyd_find_meta(leaf->meta, mod, "orig-value");
Michal Vaskob7be7a82020-08-20 09:09:04 +02001537 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(leaf)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001538
1539 /* orig-value */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001540 val1 = meta->value.canonical;
Michal Vasko4231fb62020-07-13 13:54:47 +02001541
1542 /* current value */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001543 val2 = strdup(LYD_CANON_VALUE(leaf));
Michal Vasko4231fb62020-07-13 13:54:47 +02001544
1545 /* switch values, keep default flag */
1546 flags = leaf->flags;
1547 LY_CHECK_GOTO(ret = lyd_change_term(leaf, val1), cleanup);
1548 leaf->flags = flags;
1549 LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup);
1550
1551cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001552 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001553 return ret;
1554}
1555
1556static LY_ERR
1557lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod)
1558{
1559 struct lyd_meta *meta;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001560 uint32_t flag1, flag2;
Michal Vasko4231fb62020-07-13 13:54:47 +02001561
1562 meta = lyd_find_meta(node->meta, mod, "orig-default");
Michal Vasko610e93b2020-11-09 20:58:32 +01001563 LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001564
1565 /* orig-default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001566 if (meta->value.boolean) {
Michal Vasko4231fb62020-07-13 13:54:47 +02001567 flag1 = LYD_DEFAULT;
1568 } else {
1569 flag1 = 0;
1570 }
1571
1572 /* current default */
1573 flag2 = node->flags & LYD_DEFAULT;
1574
Michal Vasko610e93b2020-11-09 20:58:32 +01001575 if (flag1 == flag2) {
1576 /* no default state change so nothing to reverse */
1577 return LY_SUCCESS;
1578 }
1579
Michal Vasko4231fb62020-07-13 13:54:47 +02001580 /* switch defaults */
1581 node->flags &= ~LYD_DEFAULT;
1582 node->flags |= flag1;
1583 LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false"));
1584
1585 return LY_SUCCESS;
1586}
1587
1588static LY_ERR
1589lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2)
1590{
1591 LY_ERR ret = LY_SUCCESS;
1592 struct lyd_meta *meta1, *meta2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001593 const char *val1 = NULL;
1594 char *val2 = NULL;
Michal Vasko4231fb62020-07-13 13:54:47 +02001595
1596 meta1 = lyd_find_meta(node->meta, mod, name1);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001597 LY_CHECK_ERR_RET(!meta1, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001598
1599 meta2 = lyd_find_meta(node->meta, mod, name2);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001600 LY_CHECK_ERR_RET(!meta2, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001601
1602 /* value1 */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001603 val1 = meta1->value.canonical;
Michal Vasko4231fb62020-07-13 13:54:47 +02001604
1605 /* value2 */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001606 val2 = strdup(meta2->value.canonical);
Michal Vasko4231fb62020-07-13 13:54:47 +02001607
1608 /* switch values */
1609 LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup);
1610 LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup);
1611
1612cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001613 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001614 return ret;
1615}
1616
1617API LY_ERR
Michal Vasko66535812020-08-11 08:44:22 +02001618lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff)
Michal Vasko4231fb62020-07-13 13:54:47 +02001619{
1620 LY_ERR ret = LY_SUCCESS;
1621 const struct lys_module *mod;
Michal Vasko56daf732020-08-10 10:57:18 +02001622 struct lyd_node *root, *elem;
Michal Vasko4231fb62020-07-13 13:54:47 +02001623 enum lyd_diff_op op;
1624
1625 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
1626
1627 if (!src_diff) {
1628 *diff = NULL;
1629 return LY_SUCCESS;
1630 }
1631
1632 /* duplicate diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001633 LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff));
Michal Vasko4231fb62020-07-13 13:54:47 +02001634
1635 /* find module with metadata needed for later */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001636 mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang");
1637 LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001638
1639 LY_LIST_FOR(*diff, root) {
Michal Vasko56daf732020-08-10 10:57:18 +02001640 LYD_TREE_DFS_BEGIN(root, elem) {
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001641 /* skip all keys */
1642 if (!lysc_is_key(elem->schema)) {
1643 /* find operation attribute, if any */
1644 LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001645
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001646 switch (op) {
1647 case LYD_DIFF_OP_CREATE:
1648 /* reverse create to delete */
1649 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001650 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001651 case LYD_DIFF_OP_DELETE:
1652 /* reverse delete to create */
1653 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001654 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001655 case LYD_DIFF_OP_REPLACE:
1656 switch (elem->schema->nodetype) {
1657 case LYS_LEAF:
1658 /* leaf value change */
1659 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
1660 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
1661 break;
1662 case LYS_LEAFLIST:
1663 /* leaf-list move */
1664 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
1665 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup);
1666 break;
1667 case LYS_LIST:
1668 /* list move */
1669 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup);
1670 break;
1671 default:
1672 LOGINT(LYD_CTX(src_diff));
1673 ret = LY_EINT;
1674 goto cleanup;
1675 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001676 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001677 case LYD_DIFF_OP_NONE:
1678 switch (elem->schema->nodetype) {
1679 case LYS_LEAF:
1680 case LYS_LEAFLIST:
1681 /* default flag change */
1682 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
1683 break;
1684 default:
1685 /* nothing to do */
1686 break;
1687 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001688 break;
1689 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001690 }
1691
Michal Vasko56daf732020-08-10 10:57:18 +02001692 LYD_TREE_DFS_END(root, elem);
Michal Vasko4231fb62020-07-13 13:54:47 +02001693 }
1694 }
1695
1696cleanup:
1697 if (ret) {
1698 lyd_free_siblings(*diff);
1699 *diff = NULL;
1700 }
1701 return ret;
1702}