blob: 4971c6fe2f11a7f290fbe3fc56a9d3b53f1ef069 [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) {
628 assert(!(iter_first->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200629 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200630 /* skip default nodes */
631 continue;
632 }
633
Michal Vaskoe78faec2021-04-08 17:24:43 +0200634 /* find a match in the second tree */
635 LY_CHECK_GOTO(ret = lyd_diff_find_match(second, iter_first, options & LYD_DIFF_DEFAULTS, &dup_inst_second,
636 &match_second), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200637
638 if (lysc_is_userordered(iter_first->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200639 /* we are handling only user-ordered node delete now */
640 if (!match_second) {
641 /* get all the attributes */
642 LY_CHECK_GOTO(ret = lyd_diff_userord_attrs(iter_first, match_second, options, &userord, &op, &orig_default,
643 &value, &orig_value, &key, &orig_key, &position, &orig_position), cleanup);
644
645 /* there must be changes, it is deleted */
646 assert(op == LYD_DIFF_OP_DELETE);
647 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, position, orig_key, orig_position, diff);
648
649 free(orig_value);
650 free(key);
651 free(value);
652 free(position);
653 free(orig_key);
654 free(orig_position);
655 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200656 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200657 } else {
658 /* get all the attributes */
659 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
660
661 /* add into diff if there are any changes */
662 if (!ret) {
663 if (op == LYD_DIFF_OP_DELETE) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200664 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200665 } else {
Michal Vasko3c2dd6c2020-11-06 17:38:55 +0100666 assert(match_second);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200667 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200668 }
669
670 free(orig_value);
671 LY_CHECK_GOTO(ret, cleanup);
672 } else if (ret == LY_ENOT) {
673 ret = LY_SUCCESS;
674 } else {
675 goto cleanup;
676 }
677 }
678
679 /* check descendants, if any, recursively */
680 if (match_second) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200681 LY_CHECK_GOTO(ret = lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second),
682 options, 0, diff), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200683 }
684
685 if (nosiblings) {
686 break;
687 }
688 }
689
690 /* reset all cached positions */
691 LY_ARRAY_FOR(userord, u) {
692 userord[u].pos = 0;
693 }
694
695 /* compare second tree to the first tree - create, user-ordered move */
696 LY_LIST_FOR(second, iter_second) {
697 assert(!(iter_second->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200698 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200699 /* skip default nodes */
700 continue;
701 }
702
Michal Vaskoe78faec2021-04-08 17:24:43 +0200703 /* find a match in the first tree */
704 LY_CHECK_GOTO(ret = lyd_diff_find_match(first, iter_second, options & LYD_DIFF_DEFAULTS, &dup_inst_first,
705 &match_first), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200706
707 if (lysc_is_userordered(iter_second->schema)) {
708 /* get all the attributes */
709 ret = lyd_diff_userord_attrs(match_first, iter_second, options, &userord, &op, &orig_default,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200710 &value, &orig_value, &key, &orig_key, &position, &orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200711
712 /* add into diff if there are any changes */
713 if (!ret) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200714 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, position, orig_key,
715 orig_position, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200716
717 free(orig_value);
718 free(key);
719 free(value);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200720 free(position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200721 free(orig_key);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200722 free(orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200723 LY_CHECK_GOTO(ret, cleanup);
724 } else if (ret == LY_ENOT) {
725 ret = LY_SUCCESS;
726 } else {
727 goto cleanup;
728 }
729 } else if (!match_first) {
730 /* get all the attributes */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200731 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 +0200732
733 /* there must be changes, it is created */
734 assert(op == LYD_DIFF_OP_CREATE);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200735 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200736
737 free(orig_value);
738 LY_CHECK_GOTO(ret, cleanup);
739 } /* else was handled */
740
741 if (nosiblings) {
742 break;
743 }
744 }
745
746cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +0200747 lyd_dup_inst_free(dup_inst_first);
748 lyd_dup_inst_free(dup_inst_second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200749 LY_ARRAY_FOR(userord, u) {
750 LY_ARRAY_FREE(userord[u].inst);
751 }
752 LY_ARRAY_FREE(userord);
753 return ret;
754}
755
Michal Vasko3a41dff2020-07-15 14:30:28 +0200756static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200757lyd_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 +0200758{
759 const struct ly_ctx *ctx;
760
761 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
762
763 if (first) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200764 ctx = LYD_CTX(first);
Michal Vaskod59035b2020-07-08 12:00:06 +0200765 } else if (second) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200766 ctx = LYD_CTX(second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200767 } else {
768 ctx = NULL;
769 }
770
771 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
772 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
773 return LY_EINVAL;
774 }
775
776 *diff = NULL;
777
Michal Vasko3a41dff2020-07-15 14:30:28 +0200778 return lyd_diff_siblings_r(first, second, options, nosiblings, diff);
779}
780
781API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200782lyd_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 +0200783{
784 return lyd_diff(first, second, options, 1, diff);
785}
786
787API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200788lyd_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 +0200789{
790 return lyd_diff(first, second, options, 0, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200791}
792
793/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200794 * @brief Learn operation of a diff node.
795 *
796 * @param[in] diff_node Diff node.
797 * @param[out] op Operation.
Michal Vaskod59035b2020-07-08 12:00:06 +0200798 * @return LY_ERR value.
799 */
800static LY_ERR
Michal Vaskoe6323f62020-07-09 15:49:02 +0200801lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op)
Michal Vaskod59035b2020-07-08 12:00:06 +0200802{
803 struct lyd_meta *meta = NULL;
804 const struct lyd_node *diff_parent;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200805 const char *str;
Michal Vaskod59035b2020-07-08 12:00:06 +0200806
Michal Vasko9e685082021-01-29 14:49:09 +0100807 for (diff_parent = diff_node; diff_parent; diff_parent = lyd_parent(diff_parent)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200808 LY_LIST_FOR(diff_parent->meta, meta) {
809 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200810 str = lyd_get_meta_value(meta);
Michal Vaskod59035b2020-07-08 12:00:06 +0200811 if ((str[0] == 'r') && (diff_parent != diff_node)) {
812 /* we do not care about this operation if it's in our parent */
813 continue;
814 }
Michal Vaskoe6323f62020-07-09 15:49:02 +0200815 *op = lyd_diff_str2op(str);
Michal Vaskod59035b2020-07-08 12:00:06 +0200816 break;
817 }
818 }
819 if (meta) {
820 break;
821 }
822 }
Michal Vaskob7be7a82020-08-20 09:09:04 +0200823 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(diff_node)), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +0200824
Michal Vaskod59035b2020-07-08 12:00:06 +0200825 return LY_SUCCESS;
826}
827
828/**
829 * @brief Insert a diff node into a data tree.
830 *
831 * @param[in,out] first_node First sibling of the data tree.
832 * @param[in] parent_node Data tree sibling parent node.
833 * @param[in] new_node Node to insert.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200834 * @param[in] userord_anchor Optional anchor (key, value, or position) of relative (leaf-)list instance. If not set,
835 * the user-ordered instance will be inserted at the first position.
Michal Vaskod59035b2020-07-08 12:00:06 +0200836 * @return err_info, NULL on success.
837 */
838static LY_ERR
839lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200840 const char *userord_anchor)
Michal Vaskod59035b2020-07-08 12:00:06 +0200841{
842 LY_ERR ret;
843 struct lyd_node *anchor;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200844 uint32_t pos, anchor_pos;
845 int found;
Michal Vaskod59035b2020-07-08 12:00:06 +0200846
847 assert(new_node);
848
849 if (!*first_node) {
850 if (!parent_node) {
851 /* no parent or siblings */
852 *first_node = new_node;
853 return LY_SUCCESS;
854 }
855
856 /* simply insert into parent, no other children */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200857 if (userord_anchor) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200858 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
Michal Vasko69730152020-10-09 16:30:07 +0200859 new_node->schema->name);
Michal Vaskod59035b2020-07-08 12:00:06 +0200860 return LY_EINVAL;
861 }
Michal Vaskob104f112020-07-17 09:54:54 +0200862 return lyd_insert_child(parent_node, new_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200863 }
864
Michal Vasko9e685082021-01-29 14:49:09 +0100865 assert(!(*first_node)->parent || (lyd_parent(*first_node) == parent_node));
Michal Vaskod59035b2020-07-08 12:00:06 +0200866
Michal Vaskod59035b2020-07-08 12:00:06 +0200867 if (!lysc_is_userordered(new_node->schema)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200868 /* simple insert */
869 return lyd_insert_sibling(*first_node, new_node, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200870 }
871
Michal Vaskoe78faec2021-04-08 17:24:43 +0200872 if (userord_anchor) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200873 /* find the anchor sibling */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200874 if (lysc_is_dup_inst_list(new_node->schema)) {
875 anchor_pos = atoi(userord_anchor);
876 LY_CHECK_ERR_RET(!anchor_pos, LOGINT(LYD_CTX(new_node)), LY_EINT);
877
878 found = 0;
879 pos = 1;
880 LYD_LIST_FOR_INST(*first_node, new_node->schema, anchor) {
881 if (pos == anchor_pos) {
882 found = 1;
883 break;
884 }
885 ++pos;
886 }
887 if (!found) {
888 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
889 new_node->schema->name);
890 return LY_EINVAL;
891 }
892 } else {
893 ret = lyd_find_sibling_val(*first_node, new_node->schema, userord_anchor, 0, &anchor);
894 if (ret == LY_ENOTFOUND) {
895 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
896 new_node->schema->name);
897 return LY_EINVAL;
898 } else if (ret) {
899 return ret;
900 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200901 }
902
903 /* insert after */
904 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
905 assert(new_node->prev == anchor);
906 if (*first_node == new_node) {
907 *first_node = anchor;
908 }
909 } else {
910 if ((*first_node)->schema->flags & LYS_KEY) {
911 assert(parent_node && (parent_node->schema->nodetype == LYS_LIST));
912
913 /* find last key */
914 anchor = *first_node;
915 while (anchor->next && (anchor->next->schema->flags & LYS_KEY)) {
916 anchor = anchor->next;
917 }
918 /* insert after the last key */
919 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
920 } else {
921 /* insert at the beginning */
922 LY_CHECK_RET(lyd_insert_before(*first_node, new_node));
923 *first_node = new_node;
924 }
925 }
926
927 return LY_SUCCESS;
928}
929
930/**
931 * @brief Apply diff subtree on data tree nodes, recursively.
932 *
933 * @param[in,out] first_node First sibling of the data tree.
934 * @param[in] parent_node Parent of the first sibling.
935 * @param[in] diff_node Current diff node.
Michal Vaskoe6323f62020-07-09 15:49:02 +0200936 * @param[in] diff_cb Optional diff callback.
937 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200938 * @param[in,out] dup_inst Duplicate instance cache for all @p diff_node siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +0200939 * @return LY_ERR value.
940 */
941static LY_ERR
942lyd_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 +0200943 lyd_diff_cb diff_cb, void *cb_data, struct lyd_dup_inst **dup_inst)
Michal Vaskod59035b2020-07-08 12:00:06 +0200944{
945 LY_ERR ret;
946 struct lyd_node *match, *diff_child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200947 const char *str_val, *meta_str;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200948 enum lyd_diff_op op;
949 struct lyd_meta *meta;
Michal Vaskod7c048c2021-05-18 16:12:55 +0200950 struct lyd_dup_inst *child_dup_inst = NULL;
Michal Vaskob7be7a82020-08-20 09:09:04 +0200951 const struct ly_ctx *ctx = LYD_CTX(diff_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200952
953 /* read all the valid attributes */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200954 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op));
Michal Vaskod59035b2020-07-08 12:00:06 +0200955
Michal Vaskoe6323f62020-07-09 15:49:02 +0200956 /* handle specific user-ordered (leaf-)lists operations separately */
957 if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) {
958 if (op == LYD_DIFF_OP_REPLACE) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200959 /* find the node (we must have some siblings because the node was only moved) */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200960 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
961 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +0200962 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200963 /* duplicate the node */
964 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +0200965 }
966
Michal Vaskoe78faec2021-04-08 17:24:43 +0200967 /* get "key", "value", or "position" metadata string value */
968 if (lysc_is_dup_inst_list(diff_node->schema)) {
969 meta_str = "yang:position";
970 } else if (diff_node->schema->nodetype == LYS_LIST) {
971 meta_str = "yang:key";
972 } else {
973 meta_str = "yang:value";
974 }
975 meta = lyd_find_meta(diff_node->meta, NULL, meta_str);
976 LY_CHECK_ERR_RET(!meta, LOGINT(ctx), LY_EINT);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200977 str_val = lyd_get_meta_value(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200978
Michal Vaskod59035b2020-07-08 12:00:06 +0200979 /* insert/move the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200980 if (str_val[0]) {
981 ret = lyd_diff_insert(first_node, parent_node, match, str_val);
Michal Vaskod59035b2020-07-08 12:00:06 +0200982 } else {
983 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
984 }
985 if (ret) {
Michal Vaskoe6323f62020-07-09 15:49:02 +0200986 if (op == LYD_DIFF_OP_CREATE) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200987 lyd_free_tree(match);
988 }
989 return ret;
990 }
991
992 goto next_iter_r;
993 }
994
995 /* apply operation */
Michal Vaskoe6323f62020-07-09 15:49:02 +0200996 switch (op) {
997 case LYD_DIFF_OP_NONE:
Michal Vaskod59035b2020-07-08 12:00:06 +0200998 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200999 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
1000 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001001
1002 if (match->schema->nodetype & LYD_NODE_TERM) {
1003 /* special case of only dflt flag change */
1004 if (diff_node->flags & LYD_DEFAULT) {
1005 match->flags |= LYD_DEFAULT;
1006 } else {
1007 match->flags &= ~LYD_DEFAULT;
1008 }
1009 } else {
1010 /* none operation on nodes without children is redundant and hence forbidden */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001011 if (!lyd_child_no_keys(diff_node)) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001012 LOGINT_RET(ctx);
1013 }
1014 }
1015 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001016 case LYD_DIFF_OP_CREATE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001017 /* duplicate the node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001018 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001019
1020 /* insert it at the end */
1021 ret = 0;
Michal Vaskob104f112020-07-17 09:54:54 +02001022 if (parent_node) {
1023 ret = lyd_insert_child(parent_node, match);
Michal Vaskod59035b2020-07-08 12:00:06 +02001024 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001025 ret = lyd_insert_sibling(*first_node, match, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001026 }
1027 if (ret) {
1028 lyd_free_tree(match);
1029 return ret;
1030 }
1031
1032 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001033 case LYD_DIFF_OP_DELETE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001034 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001035 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
1036 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001037
1038 /* remove it */
1039 if ((match == *first_node) && !match->parent) {
1040 assert(!parent_node);
1041 /* we have removed the top-level node */
1042 *first_node = (*first_node)->next;
1043 }
1044 lyd_free_tree(match);
1045
1046 /* we are not going recursively in this case, the whole subtree was already deleted */
1047 return LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001048 case LYD_DIFF_OP_REPLACE:
Michal Vaskobaba84e2021-02-05 16:33:30 +01001049 LY_CHECK_ERR_RET(!(diff_node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA)), LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001050
1051 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001052 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
1053 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001054
Michal Vaskobaba84e2021-02-05 16:33:30 +01001055 /* update the value */
1056 if (diff_node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001057 ret = lyd_change_term(match, lyd_get_value(diff_node));
Michal Vaskobaba84e2021-02-05 16:33:30 +01001058 if (ret && (ret != LY_EEXIST)) {
1059 LOGINT_RET(ctx);
1060 }
1061 } else {
1062 struct lyd_node_any *any = (struct lyd_node_any *)diff_node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001063 LY_CHECK_RET(lyd_any_copy_value(match, &any->value, any->value_type));
Michal Vaskod59035b2020-07-08 12:00:06 +02001064 }
1065
1066 /* with flags */
1067 match->flags = diff_node->flags;
1068 break;
1069 default:
1070 LOGINT_RET(ctx);
1071 }
1072
1073next_iter_r:
1074 if (diff_cb) {
1075 /* call callback */
1076 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
1077 }
1078
1079 /* apply diff recursively */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001080 ret = LY_SUCCESS;
Radek Krejcia1c1e542020-09-29 16:06:52 +02001081 LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001082 ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst);
1083 if (ret) {
1084 break;
1085 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001086 }
1087
Michal Vaskod7c048c2021-05-18 16:12:55 +02001088 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001089 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001090}
1091
1092API LY_ERR
1093lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +02001094 lyd_diff_cb diff_cb, void *cb_data)
Michal Vaskod59035b2020-07-08 12:00:06 +02001095{
1096 const struct lyd_node *root;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001097 struct lyd_dup_inst *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001098 LY_ERR ret = LY_SUCCESS;
Michal Vaskod59035b2020-07-08 12:00:06 +02001099
1100 LY_LIST_FOR(diff, root) {
1101 if (mod && (lyd_owner_module(root) != mod)) {
1102 /* skip data nodes from different modules */
1103 continue;
1104 }
1105
1106 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001107 ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst);
1108 if (ret) {
1109 break;
1110 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001111 }
1112
Michal Vaskod7c048c2021-05-18 16:12:55 +02001113 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001114 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001115}
1116
1117API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001118lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff)
Michal Vaskod59035b2020-07-08 12:00:06 +02001119{
1120 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
1121}
Michal Vaskoe6323f62020-07-09 15:49:02 +02001122
1123/**
1124 * @brief Update operations on a diff node when the new operation is NONE.
1125 *
1126 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001127 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001128 * @param[in] src_diff Current source diff node.
1129 * @return LY_ERR value.
1130 */
1131static LY_ERR
1132lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1133{
1134 switch (cur_op) {
1135 case LYD_DIFF_OP_NONE:
1136 case LYD_DIFF_OP_CREATE:
1137 case LYD_DIFF_OP_REPLACE:
1138 if (src_diff->schema->nodetype & LYD_NODE_TERM) {
1139 /* NONE on a term means only its dflt flag was changed */
1140 diff_match->flags &= ~LYD_DEFAULT;
1141 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1142 }
1143 break;
1144 default:
1145 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001146 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001147 }
1148
1149 return LY_SUCCESS;
1150}
1151
1152/**
1153 * @brief Remove an attribute from a node.
1154 *
1155 * @param[in] node Node with the metadata.
1156 * @param[in] name Metadata name.
1157 */
1158static void
1159lyd_diff_del_meta(struct lyd_node *node, const char *name)
1160{
1161 struct lyd_meta *meta;
1162
1163 LY_LIST_FOR(node->meta, meta) {
1164 if (!strcmp(meta->name, name) && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001165 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001166 return;
1167 }
1168 }
1169
1170 assert(0);
1171}
1172
1173/**
1174 * @brief Set a specific operation of a node. Delete the previous operation, if any.
Michal Vasko871a0252020-11-11 18:35:24 +01001175 * Does not change the default flag.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001176 *
1177 * @param[in] node Node to change.
1178 * @param[in] op Operation to set.
1179 * @return LY_ERR value.
1180 */
1181static LY_ERR
1182lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op)
1183{
1184 struct lyd_meta *meta;
1185
1186 LY_LIST_FOR(node->meta, meta) {
1187 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001188 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001189 break;
1190 }
1191 }
1192
Michal Vasko871a0252020-11-11 18:35:24 +01001193 return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001194}
1195
1196/**
1197 * @brief Update operations on a diff node when the new operation is REPLACE.
1198 *
1199 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001200 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001201 * @param[in] src_diff Current source diff node.
1202 * @return LY_ERR value.
1203 */
1204static LY_ERR
1205lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1206{
1207 LY_ERR ret;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001208 const char *str_val, *meta_name, *orig_meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001209 struct lyd_meta *meta;
1210 const struct lys_module *mod;
1211 const struct lyd_node_any *any;
1212
1213 /* get "yang" module for the metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001214 mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001215 assert(mod);
1216
1217 switch (cur_op) {
1218 case LYD_DIFF_OP_REPLACE:
1219 case LYD_DIFF_OP_CREATE:
1220 switch (diff_match->schema->nodetype) {
1221 case LYS_LIST:
1222 case LYS_LEAFLIST:
Michal Vasko4231fb62020-07-13 13:54:47 +02001223 /* it was created/moved somewhere, but now it will be created/moved somewhere else,
Michal Vaskoe6323f62020-07-09 15:49:02 +02001224 * keep orig_key/orig_value (only replace oper) and replace key/value */
1225 assert(lysc_is_userordered(diff_match->schema));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001226 if (lysc_is_dup_inst_list(diff_match->schema)) {
1227 meta_name = "position";
1228 } else if (diff_match->schema->nodetype == LYS_LIST) {
1229 meta_name = "key";
1230 } else {
1231 meta_name = "value";
1232 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001233
1234 lyd_diff_del_meta(diff_match, meta_name);
1235 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001236 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001237 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001238 break;
1239 case LYS_LEAF:
1240 /* replaced with the exact same value, impossible */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001241 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001242 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001243 }
1244
Michal Vaskoe6323f62020-07-09 15:49:02 +02001245 /* modify the node value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001246 if (lyd_change_term(diff_match, lyd_get_value(src_diff))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001247 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001248 }
1249
Michal Vasko8caadab2020-11-05 17:38:15 +01001250 if (cur_op == LYD_DIFF_OP_REPLACE) {
1251 /* compare values whether there is any change at all */
1252 meta = lyd_find_meta(diff_match->meta, mod, "orig-value");
1253 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(diff_match)), LY_EINT);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001254 str_val = lyd_get_meta_value(meta);
Michal Vasko8caadab2020-11-05 17:38:15 +01001255 ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val));
1256 if (!ret) {
1257 /* values are the same, remove orig-value meta and set oper to NONE */
1258 lyd_free_meta_single(meta);
1259 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1260 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001261 }
1262
1263 /* modify the default flag */
1264 diff_match->flags &= ~LYD_DEFAULT;
1265 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1266 break;
1267 case LYS_ANYXML:
1268 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +02001269 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001270 /* replaced with the exact same value, impossible */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001271 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001272 }
1273
1274 /* modify the node value */
1275 any = (struct lyd_node_any *)src_diff;
1276 LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type));
1277 break;
1278 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001279 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001280 }
1281 break;
1282 case LYD_DIFF_OP_NONE:
1283 /* it is moved now */
1284 assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST));
1285
1286 /* change the operation */
1287 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1288
Michal Vaskoe78faec2021-04-08 17:24:43 +02001289 /* set orig-meta and meta */
1290 if (lysc_is_dup_inst_list(diff_match->schema)) {
1291 meta_name = "position";
1292 orig_meta_name = "orig-position";
1293 } else {
1294 meta_name = "key";
1295 orig_meta_name = "orig-key";
1296 }
1297
1298 meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001299 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001300 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001301
Michal Vaskoe78faec2021-04-08 17:24:43 +02001302 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001303 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001304 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001305 break;
1306 default:
1307 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001308 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001309 }
1310
1311 return LY_SUCCESS;
1312}
1313
1314/**
1315 * @brief Update operations in a diff node when the new operation is CREATE.
1316 *
1317 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001318 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001319 * @param[in] src_diff Current source diff node.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001320 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001321 * @return LY_ERR value.
1322 */
1323static LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001324lyd_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 +02001325{
1326 struct lyd_node *child;
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001327 const struct lysc_node_leaf *sleaf = NULL;
Michal Vasko871a0252020-11-11 18:35:24 +01001328 uint32_t trg_flags;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001329 const char *meta_name, *orig_meta_name;
1330 struct lyd_meta *meta, *orig_meta;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001331
1332 switch (cur_op) {
1333 case LYD_DIFF_OP_DELETE:
Michal Vasko871a0252020-11-11 18:35:24 +01001334 /* remember current flags */
1335 trg_flags = diff_match->flags;
1336
Michal Vaskoe78faec2021-04-08 17:24:43 +02001337 if (lysc_is_userordered(diff_match->schema)) {
1338 /* get anchor metadata */
1339 if (lysc_is_dup_inst_list(diff_match->schema)) {
1340 meta_name = "yang:position";
1341 orig_meta_name = "yang:orig-position";
1342 } else if (diff_match->schema->nodetype == LYS_LIST) {
1343 meta_name = "yang:key";
1344 orig_meta_name = "yang:orig-key";
1345 } else {
1346 meta_name = "yang:value";
1347 orig_meta_name = "yang:orig-value";
1348 }
1349 meta = lyd_find_meta(src_diff->meta, NULL, meta_name);
1350 orig_meta = lyd_find_meta(diff_match->meta, NULL, orig_meta_name);
1351 LY_CHECK_ERR_RET(!meta || !orig_meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
1352
1353 /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect
1354 * the anchors stored in the metadata */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001355 if (strcmp(lyd_get_meta_value(meta), lyd_get_meta_value(orig_meta))) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001356 /* deleted + created at another position -> operation REPLACE */
1357 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1358
1359 /* add anchor metadata */
1360 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
1361 } else {
1362 /* deleted + created at the same position -> operation NONE */
1363 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1364
1365 /* delete anchor metadata */
1366 lyd_free_meta_single(orig_meta);
1367 }
1368 } else if (diff_match->schema->nodetype == LYS_LEAF) {
1369 if (options & LYD_DIFF_MERGE_DEFAULTS) {
1370 /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */
1371 sleaf = (struct lysc_node_leaf *)diff_match->schema;
1372 }
1373
Radek Krejci55c4bd22021-04-26 08:09:04 +02001374 if (sleaf && sleaf->dflt && !sleaf->dflt->realtype->plugin->compare(sleaf->dflt,
1375 &((struct lyd_node_term *)src_diff)->value)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001376 /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */
1377 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1378 } else if (!lyd_compare_single(diff_match, src_diff, 0)) {
1379 /* deleted + created -> operation NONE */
1380 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1381 } else {
1382 /* we deleted it, but it was created with a different value -> operation REPLACE */
1383 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1384
1385 /* current value is the previous one (meta) */
1386 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-value",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001387 lyd_get_value(diff_match), 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001388
1389 /* update the value itself */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001390 LY_CHECK_RET(lyd_change_term(diff_match, lyd_get_value(src_diff)));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001391 }
1392 } else {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001393 /* deleted + created -> operation NONE */
1394 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001395 }
1396
1397 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
Michal Vasko4b715ca2020-11-11 18:39:57 +01001398 /* add orig-dflt metadata */
1399 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1400 trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
1401
Michal Vaskoe6323f62020-07-09 15:49:02 +02001402 /* update dflt flag itself */
1403 diff_match->flags &= ~LYD_DEFAULT;
1404 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001405 }
1406
1407 /* but the operation of its children should remain DELETE */
1408 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
1409 LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001410 }
1411 break;
1412 default:
1413 /* create and replace operations are not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001414 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001415 }
1416
1417 return LY_SUCCESS;
1418}
1419
1420/**
1421 * @brief Update operations on a diff node when the new operation is DELETE.
1422 *
1423 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001424 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001425 * @param[in] src_diff Current source diff node.
1426 * @return LY_ERR value.
1427 */
1428static LY_ERR
1429lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1430{
1431 struct lyd_node *next, *child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001432 struct lyd_meta *meta;
1433 const char *meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001434
1435 /* we can delete only exact existing nodes */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001436 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 +02001437
1438 switch (cur_op) {
1439 case LYD_DIFF_OP_CREATE:
1440 /* it was created, but then deleted -> set NONE operation */
1441 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1442
1443 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
1444 /* add orig-default meta because it is expected */
Michal Vasko871a0252020-11-11 18:35:24 +01001445 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1446 diff_match->flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001447 } else if (!lysc_is_dup_inst_list(diff_match->schema)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001448 /* keep operation for all descendants (for now) */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001449 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001450 LY_CHECK_RET(lyd_diff_change_op(child, cur_op));
1451 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001452 } /* else key-less list, for which all the descendants act as keys */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001453 break;
1454 case LYD_DIFF_OP_REPLACE:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001455 /* similar to none operation but also remove the redundant metadata */
1456 if (lysc_is_userordered(diff_match->schema)) {
1457 if (lysc_is_dup_inst_list(diff_match->schema)) {
1458 meta_name = "position";
1459 } else if (diff_match->schema->nodetype == LYS_LIST) {
1460 meta_name = "key";
1461 } else {
1462 meta_name = "value";
1463 }
1464 } else {
1465 assert(diff_match->schema->nodetype == LYS_LEAF);
1466
1467 /* switch value for the original one */
1468 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value");
1469 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001470 if (lyd_change_term(diff_match, lyd_get_meta_value(meta))) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001471 LOGINT_RET(LYD_CTX(src_diff));
1472 }
1473
1474 /* switch default for the original one, then remove the meta */
1475 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default");
1476 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
1477 diff_match->flags &= ~LYD_DEFAULT;
1478 if (meta->value.boolean) {
1479 diff_match->flags |= LYD_DEFAULT;
1480 }
1481 lyd_free_meta_single(meta);
1482
1483 meta_name = "orig-value";
1484 }
1485 lyd_diff_del_meta(diff_match, meta_name);
1486
Radek Krejcif13b87b2020-12-01 22:02:17 +01001487 /* fall through */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001488 case LYD_DIFF_OP_NONE:
1489 /* it was not modified, but should be deleted -> set DELETE operation */
1490 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
1491
Michal Vasko5632e0d2020-07-31 14:13:37 +02001492 /* all descendants not in the diff will be deleted and redundant in the diff, so remove them */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001493 LY_LIST_FOR_SAFE(lyd_child_no_keys(diff_match), next, child) {
1494 if (lyd_find_sibling_first(lyd_child(src_diff), child, NULL) == LY_ENOTFOUND) {
Michal Vasko5632e0d2020-07-31 14:13:37 +02001495 lyd_free_tree(child);
1496 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001497 }
1498 break;
1499 default:
1500 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001501 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001502 }
1503
1504 return LY_SUCCESS;
1505}
1506
1507/**
1508 * @brief Check whether this diff node is redundant (does not change data).
1509 *
1510 * @param[in] diff Diff node.
1511 * @return 0 if not, non-zero if it is.
1512 */
1513static int
1514lyd_diff_is_redundant(struct lyd_node *diff)
1515{
1516 enum lyd_diff_op op;
1517 struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL;
1518 struct lyd_node *child;
1519 const struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001520 const char *str, *orig_meta_name, *meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001521
1522 assert(diff);
1523
Michal Vaskoe78faec2021-04-08 17:24:43 +02001524 if (lysc_is_dup_inst_list(diff->schema)) {
1525 /* all descendants are keys */
1526 child = NULL;
1527 } else {
1528 child = lyd_child_no_keys(diff);
1529 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02001530 mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001531 assert(mod);
1532
1533 /* get node operation */
Michal Vasko53bf6f22020-07-14 08:23:40 +02001534 LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001535
1536 if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001537 /* get metadata names */
1538 if (lysc_is_dup_inst_list(diff->schema)) {
1539 meta_name = "position";
1540 orig_meta_name = "orig-position";
1541 } else if (diff->schema->nodetype == LYS_LIST) {
1542 meta_name = "key";
1543 orig_meta_name = "orig-key";
1544 } else {
1545 meta_name = "value";
1546 orig_meta_name = "orig-value";
1547 }
1548
Michal Vaskoe6323f62020-07-09 15:49:02 +02001549 /* check for redundant move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001550 orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name);
1551 val_meta = lyd_find_meta(diff->meta, mod, meta_name);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001552 assert(orig_val_meta && val_meta);
1553
1554 if (!lyd_compare_meta(orig_val_meta, val_meta)) {
1555 /* there is actually no move */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001556 lyd_free_meta_single(orig_val_meta);
1557 lyd_free_meta_single(val_meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001558 if (child) {
1559 /* change operation to NONE, we have siblings */
1560 lyd_diff_change_op(diff, LYD_DIFF_OP_NONE);
1561 return 0;
1562 }
1563
1564 /* redundant node, BUT !!
1565 * In diff the move operation is always converted to be INSERT_AFTER, which is fine
1566 * because the data that this is applied on should not change for the diff lifetime.
1567 * However, when we are merging 2 diffs, this conversion is actually lossy because
1568 * if the data change, the move operation can also change its meaning. In this specific
1569 * case the move operation will be lost. But it can be considered a feature, it is not supported.
1570 */
1571 return 1;
1572 }
1573 } else if ((op == LYD_DIFF_OP_NONE) && (diff->schema->nodetype & LYD_NODE_TERM)) {
1574 /* check whether at least the default flags are different */
1575 meta = lyd_find_meta(diff->meta, mod, "orig-default");
1576 assert(meta);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001577 str = lyd_get_meta_value(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001578
1579 /* if previous and current dflt flags are the same, this node is redundant */
1580 if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) {
1581 return 1;
1582 }
1583 return 0;
1584 }
1585
1586 if (!child && (op == LYD_DIFF_OP_NONE)) {
1587 return 1;
1588 }
1589
1590 return 0;
1591}
1592
1593/**
Michal Vaskoe78faec2021-04-08 17:24:43 +02001594 * @brief Merge sysrepo diff subtree with another diff, recursively.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001595 *
1596 * @param[in] src_diff Source diff node.
1597 * @param[in] diff_parent Current sysrepo diff parent.
1598 * @param[in] diff_cb Optional diff callback.
1599 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001600 * @param[in,out] dup_inst Duplicate instance cache for all @p src_diff siblings.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001601 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001602 * @param[in,out] diff Diff root node.
1603 * @return LY_ERR value.
1604 */
1605static LY_ERR
1606lyd_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 +02001607 struct lyd_dup_inst **dup_inst, uint16_t options, struct lyd_node **diff)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001608{
1609 LY_ERR ret = LY_SUCCESS;
1610 struct lyd_node *child, *diff_node = NULL;
1611 enum lyd_diff_op src_op, cur_op;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001612 struct lyd_dup_inst *child_dup_inst = NULL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001613
1614 /* get source node operation */
1615 LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op));
1616
1617 /* find an equal node in the current diff */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001618 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 +02001619
1620 if (diff_node) {
1621 /* get target (current) operation */
1622 LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op));
1623
1624 /* merge operations */
1625 switch (src_op) {
1626 case LYD_DIFF_OP_REPLACE:
1627 ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff);
1628 break;
1629 case LYD_DIFF_OP_CREATE:
Michal Vasko1dc0a842021-02-04 11:04:57 +01001630 if ((cur_op == LYD_DIFF_OP_CREATE) && lysc_is_dup_inst_list(diff_node->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001631 /* special case of creating duplicate (leaf-)list instances */
Michal Vasko1dc0a842021-02-04 11:04:57 +01001632 goto add_diff;
1633 }
1634
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001635 ret = lyd_diff_merge_create(diff_node, cur_op, src_diff, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001636 break;
1637 case LYD_DIFF_OP_DELETE:
1638 ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff);
1639 break;
1640 case LYD_DIFF_OP_NONE:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001641 /* key-less list can never have "none" operation since all its descendants are acting as "keys" */
1642 assert((src_diff->schema->nodetype != LYS_LIST) || !lysc_is_dup_inst_list(src_diff->schema));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001643 ret = lyd_diff_merge_none(diff_node, cur_op, src_diff);
1644 break;
1645 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001646 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001647 }
1648 if (ret) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001649 LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001650 return ret;
1651 }
1652
1653 if (diff_cb) {
1654 /* call callback */
Michal Vaskobc5fba92020-08-07 12:14:39 +02001655 LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001656 }
1657
1658 /* update diff parent */
1659 diff_parent = diff_node;
1660
Michal Vaskoe78faec2021-04-08 17:24:43 +02001661 /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances),
1662 * so there is nothing to merge for these "keys" */
1663 if (!lysc_is_dup_inst_list(src_diff->schema)) {
1664 /* merge src_diff recursively */
1665 LY_LIST_FOR(lyd_child_no_keys(src_diff), child) {
1666 ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff);
1667 if (ret) {
1668 break;
1669 }
1670 }
Michal Vaskod7c048c2021-05-18 16:12:55 +02001671 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001672 LY_CHECK_RET(ret);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001673 }
1674 } else {
Michal Vasko1dc0a842021-02-04 11:04:57 +01001675add_diff:
Michal Vaskoe6323f62020-07-09 15:49:02 +02001676 /* add new diff node with all descendants */
Michal Vasko871a0252020-11-11 18:35:24 +01001677 LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS,
1678 &diff_node));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001679
1680 /* insert node into diff if not already */
1681 if (!diff_parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001682 lyd_insert_sibling(*diff, diff_node, diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001683 }
1684
1685 /* update operation */
1686 LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op));
1687
1688 if (diff_cb) {
Michal Vaskoe2af8412020-12-03 14:11:38 +01001689 /* call callback with no source diff node since it was duplicated and just added */
1690 LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001691 }
1692
1693 /* update diff parent */
1694 diff_parent = diff_node;
1695 }
1696
1697 /* remove any redundant nodes */
Michal Vaskob98d7082020-07-15 16:38:36 +02001698 if (lyd_diff_is_redundant(diff_parent)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001699 if (diff_parent == *diff) {
1700 *diff = (*diff)->next;
1701 }
1702 lyd_free_tree(diff_parent);
1703 }
1704
1705 return LY_SUCCESS;
1706}
1707
1708API LY_ERR
Michal Vaskofb737aa2020-08-06 13:53:53 +02001709lyd_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 +01001710 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001711{
1712 const struct lyd_node *src_root;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001713 struct lyd_dup_inst *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001714 LY_ERR ret = LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001715
1716 LY_LIST_FOR(src_diff, src_root) {
1717 if (mod && (lyd_owner_module(src_root) != mod)) {
1718 /* skip data nodes from different modules */
1719 continue;
1720 }
1721
1722 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001723 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 +02001724 }
1725
Michal Vaskoe78faec2021-04-08 17:24:43 +02001726cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +02001727 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001728 return ret;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001729}
1730
1731API LY_ERR
Michal Vasko04f85912020-08-07 12:14:58 +02001732lyd_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 +01001733 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vasko04f85912020-08-07 12:14:58 +02001734{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001735 LY_ERR ret;
Michal Vaskod7c048c2021-05-18 16:12:55 +02001736 struct lyd_dup_inst *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001737
Michal Vasko04f85912020-08-07 12:14:58 +02001738 if (!src_sibling) {
1739 return LY_SUCCESS;
1740 }
1741
Michal Vaskoe78faec2021-04-08 17:24:43 +02001742 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 +02001743 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001744 return ret;
Michal Vasko04f85912020-08-07 12:14:58 +02001745}
1746
1747API LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001748lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001749{
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001750 return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001751}
Michal Vasko4231fb62020-07-13 13:54:47 +02001752
1753static LY_ERR
Michal Vaskobaba84e2021-02-05 16:33:30 +01001754lyd_diff_reverse_value(struct lyd_node *node, const struct lys_module *mod)
Michal Vasko4231fb62020-07-13 13:54:47 +02001755{
1756 LY_ERR ret = LY_SUCCESS;
1757 struct lyd_meta *meta;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001758 const char *val1 = NULL;
1759 char *val2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001760 uint32_t flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001761
Michal Vaskobaba84e2021-02-05 16:33:30 +01001762 assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA));
1763
1764 meta = lyd_find_meta(node->meta, mod, "orig-value");
1765 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001766
1767 /* orig-value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001768 val1 = lyd_get_meta_value(meta);
Michal Vasko4231fb62020-07-13 13:54:47 +02001769
1770 /* current value */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001771 if (node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001772 val2 = strdup(lyd_get_value(node));
Michal Vaskobaba84e2021-02-05 16:33:30 +01001773 } else {
1774 LY_CHECK_RET(lyd_any_value_str(node, &val2));
1775 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001776
1777 /* switch values, keep default flag */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001778 flags = node->flags;
1779 if (node->schema->nodetype == LYS_LEAF) {
1780 LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup);
1781 } else {
1782 union lyd_any_value anyval = {.str = val1};
1783 LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup);
1784 }
1785 node->flags = flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001786 LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup);
1787
1788cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001789 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001790 return ret;
1791}
1792
1793static LY_ERR
1794lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod)
1795{
1796 struct lyd_meta *meta;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001797 uint32_t flag1, flag2;
Michal Vasko4231fb62020-07-13 13:54:47 +02001798
1799 meta = lyd_find_meta(node->meta, mod, "orig-default");
Michal Vasko610e93b2020-11-09 20:58:32 +01001800 LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001801
1802 /* orig-default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001803 if (meta->value.boolean) {
Michal Vasko4231fb62020-07-13 13:54:47 +02001804 flag1 = LYD_DEFAULT;
1805 } else {
1806 flag1 = 0;
1807 }
1808
1809 /* current default */
1810 flag2 = node->flags & LYD_DEFAULT;
1811
Michal Vasko610e93b2020-11-09 20:58:32 +01001812 if (flag1 == flag2) {
1813 /* no default state change so nothing to reverse */
1814 return LY_SUCCESS;
1815 }
1816
Michal Vasko4231fb62020-07-13 13:54:47 +02001817 /* switch defaults */
1818 node->flags &= ~LYD_DEFAULT;
1819 node->flags |= flag1;
1820 LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false"));
1821
1822 return LY_SUCCESS;
1823}
1824
1825static LY_ERR
1826lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2)
1827{
1828 LY_ERR ret = LY_SUCCESS;
1829 struct lyd_meta *meta1, *meta2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001830 const char *val1 = NULL;
1831 char *val2 = NULL;
Michal Vasko4231fb62020-07-13 13:54:47 +02001832
1833 meta1 = lyd_find_meta(node->meta, mod, name1);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001834 LY_CHECK_ERR_RET(!meta1, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001835
1836 meta2 = lyd_find_meta(node->meta, mod, name2);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001837 LY_CHECK_ERR_RET(!meta2, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001838
1839 /* value1 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001840 val1 = lyd_get_meta_value(meta1);
Michal Vasko4231fb62020-07-13 13:54:47 +02001841
1842 /* value2 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001843 val2 = strdup(lyd_get_meta_value(meta2));
Michal Vasko4231fb62020-07-13 13:54:47 +02001844
1845 /* switch values */
1846 LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup);
1847 LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup);
1848
1849cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001850 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001851 return ret;
1852}
1853
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001854/**
1855 * @brief Remove specific operation from all the nodes in a subtree.
1856 *
1857 * @param[in] diff Diff subtree to process.
1858 * @param[in] op Only expected operation.
1859 * @return LY_ERR value.
1860 */
1861static LY_ERR
1862lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op)
1863{
1864 struct lyd_node *elem;
1865 struct lyd_meta *meta;
1866
1867 LYD_TREE_DFS_BEGIN(diff, elem) {
1868 meta = lyd_find_meta(elem->meta, NULL, "yang:operation");
1869 if (meta) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001870 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 +01001871 lyd_free_meta_single(meta);
1872 }
1873
1874 LYD_TREE_DFS_END(diff, elem);
1875 }
1876
1877 return LY_SUCCESS;
1878}
1879
Michal Vasko4231fb62020-07-13 13:54:47 +02001880API LY_ERR
Michal Vasko66535812020-08-11 08:44:22 +02001881lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff)
Michal Vasko4231fb62020-07-13 13:54:47 +02001882{
1883 LY_ERR ret = LY_SUCCESS;
1884 const struct lys_module *mod;
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001885 struct lyd_node *root, *elem, *iter;
Michal Vasko4231fb62020-07-13 13:54:47 +02001886 enum lyd_diff_op op;
1887
1888 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
1889
1890 if (!src_diff) {
1891 *diff = NULL;
1892 return LY_SUCCESS;
1893 }
1894
1895 /* duplicate diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001896 LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff));
Michal Vasko4231fb62020-07-13 13:54:47 +02001897
1898 /* find module with metadata needed for later */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001899 mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang");
1900 LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001901
1902 LY_LIST_FOR(*diff, root) {
Michal Vasko56daf732020-08-10 10:57:18 +02001903 LYD_TREE_DFS_BEGIN(root, elem) {
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001904 /* skip all keys */
1905 if (!lysc_is_key(elem->schema)) {
1906 /* find operation attribute, if any */
1907 LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001908
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001909 switch (op) {
1910 case LYD_DIFF_OP_CREATE:
1911 /* reverse create to delete */
1912 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01001913
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001914 /* check all the children for the same operation, nothing else is expected */
1915 LY_LIST_FOR(lyd_child(elem), iter) {
1916 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE);
1917 }
1918
Michal Vasko9e070522021-03-05 14:00:14 +01001919 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02001920 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001921 case LYD_DIFF_OP_DELETE:
1922 /* reverse delete to create */
1923 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01001924
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001925 /* check all the children for the same operation, nothing else is expected */
1926 LY_LIST_FOR(lyd_child(elem), iter) {
1927 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE);
1928 }
1929
Michal Vasko9e070522021-03-05 14:00:14 +01001930 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02001931 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001932 case LYD_DIFF_OP_REPLACE:
1933 switch (elem->schema->nodetype) {
1934 case LYS_LEAF:
1935 /* leaf value change */
1936 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
1937 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
1938 break;
Michal Vaskobaba84e2021-02-05 16:33:30 +01001939 case LYS_ANYXML:
1940 case LYS_ANYDATA:
1941 /* any value change */
1942 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
1943 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001944 case LYS_LEAFLIST:
1945 /* leaf-list move */
1946 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001947 if (lysc_is_dup_inst_list(elem->schema)) {
1948 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
1949 } else {
1950 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup);
1951 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001952 break;
1953 case LYS_LIST:
1954 /* list move */
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-key", "key"), cleanup);
1959 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001960 break;
1961 default:
1962 LOGINT(LYD_CTX(src_diff));
1963 ret = LY_EINT;
1964 goto cleanup;
1965 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001966 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001967 case LYD_DIFF_OP_NONE:
1968 switch (elem->schema->nodetype) {
1969 case LYS_LEAF:
1970 case LYS_LEAFLIST:
1971 /* default flag change */
1972 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
1973 break;
1974 default:
1975 /* nothing to do */
1976 break;
1977 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001978 break;
1979 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001980 }
1981
Michal Vasko56daf732020-08-10 10:57:18 +02001982 LYD_TREE_DFS_END(root, elem);
Michal Vasko4231fb62020-07-13 13:54:47 +02001983 }
1984 }
1985
1986cleanup:
1987 if (ret) {
1988 lyd_free_siblings(*diff);
1989 *diff = NULL;
1990 }
1991 return ret;
1992}