Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_data.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Manipulation with libyang data structures |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 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 |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 13 | */ |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 14 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <ctype.h> |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 19 | #include <limits.h> |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 20 | #include <stdarg.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 22 | #include <sys/types.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 23 | #include <sys/mman.h> |
| 24 | #include <sys/stat.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 25 | #include <fcntl.h> |
| 26 | #include <unistd.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 27 | #include <string.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 28 | #include <errno.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 29 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 30 | #include "libyang.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 31 | #include "common.h" |
| 32 | #include "context.h" |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 33 | #include "tree_data.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 34 | #include "parser.h" |
| 35 | #include "resolve.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 36 | #include "xml_internal.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 37 | #include "tree_internal.h" |
| 38 | #include "validation.h" |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 39 | #include "xpath.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 40 | |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 41 | static struct lyd_node * |
| 42 | lyd_parse_(struct ly_ctx *ctx, const struct lys_node *parent, const char *data, LYD_FORMAT format, int options) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 43 | { |
Radek Krejci | a2264b6 | 2016-02-05 13:14:05 +0100 | [diff] [blame] | 44 | struct lyxml_elem *xml, *xmlnext; |
Radek Krejci | 5974c1f | 2015-10-09 09:53:24 +0200 | [diff] [blame] | 45 | struct lyd_node *result = NULL; |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 46 | int xmlopt = LYXML_PARSE_MULTIROOT; |
Radek Krejci | 5974c1f | 2015-10-09 09:53:24 +0200 | [diff] [blame] | 47 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 48 | if (!ctx || !data) { |
| 49 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 53 | if (options & LYD_OPT_NOSIBLINGS) { |
| 54 | xmlopt = 0; |
| 55 | } |
| 56 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 57 | switch (format) { |
| 58 | case LYD_XML: |
Radek Krejci | 452fb95 | 2015-10-02 16:07:46 +0200 | [diff] [blame] | 59 | case LYD_XML_FORMAT: |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 60 | xml = lyxml_parse_mem(ctx, data, xmlopt); |
Radek Krejci | 88b3bad | 2016-02-10 13:08:08 +0100 | [diff] [blame] | 61 | if (ly_errno) { |
| 62 | return NULL; |
| 63 | } |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 64 | result = lyd_parse_xml(ctx, &xml, options, parent); |
Radek Krejci | a2264b6 | 2016-02-05 13:14:05 +0100 | [diff] [blame] | 65 | LY_TREE_FOR_SAFE(xml, xmlnext, xml) { |
| 66 | lyxml_free(ctx, xml); |
| 67 | } |
Radek Krejci | 5974c1f | 2015-10-09 09:53:24 +0200 | [diff] [blame] | 68 | break; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 69 | case LYD_JSON: |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 70 | result = lyd_parse_json(ctx, parent, data, options); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 71 | break; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 72 | default: |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 73 | /* error */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
| 76 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 77 | if (!result && !ly_errno) { |
| 78 | /* is empty data tree really valid ? */ |
Radek Krejci | 1daa7a4 | 2016-02-10 13:09:59 +0100 | [diff] [blame] | 79 | lyd_validate(NULL, options, ctx); |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 80 | } |
Radek Krejci | 5974c1f | 2015-10-09 09:53:24 +0200 | [diff] [blame] | 81 | return result; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 84 | static struct lyd_node * |
| 85 | lyd_parse_data_(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, va_list ap) |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 86 | { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 87 | const struct lys_node *rpc = NULL; |
| 88 | |
| 89 | if (lyp_check_options(options)) { |
| 90 | LOGERR(LY_EINVAL, "%s: Invalid options (multiple data type flags set).", __func__); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | if (options & LYD_OPT_RPCREPLY) { |
| 95 | rpc = va_arg(ap, struct lys_node*); |
| 96 | if (!rpc || (rpc->nodetype != LYS_RPC)) { |
| 97 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 98 | return NULL; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return lyd_parse_(ctx, rpc, data, format, options); |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | API struct lyd_node * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 106 | lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...) |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 107 | { |
| 108 | va_list ap; |
| 109 | struct lyd_node *result; |
| 110 | |
| 111 | va_start(ap, options); |
| 112 | result = lyd_parse_data_(ctx, data, format, options, ap); |
| 113 | va_end(ap); |
| 114 | |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | API struct lyd_node * |
| 119 | lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...) |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 120 | { |
| 121 | struct lyd_node *ret; |
| 122 | struct stat sb; |
| 123 | char *data; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 124 | va_list ap; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 125 | |
| 126 | if (!ctx || (fd == -1)) { |
| 127 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | if (fstat(fd, &sb) == -1) { |
| 132 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
| 133 | return NULL; |
| 134 | } |
| 135 | |
Radek Krejci | 3006be0 | 2015-12-17 11:24:33 +0100 | [diff] [blame] | 136 | data = mmap(NULL, sb.st_size + 1, PROT_READ, MAP_PRIVATE, fd, 0); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 137 | if (data == MAP_FAILED) { |
| 138 | LOGERR(LY_ESYS, "Mapping file descriptor into memory failed."); |
| 139 | return NULL; |
| 140 | } |
| 141 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 142 | va_start(ap, options); |
| 143 | ret = lyd_parse_data_(ctx, data, format, options, ap); |
| 144 | |
| 145 | va_end(ap); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 146 | munmap(data, sb.st_size); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 147 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | API struct lyd_node * |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 152 | lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...) |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 153 | { |
| 154 | int fd; |
| 155 | struct lyd_node *ret; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 156 | va_list ap; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 157 | |
| 158 | if (!ctx || !path) { |
| 159 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | fd = open(path, O_RDONLY); |
| 164 | if (fd == -1) { |
| 165 | LOGERR(LY_ESYS, "Failed to open data file \"%s\" (%s).", path, strerror(errno)); |
| 166 | return NULL; |
| 167 | } |
| 168 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 169 | va_start(ap, options); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 170 | ret = lyd_parse_fd(ctx, fd, format, options); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 171 | |
| 172 | va_end(ap); |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 173 | close(fd); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 174 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 175 | return ret; |
| 176 | } |
| 177 | |
Michal Vasko | 7d05125 | 2016-03-16 13:16:29 +0100 | [diff] [blame] | 178 | static struct lyd_node * |
| 179 | _lyd_new(struct lyd_node *parent, const struct lys_node *schema) |
| 180 | { |
| 181 | struct lyd_node *ret; |
| 182 | |
| 183 | ret = calloc(1, sizeof *ret); |
| 184 | if (!ret) { |
| 185 | LOGMEM; |
| 186 | return NULL; |
| 187 | } |
| 188 | ret->schema = (struct lys_node *)schema; |
| 189 | ret->validity = LYD_VAL_NOT; |
| 190 | ret->prev = ret; |
| 191 | if (parent) { |
| 192 | if (lyd_insert(parent, ret)) { |
| 193 | lyd_free(ret); |
| 194 | return NULL; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 201 | API struct lyd_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 202 | lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name) |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 203 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 204 | const struct lys_node *snode = NULL, *siblings; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 205 | |
| 206 | if ((!parent && !module) || !name) { |
| 207 | ly_errno = LY_EINVAL; |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | if (!parent) { |
| 212 | siblings = module->data; |
| 213 | } else { |
Michal Vasko | a5ef4d7 | 2015-09-29 16:05:21 +0200 | [diff] [blame] | 214 | if (!parent->schema) { |
| 215 | return NULL; |
| 216 | } |
| 217 | siblings = parent->schema->child; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 220 | if (lys_get_data_sibling(module, siblings, name, LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_RPC, &snode) |
| 221 | || !snode) { |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 222 | return NULL; |
| 223 | } |
| 224 | |
Michal Vasko | 7d05125 | 2016-03-16 13:16:29 +0100 | [diff] [blame] | 225 | return _lyd_new(parent, snode); |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 228 | static struct lyd_node * |
| 229 | lyd_create_leaf(const struct lys_node *schema, const char *val_str) |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 230 | { |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 231 | struct lyd_node_leaf_list *ret; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 232 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 233 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 234 | if (!ret) { |
| 235 | LOGMEM; |
| 236 | return NULL; |
| 237 | } |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 238 | ret->schema = (struct lys_node *)schema; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 239 | ret->validity = LYD_VAL_NOT; |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 240 | ret->prev = (struct lyd_node *)ret; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 241 | ret->value_type = ((struct lys_node_leaf *)schema)->type.base; |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 242 | ret->value_str = lydict_insert(schema->module->ctx, val_str, 0); |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 243 | |
| 244 | /* resolve the type correctly */ |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 245 | if (lyp_parse_value(ret, NULL, 1, NULL, 0)) { |
| 246 | lyd_free((struct lyd_node *)ret); |
| 247 | ly_errno = LY_EINVAL; |
| 248 | return NULL; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 251 | return (struct lyd_node *)ret; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Michal Vasko | 7d05125 | 2016-03-16 13:16:29 +0100 | [diff] [blame] | 254 | static struct lyd_node * |
| 255 | _lyd_new_leaf(struct lyd_node *parent, const struct lys_node *schema, const char *val_str) |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 256 | { |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 257 | struct lyd_node *ret; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 258 | |
Michal Vasko | 7d05125 | 2016-03-16 13:16:29 +0100 | [diff] [blame] | 259 | ret = lyd_create_leaf(schema, val_str); |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 260 | if (!ret) { |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | /* connect to parent */ |
| 265 | if (parent) { |
| 266 | if (lyd_insert(parent, ret)) { |
| 267 | lyd_free(ret); |
| 268 | return NULL; |
| 269 | } |
| 270 | } |
| 271 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 272 | if (ret->schema->flags & LYS_UNIQUE) { |
| 273 | /* locate the first parent list */ |
| 274 | for (parent = ret->parent; parent && parent->schema->nodetype != LYS_LIST; parent = parent->parent); |
| 275 | |
| 276 | /* set flag for future validation */ |
| 277 | if (parent) { |
| 278 | parent->validity |= LYD_VAL_UNIQUE; |
| 279 | } |
| 280 | } |
| 281 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 282 | return ret; |
Michal Vasko | 7d05125 | 2016-03-16 13:16:29 +0100 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | API struct lyd_node * |
| 286 | lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str) |
| 287 | { |
| 288 | const struct lys_node *snode = NULL, *siblings; |
| 289 | |
| 290 | if ((!parent && !module) || !name) { |
| 291 | ly_errno = LY_EINVAL; |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | if (!parent) { |
| 296 | siblings = module->data; |
| 297 | } else { |
| 298 | if (!parent->schema) { |
| 299 | ly_errno = LY_EINVAL; |
| 300 | return NULL; |
| 301 | } |
| 302 | siblings = parent->schema->child; |
| 303 | } |
| 304 | |
| 305 | if (lys_get_data_sibling(module, siblings, name, LYS_LEAFLIST | LYS_LEAF, &snode) || !snode) { |
| 306 | ly_errno = LY_EINVAL; |
| 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | return _lyd_new_leaf(parent, snode, val_str); |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 311 | |
| 312 | } |
| 313 | |
Mislav Novakovic | e25a5c4 | 2016-03-10 18:06:11 +0100 | [diff] [blame] | 314 | API int |
Radek Krejci | b9b4d00 | 2016-01-18 13:08:51 +0100 | [diff] [blame] | 315 | lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str) |
| 316 | { |
| 317 | const char *backup; |
| 318 | struct lyd_node *parent; |
| 319 | |
| 320 | if (!leaf) { |
| 321 | ly_errno = LY_EINVAL; |
| 322 | return EXIT_FAILURE; |
| 323 | } |
| 324 | |
| 325 | backup = leaf->value_str; |
| 326 | leaf->value_str = val_str; |
| 327 | |
| 328 | /* resolve the type correctly */ |
| 329 | if (lyp_parse_value(leaf, NULL, 1, NULL, 0)) { |
| 330 | leaf->value_str = backup; |
| 331 | ly_errno = LY_EINVAL; |
| 332 | return EXIT_FAILURE; |
| 333 | } |
| 334 | |
| 335 | /* value is correct, finish the changes in leaf */ |
| 336 | lydict_remove(leaf->schema->module->ctx, backup); |
| 337 | leaf->value_str = lydict_insert(leaf->schema->module->ctx, val_str, 0); |
| 338 | |
| 339 | if (leaf->schema->flags & LYS_UNIQUE) { |
| 340 | /* locate the first parent list */ |
| 341 | for (parent = leaf->parent; parent && parent->schema->nodetype != LYS_LIST; parent = parent->parent); |
| 342 | |
| 343 | /* set flag for future validation */ |
| 344 | if (parent) { |
| 345 | parent->validity |= LYD_VAL_UNIQUE; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return EXIT_SUCCESS; |
| 350 | } |
| 351 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 352 | static struct lyd_node * |
| 353 | lyd_create_anyxml(const struct lys_node *schema, const char *val_xml) |
| 354 | { |
| 355 | struct lyd_node_anyxml *ret; |
| 356 | struct lyxml_elem *root; |
| 357 | char *xml; |
| 358 | |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 359 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 360 | if (!ret) { |
| 361 | LOGMEM; |
| 362 | return NULL; |
| 363 | } |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 364 | ret->schema = (struct lys_node *)schema; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 365 | ret->validity = LYD_VAL_NOT; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 366 | ret->prev = (struct lyd_node *)ret; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 367 | |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 368 | /* store the anyxml data together with the anyxml element */ |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 369 | if (asprintf(&xml, "<%s>%s</%s>", schema->name, (val_xml ? val_xml : ""), schema->name) == -1) { |
| 370 | LOGMEM; |
| 371 | lyd_free((struct lyd_node *)ret); |
| 372 | return NULL; |
| 373 | } |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 374 | root = lyxml_parse_mem(schema->module->ctx, xml, 0); |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 375 | free(xml); |
| 376 | if (!root) { |
| 377 | lyd_free((struct lyd_node *)ret); |
| 378 | return NULL; |
Michal Vasko | 14d8877 | 2015-12-03 10:54:59 +0100 | [diff] [blame] | 379 | } |
Michal Vasko | 334a07d | 2016-02-29 11:30:10 +0100 | [diff] [blame] | 380 | ret->value = root; |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 381 | |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 382 | return (struct lyd_node *)ret; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Michal Vasko | 7d05125 | 2016-03-16 13:16:29 +0100 | [diff] [blame] | 385 | static struct lyd_node * |
| 386 | _lyd_new_anyxml(struct lyd_node *parent, const struct lys_node *schema, const char *val_xml) |
| 387 | { |
| 388 | struct lyd_node *ret; |
| 389 | |
| 390 | ret = lyd_create_anyxml(schema, val_xml); |
| 391 | if (!ret) { |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | /* connect to parent */ |
| 396 | if (parent) { |
| 397 | if (lyd_insert(parent, ret)) { |
| 398 | lyd_free(ret); |
| 399 | return NULL; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 406 | API struct lyd_node * |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 407 | lyd_new_anyxml(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_xml) |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 408 | { |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 409 | const struct lys_node *siblings, *snode; |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 410 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 411 | if ((!parent && !module) || !name) { |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 412 | ly_errno = LY_EINVAL; |
| 413 | return NULL; |
| 414 | } |
| 415 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 416 | if (!parent) { |
| 417 | siblings = module->data; |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 418 | } else { |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 419 | if (!parent->schema) { |
| 420 | return NULL; |
| 421 | } |
| 422 | siblings = parent->schema->child; |
| 423 | } |
| 424 | |
| 425 | if (lys_get_data_sibling(module, siblings, name, LYS_ANYXML, &snode) || !snode) { |
| 426 | return NULL; |
| 427 | } |
| 428 | |
Michal Vasko | 7d05125 | 2016-03-16 13:16:29 +0100 | [diff] [blame] | 429 | return _lyd_new_anyxml(parent, snode, val_xml); |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | API struct lyd_node * |
| 433 | lyd_output_new(const struct lys_node *schema) |
| 434 | { |
| 435 | struct lyd_node *ret; |
| 436 | |
| 437 | if (!schema || !(schema->nodetype & (LYS_CONTAINER | LYS_LIST)) |
| 438 | || !lys_parent(schema) || (lys_parent(schema)->nodetype != LYS_OUTPUT)) { |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 439 | ly_errno = LY_EINVAL; |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 444 | if (!ret) { |
| 445 | LOGMEM; |
| 446 | return NULL; |
| 447 | } |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 448 | ret->schema = (struct lys_node *)schema; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 449 | ret->validity = LYD_VAL_NOT; |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 450 | ret->prev = ret; |
| 451 | |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | API struct lyd_node * |
| 456 | lyd_output_new_leaf(const struct lys_node *schema, const char *val_str) |
| 457 | { |
| 458 | if (!schema || (schema->nodetype != LYS_LEAF) |
| 459 | || !lys_parent(schema) || (lys_parent(schema)->nodetype != LYS_OUTPUT)) { |
| 460 | ly_errno = LY_EINVAL; |
| 461 | return NULL; |
| 462 | } |
| 463 | |
| 464 | return lyd_create_leaf(schema, val_str); |
| 465 | } |
| 466 | |
| 467 | API struct lyd_node * |
| 468 | lyd_output_new_anyxml(const struct lys_node *schema, const char *val_xml) |
| 469 | { |
| 470 | if (!schema || (schema->nodetype != LYS_ANYXML) |
| 471 | || !lys_parent(schema) || (lys_parent(schema)->nodetype != LYS_OUTPUT)) { |
| 472 | ly_errno = LY_EINVAL; |
| 473 | return NULL; |
| 474 | } |
| 475 | |
| 476 | return lyd_create_anyxml(schema, val_xml); |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame^] | 479 | static int |
| 480 | lyd_new_path_list_keys(struct lyd_node *list, const char *list_name, const char *predicate, int *parsed) |
| 481 | { |
| 482 | const char *name, *value; |
| 483 | char *key_val; |
| 484 | int r, i, nam_len, val_len, has_predicate = 1; |
| 485 | struct lys_node_list *slist; |
| 486 | |
| 487 | slist = (struct lys_node_list *)list->schema; |
| 488 | |
| 489 | for (i = 0; i < slist->keys_size; ++i) { |
| 490 | if (!has_predicate) { |
| 491 | LOGVAL(LYE_PATH_MISSKEY, 0, LY_VLOG_NONE, NULL, list_name); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | if ((r = parse_schema_list_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
| 496 | LOGVAL(LYE_PATH_INCHAR, 0, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
| 497 | return -1; |
| 498 | } |
| 499 | *parsed += r; |
| 500 | predicate += r; |
| 501 | |
| 502 | if (strncmp(slist->keys[i]->name, name, nam_len) || slist->keys[i]->name[nam_len]) { |
| 503 | LOGVAL(LYE_PATH_INKEY, 0, LY_VLOG_NONE, NULL, name[0], name); |
| 504 | return -1; |
| 505 | } |
| 506 | |
| 507 | key_val = malloc((val_len + 1) * sizeof(char)); |
| 508 | if (!key_val) { |
| 509 | LOGMEM; |
| 510 | return -1; |
| 511 | } |
| 512 | strncpy(key_val, value, val_len); |
| 513 | key_val[val_len] = '\0'; |
| 514 | |
| 515 | if (!_lyd_new_leaf(list, (const struct lys_node *)slist->keys[i], key_val)) { |
| 516 | free(key_val); |
| 517 | return -1; |
| 518 | } |
| 519 | free(key_val); |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | API struct lyd_node * |
| 526 | lyd_new_path(struct lyd_node *data_tree, struct ly_ctx *ctx, const char *path, const char *value, int options) |
| 527 | { |
| 528 | char module_name[LY_MODULE_NAME_MAX_LEN + 1]; |
| 529 | const char *mod_name, *name, *node_mod_name; |
| 530 | struct lyd_node *ret = NULL, *node, *parent = NULL; |
| 531 | const struct lys_node *schild, *sparent; |
| 532 | const struct lys_node_list *slist; |
| 533 | const struct lys_module *module, *prev_mod; |
| 534 | int r, i, parsed = 0, mod_name_len, nam_len, is_relative = -1; |
| 535 | |
| 536 | if (!path || (!data_tree && !ctx) || ((options & LYD_PATH_OPT_UPDATE) && !value) |
| 537 | || (data_tree && (data_tree->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)))) { |
| 538 | ly_errno = LY_EINVAL; |
| 539 | return NULL; |
| 540 | } |
| 541 | |
| 542 | if (data_tree) { |
| 543 | parent = resolve_partial_json_data_nodeid(path, data_tree, &parsed); |
| 544 | if (!parent) { |
| 545 | return NULL; |
| 546 | } |
| 547 | path += parsed; |
| 548 | if (parsed) { |
| 549 | /* if we parsed something we have a relative path for sure, otherwise we don't know */ |
| 550 | is_relative = 1; |
| 551 | } |
| 552 | |
| 553 | if (!path[0]) { |
| 554 | /* the node exists */ |
| 555 | if (!(options & LYD_PATH_OPT_UPDATE) || (parent->schema->nodetype != LYS_LEAF)) { |
| 556 | LOGVAL(LYE_PATH_EXISTS, 0, LY_VLOG_NONE, NULL); |
| 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | /* update leaf value */ |
| 561 | r = lyd_change_leaf((struct lyd_node_leaf_list *)parent, value); |
| 562 | if (r) { |
| 563 | return NULL; |
| 564 | } |
| 565 | return parent; |
| 566 | } |
| 567 | |
| 568 | sparent = parent->schema; |
| 569 | module = lys_node_module(sparent); |
| 570 | prev_mod = module; |
| 571 | } |
| 572 | |
| 573 | if ((r = parse_schema_nodeid(path, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
| 574 | LOGVAL(LYE_PATH_INCHAR, 0, LY_VLOG_NONE, NULL, path[-r], &path[-r]); |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | path += r; |
| 579 | |
| 580 | /* we are starting from scratch, absolute path */ |
| 581 | if (!data_tree) { |
| 582 | if (!mod_name) { |
| 583 | LOGVAL(LYE_PATH_MISSMOD, 0, LY_VLOG_NONE, NULL, name); |
| 584 | return NULL; |
| 585 | } else if (mod_name_len > LY_MODULE_NAME_MAX_LEN) { |
| 586 | LOGINT; |
| 587 | return NULL; |
| 588 | } |
| 589 | |
| 590 | strncpy(module_name, mod_name, mod_name_len); |
| 591 | module_name[mod_name_len] = '\0'; |
| 592 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 593 | if (!module) { |
| 594 | LOGVAL(LYE_PATH_INMOD, 0, LY_VLOG_NONE, NULL, mod_name); |
| 595 | return NULL; |
| 596 | } |
| 597 | mod_name = NULL; |
| 598 | mod_name_len = 0; |
| 599 | prev_mod = module; |
| 600 | |
| 601 | sparent = NULL; |
| 602 | } |
| 603 | |
| 604 | /* create nodes in a loop */ |
| 605 | while (1) { |
| 606 | /* find the schema node */ |
| 607 | schild = NULL; |
| 608 | while ((schild = lys_getnext(schild, sparent, module, 0))) { |
| 609 | if (schild->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_NOTIF | LYS_RPC)) { |
| 610 | /* module comparison */ |
| 611 | if (mod_name) { |
| 612 | node_mod_name = lys_node_module(schild)->name; |
| 613 | if (strncmp(node_mod_name, mod_name, mod_name_len) || node_mod_name[mod_name_len]) { |
| 614 | continue; |
| 615 | } |
| 616 | } else if (lys_node_module(schild) != prev_mod) { |
| 617 | continue; |
| 618 | } |
| 619 | |
| 620 | /* name check */ |
| 621 | if (!strncmp(schild->name, name, nam_len) && !schild->name[nam_len]) { |
| 622 | break; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | if (!schild) { |
| 628 | LOGVAL(LYE_PATH_INNODE, 0, LY_VLOG_NONE, NULL, name); |
| 629 | lyd_free(ret); |
| 630 | return NULL; |
| 631 | } |
| 632 | |
| 633 | /* we have the right schema node */ |
| 634 | switch (schild->nodetype) { |
| 635 | case LYS_CONTAINER: |
| 636 | case LYS_LIST: |
| 637 | case LYS_NOTIF: |
| 638 | case LYS_RPC: |
| 639 | node = _lyd_new(is_relative ? parent : NULL, schild); |
| 640 | break; |
| 641 | case LYS_LEAF: |
| 642 | case LYS_LEAFLIST: |
| 643 | if (path[0]) { |
| 644 | LOGVAL(LYE_PATH_INCHAR, 0, LY_VLOG_NONE, NULL, path[0], path); |
| 645 | return NULL; |
| 646 | } |
| 647 | node = _lyd_new_leaf(is_relative ? parent : NULL, schild, value); |
| 648 | break; |
| 649 | case LYS_ANYXML: |
| 650 | if (path[0]) { |
| 651 | LOGVAL(LYE_PATH_INCHAR, 0, LY_VLOG_NONE, NULL, path[0], path); |
| 652 | return NULL; |
| 653 | } |
| 654 | node = _lyd_new_anyxml(is_relative ? parent : NULL, schild, value); |
| 655 | break; |
| 656 | default: |
| 657 | LOGINT; |
| 658 | node = NULL; |
| 659 | break; |
| 660 | } |
| 661 | |
| 662 | if (!node) { |
| 663 | lyd_free(ret); |
| 664 | return NULL; |
| 665 | } |
| 666 | if (!ret) { |
| 667 | ret = node; |
| 668 | } |
| 669 | /* special case when we are creating a sibling of a top-level data node */ |
| 670 | if (!is_relative) { |
| 671 | if (data_tree) { |
| 672 | assert(data_tree == parent); |
| 673 | if (lyd_insert_before(data_tree, node)) { |
| 674 | lyd_free(ret); |
| 675 | return NULL; |
| 676 | } |
| 677 | } |
| 678 | is_relative = 1; |
| 679 | } |
| 680 | |
| 681 | parsed = 0; |
| 682 | if ((schild->nodetype == LYS_LIST) && lyd_new_path_list_keys(node, name, path, &parsed)) { |
| 683 | lyd_free(ret); |
| 684 | return NULL; |
| 685 | } |
| 686 | path += parsed; |
| 687 | |
| 688 | if (!path[0]) { |
| 689 | /* we are done */ |
| 690 | return ret; |
| 691 | } else if (!(options & LYD_PATH_OPT_RECURSIVE)) { |
| 692 | /* we were supposed to be done */ |
| 693 | LOGVAL(LYE_PATH_MISSPAR, 0, LY_VLOG_NONE, NULL, (mod_name ? mod_name : name)); |
| 694 | return NULL; |
| 695 | } |
| 696 | |
| 697 | /* prepare for another iteration */ |
| 698 | parent = node; |
| 699 | sparent = schild; |
| 700 | prev_mod = lys_node_module(schild); |
| 701 | |
| 702 | /* parse another node */ |
| 703 | if ((r = parse_schema_nodeid(path, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
| 704 | LOGVAL(LYE_PATH_INCHAR, 0, LY_VLOG_NONE, NULL, path[-r], &path[-r]); |
| 705 | lyd_free(ret); |
| 706 | return NULL; |
| 707 | } |
| 708 | path += r; |
| 709 | |
| 710 | /* if a key of a list was supposed to be created, it is created as a part of the list instance |
| 711 | * creation and can cause several corner cases that can simply be avoided by forbidding this */ |
| 712 | if ((schild->nodetype == LYS_LIST) && !mod_name) { |
| 713 | slist = (const struct lys_node_list *)schild; |
| 714 | for (i = 0; i < slist->keys_size; ++i) { |
| 715 | if (!strncmp(slist->keys[i]->name, name, nam_len) && !slist->keys[i]->name[nam_len]) { |
| 716 | LOGVAL(LYE_PATH_EXISTS, 0, LY_VLOG_NONE, NULL); |
| 717 | lyd_free(ret); |
| 718 | return NULL; |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | return ret; |
| 725 | } |
| 726 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 727 | static void |
| 728 | lyd_insert_setinvalid(struct lyd_node *node) |
| 729 | { |
| 730 | struct lyd_node *next, *elem, *parent_list; |
| 731 | |
| 732 | assert(node); |
| 733 | |
| 734 | /* overall validity of the node itself */ |
| 735 | node->validity = LYD_VAL_NOT; |
| 736 | |
| 737 | /* explore changed unique leafs */ |
| 738 | /* first, get know if there is a list in parents chain */ |
| 739 | for (parent_list = node->parent; |
| 740 | parent_list && parent_list->schema->nodetype != LYS_LIST; |
| 741 | parent_list = parent_list->parent); |
| 742 | if (parent_list && !(parent_list->validity & LYD_VAL_UNIQUE)) { |
| 743 | /* there is a list, so check if we inserted a leaf supposed to be unique */ |
| 744 | for (elem = node; elem; elem = next) { |
| 745 | if (elem->schema->nodetype == LYS_LIST) { |
| 746 | /* stop searching to the depth, children would be unique to a list in subtree */ |
| 747 | goto nextsibling; |
| 748 | } |
| 749 | |
| 750 | if (elem->schema->nodetype == LYS_LEAF && (elem->schema->flags & LYS_UNIQUE)) { |
| 751 | /* set flag to list for future validation */ |
| 752 | parent_list->validity |= LYD_VAL_UNIQUE; |
| 753 | break; |
| 754 | } |
| 755 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 756 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
| 757 | goto nextsibling; |
| 758 | } |
| 759 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 760 | /* select next elem to process */ |
| 761 | /* go into children */ |
| 762 | next = elem->child; |
| 763 | /* got through siblings */ |
| 764 | if (!next) { |
| 765 | nextsibling: |
| 766 | next = elem->next; |
| 767 | if (!next) { |
| 768 | /* no children */ |
| 769 | if (elem == node) { |
| 770 | /* we are done, back in start node */ |
| 771 | break; |
| 772 | } |
| 773 | /* try siblings */ |
| 774 | next = elem->next; |
| 775 | } |
| 776 | } |
| 777 | /* go back to parents */ |
| 778 | while (!next) { |
| 779 | if (elem->parent == node) { |
| 780 | /* we are done, back in start node */ |
| 781 | break; |
| 782 | } |
| 783 | /* parent was actually already processed, so go to the parent's sibling */ |
| 784 | next = elem->parent->next; |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 790 | API int |
| 791 | lyd_insert(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 792 | { |
| 793 | struct lys_node *sparent; |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 794 | struct lyd_node *iter; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 795 | int invalid = 0; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 796 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 797 | if (!node || !parent) { |
| 798 | ly_errno = LY_EINVAL; |
| 799 | return EXIT_FAILURE; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 800 | } |
| 801 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 802 | /* check placing the node to the appropriate place according to the schema */ |
Michal Vasko | b9f6a6c | 2016-01-04 15:41:58 +0100 | [diff] [blame] | 803 | for (sparent = lys_parent(node->schema); |
Michal Vasko | 72d65c9 | 2015-12-07 14:02:35 +0100 | [diff] [blame] | 804 | sparent && !(sparent->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_OUTPUT | LYS_NOTIF)); |
Michal Vasko | b9f6a6c | 2016-01-04 15:41:58 +0100 | [diff] [blame] | 805 | sparent = lys_parent(sparent)); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 806 | if (sparent != parent->schema) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 807 | return EXIT_FAILURE; |
| 808 | } |
| 809 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 810 | if (node->parent != parent || lyp_is_rpc(node->schema)) { |
| 811 | /* it is not just moving under a parent node or it is in an RPC where |
| 812 | * nodes order matters, so the validation will be necessary */ |
| 813 | invalid = 1; |
| 814 | } |
| 815 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 816 | if (node->parent || node->prev->next) { |
| 817 | lyd_unlink(node); |
| 818 | } |
| 819 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 820 | if (!parent->child) { |
| 821 | /* add as the only child of the parent */ |
| 822 | parent->child = node; |
| 823 | } else { |
| 824 | /* add as the last child of the parent */ |
| 825 | parent->child->prev->next = node; |
| 826 | node->prev = parent->child->prev; |
| 827 | for (iter = node; iter->next; iter = iter->next); |
| 828 | parent->child->prev = iter; |
| 829 | } |
Michal Vasko | 9cc2d0a | 2015-10-14 15:49:07 +0200 | [diff] [blame] | 830 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 831 | LY_TREE_FOR(node, iter) { |
| 832 | iter->parent = parent; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 833 | if (invalid) { |
| 834 | lyd_insert_setinvalid(iter); |
| 835 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | return EXIT_SUCCESS; |
| 839 | } |
| 840 | |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 841 | static int |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 842 | lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, int before) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 843 | { |
| 844 | struct lys_node *par1, *par2; |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 845 | struct lyd_node *iter; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 846 | int invalid = 0; |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 847 | |
| 848 | if (sibling == node) { |
| 849 | return EXIT_SUCCESS; |
| 850 | } |
| 851 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 852 | /* check placing the node to the appropriate place according to the schema */ |
Michal Vasko | b9f6a6c | 2016-01-04 15:41:58 +0100 | [diff] [blame] | 853 | for (par1 = lys_parent(sibling->schema); |
Radek Krejci | 2b7bb49 | 2015-12-02 15:46:29 +0100 | [diff] [blame] | 854 | par1 && !(par1->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)); |
Michal Vasko | b9f6a6c | 2016-01-04 15:41:58 +0100 | [diff] [blame] | 855 | par1 = lys_parent(par1)); |
| 856 | for (par2 = lys_parent(node->schema); |
Radek Krejci | 2b7bb49 | 2015-12-02 15:46:29 +0100 | [diff] [blame] | 857 | par2 && !(par2->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)); |
Michal Vasko | b9f6a6c | 2016-01-04 15:41:58 +0100 | [diff] [blame] | 858 | par2 = lys_parent(par2)); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 859 | if (par1 != par2) { |
| 860 | ly_errno = LY_EINVAL; |
| 861 | return EXIT_FAILURE; |
| 862 | } |
| 863 | |
Radek Krejci | c886a2a | 2016-02-09 15:07:16 +0100 | [diff] [blame] | 864 | if (node->parent != sibling->parent || !node->parent || lyp_is_rpc(node->schema)) { |
| 865 | /* a) it is not just moving under a parent node or |
| 866 | * b) it is top-level where we don't know if it is the same tree, or |
| 867 | * c) it is in an RPC where nodes order matters, |
| 868 | * so the validation will be necessary */ |
| 869 | if (!node->parent) { |
| 870 | /* b) search in siblings */ |
| 871 | for (iter = node->prev; iter != node; iter = iter->prev) { |
| 872 | if (iter == sibling) { |
| 873 | break; |
| 874 | } |
| 875 | } |
| 876 | if (iter == node) { |
| 877 | /* node and siblings are not currently in the same data tree */ |
| 878 | invalid = 1; |
| 879 | } |
| 880 | } else { /* a) and c) */ |
| 881 | invalid = 1; |
| 882 | } |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 885 | if (node->parent || node->next || node->prev->next) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 886 | lyd_unlink(node); |
| 887 | } |
| 888 | |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 889 | node->parent = sibling->parent; |
| 890 | if (invalid) { |
| 891 | lyd_insert_setinvalid(node); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 892 | } |
| 893 | |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 894 | if (before) { |
| 895 | if (sibling->prev->next) { |
| 896 | /* adding into the list */ |
| 897 | sibling->prev->next = node; |
| 898 | } else if (sibling->parent) { |
| 899 | /* at the beginning */ |
| 900 | sibling->parent->child = node; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 901 | } |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 902 | node->prev = sibling->prev; |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 903 | sibling->prev = node; |
| 904 | node->next = sibling; |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 905 | } else { |
| 906 | if (sibling->next) { |
| 907 | /* adding into a middle - fix the prev pointer of the node after inserted nodes */ |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 908 | node->next = sibling->next; |
| 909 | sibling->next->prev = node; |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 910 | } else { |
| 911 | /* at the end - fix the prev pointer of the first node */ |
| 912 | if (sibling->parent) { |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 913 | sibling->parent->child->prev = node; |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 914 | } else { |
| 915 | for (iter = sibling; iter->prev->next; iter = iter->prev); |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 916 | iter->prev = node; |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | sibling->next = node; |
| 920 | node->prev = sibling; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 921 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 922 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 923 | return EXIT_SUCCESS; |
| 924 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 925 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 926 | API int |
| 927 | lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node) |
| 928 | { |
| 929 | if (!node || !sibling || lyd_insert_sibling(sibling, node, 1)) { |
| 930 | ly_errno = LY_EINVAL; |
| 931 | return EXIT_FAILURE; |
| 932 | } |
| 933 | |
| 934 | return EXIT_SUCCESS; |
| 935 | } |
| 936 | |
| 937 | API int |
| 938 | lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node) |
| 939 | { |
| 940 | if (!node || !sibling || lyd_insert_sibling(sibling, node, 0)) { |
| 941 | ly_errno = LY_EINVAL; |
| 942 | return EXIT_FAILURE; |
| 943 | } |
| 944 | |
| 945 | return EXIT_SUCCESS; |
| 946 | } |
| 947 | |
| 948 | API int |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 949 | lyd_validate(struct lyd_node *node, int options, ...) |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 950 | { |
Radek Krejci | 15c733d | 2016-01-27 16:31:17 +0100 | [diff] [blame] | 951 | struct lyd_node *root, *next1, *next2, *iter, *to_free = NULL; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 952 | const struct lys_node *schema; |
| 953 | struct ly_ctx *ctx; |
| 954 | int i; |
| 955 | va_list ap; |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 956 | |
| 957 | ly_errno = 0; |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 958 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 959 | if (!node) { |
| 960 | /* TODO what about LYD_OPT_NOTIF, LYD_OPT_RPC and LYD_OPT_RPCREPLY ? */ |
| 961 | if (options & (LYD_OPT_FILTER | LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG)) { |
| 962 | return EXIT_SUCCESS; |
| 963 | } |
| 964 | /* LYD_OPT_DATA || LYD_OPT_CONFIG */ |
| 965 | |
| 966 | /* get context with schemas from the variable arguments */ |
| 967 | va_start(ap, options); |
| 968 | ctx = va_arg(ap, struct ly_ctx*); |
| 969 | if (!ctx) { |
| 970 | LOGERR(LY_EINVAL, "%s: Invalid variable argument.", __func__); |
| 971 | va_end(ap); |
| 972 | return EXIT_FAILURE; |
| 973 | } |
| 974 | |
| 975 | /* check for missing mandatory elements according to schemas in context */ |
| 976 | for (i = 0; i < ctx->models.used; i++) { |
| 977 | if (!ctx->models.list[i]->data) { |
| 978 | continue; |
| 979 | } |
| 980 | schema = ly_check_mandatory(NULL, ctx->models.list[i]->data); |
| 981 | if (schema) { |
| 982 | if (schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
Michal Vasko | 6ea3e36 | 2016-03-11 10:25:36 +0100 | [diff] [blame] | 983 | LOGVAL(LYE_TOOMANY, 0, LY_VLOG_LYS, schema, schema->name, schema->parent ? schema->parent->name : "module"); |
| 984 | LOGVAL(LYE_SPEC, 0, 0, NULL, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 985 | "Number of \"%s\" instances in \"%s\" does not follow min-elements constraint.", |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 986 | schema->name, schema->parent ? schema->parent->name : ctx->models.list[i]->name); |
| 987 | } else { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 988 | LOGVAL(LYE_MISSELEM, 0, LY_VLOG_LYS, schema, |
| 989 | schema->name, schema->parent ? schema->parent->name : ctx->models.list[i]->name); |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 990 | } |
| 991 | va_end(ap); |
| 992 | return EXIT_FAILURE; |
| 993 | |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | va_end(ap); |
| 998 | return EXIT_SUCCESS; |
| 999 | } |
| 1000 | |
Radek Krejci | 27e8226 | 2016-01-21 17:15:05 +0100 | [diff] [blame] | 1001 | if (!(options & LYD_OPT_NOSIBLINGS)) { |
| 1002 | /* check that the node is the first sibling */ |
| 1003 | while(node->prev->next) { |
| 1004 | node = node->prev; |
Radek Krejci | f03ec15 | 2015-12-03 13:18:37 +0100 | [diff] [blame] | 1005 | } |
Radek Krejci | 27e8226 | 2016-01-21 17:15:05 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
Radek Krejci | 15c733d | 2016-01-27 16:31:17 +0100 | [diff] [blame] | 1008 | LY_TREE_FOR_SAFE(node, next1, root) { |
| 1009 | LY_TREE_DFS_BEGIN(root, next2, iter) { |
Radek Krejci | 27e8226 | 2016-01-21 17:15:05 +0100 | [diff] [blame] | 1010 | if (to_free) { |
| 1011 | lyd_free(to_free); |
| 1012 | to_free = NULL; |
| 1013 | } |
| 1014 | |
| 1015 | if (lyv_data_context(iter, options, 0, NULL)) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1016 | return EXIT_FAILURE; |
Radek Krejci | 27e8226 | 2016-01-21 17:15:05 +0100 | [diff] [blame] | 1017 | } |
Radek Krejci | 15c733d | 2016-01-27 16:31:17 +0100 | [diff] [blame] | 1018 | if (lyv_data_value(iter, options)) { |
| 1019 | return EXIT_FAILURE; |
| 1020 | } |
Radek Krejci | 27e8226 | 2016-01-21 17:15:05 +0100 | [diff] [blame] | 1021 | if (lyv_data_content(iter, options, 0, NULL)) { |
| 1022 | if (ly_errno) { |
| 1023 | return EXIT_FAILURE; |
| 1024 | } else { |
| 1025 | /* safe deferred removal */ |
| 1026 | to_free = iter; |
Radek Krejci | 15c733d | 2016-01-27 16:31:17 +0100 | [diff] [blame] | 1027 | next2 = NULL; |
| 1028 | goto nextsiblings; |
Radek Krejci | efe6a14 | 2015-12-03 14:01:00 +0100 | [diff] [blame] | 1029 | } |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 1030 | } |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1031 | |
Radek Krejci | 27e8226 | 2016-01-21 17:15:05 +0100 | [diff] [blame] | 1032 | /* validation successful */ |
| 1033 | iter->validity = LYD_VAL_OK; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 1034 | |
Radek Krejci | 15c733d | 2016-01-27 16:31:17 +0100 | [diff] [blame] | 1035 | /* where go next? - modified LY_TREE_DFS_END */ |
| 1036 | if (iter->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
| 1037 | next2 = NULL; |
| 1038 | } else { |
| 1039 | next2 = iter->child; |
| 1040 | } |
| 1041 | nextsiblings: |
| 1042 | if (!next2) { |
| 1043 | /* no children */ |
| 1044 | if (iter == root) { |
| 1045 | /* we are done */ |
| 1046 | break; |
| 1047 | } |
| 1048 | /* try siblings */ |
| 1049 | next2 = iter->next; |
| 1050 | } |
| 1051 | while (!next2) { |
| 1052 | iter = iter->parent; |
| 1053 | /* parent is already processed, go to its sibling */ |
| 1054 | if (iter->parent == root->parent) { |
| 1055 | /* we are done */ |
| 1056 | break; |
| 1057 | } |
| 1058 | next2 = iter->next; |
| 1059 | } /* end of modified LY_TREE_DFS_END */ |
| 1060 | } |
| 1061 | |
| 1062 | if (to_free) { |
| 1063 | if (node == to_free) { |
| 1064 | /* we shouldn't be here */ |
| 1065 | assert(0); |
| 1066 | } |
| 1067 | lyd_free(to_free); |
| 1068 | to_free = NULL; |
| 1069 | } |
Radek Krejci | 27e8226 | 2016-01-21 17:15:05 +0100 | [diff] [blame] | 1070 | |
| 1071 | if (options & LYD_OPT_NOSIBLINGS) { |
| 1072 | break; |
| 1073 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 1076 | |
| 1077 | return EXIT_SUCCESS; |
| 1078 | } |
| 1079 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1080 | /* create an attribute copy */ |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 1081 | static struct lyd_attr * |
| 1082 | lyd_dup_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr) |
| 1083 | { |
| 1084 | struct lyd_attr *ret; |
| 1085 | |
| 1086 | /* allocate new attr */ |
| 1087 | if (!parent->attr) { |
| 1088 | parent->attr = malloc(sizeof *parent->attr); |
| 1089 | ret = parent->attr; |
| 1090 | } else { |
| 1091 | for (ret = parent->attr; ret->next; ret = ret->next); |
| 1092 | ret->next = malloc(sizeof *ret); |
| 1093 | ret = ret->next; |
| 1094 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1095 | if (!ret) { |
| 1096 | LOGMEM; |
| 1097 | return NULL; |
| 1098 | } |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 1099 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1100 | /* fill new attr except */ |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 1101 | ret->next = NULL; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1102 | ret->module = attr->module; |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 1103 | ret->name = lydict_insert(ctx, attr->name, 0); |
| 1104 | ret->value = lydict_insert(ctx, attr->value, 0); |
| 1105 | |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 1106 | return ret; |
| 1107 | } |
| 1108 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1109 | API int |
| 1110 | lyd_unlink(struct lyd_node *node) |
| 1111 | { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1112 | struct lyd_node *iter, *next; |
| 1113 | struct ly_set *set, *data; |
| 1114 | unsigned int i, j; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1115 | |
| 1116 | if (!node) { |
| 1117 | ly_errno = LY_EINVAL; |
| 1118 | return EXIT_FAILURE; |
| 1119 | } |
| 1120 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1121 | /* fix leafrefs */ |
| 1122 | LY_TREE_DFS_BEGIN(node, next, iter) { |
| 1123 | /* the node is target of a leafref */ |
| 1124 | if ((iter->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && iter->schema->child) { |
| 1125 | set = (struct ly_set *)iter->schema->child; |
| 1126 | for (i = 0; i < set->number; i++) { |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 1127 | data = lyd_get_node2(iter, set->sset[i]); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1128 | if (data) { |
| 1129 | for (j = 0; j < data->number; j++) { |
| 1130 | if (((struct lyd_node_leaf_list *)data->dset[j])->value.leafref == iter) { |
| 1131 | /* remove reference to the node we are going to replace */ |
| 1132 | ((struct lyd_node_leaf_list *)data->dset[j])->value.leafref = NULL; |
| 1133 | } |
| 1134 | } |
| 1135 | ly_set_free(data); |
| 1136 | } |
| 1137 | } |
| 1138 | } |
| 1139 | LY_TREE_DFS_END(node, next, iter) |
| 1140 | } |
| 1141 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1142 | /* unlink from siblings */ |
| 1143 | if (node->prev->next) { |
| 1144 | node->prev->next = node->next; |
| 1145 | } |
| 1146 | if (node->next) { |
| 1147 | node->next->prev = node->prev; |
| 1148 | } else { |
| 1149 | /* unlinking the last node */ |
Radek Krejci | 3263631 | 2016-01-07 13:49:48 +0100 | [diff] [blame] | 1150 | if (node->parent) { |
| 1151 | iter = node->parent->child; |
| 1152 | } else { |
| 1153 | iter = node->prev; |
| 1154 | while (iter->prev != node) { |
| 1155 | iter = iter->prev; |
| 1156 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1157 | } |
| 1158 | /* update the "last" pointer from the first node */ |
| 1159 | iter->prev = node->prev; |
| 1160 | } |
| 1161 | |
| 1162 | /* unlink from parent */ |
| 1163 | if (node->parent) { |
| 1164 | if (node->parent->child == node) { |
| 1165 | /* the node is the first child */ |
| 1166 | node->parent->child = node->next; |
| 1167 | } |
| 1168 | node->parent = NULL; |
| 1169 | } |
| 1170 | |
| 1171 | node->next = NULL; |
| 1172 | node->prev = node; |
| 1173 | |
| 1174 | return EXIT_SUCCESS; |
| 1175 | } |
| 1176 | |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1177 | API struct lyd_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1178 | lyd_dup(const struct lyd_node *node, int recursive) |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1179 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1180 | const struct lyd_node *next, *elem; |
| 1181 | struct lyd_node *ret, *parent, *new_node; |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1182 | struct lyd_attr *attr; |
| 1183 | struct lyd_node_leaf_list *new_leaf; |
| 1184 | struct lyd_node_anyxml *new_axml; |
| 1185 | struct lys_type *type; |
| 1186 | |
| 1187 | if (!node) { |
| 1188 | ly_errno = LY_EINVAL; |
| 1189 | return NULL; |
| 1190 | } |
| 1191 | |
| 1192 | ret = NULL; |
| 1193 | parent = NULL; |
| 1194 | |
| 1195 | /* LY_TREE_DFS */ |
| 1196 | for (elem = next = node; elem; elem = next) { |
| 1197 | |
| 1198 | /* fill specific part */ |
| 1199 | switch (elem->schema->nodetype) { |
| 1200 | case LYS_LEAF: |
| 1201 | case LYS_LEAFLIST: |
| 1202 | new_leaf = malloc(sizeof *new_leaf); |
| 1203 | new_node = (struct lyd_node *)new_leaf; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1204 | if (!new_node) { |
| 1205 | LOGMEM; |
| 1206 | return NULL; |
| 1207 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1208 | |
| 1209 | new_leaf->value = ((struct lyd_node_leaf_list *)elem)->value; |
| 1210 | new_leaf->value_str = lydict_insert(elem->schema->module->ctx, |
| 1211 | ((struct lyd_node_leaf_list *)elem)->value_str, 0); |
| 1212 | new_leaf->value_type = ((struct lyd_node_leaf_list *)elem)->value_type; |
| 1213 | /* bits type must be treated specially */ |
| 1214 | if (new_leaf->value_type == LY_TYPE_BITS) { |
| 1215 | for (type = &((struct lys_node_leaf *)elem->schema)->type; type->der->module; type = &type->der->type) { |
| 1216 | if (type->base != LY_TYPE_BITS) { |
| 1217 | LOGINT; |
Michal Vasko | d80e6c7 | 2015-10-15 09:37:01 +0200 | [diff] [blame] | 1218 | lyd_free(new_node); |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1219 | lyd_free(ret); |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | new_leaf->value.bit = malloc(type->info.bits.count * sizeof *new_leaf->value.bit); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1225 | if (!new_leaf->value.bit) { |
| 1226 | LOGMEM; |
| 1227 | lyd_free(new_node); |
| 1228 | lyd_free(ret); |
| 1229 | return NULL; |
| 1230 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1231 | memcpy(new_leaf->value.bit, ((struct lyd_node_leaf_list *)elem)->value.bit, |
| 1232 | type->info.bits.count * sizeof *new_leaf->value.bit); |
| 1233 | } |
| 1234 | break; |
| 1235 | case LYS_ANYXML: |
| 1236 | new_axml = malloc(sizeof *new_axml); |
| 1237 | new_node = (struct lyd_node *)new_axml; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1238 | if (!new_node) { |
| 1239 | LOGMEM; |
| 1240 | return NULL; |
| 1241 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1242 | |
| 1243 | new_axml->value = lyxml_dup_elem(elem->schema->module->ctx, ((struct lyd_node_anyxml *)elem)->value, |
| 1244 | NULL, 1); |
| 1245 | break; |
| 1246 | case LYS_CONTAINER: |
| 1247 | case LYS_LIST: |
| 1248 | case LYS_NOTIF: |
| 1249 | case LYS_RPC: |
| 1250 | new_node = malloc(sizeof *new_node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1251 | if (!new_node) { |
| 1252 | LOGMEM; |
| 1253 | return NULL; |
| 1254 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1255 | new_node->child = NULL; |
| 1256 | break; |
| 1257 | default: |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1258 | lyd_free(ret); |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1259 | LOGINT; |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1260 | return NULL; |
| 1261 | } |
| 1262 | |
| 1263 | /* fill common part */ |
| 1264 | new_node->schema = elem->schema; |
| 1265 | new_node->attr = NULL; |
| 1266 | LY_TREE_FOR(elem->attr, attr) { |
| 1267 | lyd_dup_attr(elem->schema->module->ctx, new_node, attr); |
| 1268 | } |
| 1269 | new_node->next = NULL; |
| 1270 | new_node->prev = new_node; |
| 1271 | new_node->parent = NULL; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 1272 | new_node->validity = LYD_VAL_NOT; |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1273 | |
| 1274 | if (!ret) { |
| 1275 | ret = new_node; |
| 1276 | } |
| 1277 | if (parent) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1278 | if (lyd_insert(parent, new_node)) { |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1279 | lyd_free(ret); |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1280 | LOGINT; |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1281 | return NULL; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | if (!recursive) { |
| 1286 | break; |
| 1287 | } |
| 1288 | |
| 1289 | /* LY_TREE_DFS_END */ |
| 1290 | /* select element for the next run - children first */ |
| 1291 | next = elem->child; |
| 1292 | /* child exception for lyd_node_leaf and lyd_node_leaflist */ |
| 1293 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
| 1294 | next = NULL; |
| 1295 | } |
| 1296 | if (!next) { |
| 1297 | /* no children, so try siblings */ |
| 1298 | next = elem->next; |
| 1299 | } else { |
| 1300 | parent = new_node; |
| 1301 | } |
| 1302 | while (!next) { |
| 1303 | /* no siblings, go back through parents */ |
| 1304 | elem = elem->parent; |
| 1305 | if (elem->parent == node->parent) { |
| 1306 | break; |
| 1307 | } |
Michal Vasko | 785b2ad | 2015-10-15 09:37:15 +0200 | [diff] [blame] | 1308 | if (!parent) { |
Michal Vasko | c8f5d80 | 2015-10-23 10:14:39 +0200 | [diff] [blame] | 1309 | lyd_free(ret); |
Michal Vasko | 785b2ad | 2015-10-15 09:37:15 +0200 | [diff] [blame] | 1310 | LOGINT; |
Michal Vasko | c8f5d80 | 2015-10-23 10:14:39 +0200 | [diff] [blame] | 1311 | return NULL; |
Michal Vasko | 785b2ad | 2015-10-15 09:37:15 +0200 | [diff] [blame] | 1312 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 1313 | parent = parent->parent; |
| 1314 | /* parent is already processed, go to its sibling */ |
| 1315 | next = elem->next; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | return ret; |
| 1320 | } |
| 1321 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1322 | API void |
| 1323 | lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1324 | { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1325 | struct lyd_attr *iter; |
| 1326 | |
| 1327 | if (!ctx || !attr) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1328 | return; |
| 1329 | } |
| 1330 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1331 | if (parent) { |
| 1332 | if (parent->attr == attr) { |
| 1333 | if (recursive) { |
| 1334 | parent->attr = NULL; |
| 1335 | } else { |
| 1336 | parent->attr = attr->next; |
| 1337 | } |
| 1338 | } else { |
| 1339 | for (iter = parent->attr; iter->next != attr; iter = iter->next); |
| 1340 | if (iter->next) { |
| 1341 | if (recursive) { |
| 1342 | iter->next = NULL; |
| 1343 | } else { |
| 1344 | iter->next = attr->next; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1348 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1349 | |
| 1350 | if (!recursive) { |
| 1351 | attr->next = NULL; |
| 1352 | } |
| 1353 | |
| 1354 | for(iter = attr; iter; ) { |
| 1355 | attr = iter; |
| 1356 | iter = iter->next; |
| 1357 | |
| 1358 | lydict_remove(ctx, attr->name); |
| 1359 | lydict_remove(ctx, attr->value); |
| 1360 | free(attr); |
| 1361 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1362 | } |
| 1363 | |
Michal Vasko | fd76bd1 | 2015-09-24 15:49:57 +0200 | [diff] [blame] | 1364 | struct lyd_node * |
| 1365 | lyd_attr_parent(struct lyd_node *root, struct lyd_attr *attr) |
| 1366 | { |
| 1367 | struct lyd_node *next, *elem; |
| 1368 | struct lyd_attr *node_attr; |
| 1369 | |
| 1370 | LY_TREE_DFS_BEGIN(root, next, elem) { |
| 1371 | for (node_attr = elem->attr; node_attr; node_attr = node_attr->next) { |
| 1372 | if (node_attr == attr) { |
| 1373 | return elem; |
| 1374 | } |
| 1375 | } |
| 1376 | LY_TREE_DFS_END(root, next, elem) |
| 1377 | } |
| 1378 | |
| 1379 | return NULL; |
| 1380 | } |
| 1381 | |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1382 | API struct lyd_attr * |
| 1383 | lyd_insert_attr(struct lyd_node *parent, const char *name, const char *value) |
| 1384 | { |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1385 | struct lyd_attr *a, *iter; |
| 1386 | struct ly_ctx *ctx; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1387 | const struct lys_module *module; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1388 | const char *p; |
| 1389 | char *aux; |
| 1390 | |
| 1391 | if (!parent || !name || !value) { |
| 1392 | return NULL; |
| 1393 | } |
| 1394 | ctx = parent->schema->module->ctx; |
| 1395 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1396 | if ((p = strchr(name, ':'))) { |
| 1397 | /* search for the namespace */ |
| 1398 | aux = strndup(name, p - name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1399 | if (!aux) { |
| 1400 | LOGMEM; |
| 1401 | return NULL; |
| 1402 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1403 | module = ly_ctx_get_module(ctx, aux, NULL); |
| 1404 | free(aux); |
| 1405 | name = p + 1; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1406 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1407 | if (!module) { |
| 1408 | /* module not found */ |
Radek Krejci | a008ca9 | 2015-10-30 15:52:05 +0100 | [diff] [blame] | 1409 | LOGERR(LY_EINVAL, "Attribute prefix does not match any schema in the context."); |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1410 | return NULL; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1411 | } |
| 1412 | } else { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1413 | /* no prefix -> module is the same as for the parent */ |
| 1414 | module = parent->schema->module; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1417 | a = malloc(sizeof *a); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1418 | if (!a) { |
| 1419 | LOGMEM; |
| 1420 | return NULL; |
| 1421 | } |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1422 | a->module = (struct lys_module *)module; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1423 | a->next = NULL; |
| 1424 | a->name = lydict_insert(ctx, name, 0); |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1425 | a->value = lydict_insert(ctx, value, 0); |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 1426 | |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1427 | if (!parent->attr) { |
| 1428 | parent->attr = a; |
| 1429 | } else { |
| 1430 | for (iter = parent->attr; iter->next; iter = iter->next); |
| 1431 | iter->next = a; |
| 1432 | } |
| 1433 | |
| 1434 | return a; |
| 1435 | } |
| 1436 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1437 | API void |
| 1438 | lyd_free(struct lyd_node *node) |
| 1439 | { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1440 | struct lyd_node *next, *iter; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1441 | |
| 1442 | if (!node) { |
| 1443 | return; |
| 1444 | } |
| 1445 | |
| 1446 | if (!(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) { |
| 1447 | /* free children */ |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1448 | LY_TREE_FOR_SAFE(node->child, next, iter) { |
| 1449 | lyd_free(iter); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1450 | } |
| 1451 | } else if (node->schema->nodetype == LYS_ANYXML) { |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 1452 | lyxml_free(node->schema->module->ctx, ((struct lyd_node_anyxml *)node)->value); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1453 | } else { /* LYS_LEAF | LYS_LEAFLIST */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1454 | /* free value */ |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1455 | switch (((struct lyd_node_leaf_list *)node)->value_type) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1456 | case LY_TYPE_BINARY: |
| 1457 | case LY_TYPE_STRING: |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 1458 | lydict_remove(node->schema->module->ctx, ((struct lyd_node_leaf_list *)node)->value.string); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1459 | break; |
| 1460 | case LY_TYPE_BITS: |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 1461 | if (((struct lyd_node_leaf_list *)node)->value.bit) { |
| 1462 | free(((struct lyd_node_leaf_list *)node)->value.bit); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1463 | } |
| 1464 | break; |
| 1465 | default: |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 1466 | lydict_remove(node->schema->module->ctx, ((struct lyd_node_leaf_list *)node)->value_str); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1467 | break; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | lyd_unlink(node); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1472 | lyd_free_attr(node->schema->module->ctx, node, node->attr, 1); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1473 | free(node); |
| 1474 | } |
| 1475 | |
Radek Krejci | 8146840 | 2016-01-07 13:52:40 +0100 | [diff] [blame] | 1476 | API void |
| 1477 | lyd_free_withsiblings(struct lyd_node *node) |
| 1478 | { |
| 1479 | struct lyd_node *iter, *aux; |
| 1480 | |
| 1481 | if (!node) { |
| 1482 | return; |
| 1483 | } |
| 1484 | |
| 1485 | /* optimization - avoid freeing (unlinking) the last node of the siblings list */ |
| 1486 | /* so, first, free the node's predecessors to the beginning of the list ... */ |
| 1487 | for(iter = node->prev; iter->next; iter = aux) { |
| 1488 | aux = iter->prev; |
| 1489 | lyd_free(iter); |
| 1490 | } |
| 1491 | /* ... then, the node is the first in the siblings list, so free them all */ |
| 1492 | LY_TREE_FOR_SAFE(node, aux, iter) { |
| 1493 | lyd_free(iter); |
| 1494 | } |
| 1495 | } |
| 1496 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1497 | int |
| 1498 | lyd_compare(struct lyd_node *first, struct lyd_node *second, int unique) |
| 1499 | { |
| 1500 | struct lys_node_list *slist; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1501 | const struct lys_node *snode = NULL; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1502 | struct lyd_node *diter; |
| 1503 | const char *val1, *val2; |
| 1504 | int i, j; |
| 1505 | |
| 1506 | assert(first); |
| 1507 | assert(second); |
| 1508 | |
| 1509 | if (first->schema != second->schema) { |
| 1510 | return 1; |
| 1511 | } |
| 1512 | |
| 1513 | switch (first->schema->nodetype) { |
| 1514 | case LYS_LEAFLIST: |
| 1515 | /* compare values */ |
Radek Krejci | edaaa08 | 2016-02-17 10:21:54 +0100 | [diff] [blame] | 1516 | if (ly_strequal(((struct lyd_node_leaf_list *)first)->value_str, |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1517 | ((struct lyd_node_leaf_list *)second)->value_str, 1)) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1518 | return 0; |
| 1519 | } |
| 1520 | return 1; |
| 1521 | case LYS_LIST: |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 1522 | slist = (struct lys_node_list *)first->schema; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1523 | |
| 1524 | if (unique) { |
| 1525 | /* compare unique leafs */ |
| 1526 | for (i = 0; i < slist->unique_size; i++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1527 | for (j = 0; j < slist->unique[i].expr_size; j++) { |
| 1528 | /* first */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1529 | diter = resolve_data_descendant_schema_nodeid(slist->unique[i].expr[j], first->child); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1530 | if (diter) { |
| 1531 | val1 = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 1532 | } else { |
| 1533 | /* use default value */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1534 | if (resolve_descendant_schema_nodeid(slist->unique[i].expr[j], first->schema->child, LYS_LEAF, &snode)) { |
Radek Krejci | 3eb09b6 | 2015-12-16 15:22:19 +0100 | [diff] [blame] | 1535 | /* error, but unique expression was checked when the schema was parsed */ |
| 1536 | return -1; |
| 1537 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1538 | val1 = ((struct lys_node_leaf *)snode)->dflt; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1539 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1540 | |
| 1541 | /* second */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1542 | diter = resolve_data_descendant_schema_nodeid(slist->unique[i].expr[j], second->child); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1543 | if (diter) { |
| 1544 | val2 = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 1545 | } else { |
| 1546 | /* use default value */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1547 | if (resolve_descendant_schema_nodeid(slist->unique[i].expr[j], second->schema->child, LYS_LEAF, &snode)) { |
Radek Krejci | 3eb09b6 | 2015-12-16 15:22:19 +0100 | [diff] [blame] | 1548 | /* error, but unique expression was checked when the schema was parsed */ |
| 1549 | return -1; |
| 1550 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1551 | val2 = ((struct lys_node_leaf *)snode)->dflt; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1552 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1553 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1554 | if (!ly_strequal(val1, val2, 1)) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1555 | break; |
| 1556 | } |
| 1557 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1558 | if (j && j == slist->unique[i].expr_size) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1559 | /* all unique leafs are the same in this set */ |
| 1560 | return 0; |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 1565 | if (second->validity == LYD_VAL_UNIQUE) { |
| 1566 | /* only unique part changed somewhere, so it is no need to check keys */ |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1570 | /* compare keys */ |
| 1571 | for (i = 0; i < slist->keys_size; i++) { |
| 1572 | snode = (struct lys_node *)slist->keys[i]; |
| 1573 | val1 = val2 = NULL; |
| 1574 | LY_TREE_FOR(first->child, diter) { |
| 1575 | if (diter->schema == snode) { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 1576 | val1 = ((struct lyd_node_leaf_list *)diter)->value_str; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1577 | break; |
| 1578 | } |
| 1579 | } |
| 1580 | LY_TREE_FOR(second->child, diter) { |
| 1581 | if (diter->schema == snode) { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 1582 | val2 = ((struct lyd_node_leaf_list *)diter)->value_str; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1583 | break; |
| 1584 | } |
| 1585 | } |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1586 | if (!ly_strequal(val1, val2, 1)) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1587 | return 1; |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | return 0; |
| 1592 | default: |
| 1593 | /* no additional check is needed */ |
| 1594 | return 0; |
| 1595 | } |
| 1596 | } |
| 1597 | |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1598 | API struct ly_set * |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 1599 | lyd_get_node(const struct lyd_node *data, const char *expr) |
| 1600 | { |
| 1601 | struct lyxp_set xp_set; |
| 1602 | struct ly_set *set; |
| 1603 | uint16_t i; |
| 1604 | |
| 1605 | if (!data || !expr) { |
| 1606 | ly_errno = LY_EINVAL; |
| 1607 | return NULL; |
| 1608 | } |
| 1609 | |
| 1610 | memset(&xp_set, 0, sizeof xp_set); |
| 1611 | |
| 1612 | if (lyxp_eval(expr, data, &xp_set, 0, 0) != EXIT_SUCCESS) { |
| 1613 | return NULL; |
| 1614 | } |
| 1615 | |
| 1616 | set = ly_set_new(); |
| 1617 | if (!set) { |
| 1618 | LOGMEM; |
| 1619 | return NULL; |
| 1620 | } |
| 1621 | |
| 1622 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 1623 | for (i = 0; i < xp_set.used; ++i) { |
| 1624 | if ((xp_set.node_type[i] == LYXP_NODE_ELEM) || (xp_set.node_type[i] == LYXP_NODE_TEXT)) { |
| 1625 | if (ly_set_add(set, xp_set.value.nodes[i])) { |
| 1626 | ly_set_free(set); |
| 1627 | set = NULL; |
| 1628 | break; |
| 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | lyxp_set_cast(&xp_set, LYXP_SET_EMPTY, data, 0); |
| 1634 | |
| 1635 | return set; |
| 1636 | } |
| 1637 | |
| 1638 | API struct ly_set * |
| 1639 | lyd_get_node2(const struct lyd_node *data, const struct lys_node *schema) |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1640 | { |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1641 | struct ly_set *ret, *ret_aux, *spath; |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1642 | const struct lys_node *siter; |
| 1643 | struct lyd_node *iter; |
| 1644 | unsigned int i, j; |
| 1645 | |
| 1646 | if (!data || !schema || |
| 1647 | !(schema->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_ANYXML | LYS_NOTIF | LYS_RPC))) { |
| 1648 | ly_errno = LY_EINVAL; |
| 1649 | return NULL; |
| 1650 | } |
| 1651 | |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1652 | ret = ly_set_new(); |
| 1653 | spath = ly_set_new(); |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1654 | if (!ret || !spath) { |
| 1655 | LOGMEM; |
| 1656 | goto error; |
| 1657 | } |
| 1658 | |
| 1659 | /* find data root */ |
| 1660 | while (data->parent) { |
| 1661 | /* vertical move (up) */ |
| 1662 | data = data->parent; |
| 1663 | } |
| 1664 | while (data->prev->next) { |
| 1665 | /* horizontal move (left) */ |
| 1666 | data = data->prev; |
| 1667 | } |
| 1668 | |
| 1669 | /* build schema path */ |
| 1670 | for (siter = schema; siter; ) { |
| 1671 | if (siter->nodetype == LYS_AUGMENT) { |
| 1672 | siter = ((struct lys_node_augment *)siter)->target; |
| 1673 | continue; |
| 1674 | } else if (siter->nodetype == LYS_OUTPUT) { |
| 1675 | /* done for RPC reply */ |
| 1676 | break; |
| 1677 | } else if (siter->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_ANYXML | LYS_NOTIF | LYS_RPC)) { |
| 1678 | /* standard data node */ |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1679 | ly_set_add(spath, (void*)siter); |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1680 | |
| 1681 | } /* else skip the rest node types */ |
| 1682 | siter = siter->parent; |
| 1683 | } |
| 1684 | if (!spath->number) { |
| 1685 | /* no valid path */ |
| 1686 | goto error; |
| 1687 | } |
| 1688 | |
| 1689 | /* start searching */ |
| 1690 | LY_TREE_FOR((struct lyd_node *)data, iter) { |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1691 | if (iter->schema == spath->sset[spath->number - 1]) { |
| 1692 | ly_set_add(ret, iter); |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | for (i = spath->number - 1; i; i--) { |
| 1696 | if (!ret->number) { |
| 1697 | /* nothing found */ |
Radek Krejci | 6c0993c | 2016-01-19 09:40:16 +0100 | [diff] [blame] | 1698 | break; |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1699 | } |
| 1700 | |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1701 | ret_aux = ly_set_new(); |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1702 | if (!ret_aux) { |
| 1703 | LOGMEM; |
| 1704 | goto error; |
| 1705 | } |
| 1706 | for (j = 0; j < ret->number; j++) { |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1707 | LY_TREE_FOR(ret->dset[j]->child, iter) { |
| 1708 | if (iter->schema == spath->sset[i - 1]) { |
| 1709 | ly_set_add(ret_aux, iter); |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | } |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1713 | ly_set_free(ret); |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1714 | ret = ret_aux; |
| 1715 | } |
| 1716 | |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1717 | ly_set_free(spath); |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1718 | return ret; |
| 1719 | |
| 1720 | error: |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1721 | ly_set_free(ret); |
| 1722 | ly_set_free(spath); |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1723 | |
| 1724 | return NULL; |
| 1725 | } |
| 1726 | |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1727 | API struct ly_set * |
Michal Vasko | 6a1ab6f | 2016-02-04 12:08:11 +0100 | [diff] [blame] | 1728 | lyd_get_list_keys(const struct lyd_node *list) |
| 1729 | { |
| 1730 | struct lyd_node *key; |
| 1731 | struct lys_node_list *slist; |
| 1732 | struct ly_set *set; |
| 1733 | unsigned int i; |
| 1734 | |
| 1735 | if (!list || (list->schema->nodetype != LYS_LIST)) { |
| 1736 | ly_errno = LY_EINVAL; |
| 1737 | return NULL; |
| 1738 | } |
| 1739 | |
| 1740 | slist = (struct lys_node_list *)list->schema; |
| 1741 | |
| 1742 | set = ly_set_new(); |
| 1743 | if (!set) { |
| 1744 | LOGMEM; |
| 1745 | return NULL; |
| 1746 | } |
| 1747 | |
| 1748 | for (i = 0; i < slist->keys_size; ++i) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1749 | key = resolve_data_descendant_schema_nodeid(slist->keys[i]->name, list->child); |
Michal Vasko | 6a1ab6f | 2016-02-04 12:08:11 +0100 | [diff] [blame] | 1750 | if (key) { |
| 1751 | ly_set_add(set, key); |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | return set; |
| 1756 | } |
| 1757 | |
| 1758 | API struct ly_set * |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1759 | ly_set_new(void) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1760 | { |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1761 | return calloc(1, sizeof(struct ly_set)); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | API void |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1765 | ly_set_free(struct ly_set *set) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1766 | { |
| 1767 | if (!set) { |
| 1768 | return; |
| 1769 | } |
| 1770 | |
| 1771 | free(set->set); |
| 1772 | free(set); |
| 1773 | } |
| 1774 | |
| 1775 | API int |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1776 | ly_set_add(struct ly_set *set, void *node) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1777 | { |
Radek Krejci | 87fd4df | 2016-01-18 14:44:20 +0100 | [diff] [blame] | 1778 | unsigned int i; |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1779 | void **new; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1780 | |
Radek Krejci | 87fd4df | 2016-01-18 14:44:20 +0100 | [diff] [blame] | 1781 | if (!set || !node) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1782 | ly_errno = LY_EINVAL; |
| 1783 | return EXIT_FAILURE; |
| 1784 | } |
| 1785 | |
Radek Krejci | 87fd4df | 2016-01-18 14:44:20 +0100 | [diff] [blame] | 1786 | /* search for duplication */ |
| 1787 | for (i = 0; i < set->number; i++) { |
| 1788 | if (set->set[i] == node) { |
| 1789 | /* already in set */ |
| 1790 | return EXIT_SUCCESS; |
| 1791 | } |
| 1792 | } |
| 1793 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1794 | if (set->size == set->number) { |
Radek Krejci | c5c4598 | 2016-01-07 12:50:44 +0100 | [diff] [blame] | 1795 | new = realloc(set->set, (set->size + 8) * sizeof *(set->set)); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1796 | if (!new) { |
| 1797 | LOGMEM; |
| 1798 | return EXIT_FAILURE; |
| 1799 | } |
| 1800 | set->size += 8; |
| 1801 | set->set = new; |
| 1802 | } |
| 1803 | |
| 1804 | set->set[set->number++] = node; |
| 1805 | |
| 1806 | return EXIT_SUCCESS; |
| 1807 | } |
Radek Krejci | 8cc31e5 | 2016-01-18 14:45:33 +0100 | [diff] [blame] | 1808 | |
| 1809 | API int |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1810 | ly_set_rm_index(struct ly_set *set, unsigned int index) |
Radek Krejci | 8cc31e5 | 2016-01-18 14:45:33 +0100 | [diff] [blame] | 1811 | { |
| 1812 | if (!set || (index + 1) > set->number) { |
| 1813 | ly_errno = LY_EINVAL; |
| 1814 | return EXIT_FAILURE; |
| 1815 | } |
| 1816 | |
| 1817 | if (index == set->number - 1) { |
| 1818 | /* removing last item in set */ |
| 1819 | set->set[index] = NULL; |
| 1820 | } else { |
| 1821 | /* removing item somewhere in a middle, so put there the last item */ |
| 1822 | set->set[index] = set->set[set->number - 1]; |
| 1823 | set->set[set->number - 1] = NULL; |
| 1824 | } |
| 1825 | set->number--; |
| 1826 | |
| 1827 | return EXIT_SUCCESS; |
| 1828 | } |
| 1829 | |
| 1830 | API int |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1831 | ly_set_rm(struct ly_set *set, void *node) |
Radek Krejci | 8cc31e5 | 2016-01-18 14:45:33 +0100 | [diff] [blame] | 1832 | { |
| 1833 | unsigned int i; |
| 1834 | |
| 1835 | if (!set || !node) { |
| 1836 | ly_errno = LY_EINVAL; |
| 1837 | return EXIT_FAILURE; |
| 1838 | } |
| 1839 | |
| 1840 | /* get index */ |
| 1841 | for (i = 0; i < set->number; i++) { |
| 1842 | if (set->set[i] == node) { |
| 1843 | break; |
| 1844 | } |
| 1845 | } |
| 1846 | if (i == set->number) { |
| 1847 | /* node is not in set */ |
| 1848 | ly_errno = LY_EINVAL; |
| 1849 | return EXIT_FAILURE; |
| 1850 | } |
| 1851 | |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 1852 | return ly_set_rm_index(set, i); |
Radek Krejci | 8cc31e5 | 2016-01-18 14:45:33 +0100 | [diff] [blame] | 1853 | } |