Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 2 | /** |
| 3 | * @file tree_data.c |
| 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 5 | * @brief Manipulation with libyang data structures |
| 6 | * |
| 7 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * 3. Neither the name of the Company nor the names of its contributors |
| 19 | * may be used to endorse or promote products derived from this |
| 20 | * software without specific prior written permission. |
| 21 | */ |
| 22 | #define _GNU_SOURCE |
Michal Vasko | ff4c283 | 2015-10-15 13:30:50 +0200 | [diff] [blame] | 23 | #define _XOPEN_SOURCE 700 |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 24 | |
| 25 | #include <assert.h> |
| 26 | #include <ctype.h> |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 27 | #include <limits.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 28 | #include <stdlib.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 29 | #include <sys/types.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 30 | #include <sys/mman.h> |
| 31 | #include <sys/stat.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 32 | #include <fcntl.h> |
| 33 | #include <unistd.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 34 | #include <string.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 35 | #include <errno.h> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 36 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 37 | #include "libyang.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 38 | #include "common.h" |
| 39 | #include "context.h" |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 40 | #include "tree_data.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 41 | #include "parser.h" |
| 42 | #include "resolve.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 43 | #include "xml_internal.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 44 | #include "tree_internal.h" |
| 45 | #include "validation.h" |
| 46 | |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 47 | static struct lyd_node * |
| 48 | 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] | 49 | { |
Radek Krejci | 5974c1f | 2015-10-09 09:53:24 +0200 | [diff] [blame] | 50 | struct lyxml_elem *xml; |
| 51 | struct lyd_node *result = NULL; |
| 52 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 53 | if (!ctx || !data) { |
| 54 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | switch (format) { |
| 59 | case LYD_XML: |
Radek Krejci | 452fb95 | 2015-10-02 16:07:46 +0200 | [diff] [blame] | 60 | case LYD_XML_FORMAT: |
Michal Vasko | 70f92c3 | 2015-12-10 09:57:24 +0100 | [diff] [blame] | 61 | xml = lyxml_read_data(ctx, data, 0); |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 62 | if (parent) { |
| 63 | result = lyd_parse_output_xml(parent, xml, options); |
| 64 | } else { |
| 65 | result = lyd_parse_xml(ctx, xml, options); |
| 66 | } |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 67 | lyxml_free(ctx, xml); |
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 | 5974c1f | 2015-10-09 09:53:24 +0200 | [diff] [blame] | 77 | return result; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 80 | API struct lyd_node * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 81 | lyd_parse_data(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options) |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 82 | { |
| 83 | return lyd_parse_(ctx, NULL, data, format, options); |
| 84 | } |
| 85 | |
| 86 | API struct lyd_node * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 87 | lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options) |
| 88 | { |
| 89 | struct lyd_node *ret; |
| 90 | struct stat sb; |
| 91 | char *data; |
| 92 | |
| 93 | if (!ctx || (fd == -1)) { |
| 94 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | if (fstat(fd, &sb) == -1) { |
| 99 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | data = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 104 | if (data == MAP_FAILED) { |
| 105 | LOGERR(LY_ESYS, "Mapping file descriptor into memory failed."); |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | ret = lyd_parse_data(ctx, data, format, options); |
| 110 | munmap(data, sb.st_size); |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | API struct lyd_node * |
| 115 | lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options) |
| 116 | { |
| 117 | int fd; |
| 118 | struct lyd_node *ret; |
| 119 | |
| 120 | if (!ctx || !path) { |
| 121 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | fd = open(path, O_RDONLY); |
| 126 | if (fd == -1) { |
| 127 | LOGERR(LY_ESYS, "Failed to open data file \"%s\" (%s).", path, strerror(errno)); |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | ret = lyd_parse_fd(ctx, fd, format, options); |
| 132 | close(fd); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | API struct lyd_node * |
| 137 | lyd_parse_output_data(const struct lys_node *rpc, const char *data, LYD_FORMAT format, int options) |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 138 | { |
| 139 | if (!rpc || (rpc->nodetype != LYS_RPC)) { |
| 140 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | return lyd_parse_(rpc->module->ctx, rpc, data, format, options); |
| 145 | } |
| 146 | |
| 147 | API struct lyd_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 148 | 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] | 149 | { |
| 150 | struct lyd_node *ret; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 151 | const struct lys_node *snode = NULL, *siblings; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 152 | |
| 153 | if ((!parent && !module) || !name) { |
| 154 | ly_errno = LY_EINVAL; |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | if (!parent) { |
| 159 | siblings = module->data; |
| 160 | } else { |
Michal Vasko | a5ef4d7 | 2015-09-29 16:05:21 +0200 | [diff] [blame] | 161 | if (!parent->schema) { |
| 162 | return NULL; |
| 163 | } |
| 164 | siblings = parent->schema->child; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 167 | if (lys_get_data_sibling(module, siblings, name, LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_RPC, &snode) |
| 168 | || !snode) { |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 173 | if (!ret) { |
| 174 | LOGMEM; |
| 175 | return NULL; |
| 176 | } |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 177 | ret->schema = (struct lys_node *)snode; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 178 | ret->prev = ret; |
| 179 | if (parent) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 180 | if (lyd_insert(parent, ret)) { |
| 181 | lyd_free(ret); |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | API struct lyd_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 190 | lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str) |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 191 | { |
| 192 | struct lyd_node_leaf_list *ret; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 193 | const struct lys_node *snode = NULL, *siblings; |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 194 | struct lys_type *stype, *type; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 195 | int found; |
| 196 | |
| 197 | if ((!parent && !module) || !name) { |
| 198 | ly_errno = LY_EINVAL; |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | if (!parent) { |
| 203 | siblings = module->data; |
| 204 | } else { |
Michal Vasko | a5ef4d7 | 2015-09-29 16:05:21 +0200 | [diff] [blame] | 205 | if (!parent->schema) { |
Michal Vasko | 6a9dd1f | 2015-10-15 15:09:11 +0200 | [diff] [blame] | 206 | ly_errno = LY_EINVAL; |
Michal Vasko | a5ef4d7 | 2015-09-29 16:05:21 +0200 | [diff] [blame] | 207 | return NULL; |
| 208 | } |
| 209 | siblings = parent->schema->child; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 212 | if (lys_get_data_sibling(module, siblings, name, LYS_LEAFLIST | LYS_LEAF, &snode) || !snode) { |
Michal Vasko | 6a9dd1f | 2015-10-15 15:09:11 +0200 | [diff] [blame] | 213 | ly_errno = LY_EINVAL; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 214 | return NULL; |
| 215 | } |
| 216 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 217 | /* create the new leaf */ |
| 218 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 219 | if (!ret) { |
| 220 | LOGMEM; |
| 221 | return NULL; |
| 222 | } |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 223 | ret->schema = (struct lys_node *)snode; |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 224 | ret->prev = (struct lyd_node *)ret; |
| 225 | ret->value_str = lydict_insert((module ? module->ctx : parent->schema->module->ctx), val_str, 0); |
| 226 | |
| 227 | /* resolve the type correctly */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 228 | stype = &((struct lys_node_leaf *)snode)->type; |
| 229 | if (stype->base == LY_TYPE_UNION) { |
| 230 | found = 0; |
Radek Krejci | f738849 | 2015-10-30 16:12:53 +0100 | [diff] [blame] | 231 | type = NULL; |
| 232 | while ((type = lyp_get_next_union_type(stype, type, &found))) { |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 233 | ret->value_type = type->base; |
| 234 | if (!lyp_parse_value(ret, type, 1, NULL, UINT_MAX)) { |
| 235 | /* success! */ |
| 236 | break; |
| 237 | } |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 238 | found = 0; |
Radek Krejci | f738849 | 2015-10-30 16:12:53 +0100 | [diff] [blame] | 239 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 240 | |
| 241 | if (!type) { |
| 242 | /* fail */ |
| 243 | ly_errno = LY_EINVAL; |
| 244 | lyd_free((struct lyd_node *)ret); |
| 245 | return NULL; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 246 | } |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 247 | } else { |
| 248 | ret->value_type = stype->base; |
| 249 | if (lyp_parse_value(ret, stype, 1, NULL, 0)) { |
| 250 | lyd_free((struct lyd_node *)ret); |
Michal Vasko | 6a9dd1f | 2015-10-15 15:09:11 +0200 | [diff] [blame] | 251 | ly_errno = LY_EINVAL; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 252 | return NULL; |
| 253 | } |
| 254 | } |
| 255 | |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 256 | /* connect to parent */ |
Michal Vasko | 6a9dd1f | 2015-10-15 15:09:11 +0200 | [diff] [blame] | 257 | if (parent) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 258 | if (lyd_insert(parent, (struct lyd_node *)ret)) { |
| 259 | lyd_free((struct lyd_node *)ret); |
Michal Vasko | 6a9dd1f | 2015-10-15 15:09:11 +0200 | [diff] [blame] | 260 | return NULL; |
| 261 | } |
| 262 | } |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 263 | |
| 264 | return (struct lyd_node *)ret; |
| 265 | |
| 266 | } |
| 267 | |
| 268 | API struct lyd_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 269 | lyd_new_anyxml(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_xml) |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 270 | { |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 271 | struct lyd_node_anyxml *ret; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 272 | const struct lys_node *siblings, *snode; |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 273 | struct lyxml_elem *root; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 274 | struct ly_ctx *ctx; |
| 275 | char *xml; |
| 276 | |
Michal Vasko | 14d8877 | 2015-12-03 10:54:59 +0100 | [diff] [blame] | 277 | if ((!parent && !module) || !name) { |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 278 | ly_errno = LY_EINVAL; |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | if (!parent) { |
| 283 | siblings = module->data; |
| 284 | ctx = module->ctx; |
| 285 | } else { |
| 286 | if (!parent->schema) { |
| 287 | return NULL; |
| 288 | } |
| 289 | siblings = parent->schema->child; |
| 290 | ctx = parent->schema->module->ctx; |
| 291 | } |
| 292 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 293 | if (lys_get_data_sibling(module, siblings, name, LYS_ANYXML, &snode) || !snode) { |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 298 | if (!ret) { |
| 299 | LOGMEM; |
| 300 | return NULL; |
| 301 | } |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 302 | ret->schema = (struct lys_node *)snode; |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 303 | ret->prev = (struct lyd_node *)ret; |
| 304 | if (parent) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 305 | if (lyd_insert(parent, (struct lyd_node *)ret)) { |
| 306 | lyd_free((struct lyd_node *)ret); |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 307 | return NULL; |
| 308 | } |
| 309 | } |
| 310 | |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 311 | /* add fake root and top-level and parse the data */ |
| 312 | asprintf(&xml, "<root><%s>%s</%s></root>", name, (val_xml ? val_xml : ""), name); |
| 313 | root = lyxml_read_data(ctx, xml, 0); |
| 314 | free(xml); |
| 315 | if (!root) { |
| 316 | lyd_free((struct lyd_node *)ret); |
| 317 | return NULL; |
Michal Vasko | 14d8877 | 2015-12-03 10:54:59 +0100 | [diff] [blame] | 318 | } |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 319 | |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 320 | /* remove the root */ |
| 321 | ret->value = root->child; |
| 322 | lyxml_unlink_elem(ctx, root->child, 1); |
| 323 | |
| 324 | lyxml_free(ctx, root); |
| 325 | |
Michal Vasko | 587998f | 2015-09-29 16:07:53 +0200 | [diff] [blame] | 326 | return (struct lyd_node *)ret; |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 329 | API struct lyd_node * |
| 330 | lyd_new_output(const struct lys_node *rpc_schema) |
| 331 | { |
| 332 | struct lyd_node *ret; |
| 333 | struct lys_node *output; |
| 334 | |
| 335 | if (!rpc_schema || (rpc_schema->nodetype != LYS_RPC)) { |
| 336 | ly_errno = LY_EINVAL; |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | if (rpc_schema->child->nodetype == LYS_OUTPUT) { |
| 341 | output = rpc_schema->child; |
| 342 | } else if (rpc_schema->child->next->nodetype == LYS_OUTPUT) { |
| 343 | output = rpc_schema->child->next; |
| 344 | } else { |
| 345 | ly_errno = LY_EINVAL; |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | ret = calloc(1, sizeof *ret); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 350 | if (!ret) { |
| 351 | LOGMEM; |
| 352 | return NULL; |
| 353 | } |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 354 | ret->schema = output; |
| 355 | ret->prev = ret; |
| 356 | |
| 357 | return (struct lyd_node *)ret; |
| 358 | } |
| 359 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 360 | API int |
| 361 | lyd_insert(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 362 | { |
| 363 | struct lys_node *sparent; |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 364 | struct lyd_node *iter; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 365 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 366 | if (!node || !parent) { |
| 367 | ly_errno = LY_EINVAL; |
| 368 | return EXIT_FAILURE; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Michal Vasko | 72d65c9 | 2015-12-07 14:02:35 +0100 | [diff] [blame] | 371 | /* check placing the node to the appropriate place according to the schema (if LYS_OUTPUT is returned, |
| 372 | * the parent's schema will never match and it fails as it should) */ |
| 373 | for (sparent = node->schema->parent; |
| 374 | sparent && !(sparent->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_OUTPUT | LYS_NOTIF)); |
| 375 | sparent = sparent->parent); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 376 | if (sparent != parent->schema) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 377 | return EXIT_FAILURE; |
| 378 | } |
| 379 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 380 | if (node->parent || node->prev->next) { |
| 381 | lyd_unlink(node); |
| 382 | } |
| 383 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 384 | if (!parent->child) { |
| 385 | /* add as the only child of the parent */ |
| 386 | parent->child = node; |
| 387 | } else { |
| 388 | /* add as the last child of the parent */ |
| 389 | parent->child->prev->next = node; |
| 390 | node->prev = parent->child->prev; |
| 391 | for (iter = node; iter->next; iter = iter->next); |
| 392 | parent->child->prev = iter; |
| 393 | } |
Michal Vasko | 9cc2d0a | 2015-10-14 15:49:07 +0200 | [diff] [blame] | 394 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 395 | LY_TREE_FOR(node, iter) { |
| 396 | iter->parent = parent; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | return EXIT_SUCCESS; |
| 400 | } |
| 401 | |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 402 | static int |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 403 | 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] | 404 | { |
| 405 | struct lys_node *par1, *par2; |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 406 | struct lyd_node *iter, *last; |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 407 | |
| 408 | if (sibling == node) { |
| 409 | return EXIT_SUCCESS; |
| 410 | } |
| 411 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 412 | /* check placing the node to the appropriate place according to the schema */ |
Radek Krejci | 2b7bb49 | 2015-12-02 15:46:29 +0100 | [diff] [blame] | 413 | for (par1 = sibling->schema->parent; |
| 414 | par1 && !(par1->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)); |
| 415 | par1 = par1->parent); |
| 416 | for (par2 = node->schema->parent; |
| 417 | par2 && !(par2->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)); |
| 418 | par2 = par2->parent); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 419 | if (par1 != par2) { |
| 420 | ly_errno = LY_EINVAL; |
| 421 | return EXIT_FAILURE; |
| 422 | } |
| 423 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 424 | if (node->parent || node->prev->next) { |
| 425 | lyd_unlink(node); |
| 426 | } |
| 427 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 428 | LY_TREE_FOR(node, iter) { |
| 429 | iter->parent = sibling->parent; |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 430 | last = iter; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 433 | if (before) { |
| 434 | if (sibling->prev->next) { |
| 435 | /* adding into the list */ |
| 436 | sibling->prev->next = node; |
| 437 | } else if (sibling->parent) { |
| 438 | /* at the beginning */ |
| 439 | sibling->parent->child = node; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 440 | } |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 441 | node->prev = sibling->prev; |
| 442 | sibling->prev = last; |
| 443 | last->next = sibling; |
| 444 | } else { |
| 445 | if (sibling->next) { |
| 446 | /* adding into a middle - fix the prev pointer of the node after inserted nodes */ |
| 447 | last->next = sibling->next; |
| 448 | sibling->next->prev = last; |
| 449 | } else { |
| 450 | /* at the end - fix the prev pointer of the first node */ |
| 451 | if (sibling->parent) { |
| 452 | sibling->parent->child->prev = last; |
| 453 | } else { |
| 454 | for (iter = sibling; iter->prev->next; iter = iter->prev); |
| 455 | iter->prev = last; |
| 456 | } |
| 457 | } |
| 458 | sibling->next = node; |
| 459 | node->prev = sibling; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 460 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 461 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 462 | return EXIT_SUCCESS; |
| 463 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 464 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 465 | API int |
| 466 | lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node) |
| 467 | { |
| 468 | if (!node || !sibling || lyd_insert_sibling(sibling, node, 1)) { |
| 469 | ly_errno = LY_EINVAL; |
| 470 | return EXIT_FAILURE; |
| 471 | } |
| 472 | |
| 473 | return EXIT_SUCCESS; |
| 474 | } |
| 475 | |
| 476 | API int |
| 477 | lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node) |
| 478 | { |
| 479 | if (!node || !sibling || lyd_insert_sibling(sibling, node, 0)) { |
| 480 | ly_errno = LY_EINVAL; |
| 481 | return EXIT_FAILURE; |
| 482 | } |
| 483 | |
| 484 | return EXIT_SUCCESS; |
| 485 | } |
| 486 | |
| 487 | API int |
| 488 | lyd_validate(struct lyd_node *node, int options) |
| 489 | { |
| 490 | struct lyd_node *next, *iter, *to_free = NULL; |
| 491 | |
| 492 | ly_errno = 0; |
| 493 | LY_TREE_DFS_BEGIN(node, next, iter) { |
| 494 | if (to_free) { |
| 495 | lyd_free(to_free); |
| 496 | to_free = NULL; |
| 497 | } |
| 498 | |
Radek Krejci | f03ec15 | 2015-12-03 13:18:37 +0100 | [diff] [blame] | 499 | if (lyv_data_context(iter, options, 0, NULL)) { |
| 500 | return EXIT_FAILURE; |
| 501 | } |
| 502 | if (lyv_data_content(iter, options, 0, NULL)) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 503 | if (ly_errno) { |
| 504 | return EXIT_FAILURE; |
| 505 | } else { |
| 506 | /* safe deferred removal */ |
| 507 | to_free = iter; |
Radek Krejci | efe6a14 | 2015-12-03 14:01:00 +0100 | [diff] [blame] | 508 | if (iter == node) { |
| 509 | /* removing the whole subtree */ |
| 510 | break; |
| 511 | } |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 512 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 513 | } |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 514 | |
| 515 | LY_TREE_DFS_END(node, next, iter); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 516 | } |
| 517 | |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 518 | if (to_free) { |
| 519 | lyd_free(to_free); |
| 520 | to_free = NULL; |
Michal Vasko | 0259290 | 2015-10-15 12:14:40 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | return EXIT_SUCCESS; |
| 524 | } |
| 525 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 526 | /* create an attribute copy */ |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 527 | static struct lyd_attr * |
| 528 | lyd_dup_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr) |
| 529 | { |
| 530 | struct lyd_attr *ret; |
| 531 | |
| 532 | /* allocate new attr */ |
| 533 | if (!parent->attr) { |
| 534 | parent->attr = malloc(sizeof *parent->attr); |
| 535 | ret = parent->attr; |
| 536 | } else { |
| 537 | for (ret = parent->attr; ret->next; ret = ret->next); |
| 538 | ret->next = malloc(sizeof *ret); |
| 539 | ret = ret->next; |
| 540 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 541 | if (!ret) { |
| 542 | LOGMEM; |
| 543 | return NULL; |
| 544 | } |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 545 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 546 | /* fill new attr except */ |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 547 | ret->next = NULL; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 548 | ret->module = attr->module; |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 549 | ret->name = lydict_insert(ctx, attr->name, 0); |
| 550 | ret->value = lydict_insert(ctx, attr->value, 0); |
| 551 | |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 552 | return ret; |
| 553 | } |
| 554 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 555 | API int |
| 556 | lyd_unlink(struct lyd_node *node) |
| 557 | { |
| 558 | struct lyd_node *iter; |
| 559 | |
| 560 | if (!node) { |
| 561 | ly_errno = LY_EINVAL; |
| 562 | return EXIT_FAILURE; |
| 563 | } |
| 564 | |
| 565 | /* unlink from siblings */ |
| 566 | if (node->prev->next) { |
| 567 | node->prev->next = node->next; |
| 568 | } |
| 569 | if (node->next) { |
| 570 | node->next->prev = node->prev; |
| 571 | } else { |
| 572 | /* unlinking the last node */ |
| 573 | iter = node->prev; |
| 574 | while (iter->prev != node) { |
| 575 | iter = iter->prev; |
| 576 | } |
| 577 | /* update the "last" pointer from the first node */ |
| 578 | iter->prev = node->prev; |
| 579 | } |
| 580 | |
| 581 | /* unlink from parent */ |
| 582 | if (node->parent) { |
| 583 | if (node->parent->child == node) { |
| 584 | /* the node is the first child */ |
| 585 | node->parent->child = node->next; |
| 586 | } |
| 587 | node->parent = NULL; |
| 588 | } |
| 589 | |
| 590 | node->next = NULL; |
| 591 | node->prev = node; |
| 592 | |
| 593 | return EXIT_SUCCESS; |
| 594 | } |
| 595 | |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 596 | API struct lyd_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 597 | lyd_dup(const struct lyd_node *node, int recursive) |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 598 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 599 | const struct lyd_node *next, *elem; |
| 600 | struct lyd_node *ret, *parent, *new_node; |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 601 | struct lyd_attr *attr; |
| 602 | struct lyd_node_leaf_list *new_leaf; |
| 603 | struct lyd_node_anyxml *new_axml; |
| 604 | struct lys_type *type; |
| 605 | |
| 606 | if (!node) { |
| 607 | ly_errno = LY_EINVAL; |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | ret = NULL; |
| 612 | parent = NULL; |
| 613 | |
| 614 | /* LY_TREE_DFS */ |
| 615 | for (elem = next = node; elem; elem = next) { |
| 616 | |
| 617 | /* fill specific part */ |
| 618 | switch (elem->schema->nodetype) { |
| 619 | case LYS_LEAF: |
| 620 | case LYS_LEAFLIST: |
| 621 | new_leaf = malloc(sizeof *new_leaf); |
| 622 | new_node = (struct lyd_node *)new_leaf; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 623 | if (!new_node) { |
| 624 | LOGMEM; |
| 625 | return NULL; |
| 626 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 627 | |
| 628 | new_leaf->value = ((struct lyd_node_leaf_list *)elem)->value; |
| 629 | new_leaf->value_str = lydict_insert(elem->schema->module->ctx, |
| 630 | ((struct lyd_node_leaf_list *)elem)->value_str, 0); |
| 631 | new_leaf->value_type = ((struct lyd_node_leaf_list *)elem)->value_type; |
| 632 | /* bits type must be treated specially */ |
| 633 | if (new_leaf->value_type == LY_TYPE_BITS) { |
| 634 | for (type = &((struct lys_node_leaf *)elem->schema)->type; type->der->module; type = &type->der->type) { |
| 635 | if (type->base != LY_TYPE_BITS) { |
| 636 | LOGINT; |
Michal Vasko | d80e6c7 | 2015-10-15 09:37:01 +0200 | [diff] [blame] | 637 | lyd_free(new_node); |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 638 | lyd_free(ret); |
| 639 | return NULL; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | 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^] | 644 | if (!new_leaf->value.bit) { |
| 645 | LOGMEM; |
| 646 | lyd_free(new_node); |
| 647 | lyd_free(ret); |
| 648 | return NULL; |
| 649 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 650 | memcpy(new_leaf->value.bit, ((struct lyd_node_leaf_list *)elem)->value.bit, |
| 651 | type->info.bits.count * sizeof *new_leaf->value.bit); |
| 652 | } |
| 653 | break; |
| 654 | case LYS_ANYXML: |
| 655 | new_axml = malloc(sizeof *new_axml); |
| 656 | new_node = (struct lyd_node *)new_axml; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 657 | if (!new_node) { |
| 658 | LOGMEM; |
| 659 | return NULL; |
| 660 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 661 | |
| 662 | new_axml->value = lyxml_dup_elem(elem->schema->module->ctx, ((struct lyd_node_anyxml *)elem)->value, |
| 663 | NULL, 1); |
| 664 | break; |
| 665 | case LYS_CONTAINER: |
| 666 | case LYS_LIST: |
| 667 | case LYS_NOTIF: |
| 668 | case LYS_RPC: |
| 669 | new_node = malloc(sizeof *new_node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 670 | if (!new_node) { |
| 671 | LOGMEM; |
| 672 | return NULL; |
| 673 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 674 | new_node->child = NULL; |
| 675 | break; |
| 676 | default: |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 677 | lyd_free(ret); |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 678 | LOGINT; |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | /* fill common part */ |
| 683 | new_node->schema = elem->schema; |
| 684 | new_node->attr = NULL; |
| 685 | LY_TREE_FOR(elem->attr, attr) { |
| 686 | lyd_dup_attr(elem->schema->module->ctx, new_node, attr); |
| 687 | } |
| 688 | new_node->next = NULL; |
| 689 | new_node->prev = new_node; |
| 690 | new_node->parent = NULL; |
| 691 | |
| 692 | if (!ret) { |
| 693 | ret = new_node; |
| 694 | } |
| 695 | if (parent) { |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 696 | if (lyd_insert(parent, new_node)) { |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 697 | lyd_free(ret); |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 698 | LOGINT; |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 699 | return NULL; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | if (!recursive) { |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | /* LY_TREE_DFS_END */ |
| 708 | /* select element for the next run - children first */ |
| 709 | next = elem->child; |
| 710 | /* child exception for lyd_node_leaf and lyd_node_leaflist */ |
| 711 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
| 712 | next = NULL; |
| 713 | } |
| 714 | if (!next) { |
| 715 | /* no children, so try siblings */ |
| 716 | next = elem->next; |
| 717 | } else { |
| 718 | parent = new_node; |
| 719 | } |
| 720 | while (!next) { |
| 721 | /* no siblings, go back through parents */ |
| 722 | elem = elem->parent; |
| 723 | if (elem->parent == node->parent) { |
| 724 | break; |
| 725 | } |
Michal Vasko | 785b2ad | 2015-10-15 09:37:15 +0200 | [diff] [blame] | 726 | if (!parent) { |
Michal Vasko | c8f5d80 | 2015-10-23 10:14:39 +0200 | [diff] [blame] | 727 | lyd_free(ret); |
Michal Vasko | 785b2ad | 2015-10-15 09:37:15 +0200 | [diff] [blame] | 728 | LOGINT; |
Michal Vasko | c8f5d80 | 2015-10-23 10:14:39 +0200 | [diff] [blame] | 729 | return NULL; |
Michal Vasko | 785b2ad | 2015-10-15 09:37:15 +0200 | [diff] [blame] | 730 | } |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 731 | parent = parent->parent; |
| 732 | /* parent is already processed, go to its sibling */ |
| 733 | next = elem->next; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | return ret; |
| 738 | } |
| 739 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 740 | API void |
| 741 | 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] | 742 | { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 743 | struct lyd_attr *iter; |
| 744 | |
| 745 | if (!ctx || !attr) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 749 | if (parent) { |
| 750 | if (parent->attr == attr) { |
| 751 | if (recursive) { |
| 752 | parent->attr = NULL; |
| 753 | } else { |
| 754 | parent->attr = attr->next; |
| 755 | } |
| 756 | } else { |
| 757 | for (iter = parent->attr; iter->next != attr; iter = iter->next); |
| 758 | if (iter->next) { |
| 759 | if (recursive) { |
| 760 | iter->next = NULL; |
| 761 | } else { |
| 762 | iter->next = attr->next; |
| 763 | } |
| 764 | } |
| 765 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 766 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 767 | |
| 768 | if (!recursive) { |
| 769 | attr->next = NULL; |
| 770 | } |
| 771 | |
| 772 | for(iter = attr; iter; ) { |
| 773 | attr = iter; |
| 774 | iter = iter->next; |
| 775 | |
| 776 | lydict_remove(ctx, attr->name); |
| 777 | lydict_remove(ctx, attr->value); |
| 778 | free(attr); |
| 779 | } |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 780 | } |
| 781 | |
Michal Vasko | fd76bd1 | 2015-09-24 15:49:57 +0200 | [diff] [blame] | 782 | struct lyd_node * |
| 783 | lyd_attr_parent(struct lyd_node *root, struct lyd_attr *attr) |
| 784 | { |
| 785 | struct lyd_node *next, *elem; |
| 786 | struct lyd_attr *node_attr; |
| 787 | |
| 788 | LY_TREE_DFS_BEGIN(root, next, elem) { |
| 789 | for (node_attr = elem->attr; node_attr; node_attr = node_attr->next) { |
| 790 | if (node_attr == attr) { |
| 791 | return elem; |
| 792 | } |
| 793 | } |
| 794 | LY_TREE_DFS_END(root, next, elem) |
| 795 | } |
| 796 | |
| 797 | return NULL; |
| 798 | } |
| 799 | |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 800 | API struct lyd_attr * |
| 801 | lyd_insert_attr(struct lyd_node *parent, const char *name, const char *value) |
| 802 | { |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 803 | struct lyd_attr *a, *iter; |
| 804 | struct ly_ctx *ctx; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 805 | const struct lys_module *module; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 806 | const char *p; |
| 807 | char *aux; |
| 808 | |
| 809 | if (!parent || !name || !value) { |
| 810 | return NULL; |
| 811 | } |
| 812 | ctx = parent->schema->module->ctx; |
| 813 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 814 | if ((p = strchr(name, ':'))) { |
| 815 | /* search for the namespace */ |
| 816 | aux = strndup(name, p - name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 817 | if (!aux) { |
| 818 | LOGMEM; |
| 819 | return NULL; |
| 820 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 821 | module = ly_ctx_get_module(ctx, aux, NULL); |
| 822 | free(aux); |
| 823 | name = p + 1; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 824 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 825 | if (!module) { |
| 826 | /* module not found */ |
Radek Krejci | a008ca9 | 2015-10-30 15:52:05 +0100 | [diff] [blame] | 827 | 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] | 828 | return NULL; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 829 | } |
| 830 | } else { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 831 | /* no prefix -> module is the same as for the parent */ |
| 832 | module = parent->schema->module; |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 835 | a = malloc(sizeof *a); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 836 | if (!a) { |
| 837 | LOGMEM; |
| 838 | return NULL; |
| 839 | } |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 840 | a->module = (struct lys_module *)module; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 841 | a->next = NULL; |
| 842 | a->name = lydict_insert(ctx, name, 0); |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 843 | a->value = lydict_insert(ctx, value, 0); |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 844 | |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 845 | if (!parent->attr) { |
| 846 | parent->attr = a; |
| 847 | } else { |
| 848 | for (iter = parent->attr; iter->next; iter = iter->next); |
| 849 | iter->next = a; |
| 850 | } |
| 851 | |
| 852 | return a; |
| 853 | } |
| 854 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 855 | API void |
| 856 | lyd_free(struct lyd_node *node) |
| 857 | { |
| 858 | struct lyd_node *next, *child; |
| 859 | |
| 860 | if (!node) { |
| 861 | return; |
| 862 | } |
| 863 | |
| 864 | if (!(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) { |
| 865 | /* free children */ |
| 866 | LY_TREE_FOR_SAFE(node->child, next, child) { |
| 867 | lyd_free(child); |
| 868 | } |
| 869 | } else if (node->schema->nodetype == LYS_ANYXML) { |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 870 | lyxml_free(node->schema->module->ctx, ((struct lyd_node_anyxml *)node)->value); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 871 | } else { |
| 872 | /* free value */ |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 873 | switch(((struct lyd_node_leaf_list *)node)->value_type) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 874 | case LY_TYPE_BINARY: |
| 875 | case LY_TYPE_STRING: |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 876 | 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] | 877 | break; |
| 878 | case LY_TYPE_BITS: |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 879 | if (((struct lyd_node_leaf_list *)node)->value.bit) { |
| 880 | free(((struct lyd_node_leaf_list *)node)->value.bit); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 881 | } |
| 882 | break; |
| 883 | default: |
| 884 | /* TODO nothing needed : LY_TYPE_BOOL, LY_TYPE_DEC64*/ |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | lyd_unlink(node); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 890 | lyd_free_attr(node->schema->module->ctx, node, node->attr, 1); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 891 | free(node); |
| 892 | } |
| 893 | |
Michal Vasko | ff4c283 | 2015-10-15 13:30:50 +0200 | [diff] [blame] | 894 | API char * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 895 | lyxml_serialize(const struct lyxml_elem *anyxml) |
Michal Vasko | ff4c283 | 2015-10-15 13:30:50 +0200 | [diff] [blame] | 896 | { |
| 897 | FILE *stream; |
| 898 | char *buf; |
| 899 | size_t buf_size; |
| 900 | |
| 901 | if (!anyxml) { |
| 902 | ly_errno = LY_EINVAL; |
| 903 | return NULL; |
| 904 | } |
| 905 | |
| 906 | stream = open_memstream(&buf, &buf_size); |
| 907 | if (!stream) { |
| 908 | ly_errno = LY_ESYS; |
| 909 | return NULL; |
| 910 | } |
Michal Vasko | 70f92c3 | 2015-12-10 09:57:24 +0100 | [diff] [blame] | 911 | if (lyxml_dump_file(stream, anyxml, 0) == 0) { |
Michal Vasko | ff4c283 | 2015-10-15 13:30:50 +0200 | [diff] [blame] | 912 | free(buf); |
| 913 | buf = NULL; |
| 914 | ly_errno = LY_EINVAL; |
| 915 | } |
| 916 | fclose(stream); |
| 917 | |
| 918 | return buf; |
| 919 | } |
| 920 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 921 | int |
| 922 | lyd_compare(struct lyd_node *first, struct lyd_node *second, int unique) |
| 923 | { |
| 924 | struct lys_node_list *slist; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 925 | const struct lys_node *snode = NULL; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 926 | struct lyd_node *diter; |
| 927 | const char *val1, *val2; |
| 928 | int i, j; |
| 929 | |
| 930 | assert(first); |
| 931 | assert(second); |
| 932 | |
| 933 | if (first->schema != second->schema) { |
| 934 | return 1; |
| 935 | } |
| 936 | |
| 937 | switch (first->schema->nodetype) { |
| 938 | case LYS_LEAFLIST: |
| 939 | /* compare values */ |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 940 | if (((struct lyd_node_leaf_list *)first)->value_str == ((struct lyd_node_leaf_list *)second)->value_str) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 941 | return 0; |
| 942 | } |
| 943 | return 1; |
| 944 | case LYS_LIST: |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 945 | slist = (struct lys_node_list *)first->schema; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 946 | |
| 947 | if (unique) { |
| 948 | /* compare unique leafs */ |
| 949 | for (i = 0; i < slist->unique_size; i++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 950 | for (j = 0; j < slist->unique[i].expr_size; j++) { |
| 951 | /* first */ |
| 952 | diter = resolve_data_nodeid(slist->unique[i].expr[j], first->child); |
| 953 | if (diter) { |
| 954 | val1 = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 955 | } else { |
| 956 | /* use default value */ |
Radek Krejci | 3eb09b6 | 2015-12-16 15:22:19 +0100 | [diff] [blame] | 957 | if (resolve_schema_nodeid(slist->unique[i].expr[j], first->schema->child, first->schema->module, LYS_LEAF, &snode)) { |
| 958 | /* error, but unique expression was checked when the schema was parsed */ |
| 959 | return -1; |
| 960 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 961 | val1 = ((struct lys_node_leaf *)snode)->dflt; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 962 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 963 | |
| 964 | /* second */ |
| 965 | diter = resolve_data_nodeid(slist->unique[i].expr[j], second->child); |
| 966 | if (diter) { |
| 967 | val2 = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 968 | } else { |
| 969 | /* use default value */ |
Radek Krejci | 3eb09b6 | 2015-12-16 15:22:19 +0100 | [diff] [blame] | 970 | if (resolve_schema_nodeid(slist->unique[i].expr[j], second->schema->child, second->schema->module, LYS_LEAF, &snode)) { |
| 971 | /* error, but unique expression was checked when the schema was parsed */ |
| 972 | return -1; |
| 973 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 974 | val2 = ((struct lys_node_leaf *)snode)->dflt; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 975 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 976 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 977 | if (val1 != val2) { |
| 978 | break; |
| 979 | } |
| 980 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 981 | if (j && j == slist->unique[i].expr_size) { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 982 | /* all unique leafs are the same in this set */ |
| 983 | return 0; |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /* compare keys */ |
| 989 | for (i = 0; i < slist->keys_size; i++) { |
| 990 | snode = (struct lys_node *)slist->keys[i]; |
| 991 | val1 = val2 = NULL; |
| 992 | LY_TREE_FOR(first->child, diter) { |
| 993 | if (diter->schema == snode) { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 994 | val1 = ((struct lyd_node_leaf_list *)diter)->value_str; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 995 | break; |
| 996 | } |
| 997 | } |
| 998 | LY_TREE_FOR(second->child, diter) { |
| 999 | if (diter->schema == snode) { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 1000 | val2 = ((struct lyd_node_leaf_list *)diter)->value_str; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1001 | break; |
| 1002 | } |
| 1003 | } |
| 1004 | if (val1 != val2) { |
| 1005 | return 1; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | return 0; |
| 1010 | default: |
| 1011 | /* no additional check is needed */ |
| 1012 | return 0; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | API struct lyd_set * |
| 1017 | lyd_set_new(void) |
| 1018 | { |
| 1019 | return calloc(1, sizeof(struct lyd_set)); |
| 1020 | } |
| 1021 | |
| 1022 | API void |
| 1023 | lyd_set_free(struct lyd_set *set) |
| 1024 | { |
| 1025 | if (!set) { |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | free(set->set); |
| 1030 | free(set); |
| 1031 | } |
| 1032 | |
| 1033 | API int |
| 1034 | lyd_set_add(struct lyd_set *set, struct lyd_node *node) |
| 1035 | { |
| 1036 | struct lyd_node **new; |
| 1037 | |
| 1038 | if (!set) { |
| 1039 | ly_errno = LY_EINVAL; |
| 1040 | return EXIT_FAILURE; |
| 1041 | } |
| 1042 | |
| 1043 | if (set->size == set->number) { |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame^] | 1044 | new = ly_realloc(set->set, (set->size + 8) * sizeof *(set->set)); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1045 | if (!new) { |
| 1046 | LOGMEM; |
| 1047 | return EXIT_FAILURE; |
| 1048 | } |
| 1049 | set->size += 8; |
| 1050 | set->set = new; |
| 1051 | } |
| 1052 | |
| 1053 | set->set[set->number++] = node; |
| 1054 | |
| 1055 | return EXIT_SUCCESS; |
| 1056 | } |