blob: bfc583a29da22f8485772cae6d0508461643a6b2 [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
38static const char *
39lyd_diff_op2str(enum lyd_diff_op op)
40{
41 switch (op) {
42 case LYD_DIFF_OP_CREATE:
43 return "create";
44 case LYD_DIFF_OP_DELETE:
45 return "delete";
46 case LYD_DIFF_OP_REPLACE:
47 return "replace";
48 case LYD_DIFF_OP_NONE:
49 return "none";
50 }
51
52 LOGINT(NULL);
53 return NULL;
54}
55
Michal Vaskoe6323f62020-07-09 15:49:02 +020056static enum lyd_diff_op
57lyd_diff_str2op(const char *str)
58{
59 switch (str[0]) {
60 case 'c':
61 assert(!strcmp(str, "create"));
62 return LYD_DIFF_OP_CREATE;
63 case 'd':
64 assert(!strcmp(str, "delete"));
65 return LYD_DIFF_OP_DELETE;
66 case 'r':
67 assert(!strcmp(str, "replace"));
68 return LYD_DIFF_OP_REPLACE;
69 case 'n':
70 assert(!strcmp(str, "none"));
71 return LYD_DIFF_OP_NONE;
72 }
73
74 LOGINT(NULL);
75 return 0;
76}
77
Michal Vaskod59035b2020-07-08 12:00:06 +020078LY_ERR
79lyd_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 +020080 const char *key, const char *value, const char *position, const char *orig_key, const char *orig_position,
81 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +020082{
83 struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL;
84 const struct lyd_node *parent = NULL;
85 const struct lys_module *yang_mod;
86
87 assert(diff);
88
Michal Vasko53d48422020-11-13 18:02:29 +010089 /* replace leaf always needs orig-default and orig-value */
90 assert((node->schema->nodetype != LYS_LEAF) || (op != LYD_DIFF_OP_REPLACE) || (orig_default && orig_value));
91
92 /* create on userord needs key/value */
93 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_CREATE) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +020094 (lysc_is_dup_inst_list(node->schema) && position) || key);
Michal Vasko53d48422020-11-13 18:02:29 +010095 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +020096 (op != LYD_DIFF_OP_CREATE) || (lysc_is_dup_inst_list(node->schema) && position) || value);
Michal Vasko53d48422020-11-13 18:02:29 +010097
98 /* move on userord needs both key and orig-key/value and orig-value */
99 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_REPLACE) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200100 (lysc_is_dup_inst_list(node->schema) && position && orig_position) || (key && orig_key));
Michal Vasko53d48422020-11-13 18:02:29 +0100101 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200102 (op != LYD_DIFF_OP_REPLACE) || (lysc_is_dup_inst_list(node->schema) && position && orig_position) ||
103 (value && orig_value));
Michal Vasko53d48422020-11-13 18:02:29 +0100104
Michal Vaskod59035b2020-07-08 12:00:06 +0200105 /* find the first existing parent */
106 siblings = *diff;
107 while (1) {
108 /* find next node parent */
109 parent = node;
110 while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
Michal Vasko9e685082021-01-29 14:49:09 +0100111 parent = lyd_parent(parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200112 }
113 if (parent == node) {
114 /* no more parents to find */
115 break;
116 }
117
118 /* check whether it exists in the diff */
119 if (lyd_find_sibling_first(siblings, parent, &match)) {
120 break;
121 }
122
123 /* another parent found */
124 diff_parent = match;
125
126 /* move down in the diff */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200127 siblings = lyd_child_no_keys(match);
Michal Vaskod59035b2020-07-08 12:00:06 +0200128 }
129
130 /* duplicate the subtree (and connect to the diff if possible) */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200131 LY_CHECK_RET(lyd_dup_single(node, (struct lyd_node_inner *)diff_parent,
Michal Vasko871a0252020-11-11 18:35:24 +0100132 LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS, &dup));
Michal Vaskod59035b2020-07-08 12:00:06 +0200133
134 /* find the first duplicated parent */
135 if (!diff_parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100136 diff_parent = lyd_parent(dup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200137 while (diff_parent && diff_parent->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100138 diff_parent = lyd_parent(diff_parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200139 }
140 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100141 diff_parent = dup;
Michal Vaskod59035b2020-07-08 12:00:06 +0200142 while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100143 diff_parent = lyd_parent(diff_parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200144 }
145 }
146
147 /* no parent existed, must be manually connected */
148 if (!diff_parent) {
149 /* there actually was no parent to duplicate */
Michal Vaskob104f112020-07-17 09:54:54 +0200150 lyd_insert_sibling(*diff, dup, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200151 } else if (!diff_parent->parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200152 lyd_insert_sibling(*diff, diff_parent, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200153 }
154
155 /* get module with the operation metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200156 yang_mod = LYD_CTX(node)->list.objs[1];
Michal Vaskod59035b2020-07-08 12:00:06 +0200157 assert(!strcmp(yang_mod->name, "yang"));
158
159 /* add parent operation, if any */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200160 if (diff_parent && (diff_parent != dup)) {
Michal Vasko871a0252020-11-11 18:35:24 +0100161 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), diff_parent, yang_mod, "operation", "none", 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200162 }
163
164 /* add subtree operation */
Michal Vasko871a0252020-11-11 18:35:24 +0100165 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 +0200166
167 /* orig-default */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200168 if (orig_default) {
Michal Vasko871a0252020-11-11 18:35:24 +0100169 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 +0200170 }
171
172 /* orig-value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200173 if (orig_value) {
Michal Vasko871a0252020-11-11 18:35:24 +0100174 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 +0200175 }
176
177 /* key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200178 if (key) {
Michal Vasko871a0252020-11-11 18:35:24 +0100179 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "key", key, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200180 }
181
182 /* value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200183 if (value) {
Michal Vasko871a0252020-11-11 18:35:24 +0100184 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "value", value, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200185 }
186
Michal Vaskoe78faec2021-04-08 17:24:43 +0200187 /* position */
188 if (position) {
189 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "position", position, 0, NULL));
190 }
191
Michal Vaskod59035b2020-07-08 12:00:06 +0200192 /* orig-key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200193 if (orig_key) {
Michal Vasko871a0252020-11-11 18:35:24 +0100194 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 +0200195 }
196
Michal Vaskoe78faec2021-04-08 17:24:43 +0200197 /* orig-position */
198 if (orig_position) {
199 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "orig-position", orig_position, 0, NULL));
200 }
201
Michal Vaskod59035b2020-07-08 12:00:06 +0200202 return LY_SUCCESS;
203}
204
205/**
206 * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
207 *
Michal Vasko1dcd73b2020-12-08 10:04:33 +0100208 * @param[in] first Node from the first tree, can be NULL (on create).
Michal Vaskod59035b2020-07-08 12:00:06 +0200209 * @param[in] schema Schema node of the list/leaf-list.
210 * @param[in,out] userord Sized array of userord items.
211 * @return Userord item for all the user-ordered list/leaf-list instances.
212 */
213static struct lyd_diff_userord *
214lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
215{
216 struct lyd_diff_userord *item;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200217 struct lyd_node *iter;
218 const struct lyd_node **node;
Michal Vaskod59035b2020-07-08 12:00:06 +0200219 LY_ARRAY_COUNT_TYPE u;
220
221 LY_ARRAY_FOR(*userord, u) {
222 if ((*userord)[u].schema == schema) {
223 return &(*userord)[u];
224 }
225 }
226
227 /* it was not added yet, add it now */
228 LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
229
230 item->schema = schema;
231 item->pos = 0;
232 item->inst = NULL;
233
234 /* store all the instance pointers in the current order */
235 if (first) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200236 LYD_LIST_FOR_INST(lyd_first_sibling(first), first->schema, iter) {
237 LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
238 *node = iter;
Michal Vaskod59035b2020-07-08 12:00:06 +0200239 }
240 }
241
242 return item;
243}
244
245/**
246 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
247 * lists/leaf-lists.
248 *
249 * @param[in] first Node from the first tree, can be NULL (on create).
250 * @param[in] second Node from the second tree, can be NULL (on delete).
251 * @param[in] options Diff options.
252 * @param[in,out] userord Sized array of userord items for keeping the current node order.
253 * @param[out] op Operation.
254 * @param[out] orig_default Original default metadata.
255 * @param[out] value Value metadata.
256 * @param[out] orig_value Original value metadata
257 * @param[out] key Key metadata.
258 * @param[out] orig_key Original key metadata.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200259 * @param[out] position Position metadata.
260 * @param[out] orig_position Original position metadata.
Michal Vaskod59035b2020-07-08 12:00:06 +0200261 * @return LY_SUCCESS on success,
262 * @return LY_ENOT if there is no change to be added into diff,
263 * @return LY_ERR value on other errors.
264 */
265static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200266lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options,
Radek Krejci0f969882020-08-21 16:56:47 +0200267 struct lyd_diff_userord **userord, enum lyd_diff_op *op, const char **orig_default, char **value,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200268 char **orig_value, char **key, char **orig_key, char **position, char **orig_position)
Michal Vaskod59035b2020-07-08 12:00:06 +0200269{
270 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200271 size_t buflen, bufused;
272 uint32_t first_pos, second_pos;
Michal Vaskod59035b2020-07-08 12:00:06 +0200273 struct lyd_diff_userord *userord_item;
274
275 assert(first || second);
276
277 *orig_default = NULL;
278 *value = NULL;
279 *orig_value = NULL;
280 *key = NULL;
281 *orig_key = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200282 *position = NULL;
283 *orig_position = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200284
285 schema = first ? first->schema : second->schema;
286 assert(lysc_is_userordered(schema));
287
288 /* get userord entry */
289 userord_item = lyd_diff_userord_get(first, schema, userord);
290 LY_CHECK_RET(!userord_item, LY_EMEM);
291
Michal Vaskod59035b2020-07-08 12:00:06 +0200292 /* find user-ordered first position */
293 if (first) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200294 for (first_pos = 0; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200295 if (userord_item->inst[first_pos] == first) {
296 break;
297 }
298 }
299 assert(first_pos < LY_ARRAY_COUNT(userord_item->inst));
300 } else {
301 first_pos = 0;
302 }
303
Michal Vaskoe78faec2021-04-08 17:24:43 +0200304 /* prepare position of the next instance */
305 second_pos = userord_item->pos++;
306
Michal Vaskod59035b2020-07-08 12:00:06 +0200307 /* learn operation first */
308 if (!second) {
309 *op = LYD_DIFF_OP_DELETE;
310 } else if (!first) {
311 *op = LYD_DIFF_OP_CREATE;
312 } else {
Michal Vasko8f359bf2020-07-28 10:41:15 +0200313 if (lyd_compare_single(second, userord_item->inst[second_pos], 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200314 /* in first, there is a different instance on the second position, we are going to move 'first' node */
315 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200316 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200317 /* default flag change */
318 *op = LYD_DIFF_OP_NONE;
319 } else {
320 /* no changes */
321 return LY_ENOT;
322 }
323 }
324
325 /*
326 * set each attribute correctly based on the operation and node type
327 */
328
329 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100330 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200331 if (first->flags & LYD_DEFAULT) {
332 *orig_default = "true";
333 } else {
334 *orig_default = "false";
335 }
336 }
337
338 /* value */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200339 if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
340 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200341 if (second_pos) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200342 *value = strdup(lyd_get_value(userord_item->inst[second_pos - 1]));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200343 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200344 } else {
345 *value = strdup("");
346 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
347 }
348 }
349
350 /* orig-value */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200351 if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
352 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200353 if (first_pos) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200354 *orig_value = strdup(lyd_get_value(userord_item->inst[first_pos - 1]));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200355 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200356 } else {
357 *orig_value = strdup("");
358 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
359 }
360 }
361
362 /* key */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200363 if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
364 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200365 if (second_pos) {
366 buflen = bufused = 0;
367 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0));
368 } else {
369 *key = strdup("");
370 LY_CHECK_ERR_RET(!*key, LOGMEM(schema->module->ctx), LY_EMEM);
371 }
372 }
373
374 /* orig-key */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200375 if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
376 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200377 if (first_pos) {
378 buflen = bufused = 0;
379 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0));
380 } else {
381 *orig_key = strdup("");
382 LY_CHECK_ERR_RET(!*orig_key, LOGMEM(schema->module->ctx), LY_EMEM);
383 }
384 }
385
Michal Vaskoe78faec2021-04-08 17:24:43 +0200386 /* position */
387 if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
388 if (second_pos) {
389 if (asprintf(position, "%" PRIu32, second_pos) == -1) {
390 LOGMEM(schema->module->ctx);
391 return LY_EMEM;
392 }
393 } else {
394 *position = strdup("");
395 LY_CHECK_ERR_RET(!*position, LOGMEM(schema->module->ctx), LY_EMEM);
396 }
397 }
398
399 /* orig-position */
400 if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
401 if (first_pos) {
402 if (asprintf(orig_position, "%" PRIu32, first_pos) == -1) {
403 LOGMEM(schema->module->ctx);
404 return LY_EMEM;
405 }
406 } else {
407 *orig_position = strdup("");
408 LY_CHECK_ERR_RET(!*orig_position, LOGMEM(schema->module->ctx), LY_EMEM);
409 }
410 }
411
Michal Vaskod59035b2020-07-08 12:00:06 +0200412 /*
413 * update our instances - apply the change
414 */
415 if (*op == LYD_DIFF_OP_CREATE) {
416 /* insert the instance */
Michal Vasko5cde11b2020-12-08 10:04:48 +0100417 LY_ARRAY_CREATE_RET(schema->module->ctx, userord_item->inst, 1, LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200418 if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) {
419 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
420 (LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
421 }
422 LY_ARRAY_INCREMENT(userord_item->inst);
423 userord_item->inst[second_pos] = second;
424
425 } else if (*op == LYD_DIFF_OP_DELETE) {
426 /* remove the instance */
427 if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) {
428 memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
429 (LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
430 }
431 LY_ARRAY_DECREMENT(userord_item->inst);
432
433 } else if (*op == LYD_DIFF_OP_REPLACE) {
434 /* move the instances */
435 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
436 (first_pos - second_pos) * sizeof *userord_item->inst);
437 userord_item->inst[second_pos] = first;
438 }
439
440 return LY_SUCCESS;
441}
442
443/**
444 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
445 * lists/leaf-lists.
446 *
447 * @param[in] first Node from the first tree, can be NULL (on create).
448 * @param[in] second Node from the second tree, can be NULL (on delete).
449 * @param[in] options Diff options.
450 * @param[out] op Operation.
451 * @param[out] orig_default Original default metadata.
452 * @param[out] orig_value Original value metadata.
453 * @return LY_SUCCESS on success,
454 * @return LY_ENOT if there is no change to be added into diff,
455 * @return LY_ERR value on other errors.
456 */
457static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200458lyd_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 +0200459 const char **orig_default, char **orig_value)
Michal Vaskod59035b2020-07-08 12:00:06 +0200460{
461 const struct lysc_node *schema;
Michal Vaskod59035b2020-07-08 12:00:06 +0200462
463 assert(first || second);
464
465 *orig_default = NULL;
466 *orig_value = NULL;
467
468 schema = first ? first->schema : second->schema;
469 assert(!lysc_is_userordered(schema));
470
471 /* learn operation first */
472 if (!second) {
473 *op = LYD_DIFF_OP_DELETE;
474 } else if (!first) {
475 *op = LYD_DIFF_OP_CREATE;
476 } else {
477 switch (schema->nodetype) {
478 case LYS_CONTAINER:
479 case LYS_RPC:
480 case LYS_ACTION:
481 case LYS_NOTIF:
482 /* no changes */
483 return LY_ENOT;
484 case LYS_LIST:
485 case LYS_LEAFLIST:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200486 if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200487 /* default flag change */
488 *op = LYD_DIFF_OP_NONE;
489 } else {
490 /* no changes */
491 return LY_ENOT;
492 }
493 break;
494 case LYS_LEAF:
495 case LYS_ANYXML:
496 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +0200497 if (lyd_compare_single(first, second, 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200498 /* different values */
499 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200500 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200501 /* default flag change */
502 *op = LYD_DIFF_OP_NONE;
503 } else {
504 /* no changes */
505 return LY_ENOT;
506 }
507 break;
508 default:
509 LOGINT_RET(schema->module->ctx);
510 }
511 }
512
513 /*
514 * set each attribute correctly based on the operation and node type
515 */
516
517 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100518 if ((schema->nodetype & LYD_NODE_TERM) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200519 if (first->flags & LYD_DEFAULT) {
520 *orig_default = "true";
521 } else {
522 *orig_default = "false";
523 }
524 }
525
526 /* orig-value */
Michal Vaskobaba84e2021-02-05 16:33:30 +0100527 if ((schema->nodetype & (LYS_LEAF | LYS_ANYDATA)) && (*op == LYD_DIFF_OP_REPLACE)) {
528 if (schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200529 *orig_value = strdup(lyd_get_value(first));
Michal Vaskobaba84e2021-02-05 16:33:30 +0100530 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
531 } else {
532 LY_CHECK_RET(lyd_any_value_str(first, orig_value));
533 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200534 }
535
536 return LY_SUCCESS;
537}
538
539/**
Michal Vaskoe78faec2021-04-08 17:24:43 +0200540 * @brief Find a matching instance of a node in a data tree.
541 *
542 * @param[in] siblings Siblings to search in.
543 * @param[in] target Target node to search for.
544 * @param[in] defaults Whether to consider (or ignore) default values.
545 * @param[in,out] dup_inst_cache Duplicate instance cache.
546 * @param[out] match Found match, NULL if no matching node found.
547 * @return LY_ERR value.
548 */
549static LY_ERR
550lyd_diff_find_match(const struct lyd_node *siblings, const struct lyd_node *target, ly_bool defaults,
Michal Vaskod7c048c2021-05-18 16:12:55 +0200551 struct lyd_dup_inst **dup_inst_cache, struct lyd_node **match)
Michal Vaskoe78faec2021-04-08 17:24:43 +0200552{
Michal Vaskoe78faec2021-04-08 17:24:43 +0200553 if (target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
554 /* try to find the exact instance */
555 lyd_find_sibling_first(siblings, target, match);
556 } else {
557 /* try to simply find the node, there cannot be more instances */
558 lyd_find_sibling_val(siblings, target->schema, NULL, 0, match);
559 }
560
Michal Vaskod7c048c2021-05-18 16:12:55 +0200561 /* update match as needed */
562 LY_CHECK_RET(lyd_dup_inst_next(match, siblings, dup_inst_cache));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200563
564 if (*match && ((*match)->flags & LYD_DEFAULT) && !defaults) {
565 /* ignore default nodes */
566 *match = NULL;
567 }
568 return LY_SUCCESS;
569}
570
571/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200572 * @brief Perform diff for all siblings at certain depth, recursively.
573 *
574 * For user-ordered lists/leaf-lists a specific structure is used for storing
575 * the current order. The idea is to apply all the generated diff changes
576 * virtually on the first tree so that we can continue to generate correct
577 * changes after some were already generated.
578 *
579 * The algorithm then uses second tree position-based changes with a before
580 * (preceding) item anchor.
581 *
582 * Example:
583 *
584 * Virtual first tree leaf-list order:
585 * 1 2 [3] 4 5
586 *
587 * Second tree leaf-list order:
588 * 1 2 [5] 3 4
589 *
590 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
591 * match - they do not - move nodes so that the 3rd position node is final ->
592 * -> move node 5 to the 3rd position -> move node 5 after node 2.
593 *
594 * Required properties:
595 * Stored operations (move) should not be affected by later operations -
596 * - would cause a redundantly long list of operations, possibly inifinite.
597 *
598 * Implemenation justification:
599 * First, all delete operations and only then move/create operations are stored.
600 * Also, preceding anchor is used and after each iteration another node is
601 * at its final position. That results in the invariant that all preceding
602 * nodes are final and will not be changed by the later operations, meaning
603 * they can safely be used as anchors for the later operations.
604 *
605 * @param[in] first First tree first sibling.
606 * @param[in] second Second tree first sibling.
607 * @param[in] options Diff options.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200608 * @param[in] nosiblings Whether to skip following siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +0200609 * @param[in,out] diff Diff to append to.
610 * @return LY_ERR value.
611 */
612static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200613lyd_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 +0200614 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200615{
616 LY_ERR ret = LY_SUCCESS;
617 const struct lyd_node *iter_first, *iter_second;
618 struct lyd_node *match_second, *match_first;
Michal Vaskod59035b2020-07-08 12:00:06 +0200619 struct lyd_diff_userord *userord = NULL;
Michal Vaskod7c048c2021-05-18 16:12:55 +0200620 struct lyd_dup_inst *dup_inst_first = NULL, *dup_inst_second = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200621 LY_ARRAY_COUNT_TYPE u;
622 enum lyd_diff_op op;
623 const char *orig_default;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200624 char *orig_value, *key, *value, *position, *orig_key, *orig_position;
Michal Vaskod59035b2020-07-08 12:00:06 +0200625
Michal Vaskod59035b2020-07-08 12:00:06 +0200626 /* compare first tree to the second tree - delete, replace, none */
627 LY_LIST_FOR(first, iter_first) {
Michal Vaskoc825ed72021-07-21 16:05:59 +0200628 if (!iter_first->schema) {
629 continue;
630 }
631
Michal Vaskod59035b2020-07-08 12:00:06 +0200632 assert(!(iter_first->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200633 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200634 /* skip default nodes */
635 continue;
636 }
637
Michal Vaskoe78faec2021-04-08 17:24:43 +0200638 /* find a match in the second tree */
639 LY_CHECK_GOTO(ret = lyd_diff_find_match(second, iter_first, options & LYD_DIFF_DEFAULTS, &dup_inst_second,
640 &match_second), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200641
642 if (lysc_is_userordered(iter_first->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200643 /* we are handling only user-ordered node delete now */
644 if (!match_second) {
645 /* get all the attributes */
646 LY_CHECK_GOTO(ret = lyd_diff_userord_attrs(iter_first, match_second, options, &userord, &op, &orig_default,
647 &value, &orig_value, &key, &orig_key, &position, &orig_position), cleanup);
648
649 /* there must be changes, it is deleted */
650 assert(op == LYD_DIFF_OP_DELETE);
651 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, position, orig_key, orig_position, diff);
652
653 free(orig_value);
654 free(key);
655 free(value);
656 free(position);
657 free(orig_key);
658 free(orig_position);
659 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200660 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200661 } else {
662 /* get all the attributes */
663 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
664
665 /* add into diff if there are any changes */
666 if (!ret) {
667 if (op == LYD_DIFF_OP_DELETE) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200668 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200669 } else {
Michal Vasko3c2dd6c2020-11-06 17:38:55 +0100670 assert(match_second);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200671 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200672 }
673
674 free(orig_value);
675 LY_CHECK_GOTO(ret, cleanup);
676 } else if (ret == LY_ENOT) {
677 ret = LY_SUCCESS;
678 } else {
679 goto cleanup;
680 }
681 }
682
683 /* check descendants, if any, recursively */
684 if (match_second) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200685 LY_CHECK_GOTO(ret = lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second),
686 options, 0, diff), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200687 }
688
689 if (nosiblings) {
690 break;
691 }
692 }
693
694 /* reset all cached positions */
695 LY_ARRAY_FOR(userord, u) {
696 userord[u].pos = 0;
697 }
698
699 /* compare second tree to the first tree - create, user-ordered move */
700 LY_LIST_FOR(second, iter_second) {
Michal Vaskoc825ed72021-07-21 16:05:59 +0200701 if (!iter_second->schema) {
702 continue;
703 }
704
Michal Vaskod59035b2020-07-08 12:00:06 +0200705 assert(!(iter_second->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200706 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200707 /* skip default nodes */
708 continue;
709 }
710
Michal Vaskoe78faec2021-04-08 17:24:43 +0200711 /* find a match in the first tree */
712 LY_CHECK_GOTO(ret = lyd_diff_find_match(first, iter_second, options & LYD_DIFF_DEFAULTS, &dup_inst_first,
713 &match_first), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200714
715 if (lysc_is_userordered(iter_second->schema)) {
716 /* get all the attributes */
717 ret = lyd_diff_userord_attrs(match_first, iter_second, options, &userord, &op, &orig_default,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200718 &value, &orig_value, &key, &orig_key, &position, &orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200719
720 /* add into diff if there are any changes */
721 if (!ret) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200722 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, position, orig_key,
723 orig_position, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200724
725 free(orig_value);
726 free(key);
727 free(value);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200728 free(position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200729 free(orig_key);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200730 free(orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200731 LY_CHECK_GOTO(ret, cleanup);
732 } else if (ret == LY_ENOT) {
733 ret = LY_SUCCESS;
734 } else {
735 goto cleanup;
736 }
737 } else if (!match_first) {
738 /* get all the attributes */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200739 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 +0200740
741 /* there must be changes, it is created */
742 assert(op == LYD_DIFF_OP_CREATE);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200743 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200744
745 free(orig_value);
746 LY_CHECK_GOTO(ret, cleanup);
747 } /* else was handled */
748
749 if (nosiblings) {
750 break;
751 }
752 }
753
754cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +0200755 lyd_dup_inst_free(dup_inst_first);
756 lyd_dup_inst_free(dup_inst_second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200757 LY_ARRAY_FOR(userord, u) {
758 LY_ARRAY_FREE(userord[u].inst);
759 }
760 LY_ARRAY_FREE(userord);
761 return ret;
762}
763
Michal Vasko3a41dff2020-07-15 14:30:28 +0200764static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200765lyd_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 +0200766{
767 const struct ly_ctx *ctx;
768
769 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
770
771 if (first) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200772 ctx = LYD_CTX(first);
Michal Vaskod59035b2020-07-08 12:00:06 +0200773 } else if (second) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200774 ctx = LYD_CTX(second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200775 } else {
776 ctx = NULL;
777 }
778
779 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
780 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
781 return LY_EINVAL;
782 }
783
784 *diff = NULL;
785
Michal Vasko3a41dff2020-07-15 14:30:28 +0200786 return lyd_diff_siblings_r(first, second, options, nosiblings, diff);
787}
788
789API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200790lyd_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 +0200791{
792 return lyd_diff(first, second, options, 1, diff);
793}
794
795API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200796lyd_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 +0200797{
798 return lyd_diff(first, second, options, 0, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200799}
800
801/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200802 * @brief Learn operation of a diff node.
803 *
804 * @param[in] diff_node Diff node.
805 * @param[out] op Operation.
Michal Vaskod59035b2020-07-08 12:00:06 +0200806 * @return LY_ERR value.
807 */
808static LY_ERR
Michal Vaskoe6323f62020-07-09 15:49:02 +0200809lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op)
Michal Vaskod59035b2020-07-08 12:00:06 +0200810{
811 struct lyd_meta *meta = NULL;
812 const struct lyd_node *diff_parent;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200813 const char *str;
Michal Vaskod59035b2020-07-08 12:00:06 +0200814
Michal Vasko9e685082021-01-29 14:49:09 +0100815 for (diff_parent = diff_node; diff_parent; diff_parent = lyd_parent(diff_parent)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200816 LY_LIST_FOR(diff_parent->meta, meta) {
817 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200818 str = lyd_get_meta_value(meta);
Michal Vaskod59035b2020-07-08 12:00:06 +0200819 if ((str[0] == 'r') && (diff_parent != diff_node)) {
820 /* we do not care about this operation if it's in our parent */
821 continue;
822 }
Michal Vaskoe6323f62020-07-09 15:49:02 +0200823 *op = lyd_diff_str2op(str);
Michal Vaskod59035b2020-07-08 12:00:06 +0200824 break;
825 }
826 }
827 if (meta) {
828 break;
829 }
830 }
Michal Vaskob7be7a82020-08-20 09:09:04 +0200831 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(diff_node)), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +0200832
Michal Vaskod59035b2020-07-08 12:00:06 +0200833 return LY_SUCCESS;
834}
835
836/**
837 * @brief Insert a diff node into a data tree.
838 *
839 * @param[in,out] first_node First sibling of the data tree.
840 * @param[in] parent_node Data tree sibling parent node.
841 * @param[in] new_node Node to insert.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200842 * @param[in] userord_anchor Optional anchor (key, value, or position) of relative (leaf-)list instance. If not set,
843 * the user-ordered instance will be inserted at the first position.
Michal Vaskod59035b2020-07-08 12:00:06 +0200844 * @return err_info, NULL on success.
845 */
846static LY_ERR
847lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200848 const char *userord_anchor)
Michal Vaskod59035b2020-07-08 12:00:06 +0200849{
850 LY_ERR ret;
851 struct lyd_node *anchor;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200852 uint32_t pos, anchor_pos;
853 int found;
Michal Vaskod59035b2020-07-08 12:00:06 +0200854
855 assert(new_node);
856
857 if (!*first_node) {
858 if (!parent_node) {
859 /* no parent or siblings */
860 *first_node = new_node;
861 return LY_SUCCESS;
862 }
863
864 /* simply insert into parent, no other children */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200865 if (userord_anchor) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200866 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
Michal Vasko69730152020-10-09 16:30:07 +0200867 new_node->schema->name);
Michal Vaskod59035b2020-07-08 12:00:06 +0200868 return LY_EINVAL;
869 }
Michal Vaskob104f112020-07-17 09:54:54 +0200870 return lyd_insert_child(parent_node, new_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200871 }
872
Michal Vasko9e685082021-01-29 14:49:09 +0100873 assert(!(*first_node)->parent || (lyd_parent(*first_node) == parent_node));
Michal Vaskod59035b2020-07-08 12:00:06 +0200874
Michal Vaskod59035b2020-07-08 12:00:06 +0200875 if (!lysc_is_userordered(new_node->schema)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200876 /* simple insert */
877 return lyd_insert_sibling(*first_node, new_node, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200878 }
879
Michal Vaskoe78faec2021-04-08 17:24:43 +0200880 if (userord_anchor) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200881 /* find the anchor sibling */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200882 if (lysc_is_dup_inst_list(new_node->schema)) {
883 anchor_pos = atoi(userord_anchor);
884 LY_CHECK_ERR_RET(!anchor_pos, LOGINT(LYD_CTX(new_node)), LY_EINT);
885
886 found = 0;
887 pos = 1;
888 LYD_LIST_FOR_INST(*first_node, new_node->schema, anchor) {
889 if (pos == anchor_pos) {
890 found = 1;
891 break;
892 }
893 ++pos;
894 }
895 if (!found) {
896 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
897 new_node->schema->name);
898 return LY_EINVAL;
899 }
900 } else {
901 ret = lyd_find_sibling_val(*first_node, new_node->schema, userord_anchor, 0, &anchor);
902 if (ret == LY_ENOTFOUND) {
903 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
904 new_node->schema->name);
905 return LY_EINVAL;
906 } else if (ret) {
907 return ret;
908 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200909 }
910
911 /* insert after */
912 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
913 assert(new_node->prev == anchor);
914 if (*first_node == new_node) {
915 *first_node = anchor;
916 }
917 } else {
918 if ((*first_node)->schema->flags & LYS_KEY) {
919 assert(parent_node && (parent_node->schema->nodetype == LYS_LIST));
920
921 /* find last key */
922 anchor = *first_node;
923 while (anchor->next && (anchor->next->schema->flags & LYS_KEY)) {
924 anchor = anchor->next;
925 }
926 /* insert after the last key */
927 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
928 } else {
929 /* insert at the beginning */
930 LY_CHECK_RET(lyd_insert_before(*first_node, new_node));
931 *first_node = new_node;
932 }
933 }
934
935 return LY_SUCCESS;
936}
937
938/**
939 * @brief Apply diff subtree on data tree nodes, recursively.
940 *
941 * @param[in,out] first_node First sibling of the data tree.
942 * @param[in] parent_node Parent of the first sibling.
943 * @param[in] diff_node Current diff node.
Michal Vaskoe6323f62020-07-09 15:49:02 +0200944 * @param[in] diff_cb Optional diff callback.
945 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200946 * @param[in,out] dup_inst Duplicate instance cache for all @p diff_node siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +0200947 * @return LY_ERR value.
948 */
949static LY_ERR
950lyd_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 +0200951 lyd_diff_cb diff_cb, void *cb_data, struct lyd_dup_inst **dup_inst)
Michal Vaskod59035b2020-07-08 12:00:06 +0200952{
953 LY_ERR ret;
954 struct lyd_node *match, *diff_child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200955 const char *str_val, *meta_str;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200956 enum lyd_diff_op op;
957 struct lyd_meta *meta;
Michal Vaskod7c048c2021-05-18 16:12:55 +0200958 struct lyd_dup_inst *child_dup_inst = NULL;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200959 const struct ly_ctx *ctx = LYD_CTX(diff_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200960
961 /* read all the valid attributes */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200962 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op));
Michal Vaskod59035b2020-07-08 12:00:06 +0200963
Michal Vaskoe6323f62020-07-09 15:49:02 +0200964 /* handle specific user-ordered (leaf-)lists operations separately */
965 if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) {
966 if (op == LYD_DIFF_OP_REPLACE) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200967 /* find the node (we must have some siblings because the node was only moved) */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200968 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
969 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +0200970 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200971 /* duplicate the node */
972 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +0200973 }
974
Michal Vaskoe78faec2021-04-08 17:24:43 +0200975 /* get "key", "value", or "position" metadata string value */
976 if (lysc_is_dup_inst_list(diff_node->schema)) {
977 meta_str = "yang:position";
978 } else if (diff_node->schema->nodetype == LYS_LIST) {
979 meta_str = "yang:key";
980 } else {
981 meta_str = "yang:value";
982 }
983 meta = lyd_find_meta(diff_node->meta, NULL, meta_str);
984 LY_CHECK_ERR_RET(!meta, LOGINT(ctx), LY_EINT);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200985 str_val = lyd_get_meta_value(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200986
Michal Vaskod59035b2020-07-08 12:00:06 +0200987 /* insert/move the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200988 if (str_val[0]) {
989 ret = lyd_diff_insert(first_node, parent_node, match, str_val);
Michal Vaskod59035b2020-07-08 12:00:06 +0200990 } else {
991 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
992 }
993 if (ret) {
Michal Vaskoe6323f62020-07-09 15:49:02 +0200994 if (op == LYD_DIFF_OP_CREATE) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200995 lyd_free_tree(match);
996 }
997 return ret;
998 }
999
1000 goto next_iter_r;
1001 }
1002
1003 /* apply operation */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001004 switch (op) {
1005 case LYD_DIFF_OP_NONE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001006 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001007 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
1008 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001009
1010 if (match->schema->nodetype & LYD_NODE_TERM) {
1011 /* special case of only dflt flag change */
1012 if (diff_node->flags & LYD_DEFAULT) {
1013 match->flags |= LYD_DEFAULT;
1014 } else {
1015 match->flags &= ~LYD_DEFAULT;
1016 }
1017 } else {
1018 /* none operation on nodes without children is redundant and hence forbidden */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001019 if (!lyd_child_no_keys(diff_node)) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001020 LOGINT_RET(ctx);
1021 }
1022 }
1023 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001024 case LYD_DIFF_OP_CREATE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001025 /* duplicate the node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001026 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001027
1028 /* insert it at the end */
1029 ret = 0;
Michal Vaskob104f112020-07-17 09:54:54 +02001030 if (parent_node) {
1031 ret = lyd_insert_child(parent_node, match);
Michal Vaskod59035b2020-07-08 12:00:06 +02001032 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001033 ret = lyd_insert_sibling(*first_node, match, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001034 }
1035 if (ret) {
1036 lyd_free_tree(match);
1037 return ret;
1038 }
1039
1040 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001041 case LYD_DIFF_OP_DELETE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001042 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001043 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
1044 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001045
1046 /* remove it */
1047 if ((match == *first_node) && !match->parent) {
1048 assert(!parent_node);
1049 /* we have removed the top-level node */
1050 *first_node = (*first_node)->next;
1051 }
1052 lyd_free_tree(match);
1053
1054 /* we are not going recursively in this case, the whole subtree was already deleted */
1055 return LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001056 case LYD_DIFF_OP_REPLACE:
Michal Vaskobaba84e2021-02-05 16:33:30 +01001057 LY_CHECK_ERR_RET(!(diff_node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA)), LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001058
1059 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001060 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
1061 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001062
Michal Vaskobaba84e2021-02-05 16:33:30 +01001063 /* update the value */
1064 if (diff_node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001065 ret = lyd_change_term(match, lyd_get_value(diff_node));
Michal Vaskobaba84e2021-02-05 16:33:30 +01001066 if (ret && (ret != LY_EEXIST)) {
1067 LOGINT_RET(ctx);
1068 }
1069 } else {
1070 struct lyd_node_any *any = (struct lyd_node_any *)diff_node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001071 LY_CHECK_RET(lyd_any_copy_value(match, &any->value, any->value_type));
Michal Vaskod59035b2020-07-08 12:00:06 +02001072 }
1073
1074 /* with flags */
1075 match->flags = diff_node->flags;
1076 break;
1077 default:
1078 LOGINT_RET(ctx);
1079 }
1080
1081next_iter_r:
1082 if (diff_cb) {
1083 /* call callback */
1084 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
1085 }
1086
1087 /* apply diff recursively */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001088 ret = LY_SUCCESS;
Radek Krejcia1c1e542020-09-29 16:06:52 +02001089 LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001090 ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst);
1091 if (ret) {
1092 break;
1093 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001094 }
1095
Michal Vaskod7c048c2021-05-18 16:12:55 +02001096 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001097 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001098}
1099
1100API LY_ERR
1101lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +02001102 lyd_diff_cb diff_cb, void *cb_data)
Michal Vaskod59035b2020-07-08 12:00:06 +02001103{
1104 const struct lyd_node *root;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001105 struct lyd_dup_inst *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001106 LY_ERR ret = LY_SUCCESS;
Michal Vaskod59035b2020-07-08 12:00:06 +02001107
1108 LY_LIST_FOR(diff, root) {
1109 if (mod && (lyd_owner_module(root) != mod)) {
1110 /* skip data nodes from different modules */
1111 continue;
1112 }
1113
1114 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001115 ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst);
1116 if (ret) {
1117 break;
1118 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001119 }
1120
Michal Vaskod7c048c2021-05-18 16:12:55 +02001121 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001122 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001123}
1124
1125API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001126lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff)
Michal Vaskod59035b2020-07-08 12:00:06 +02001127{
1128 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
1129}
Michal Vaskoe6323f62020-07-09 15:49:02 +02001130
1131/**
1132 * @brief Update operations on a diff node when the new operation is NONE.
1133 *
1134 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001135 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001136 * @param[in] src_diff Current source diff node.
1137 * @return LY_ERR value.
1138 */
1139static LY_ERR
1140lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1141{
1142 switch (cur_op) {
1143 case LYD_DIFF_OP_NONE:
1144 case LYD_DIFF_OP_CREATE:
1145 case LYD_DIFF_OP_REPLACE:
1146 if (src_diff->schema->nodetype & LYD_NODE_TERM) {
1147 /* NONE on a term means only its dflt flag was changed */
1148 diff_match->flags &= ~LYD_DEFAULT;
1149 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1150 }
1151 break;
1152 default:
1153 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001154 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001155 }
1156
1157 return LY_SUCCESS;
1158}
1159
1160/**
1161 * @brief Remove an attribute from a node.
1162 *
1163 * @param[in] node Node with the metadata.
1164 * @param[in] name Metadata name.
1165 */
1166static void
1167lyd_diff_del_meta(struct lyd_node *node, const char *name)
1168{
1169 struct lyd_meta *meta;
1170
1171 LY_LIST_FOR(node->meta, meta) {
1172 if (!strcmp(meta->name, name) && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001173 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001174 return;
1175 }
1176 }
1177
1178 assert(0);
1179}
1180
1181/**
1182 * @brief Set a specific operation of a node. Delete the previous operation, if any.
Michal Vasko871a0252020-11-11 18:35:24 +01001183 * Does not change the default flag.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001184 *
1185 * @param[in] node Node to change.
1186 * @param[in] op Operation to set.
1187 * @return LY_ERR value.
1188 */
1189static LY_ERR
1190lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op)
1191{
1192 struct lyd_meta *meta;
1193
1194 LY_LIST_FOR(node->meta, meta) {
1195 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001196 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001197 break;
1198 }
1199 }
1200
Michal Vasko871a0252020-11-11 18:35:24 +01001201 return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001202}
1203
1204/**
1205 * @brief Update operations on a diff node when the new operation is REPLACE.
1206 *
1207 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001208 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001209 * @param[in] src_diff Current source diff node.
1210 * @return LY_ERR value.
1211 */
1212static LY_ERR
1213lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1214{
1215 LY_ERR ret;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001216 const char *str_val, *meta_name, *orig_meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001217 struct lyd_meta *meta;
1218 const struct lys_module *mod;
1219 const struct lyd_node_any *any;
1220
1221 /* get "yang" module for the metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001222 mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001223 assert(mod);
1224
1225 switch (cur_op) {
1226 case LYD_DIFF_OP_REPLACE:
1227 case LYD_DIFF_OP_CREATE:
1228 switch (diff_match->schema->nodetype) {
1229 case LYS_LIST:
1230 case LYS_LEAFLIST:
Michal Vasko4231fb62020-07-13 13:54:47 +02001231 /* it was created/moved somewhere, but now it will be created/moved somewhere else,
Michal Vaskoe6323f62020-07-09 15:49:02 +02001232 * keep orig_key/orig_value (only replace oper) and replace key/value */
1233 assert(lysc_is_userordered(diff_match->schema));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001234 if (lysc_is_dup_inst_list(diff_match->schema)) {
1235 meta_name = "position";
1236 } else if (diff_match->schema->nodetype == LYS_LIST) {
1237 meta_name = "key";
1238 } else {
1239 meta_name = "value";
1240 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001241
1242 lyd_diff_del_meta(diff_match, meta_name);
1243 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001244 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001245 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001246 break;
1247 case LYS_LEAF:
1248 /* replaced with the exact same value, impossible */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001249 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001250 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001251 }
1252
Michal Vaskoe6323f62020-07-09 15:49:02 +02001253 /* modify the node value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001254 if (lyd_change_term(diff_match, lyd_get_value(src_diff))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001255 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001256 }
1257
Michal Vasko8caadab2020-11-05 17:38:15 +01001258 if (cur_op == LYD_DIFF_OP_REPLACE) {
1259 /* compare values whether there is any change at all */
1260 meta = lyd_find_meta(diff_match->meta, mod, "orig-value");
1261 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(diff_match)), LY_EINT);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001262 str_val = lyd_get_meta_value(meta);
Michal Vasko8caadab2020-11-05 17:38:15 +01001263 ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val));
1264 if (!ret) {
1265 /* values are the same, remove orig-value meta and set oper to NONE */
1266 lyd_free_meta_single(meta);
1267 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1268 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001269 }
1270
1271 /* modify the default flag */
1272 diff_match->flags &= ~LYD_DEFAULT;
1273 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1274 break;
1275 case LYS_ANYXML:
1276 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +02001277 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001278 /* replaced with the exact same value, impossible */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001279 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001280 }
1281
1282 /* modify the node value */
1283 any = (struct lyd_node_any *)src_diff;
1284 LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type));
1285 break;
1286 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001287 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001288 }
1289 break;
1290 case LYD_DIFF_OP_NONE:
1291 /* it is moved now */
1292 assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST));
1293
1294 /* change the operation */
1295 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1296
Michal Vaskoe78faec2021-04-08 17:24:43 +02001297 /* set orig-meta and meta */
1298 if (lysc_is_dup_inst_list(diff_match->schema)) {
1299 meta_name = "position";
1300 orig_meta_name = "orig-position";
1301 } else {
1302 meta_name = "key";
1303 orig_meta_name = "orig-key";
1304 }
1305
1306 meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001307 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001308 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001309
Michal Vaskoe78faec2021-04-08 17:24:43 +02001310 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001311 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001312 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001313 break;
1314 default:
1315 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001316 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001317 }
1318
1319 return LY_SUCCESS;
1320}
1321
1322/**
1323 * @brief Update operations in a diff node when the new operation is CREATE.
1324 *
1325 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001326 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001327 * @param[in] src_diff Current source diff node.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001328 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001329 * @return LY_ERR value.
1330 */
1331static LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001332lyd_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 +02001333{
1334 struct lyd_node *child;
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001335 const struct lysc_node_leaf *sleaf = NULL;
Michal Vasko871a0252020-11-11 18:35:24 +01001336 uint32_t trg_flags;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001337 const char *meta_name, *orig_meta_name;
1338 struct lyd_meta *meta, *orig_meta;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001339
1340 switch (cur_op) {
1341 case LYD_DIFF_OP_DELETE:
Michal Vasko871a0252020-11-11 18:35:24 +01001342 /* remember current flags */
1343 trg_flags = diff_match->flags;
1344
Michal Vaskoe78faec2021-04-08 17:24:43 +02001345 if (lysc_is_userordered(diff_match->schema)) {
1346 /* get anchor metadata */
1347 if (lysc_is_dup_inst_list(diff_match->schema)) {
1348 meta_name = "yang:position";
1349 orig_meta_name = "yang:orig-position";
1350 } else if (diff_match->schema->nodetype == LYS_LIST) {
1351 meta_name = "yang:key";
1352 orig_meta_name = "yang:orig-key";
1353 } else {
1354 meta_name = "yang:value";
1355 orig_meta_name = "yang:orig-value";
1356 }
1357 meta = lyd_find_meta(src_diff->meta, NULL, meta_name);
1358 orig_meta = lyd_find_meta(diff_match->meta, NULL, orig_meta_name);
1359 LY_CHECK_ERR_RET(!meta || !orig_meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
1360
1361 /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect
1362 * the anchors stored in the metadata */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001363 if (strcmp(lyd_get_meta_value(meta), lyd_get_meta_value(orig_meta))) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001364 /* deleted + created at another position -> operation REPLACE */
1365 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1366
1367 /* add anchor metadata */
1368 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
1369 } else {
1370 /* deleted + created at the same position -> operation NONE */
1371 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1372
1373 /* delete anchor metadata */
1374 lyd_free_meta_single(orig_meta);
1375 }
1376 } else if (diff_match->schema->nodetype == LYS_LEAF) {
1377 if (options & LYD_DIFF_MERGE_DEFAULTS) {
1378 /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */
1379 sleaf = (struct lysc_node_leaf *)diff_match->schema;
1380 }
1381
Radek Krejci55c4bd22021-04-26 08:09:04 +02001382 if (sleaf && sleaf->dflt && !sleaf->dflt->realtype->plugin->compare(sleaf->dflt,
1383 &((struct lyd_node_term *)src_diff)->value)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001384 /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */
1385 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1386 } else if (!lyd_compare_single(diff_match, src_diff, 0)) {
1387 /* deleted + created -> operation NONE */
1388 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1389 } else {
1390 /* we deleted it, but it was created with a different value -> operation REPLACE */
1391 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1392
1393 /* current value is the previous one (meta) */
1394 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-value",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001395 lyd_get_value(diff_match), 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001396
1397 /* update the value itself */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001398 LY_CHECK_RET(lyd_change_term(diff_match, lyd_get_value(src_diff)));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001399 }
1400 } else {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001401 /* deleted + created -> operation NONE */
1402 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001403 }
1404
1405 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
Michal Vasko4b715ca2020-11-11 18:39:57 +01001406 /* add orig-dflt metadata */
1407 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1408 trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
1409
Michal Vaskoe6323f62020-07-09 15:49:02 +02001410 /* update dflt flag itself */
1411 diff_match->flags &= ~LYD_DEFAULT;
1412 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001413 }
1414
1415 /* but the operation of its children should remain DELETE */
1416 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
1417 LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001418 }
1419 break;
1420 default:
1421 /* create and replace operations are not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001422 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001423 }
1424
1425 return LY_SUCCESS;
1426}
1427
1428/**
1429 * @brief Update operations on a diff node when the new operation is DELETE.
1430 *
1431 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001432 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001433 * @param[in] src_diff Current source diff node.
1434 * @return LY_ERR value.
1435 */
1436static LY_ERR
1437lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1438{
1439 struct lyd_node *next, *child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001440 struct lyd_meta *meta;
1441 const char *meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001442
1443 /* we can delete only exact existing nodes */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001444 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 +02001445
1446 switch (cur_op) {
1447 case LYD_DIFF_OP_CREATE:
1448 /* it was created, but then deleted -> set NONE operation */
1449 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1450
1451 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
1452 /* add orig-default meta because it is expected */
Michal Vasko871a0252020-11-11 18:35:24 +01001453 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1454 diff_match->flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001455 } else if (!lysc_is_dup_inst_list(diff_match->schema)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001456 /* keep operation for all descendants (for now) */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001457 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001458 LY_CHECK_RET(lyd_diff_change_op(child, cur_op));
1459 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001460 } /* else key-less list, for which all the descendants act as keys */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001461 break;
1462 case LYD_DIFF_OP_REPLACE:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001463 /* similar to none operation but also remove the redundant metadata */
1464 if (lysc_is_userordered(diff_match->schema)) {
1465 if (lysc_is_dup_inst_list(diff_match->schema)) {
1466 meta_name = "position";
1467 } else if (diff_match->schema->nodetype == LYS_LIST) {
1468 meta_name = "key";
1469 } else {
1470 meta_name = "value";
1471 }
1472 } else {
1473 assert(diff_match->schema->nodetype == LYS_LEAF);
1474
1475 /* switch value for the original one */
1476 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value");
1477 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001478 if (lyd_change_term(diff_match, lyd_get_meta_value(meta))) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001479 LOGINT_RET(LYD_CTX(src_diff));
1480 }
1481
1482 /* switch default for the original one, then remove the meta */
1483 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default");
1484 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
1485 diff_match->flags &= ~LYD_DEFAULT;
1486 if (meta->value.boolean) {
1487 diff_match->flags |= LYD_DEFAULT;
1488 }
1489 lyd_free_meta_single(meta);
1490
1491 meta_name = "orig-value";
1492 }
1493 lyd_diff_del_meta(diff_match, meta_name);
1494
Radek Krejcif13b87b2020-12-01 22:02:17 +01001495 /* fall through */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001496 case LYD_DIFF_OP_NONE:
1497 /* it was not modified, but should be deleted -> set DELETE operation */
1498 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
1499
Michal Vasko5632e0d2020-07-31 14:13:37 +02001500 /* all descendants not in the diff will be deleted and redundant in the diff, so remove them */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001501 LY_LIST_FOR_SAFE(lyd_child_no_keys(diff_match), next, child) {
1502 if (lyd_find_sibling_first(lyd_child(src_diff), child, NULL) == LY_ENOTFOUND) {
Michal Vasko5632e0d2020-07-31 14:13:37 +02001503 lyd_free_tree(child);
1504 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001505 }
1506 break;
1507 default:
1508 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001509 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001510 }
1511
1512 return LY_SUCCESS;
1513}
1514
1515/**
1516 * @brief Check whether this diff node is redundant (does not change data).
1517 *
1518 * @param[in] diff Diff node.
1519 * @return 0 if not, non-zero if it is.
1520 */
1521static int
1522lyd_diff_is_redundant(struct lyd_node *diff)
1523{
1524 enum lyd_diff_op op;
1525 struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL;
1526 struct lyd_node *child;
1527 const struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001528 const char *str, *orig_meta_name, *meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001529
1530 assert(diff);
1531
Michal Vaskoe78faec2021-04-08 17:24:43 +02001532 if (lysc_is_dup_inst_list(diff->schema)) {
1533 /* all descendants are keys */
1534 child = NULL;
1535 } else {
1536 child = lyd_child_no_keys(diff);
1537 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02001538 mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001539 assert(mod);
1540
1541 /* get node operation */
Michal Vasko53bf6f22020-07-14 08:23:40 +02001542 LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001543
1544 if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001545 /* get metadata names */
1546 if (lysc_is_dup_inst_list(diff->schema)) {
1547 meta_name = "position";
1548 orig_meta_name = "orig-position";
1549 } else if (diff->schema->nodetype == LYS_LIST) {
1550 meta_name = "key";
1551 orig_meta_name = "orig-key";
1552 } else {
1553 meta_name = "value";
1554 orig_meta_name = "orig-value";
1555 }
1556
Michal Vaskoe6323f62020-07-09 15:49:02 +02001557 /* check for redundant move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001558 orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name);
1559 val_meta = lyd_find_meta(diff->meta, mod, meta_name);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001560 assert(orig_val_meta && val_meta);
1561
1562 if (!lyd_compare_meta(orig_val_meta, val_meta)) {
1563 /* there is actually no move */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001564 lyd_free_meta_single(orig_val_meta);
1565 lyd_free_meta_single(val_meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001566 if (child) {
1567 /* change operation to NONE, we have siblings */
1568 lyd_diff_change_op(diff, LYD_DIFF_OP_NONE);
1569 return 0;
1570 }
1571
1572 /* redundant node, BUT !!
1573 * In diff the move operation is always converted to be INSERT_AFTER, which is fine
1574 * because the data that this is applied on should not change for the diff lifetime.
1575 * However, when we are merging 2 diffs, this conversion is actually lossy because
1576 * if the data change, the move operation can also change its meaning. In this specific
1577 * case the move operation will be lost. But it can be considered a feature, it is not supported.
1578 */
1579 return 1;
1580 }
1581 } else if ((op == LYD_DIFF_OP_NONE) && (diff->schema->nodetype & LYD_NODE_TERM)) {
1582 /* check whether at least the default flags are different */
1583 meta = lyd_find_meta(diff->meta, mod, "orig-default");
1584 assert(meta);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001585 str = lyd_get_meta_value(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001586
1587 /* if previous and current dflt flags are the same, this node is redundant */
1588 if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) {
1589 return 1;
1590 }
1591 return 0;
1592 }
1593
1594 if (!child && (op == LYD_DIFF_OP_NONE)) {
1595 return 1;
1596 }
1597
1598 return 0;
1599}
1600
1601/**
Michal Vaskoe78faec2021-04-08 17:24:43 +02001602 * @brief Merge sysrepo diff subtree with another diff, recursively.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001603 *
1604 * @param[in] src_diff Source diff node.
1605 * @param[in] diff_parent Current sysrepo diff parent.
1606 * @param[in] diff_cb Optional diff callback.
1607 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001608 * @param[in,out] dup_inst Duplicate instance cache for all @p src_diff siblings.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001609 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001610 * @param[in,out] diff Diff root node.
1611 * @return LY_ERR value.
1612 */
1613static LY_ERR
1614lyd_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 +02001615 struct lyd_dup_inst **dup_inst, uint16_t options, struct lyd_node **diff)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001616{
1617 LY_ERR ret = LY_SUCCESS;
1618 struct lyd_node *child, *diff_node = NULL;
1619 enum lyd_diff_op src_op, cur_op;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001620 struct lyd_dup_inst *child_dup_inst = NULL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001621
1622 /* get source node operation */
1623 LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op));
1624
1625 /* find an equal node in the current diff */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001626 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 +02001627
1628 if (diff_node) {
1629 /* get target (current) operation */
1630 LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op));
1631
1632 /* merge operations */
1633 switch (src_op) {
1634 case LYD_DIFF_OP_REPLACE:
1635 ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff);
1636 break;
1637 case LYD_DIFF_OP_CREATE:
Michal Vasko1dc0a842021-02-04 11:04:57 +01001638 if ((cur_op == LYD_DIFF_OP_CREATE) && lysc_is_dup_inst_list(diff_node->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001639 /* special case of creating duplicate (leaf-)list instances */
Michal Vasko1dc0a842021-02-04 11:04:57 +01001640 goto add_diff;
1641 }
1642
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001643 ret = lyd_diff_merge_create(diff_node, cur_op, src_diff, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001644 break;
1645 case LYD_DIFF_OP_DELETE:
1646 ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff);
1647 break;
1648 case LYD_DIFF_OP_NONE:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001649 /* key-less list can never have "none" operation since all its descendants are acting as "keys" */
1650 assert((src_diff->schema->nodetype != LYS_LIST) || !lysc_is_dup_inst_list(src_diff->schema));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001651 ret = lyd_diff_merge_none(diff_node, cur_op, src_diff);
1652 break;
1653 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001654 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001655 }
1656 if (ret) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001657 LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001658 return ret;
1659 }
1660
1661 if (diff_cb) {
1662 /* call callback */
Michal Vaskobc5fba92020-08-07 12:14:39 +02001663 LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001664 }
1665
1666 /* update diff parent */
1667 diff_parent = diff_node;
1668
Michal Vaskoe78faec2021-04-08 17:24:43 +02001669 /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances),
1670 * so there is nothing to merge for these "keys" */
1671 if (!lysc_is_dup_inst_list(src_diff->schema)) {
1672 /* merge src_diff recursively */
1673 LY_LIST_FOR(lyd_child_no_keys(src_diff), child) {
1674 ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff);
1675 if (ret) {
1676 break;
1677 }
1678 }
Michal Vaskod7c048c2021-05-18 16:12:55 +02001679 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001680 LY_CHECK_RET(ret);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001681 }
1682 } else {
Michal Vasko1dc0a842021-02-04 11:04:57 +01001683add_diff:
Michal Vaskoe6323f62020-07-09 15:49:02 +02001684 /* add new diff node with all descendants */
Michal Vasko871a0252020-11-11 18:35:24 +01001685 LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS,
1686 &diff_node));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001687
1688 /* insert node into diff if not already */
1689 if (!diff_parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001690 lyd_insert_sibling(*diff, diff_node, diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001691 }
1692
1693 /* update operation */
1694 LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op));
1695
1696 if (diff_cb) {
Michal Vaskoe2af8412020-12-03 14:11:38 +01001697 /* call callback with no source diff node since it was duplicated and just added */
1698 LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001699 }
1700
1701 /* update diff parent */
1702 diff_parent = diff_node;
1703 }
1704
1705 /* remove any redundant nodes */
Michal Vaskob98d7082020-07-15 16:38:36 +02001706 if (lyd_diff_is_redundant(diff_parent)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001707 if (diff_parent == *diff) {
1708 *diff = (*diff)->next;
1709 }
1710 lyd_free_tree(diff_parent);
1711 }
1712
1713 return LY_SUCCESS;
1714}
1715
1716API LY_ERR
Michal Vaskofb737aa2020-08-06 13:53:53 +02001717lyd_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 +01001718 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001719{
1720 const struct lyd_node *src_root;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001721 struct lyd_dup_inst *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001722 LY_ERR ret = LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001723
1724 LY_LIST_FOR(src_diff, src_root) {
1725 if (mod && (lyd_owner_module(src_root) != mod)) {
1726 /* skip data nodes from different modules */
1727 continue;
1728 }
1729
1730 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001731 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 +02001732 }
1733
Michal Vaskoe78faec2021-04-08 17:24:43 +02001734cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +02001735 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001736 return ret;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001737}
1738
1739API LY_ERR
Michal Vasko04f85912020-08-07 12:14:58 +02001740lyd_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 +01001741 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vasko04f85912020-08-07 12:14:58 +02001742{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001743 LY_ERR ret;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001744 struct lyd_dup_inst *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001745
Michal Vasko04f85912020-08-07 12:14:58 +02001746 if (!src_sibling) {
1747 return LY_SUCCESS;
1748 }
1749
Michal Vaskoe78faec2021-04-08 17:24:43 +02001750 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 +02001751 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001752 return ret;
Michal Vasko04f85912020-08-07 12:14:58 +02001753}
1754
1755API LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001756lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001757{
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001758 return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001759}
Michal Vasko4231fb62020-07-13 13:54:47 +02001760
1761static LY_ERR
Michal Vaskobaba84e2021-02-05 16:33:30 +01001762lyd_diff_reverse_value(struct lyd_node *node, const struct lys_module *mod)
Michal Vasko4231fb62020-07-13 13:54:47 +02001763{
1764 LY_ERR ret = LY_SUCCESS;
1765 struct lyd_meta *meta;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001766 const char *val1 = NULL;
1767 char *val2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001768 uint32_t flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001769
Michal Vaskobaba84e2021-02-05 16:33:30 +01001770 assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA));
1771
1772 meta = lyd_find_meta(node->meta, mod, "orig-value");
1773 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001774
1775 /* orig-value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001776 val1 = lyd_get_meta_value(meta);
Michal Vasko4231fb62020-07-13 13:54:47 +02001777
1778 /* current value */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001779 if (node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001780 val2 = strdup(lyd_get_value(node));
Michal Vaskobaba84e2021-02-05 16:33:30 +01001781 } else {
1782 LY_CHECK_RET(lyd_any_value_str(node, &val2));
1783 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001784
1785 /* switch values, keep default flag */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001786 flags = node->flags;
1787 if (node->schema->nodetype == LYS_LEAF) {
1788 LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup);
1789 } else {
1790 union lyd_any_value anyval = {.str = val1};
1791 LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup);
1792 }
1793 node->flags = flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001794 LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup);
1795
1796cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001797 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001798 return ret;
1799}
1800
1801static LY_ERR
1802lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod)
1803{
1804 struct lyd_meta *meta;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001805 uint32_t flag1, flag2;
Michal Vasko4231fb62020-07-13 13:54:47 +02001806
1807 meta = lyd_find_meta(node->meta, mod, "orig-default");
Michal Vasko610e93b2020-11-09 20:58:32 +01001808 LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001809
1810 /* orig-default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001811 if (meta->value.boolean) {
Michal Vasko4231fb62020-07-13 13:54:47 +02001812 flag1 = LYD_DEFAULT;
1813 } else {
1814 flag1 = 0;
1815 }
1816
1817 /* current default */
1818 flag2 = node->flags & LYD_DEFAULT;
1819
Michal Vasko610e93b2020-11-09 20:58:32 +01001820 if (flag1 == flag2) {
1821 /* no default state change so nothing to reverse */
1822 return LY_SUCCESS;
1823 }
1824
Michal Vasko4231fb62020-07-13 13:54:47 +02001825 /* switch defaults */
1826 node->flags &= ~LYD_DEFAULT;
1827 node->flags |= flag1;
1828 LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false"));
1829
1830 return LY_SUCCESS;
1831}
1832
1833static LY_ERR
1834lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2)
1835{
1836 LY_ERR ret = LY_SUCCESS;
1837 struct lyd_meta *meta1, *meta2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001838 const char *val1 = NULL;
1839 char *val2 = NULL;
Michal Vasko4231fb62020-07-13 13:54:47 +02001840
1841 meta1 = lyd_find_meta(node->meta, mod, name1);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001842 LY_CHECK_ERR_RET(!meta1, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001843
1844 meta2 = lyd_find_meta(node->meta, mod, name2);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001845 LY_CHECK_ERR_RET(!meta2, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001846
1847 /* value1 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001848 val1 = lyd_get_meta_value(meta1);
Michal Vasko4231fb62020-07-13 13:54:47 +02001849
1850 /* value2 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001851 val2 = strdup(lyd_get_meta_value(meta2));
Michal Vasko4231fb62020-07-13 13:54:47 +02001852
1853 /* switch values */
1854 LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup);
1855 LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup);
1856
1857cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001858 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001859 return ret;
1860}
1861
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001862/**
1863 * @brief Remove specific operation from all the nodes in a subtree.
1864 *
1865 * @param[in] diff Diff subtree to process.
1866 * @param[in] op Only expected operation.
1867 * @return LY_ERR value.
1868 */
1869static LY_ERR
1870lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op)
1871{
1872 struct lyd_node *elem;
1873 struct lyd_meta *meta;
1874
1875 LYD_TREE_DFS_BEGIN(diff, elem) {
1876 meta = lyd_find_meta(elem->meta, NULL, "yang:operation");
1877 if (meta) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001878 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 +01001879 lyd_free_meta_single(meta);
1880 }
1881
1882 LYD_TREE_DFS_END(diff, elem);
1883 }
1884
1885 return LY_SUCCESS;
1886}
1887
Michal Vasko4231fb62020-07-13 13:54:47 +02001888API LY_ERR
Michal Vasko66535812020-08-11 08:44:22 +02001889lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff)
Michal Vasko4231fb62020-07-13 13:54:47 +02001890{
1891 LY_ERR ret = LY_SUCCESS;
1892 const struct lys_module *mod;
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001893 struct lyd_node *root, *elem, *iter;
Michal Vasko4231fb62020-07-13 13:54:47 +02001894 enum lyd_diff_op op;
1895
1896 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
1897
1898 if (!src_diff) {
1899 *diff = NULL;
1900 return LY_SUCCESS;
1901 }
1902
1903 /* duplicate diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001904 LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff));
Michal Vasko4231fb62020-07-13 13:54:47 +02001905
1906 /* find module with metadata needed for later */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001907 mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang");
1908 LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001909
1910 LY_LIST_FOR(*diff, root) {
Michal Vasko56daf732020-08-10 10:57:18 +02001911 LYD_TREE_DFS_BEGIN(root, elem) {
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001912 /* skip all keys */
1913 if (!lysc_is_key(elem->schema)) {
1914 /* find operation attribute, if any */
1915 LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001916
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001917 switch (op) {
1918 case LYD_DIFF_OP_CREATE:
1919 /* reverse create to delete */
1920 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01001921
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001922 /* check all the children for the same operation, nothing else is expected */
1923 LY_LIST_FOR(lyd_child(elem), iter) {
1924 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE);
1925 }
1926
Michal Vasko9e070522021-03-05 14:00:14 +01001927 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02001928 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001929 case LYD_DIFF_OP_DELETE:
1930 /* reverse delete to create */
1931 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01001932
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001933 /* check all the children for the same operation, nothing else is expected */
1934 LY_LIST_FOR(lyd_child(elem), iter) {
1935 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE);
1936 }
1937
Michal Vasko9e070522021-03-05 14:00:14 +01001938 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02001939 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001940 case LYD_DIFF_OP_REPLACE:
1941 switch (elem->schema->nodetype) {
1942 case LYS_LEAF:
1943 /* leaf value change */
1944 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
1945 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
1946 break;
Michal Vaskobaba84e2021-02-05 16:33:30 +01001947 case LYS_ANYXML:
1948 case LYS_ANYDATA:
1949 /* any value change */
1950 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
1951 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001952 case LYS_LEAFLIST:
1953 /* leaf-list move */
1954 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001955 if (lysc_is_dup_inst_list(elem->schema)) {
1956 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
1957 } else {
1958 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup);
1959 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001960 break;
1961 case LYS_LIST:
1962 /* list move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001963 if (lysc_is_dup_inst_list(elem->schema)) {
1964 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
1965 } else {
1966 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup);
1967 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001968 break;
1969 default:
1970 LOGINT(LYD_CTX(src_diff));
1971 ret = LY_EINT;
1972 goto cleanup;
1973 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001974 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001975 case LYD_DIFF_OP_NONE:
1976 switch (elem->schema->nodetype) {
1977 case LYS_LEAF:
1978 case LYS_LEAFLIST:
1979 /* default flag change */
1980 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
1981 break;
1982 default:
1983 /* nothing to do */
1984 break;
1985 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001986 break;
1987 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001988 }
1989
Michal Vasko56daf732020-08-10 10:57:18 +02001990 LYD_TREE_DFS_END(root, elem);
Michal Vasko4231fb62020-07-13 13:54:47 +02001991 }
1992 }
1993
1994cleanup:
1995 if (ret) {
1996 lyd_free_siblings(*diff);
1997 *diff = NULL;
1998 }
1999 return ret;
2000}