Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include "common.h" |
| 16 | |
| 17 | #include <ctype.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdarg.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "log.h" |
| 26 | #include "tree.h" |
| 27 | #include "tree_data.h" |
| 28 | #include "tree_data_internal.h" |
| 29 | #include "tree_schema.h" |
| 30 | |
| 31 | static int |
Radek Krejci | 357398c | 2019-05-27 14:15:01 +0200 | [diff] [blame] | 32 | cmp_str(const char *refstr, const char *str, size_t str_len) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 33 | { |
| 34 | |
Radek Krejci | 357398c | 2019-05-27 14:15:01 +0200 | [diff] [blame] | 35 | if (str_len) { |
| 36 | int r = strncmp(refstr, str, str_len); |
| 37 | if (!r && !refstr[str_len]) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 38 | return 0; |
| 39 | } else { |
| 40 | return 1; |
| 41 | } |
| 42 | } else { |
| 43 | return strcmp(refstr, str); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | API const struct lyd_node * |
| 48 | lyd_search(const struct lyd_node *first, const struct lys_module *module, |
| 49 | const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len) |
| 50 | { |
| 51 | const struct lyd_node *node = NULL; |
| 52 | const struct lysc_node *snode; |
| 53 | |
| 54 | LY_CHECK_ARG_RET(NULL, module, name, NULL); |
| 55 | if (!nodetype) { |
| 56 | nodetype = 0xffff; |
| 57 | } |
| 58 | |
| 59 | LY_LIST_FOR(first, node) { |
| 60 | snode = node->schema; |
| 61 | if (!(snode->nodetype & nodetype)) { |
| 62 | continue; |
| 63 | } |
| 64 | if (snode->module != module) { |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if (cmp_str(snode->name, name, name_len)) { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | if (value) { |
| 73 | if (snode->nodetype == LYS_LIST) { |
| 74 | /* TODO handle value as keys of the list instance */ |
| 75 | } else if (snode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 76 | if (cmp_str(((struct lyd_node_term*)node)->value.canonized, value, value_len)) { |
| 77 | continue; |
| 78 | } |
| 79 | } else { |
| 80 | continue; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* all criteria passed */ |
| 85 | return node; |
| 86 | } |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | static struct lyd_node * |
| 91 | lyd_parse_mem_(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, va_list ap) |
| 92 | { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 93 | struct lyd_node *result = NULL; |
| 94 | const struct lyd_node *rpc_act = NULL, *data_tree = NULL, *iter; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 95 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 96 | const char *yang_data_name = NULL; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 97 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 98 | |
| 99 | if (lyd_parse_check_options(ctx, options, __func__)) { |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | if (options & LYD_OPT_RPCREPLY) { |
| 104 | rpc_act = va_arg(ap, const struct lyd_node *); |
| 105 | if (!rpc_act || rpc_act->parent || !(rpc_act->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) { |
| 106 | LOGERR(ctx, LY_EINVAL, "Data parser invalid variable parameter (const struct lyd_node *rpc_act)."); |
| 107 | return NULL; |
| 108 | } |
| 109 | } |
| 110 | if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF | LYD_OPT_RPCREPLY)) { |
| 111 | data_tree = va_arg(ap, const struct lyd_node *); |
| 112 | if (data_tree) { |
| 113 | if (options & LYD_OPT_NOEXTDEPS) { |
| 114 | LOGERR(ctx, LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree and LYD_OPT_NOEXTDEPS set).", |
| 115 | __func__); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | LY_LIST_FOR(data_tree, iter) { |
| 120 | if (iter->parent) { |
| 121 | /* a sibling is not top-level */ |
| 122 | LOGERR(ctx, LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *data_tree).", __func__); |
| 123 | return NULL; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* move it to the beginning */ |
| 128 | for (; data_tree->prev->next; data_tree = data_tree->prev); |
| 129 | |
| 130 | /* LYD_OPT_NOSIBLINGS cannot be set in this case */ |
| 131 | if (options & LYD_OPT_NOSIBLINGS) { |
| 132 | LOGERR(ctx, LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree with LYD_OPT_NOSIBLINGS).", __func__); |
| 133 | return NULL; |
| 134 | } |
| 135 | } |
| 136 | } |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 137 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 138 | if (options & LYD_OPT_DATA_TEMPLATE) { |
| 139 | yang_data_name = va_arg(ap, const char *); |
| 140 | } |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 141 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 142 | |
| 143 | if (!format) { |
| 144 | /* TODO try to detect format from the content */ |
| 145 | } |
| 146 | |
| 147 | switch (format) { |
| 148 | case LYD_XML: |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 149 | lyd_parse_xml(ctx, data, options, &result); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 150 | break; |
| 151 | #if 0 |
| 152 | case LYD_JSON: |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 153 | lyd_parse_json(ctx, data, options, rpc_act, data_tree, yang_data_name); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 154 | break; |
| 155 | case LYD_LYB: |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 156 | lyd_parse_lyb(ctx, data, options, data_tree, yang_data_name, NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 157 | break; |
| 158 | #endif |
| 159 | case LYD_UNKNOWN: |
| 160 | LOGINT(ctx); |
| 161 | break; |
| 162 | } |
| 163 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 164 | return result; |
| 165 | } |
| 166 | |
| 167 | API struct lyd_node * |
| 168 | lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...) |
| 169 | { |
| 170 | va_list ap; |
| 171 | struct lyd_node *result; |
| 172 | |
| 173 | va_start(ap, options); |
| 174 | result = lyd_parse_mem_(ctx, data, format, options, ap); |
| 175 | va_end(ap); |
| 176 | |
| 177 | return result; |
| 178 | } |
| 179 | |
| 180 | static struct lyd_node * |
| 181 | lyd_parse_fd_(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, va_list ap) |
| 182 | { |
| 183 | struct lyd_node *result; |
| 184 | size_t length; |
| 185 | char *addr; |
| 186 | |
| 187 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 188 | if (fd < 0) { |
| 189 | LOGARG(ctx, fd); |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL); |
| 194 | result = lyd_parse_mem_(ctx, addr ? addr : "", format, options, ap); |
Radek Krejci | df3da79 | 2019-05-17 10:32:24 +0200 | [diff] [blame] | 195 | if (addr) { |
| 196 | ly_munmap(addr, length); |
| 197 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 198 | |
| 199 | return result; |
| 200 | } |
| 201 | |
| 202 | API struct lyd_node * |
| 203 | lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...) |
| 204 | { |
| 205 | struct lyd_node *ret; |
| 206 | va_list ap; |
| 207 | |
| 208 | va_start(ap, options); |
| 209 | ret = lyd_parse_fd_(ctx, fd, format, options, ap); |
| 210 | va_end(ap); |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | API struct lyd_node * |
| 216 | lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...) |
| 217 | { |
| 218 | int fd; |
| 219 | struct lyd_node *result; |
| 220 | size_t len; |
| 221 | va_list ap; |
| 222 | |
| 223 | LY_CHECK_ARG_RET(ctx, ctx, path, NULL); |
| 224 | |
| 225 | fd = open(path, O_RDONLY); |
| 226 | LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL); |
| 227 | |
| 228 | if (!format) { |
| 229 | /* unknown format - try to detect it from filename's suffix */ |
| 230 | len = strlen(path); |
| 231 | |
| 232 | /* ignore trailing whitespaces */ |
| 233 | for (; len > 0 && isspace(path[len - 1]); len--); |
| 234 | |
| 235 | if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) { |
| 236 | format = LYD_XML; |
| 237 | #if 0 |
| 238 | } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) { |
| 239 | format = LYD_JSON; |
| 240 | } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) { |
| 241 | format = LYD_LYB; |
| 242 | #endif |
| 243 | } /* else still unknown, try later to detect it from the content */ |
| 244 | } |
| 245 | |
| 246 | va_start(ap, options); |
| 247 | result = lyd_parse_fd_(ctx, fd, format, options, ap); |
| 248 | |
| 249 | va_end(ap); |
| 250 | close(fd); |
| 251 | |
| 252 | return result; |
| 253 | } |