Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_yang.c |
| 3 | * @author Pavol Vican |
| 4 | * @brief YANG parser for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Pavol Vican | 9a3a721 | 2016-03-23 10:04:00 +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 |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 16 | #include "parser_yang.h" |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 17 | #include "parser_yang_lex.h" |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 18 | #include "parser.h" |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 19 | #include "xpath.h" |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 20 | |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 21 | static int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 22 | yang_check_string(struct lys_module *module, const char **target, char *what, char *where, char *value) |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 23 | { |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 24 | if (*target) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 25 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, what, where); |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 26 | free(value); |
| 27 | return 1; |
| 28 | } else { |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 29 | *target = lydict_insert_zc(module->ctx, value); |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 30 | return 0; |
| 31 | } |
| 32 | } |
| 33 | |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 34 | static int |
| 35 | yang_check_typedef_identif(struct lys_node *root, struct lys_node *node, char *id) |
| 36 | { |
| 37 | struct lys_node *child, *next; |
| 38 | int size; |
| 39 | struct lys_tpdf *tpdf; |
| 40 | |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 41 | if (root) { |
| 42 | node = root; |
| 43 | } |
| 44 | |
| 45 | do { |
| 46 | LY_TREE_DFS_BEGIN(node, next, child) { |
| 47 | if (child->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_GROUPING | LYS_RPC | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) { |
| 48 | switch (child->nodetype) { |
| 49 | case LYS_CONTAINER: |
| 50 | tpdf = ((struct lys_node_container *)child)->tpdf; |
| 51 | size = ((struct lys_node_container *)child)->tpdf_size; |
| 52 | break; |
| 53 | case LYS_LIST: |
| 54 | tpdf = ((struct lys_node_list *)child)->tpdf; |
| 55 | size = ((struct lys_node_list *)child)->tpdf_size; |
| 56 | break; |
| 57 | case LYS_GROUPING: |
| 58 | tpdf = ((struct lys_node_grp *)child)->tpdf; |
| 59 | size = ((struct lys_node_grp *)child)->tpdf_size; |
| 60 | break; |
| 61 | case LYS_RPC: |
Radek Krejci | 71d5183 | 2016-07-14 15:59:06 +0200 | [diff] [blame] | 62 | tpdf = ((struct lys_node_rpc_action *)child)->tpdf; |
| 63 | size = ((struct lys_node_rpc_action *)child)->tpdf_size; |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 64 | break; |
| 65 | case LYS_INPUT: |
| 66 | case LYS_OUTPUT: |
Radek Krejci | 71d5183 | 2016-07-14 15:59:06 +0200 | [diff] [blame] | 67 | tpdf = ((struct lys_node_inout *)child)->tpdf; |
| 68 | size = ((struct lys_node_inout *)child)->tpdf_size; |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 69 | break; |
| 70 | case LYS_NOTIF: |
| 71 | tpdf = ((struct lys_node_notif *)child)->tpdf; |
| 72 | size = ((struct lys_node_notif *)child)->tpdf_size; |
| 73 | break; |
| 74 | default: |
| 75 | size = 0; |
| 76 | break; |
| 77 | } |
Radek Krejci | fc824a4 | 2016-07-14 15:48:38 +0200 | [diff] [blame] | 78 | if (size && dup_typedef_check(id, tpdf, size)) { |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 79 | LOGVAL(LYE_DUPID, LY_VLOG_NONE, NULL, "typedef", id); |
| 80 | return EXIT_FAILURE; |
| 81 | } |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 82 | } |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 83 | LY_TREE_DFS_END(node, next, child)} |
| 84 | } while (root && (node = node->next)); |
| 85 | return EXIT_SUCCESS; |
| 86 | } |
| 87 | |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 88 | int |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 89 | yang_read_common(struct lys_module *module, char *value, enum yytokentype type) |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 90 | { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 91 | int ret = 0; |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 92 | |
| 93 | switch (type) { |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 94 | case MODULE_KEYWORD: |
| 95 | module->name = lydict_insert_zc(module->ctx, value); |
| 96 | break; |
| 97 | case NAMESPACE_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 98 | ret = yang_check_string(module, &module->ns, "namespace", "module", value); |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 99 | break; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 100 | case ORGANIZATION_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 101 | ret = yang_check_string(module, &module->org, "organization", "module", value); |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 102 | break; |
| 103 | case CONTACT_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 104 | ret = yang_check_string(module, &module->contact, "contact", "module", value); |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 105 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 106 | default: |
| 107 | free(value); |
| 108 | LOGINT; |
| 109 | ret = EXIT_FAILURE; |
| 110 | break; |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Pavol Vican | 021488a | 2016-01-25 23:56:12 +0100 | [diff] [blame] | 113 | return ret; |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 116 | int |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 117 | yang_check_version(struct lys_module *module, struct lys_submodule *submodule, char *value, int repeat) |
| 118 | { |
| 119 | int ret = EXIT_SUCCESS; |
| 120 | |
| 121 | if (repeat) { |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 122 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "yang version", "module"); |
| 123 | ret = EXIT_FAILURE; |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 124 | } else { |
| 125 | if (!strcmp(value, "1")) { |
| 126 | if (submodule) { |
| 127 | if (module->version > 1) { |
| 128 | LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL); |
| 129 | ret = EXIT_FAILURE; |
| 130 | } |
| 131 | } else { |
| 132 | module->version = 1; |
| 133 | } |
| 134 | } else if (!strcmp(value, "1.1")) { |
| 135 | if (submodule) { |
| 136 | if (module->version != 2) { |
| 137 | LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL); |
| 138 | ret = EXIT_FAILURE; |
| 139 | } |
| 140 | } else { |
| 141 | module->version = 2; |
| 142 | } |
| 143 | } else { |
| 144 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "yang-version"); |
| 145 | ret = EXIT_FAILURE; |
Michal Vasko | 3767fb2 | 2016-07-21 12:10:57 +0200 | [diff] [blame] | 146 | } |
Pavol Vican | d0b64c1 | 2016-07-15 09:56:19 +0200 | [diff] [blame] | 147 | } |
| 148 | free(value); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | int |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 153 | yang_read_prefix(struct lys_module *module, struct lys_import *imp, char *value) |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 154 | { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 155 | int ret = 0; |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 156 | |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 157 | if (lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) { |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 158 | free(value); |
| 159 | return EXIT_FAILURE; |
| 160 | } |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 161 | |
| 162 | if (imp) { |
| 163 | ret = yang_check_string(module, &imp->prefix, "prefix", "import", value); |
| 164 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 165 | ret = yang_check_string(module, &module->prefix, "prefix", "module", value); |
Pavol Vican | 2a06465 | 2016-02-02 22:54:34 +0100 | [diff] [blame] | 166 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 167 | |
Pavol Vican | bf80547 | 2016-01-26 14:24:56 +0100 | [diff] [blame] | 168 | return ret; |
| 169 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 170 | |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 171 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 172 | yang_fill_import(struct lys_module *module, struct lys_import *imp, char *value) |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 173 | { |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 174 | const char *exp; |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 175 | int rc; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 176 | |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 177 | if (!imp->prefix) { |
| 178 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", "import"); |
| 179 | return EXIT_FAILURE; |
| 180 | } |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 181 | exp = lydict_insert_zc(module->ctx, value); |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 182 | rc = lyp_check_import(module, exp, imp); |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 183 | lydict_remove(module->ctx, exp); |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 184 | module->imp_size++; |
Pavol Vican | 0da132e | 2016-03-21 12:03:03 +0100 | [diff] [blame] | 185 | if (rc) { |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 186 | return EXIT_FAILURE; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 187 | } |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 188 | |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 189 | return EXIT_SUCCESS; |
Pavol Vican | 6eb14e8 | 2016-02-03 12:27:13 +0100 | [diff] [blame] | 190 | } |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 191 | |
| 192 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 193 | yang_read_description(struct lys_module *module, void *node, char *value, char *where) |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 194 | { |
| 195 | int ret; |
Pavol Vican | 9bcd7c6 | 2016-03-17 19:36:35 +0100 | [diff] [blame] | 196 | char *dsc = "description"; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 197 | |
| 198 | if (!node) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 199 | ret = yang_check_string(module, &module->dsc, dsc, "module", value); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 200 | } else { |
Pavol Vican | 9bcd7c6 | 2016-03-17 19:36:35 +0100 | [diff] [blame] | 201 | if (!strcmp("revision", where)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 202 | ret = yang_check_string(module, &((struct lys_revision *)node)->dsc, dsc, where, value); |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 203 | } else if (!strcmp("import", where)){ |
| 204 | ret = yang_check_string(module, &((struct lys_import *)node)->dsc, dsc, where, value); |
| 205 | } else if (!strcmp("include", where)){ |
| 206 | ret = yang_check_string(module, &((struct lys_include *)node)->dsc, dsc, where, value); |
Pavol Vican | 9bcd7c6 | 2016-03-17 19:36:35 +0100 | [diff] [blame] | 207 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 208 | ret = yang_check_string(module, &((struct lys_node *)node)->dsc, dsc, where, value); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 209 | } |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 210 | } |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 215 | yang_read_reference(struct lys_module *module, void *node, char *value, char *where) |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 216 | { |
| 217 | int ret; |
Pavol Vican | f5fe966 | 2016-03-17 20:00:16 +0100 | [diff] [blame] | 218 | char *ref = "reference"; |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 219 | |
| 220 | if (!node) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 221 | ret = yang_check_string(module, &module->ref, "reference", "module", value); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 222 | } else { |
Pavol Vican | f5fe966 | 2016-03-17 20:00:16 +0100 | [diff] [blame] | 223 | if (!strcmp("revision", where)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 224 | ret = yang_check_string(module, &((struct lys_revision *)node)->ref, ref, where, value); |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 225 | } else if (!strcmp("import", where)){ |
| 226 | ret = yang_check_string(module, &((struct lys_import *)node)->ref, ref, where, value); |
| 227 | } else if (!strcmp("include", where)){ |
| 228 | ret = yang_check_string(module, &((struct lys_include *)node)->ref, ref, where, value); |
Pavol Vican | f5fe966 | 2016-03-17 20:00:16 +0100 | [diff] [blame] | 229 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 230 | ret = yang_check_string(module, &((struct lys_node *)node)->ref, ref, where, value); |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 231 | } |
Pavol Vican | 1ca072c | 2016-02-03 13:03:56 +0100 | [diff] [blame] | 232 | } |
| 233 | return ret; |
| 234 | } |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 235 | |
| 236 | void * |
| 237 | yang_read_revision(struct lys_module *module, char *value) |
| 238 | { |
| 239 | struct lys_revision *retval; |
| 240 | |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 241 | retval = &module->rev[module->rev_size]; |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 242 | |
| 243 | /* first member of array is last revision */ |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 244 | if (module->rev_size && strcmp(module->rev[0].date, value) < 0) { |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 245 | memcpy(retval->date, module->rev[0].date, LY_REV_SIZE); |
| 246 | memcpy(module->rev[0].date, value, LY_REV_SIZE); |
| 247 | retval->dsc = module->rev[0].dsc; |
| 248 | retval->ref = module->rev[0].ref; |
| 249 | retval = module->rev; |
| 250 | retval->dsc = NULL; |
| 251 | retval->ref = NULL; |
| 252 | } else { |
| 253 | memcpy(retval->date, value, LY_REV_SIZE); |
| 254 | } |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 255 | module->rev_size++; |
Pavol Vican | bedff69 | 2016-02-03 14:29:17 +0100 | [diff] [blame] | 256 | free(value); |
| 257 | return retval; |
| 258 | } |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 259 | |
| 260 | int |
Pavol Vican | a182796 | 2016-02-29 15:39:42 +0100 | [diff] [blame] | 261 | yang_add_elem(struct lys_node_array **node, uint32_t *size) |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 262 | { |
Pavol Vican | 45ccc59 | 2016-03-09 18:53:48 +0100 | [diff] [blame] | 263 | if (!(*size % LY_ARRAY_SIZE)) { |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 264 | if (!(*node = ly_realloc(*node, (*size + LY_ARRAY_SIZE) * sizeof **node))) { |
| 265 | LOGMEM; |
| 266 | return EXIT_FAILURE; |
| 267 | } else { |
Pavol Vican | 45ccc59 | 2016-03-09 18:53:48 +0100 | [diff] [blame] | 268 | memset(*node + *size, 0, LY_ARRAY_SIZE * sizeof **node); |
Pavol Vican | 1eeb199 | 2016-02-09 11:10:45 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | (*size)++; |
| 272 | return EXIT_SUCCESS; |
| 273 | } |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 274 | |
| 275 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 276 | yang_read_feature(struct lys_module *module, char *value) |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 277 | { |
| 278 | struct lys_feature *retval; |
| 279 | |
| 280 | /* check uniqueness of feature's names */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 281 | if (lyp_check_identifier(value, LY_IDENT_FEATURE, module, NULL)) { |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 282 | goto error; |
| 283 | } |
| 284 | retval = &module->features[module->features_size]; |
| 285 | retval->name = lydict_insert_zc(module->ctx, value); |
| 286 | retval->module = module; |
| 287 | module->features_size++; |
| 288 | return retval; |
| 289 | |
| 290 | error: |
| 291 | free(value); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | int |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 296 | yang_read_if_feature(struct lys_module *module, void *ptr, char *value, struct unres_schema *unres, enum yytokentype type) |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 297 | { |
| 298 | const char *exp; |
| 299 | int ret; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 300 | struct lys_feature *f; |
Pavol Vican | 4df8054 | 2016-08-08 09:37:55 +0200 | [diff] [blame] | 301 | struct lys_ident *i; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 302 | struct lys_node *n; |
Pavol Vican | 8ff0e2e | 2016-08-09 09:09:10 +0200 | [diff] [blame] | 303 | struct lys_type_enum *e; |
| 304 | struct lys_type_bit *b; |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 305 | |
Michal Vasko | 97b32be | 2016-07-25 10:59:53 +0200 | [diff] [blame] | 306 | if ((module->version != 2) && ((value[0] == '(') || strchr(value, ' '))) { |
| 307 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature"); |
| 308 | free(value); |
| 309 | return EXIT_FAILURE; |
| 310 | } |
| 311 | |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 312 | if (!(exp = transform_schema2json(module, value))) { |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 313 | free(value); |
| 314 | return EXIT_FAILURE; |
| 315 | } |
| 316 | free(value); |
| 317 | |
Pavol Vican | 8ff0e2e | 2016-08-09 09:09:10 +0200 | [diff] [blame] | 318 | switch (type) { |
| 319 | case FEATURE_KEYWORD: |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 320 | f = (struct lys_feature *) ptr; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 321 | ret = resolve_iffeature_compile(&f->iffeature[f->iffeature_size], exp, (struct lys_node *)f, unres); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 322 | f->iffeature_size++; |
Pavol Vican | 8ff0e2e | 2016-08-09 09:09:10 +0200 | [diff] [blame] | 323 | break; |
| 324 | case IDENTITY_KEYWORD: |
Pavol Vican | 4df8054 | 2016-08-08 09:37:55 +0200 | [diff] [blame] | 325 | i = (struct lys_ident *) ptr; |
| 326 | ret = resolve_iffeature_compile(&i->iffeature[i->iffeature_size], exp, (struct lys_node *)i, unres); |
| 327 | i->iffeature_size++; |
Pavol Vican | 8ff0e2e | 2016-08-09 09:09:10 +0200 | [diff] [blame] | 328 | break; |
| 329 | case ENUM_KEYWORD: |
| 330 | e = &((struct yang_type *)ptr)->type->info.enums.enm[((struct yang_type *)ptr)->type->info.enums.count - 1]; |
| 331 | ret = resolve_iffeature_compile(&e->iffeature[e->iffeature_size], exp, (struct lys_node *)((struct yang_type *)ptr)->type->parent, unres); |
| 332 | e->iffeature_size++; |
| 333 | break; |
| 334 | case BIT_KEYWORD: |
| 335 | b = &((struct yang_type *)ptr)->type->info.bits.bit[((struct yang_type *)ptr)->type->info.bits.count - 1]; |
| 336 | ret = resolve_iffeature_compile(&b->iffeature[b->iffeature_size], exp, (struct lys_node *)((struct yang_type *)ptr)->type->parent, unres); |
| 337 | b->iffeature_size++; |
| 338 | break; |
| 339 | default: |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 340 | n = (struct lys_node *) ptr; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 341 | ret = resolve_iffeature_compile(&n->iffeature[n->iffeature_size], exp, n, unres); |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 342 | n->iffeature_size++; |
Pavol Vican | 8ff0e2e | 2016-08-09 09:09:10 +0200 | [diff] [blame] | 343 | break; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 344 | } |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 345 | lydict_remove(module->ctx, exp); |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 346 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 347 | if (ret) { |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 348 | return EXIT_FAILURE; |
| 349 | } |
| 350 | return EXIT_SUCCESS; |
| 351 | } |
| 352 | |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 353 | int |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 354 | yang_check_flags(uint16_t *flags, uint16_t mask, char *what, char *where, uint16_t value, int shortint) |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 355 | { |
| 356 | if (*flags & mask) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 357 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, what, where); |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 358 | return EXIT_FAILURE; |
| 359 | } else { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 360 | if (shortint) { |
| 361 | *((uint8_t *)flags) |= (uint8_t)value; |
| 362 | } else { |
| 363 | *flags |= value; |
| 364 | } |
Pavol Vican | e1354e9 | 2016-02-09 14:02:09 +0100 | [diff] [blame] | 365 | return EXIT_SUCCESS; |
| 366 | } |
| 367 | } |
| 368 | |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 369 | void * |
| 370 | yang_read_identity(struct lys_module *module, char *value) |
| 371 | { |
| 372 | struct lys_ident *ret; |
| 373 | |
| 374 | ret = &module->ident[module->ident_size]; |
| 375 | ret->name = lydict_insert_zc(module->ctx, value); |
| 376 | ret->module = module; |
Pavol Vican | d6cda45 | 2016-07-13 15:08:29 +0200 | [diff] [blame] | 377 | if (dup_identities_check(ret->name, module)) { |
| 378 | lydict_remove(module->ctx, ret->name); |
| 379 | return NULL; |
| 380 | } |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 381 | module->ident_size++; |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 386 | yang_read_base(struct lys_module *module, struct lys_ident *ident, char *value, struct unres_schema *unres) |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 387 | { |
| 388 | const char *exp; |
| 389 | |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 390 | exp = transform_schema2json(module, value); |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 391 | free(value); |
| 392 | if (!exp) { |
| 393 | return EXIT_FAILURE; |
| 394 | } |
Pavol Vican | 4ffd0ab | 2016-08-27 16:35:04 +0200 | [diff] [blame] | 395 | |
| 396 | /* temporarily decrement identity_size due to resolve base */ |
| 397 | module->ident_size--; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 398 | if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, exp) == -1) { |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 399 | lydict_remove(module->ctx, exp); |
Pavol Vican | 4ffd0ab | 2016-08-27 16:35:04 +0200 | [diff] [blame] | 400 | /* undo change identity_size */ |
| 401 | module->ident_size++; |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 402 | return EXIT_FAILURE; |
| 403 | } |
Pavol Vican | 4ffd0ab | 2016-08-27 16:35:04 +0200 | [diff] [blame] | 404 | /* undo change identity_size */ |
| 405 | module->ident_size++; |
Pavol Vican | 44dde2c | 2016-02-10 11:18:14 +0100 | [diff] [blame] | 406 | |
Pavol Vican | bbdef53 | 2016-02-09 14:52:12 +0100 | [diff] [blame] | 407 | lydict_remove(module->ctx, exp); |
| 408 | return EXIT_SUCCESS; |
| 409 | } |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 410 | |
| 411 | void * |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 412 | yang_read_must(struct lys_module *module, struct lys_node *node, char *value, enum yytokentype type) |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 413 | { |
| 414 | struct lys_restr *retval; |
| 415 | |
| 416 | switch (type) { |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 417 | case CONTAINER_KEYWORD: |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 418 | retval = &((struct lys_node_container *)node)->must[((struct lys_node_container *)node)->must_size++]; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 419 | break; |
Pavol Vican | db7489e | 2016-08-23 17:23:39 +0200 | [diff] [blame] | 420 | case ANYDATA_KEYWORD: |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 421 | case ANYXML_KEYWORD: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 422 | retval = &((struct lys_node_anydata *)node)->must[((struct lys_node_anydata *)node)->must_size++]; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 423 | break; |
| 424 | case LEAF_KEYWORD: |
| 425 | retval = &((struct lys_node_leaf *)node)->must[((struct lys_node_leaf *)node)->must_size++]; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 426 | break; |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 427 | case LEAF_LIST_KEYWORD: |
| 428 | retval = &((struct lys_node_leaflist *)node)->must[((struct lys_node_leaflist *)node)->must_size++]; |
| 429 | break; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 430 | case LIST_KEYWORD: |
| 431 | retval = &((struct lys_node_list *)node)->must[((struct lys_node_list *)node)->must_size++]; |
| 432 | break; |
Pavol Vican | 1003ead | 2016-03-02 12:24:52 +0100 | [diff] [blame] | 433 | case REFINE_KEYWORD: |
| 434 | retval = &((struct lys_refine *)node)->must[((struct lys_refine *)node)->must_size++]; |
| 435 | break; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 436 | case ADD_KEYWORD: |
| 437 | retval = &(*((struct type_deviation *)node)->trg_must)[(*((struct type_deviation *)node)->trg_must_size)++]; |
| 438 | memset(retval, 0, sizeof *retval); |
| 439 | break; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 440 | case DELETE_KEYWORD: |
| 441 | retval = &((struct type_deviation *)node)->deviate->must[((struct type_deviation *)node)->deviate->must_size++]; |
| 442 | break; |
Pavol Vican | 9b14b49 | 2016-08-09 14:55:10 +0200 | [diff] [blame] | 443 | case NOTIFICATION_KEYWORD: |
| 444 | retval = &((struct lys_node_notif *)node)->must[((struct lys_node_notif *)node)->must_size++]; |
| 445 | break; |
| 446 | case INPUT_KEYWORD: |
| 447 | retval = &((struct lys_node_inout *)node)->must[((struct lys_node_inout *)node)->must_size++]; |
| 448 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 449 | default: |
| 450 | goto error; |
| 451 | break; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 452 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 453 | retval->expr = transform_schema2json(module, value); |
| 454 | if (!retval->expr || lyxp_syntax_check(retval->expr)) { |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 455 | goto error; |
| 456 | } |
| 457 | free(value); |
| 458 | return retval; |
| 459 | |
| 460 | error: |
| 461 | free(value); |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 466 | yang_read_message(struct lys_module *module,struct lys_restr *save,char *value, char *what, int message) |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 467 | { |
| 468 | int ret; |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 469 | |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 470 | if (message==ERROR_APP_TAG_KEYWORD) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 471 | ret = yang_check_string(module, &save->eapptag, "error_app_tag", what, value); |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 472 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 473 | ret = yang_check_string(module, &save->emsg, "error_app_tag", what, value); |
Pavol Vican | f37eeaa | 2016-02-09 20:54:06 +0100 | [diff] [blame] | 474 | } |
| 475 | return ret; |
| 476 | } |
Pavol Vican | b568711 | 2016-02-09 22:35:59 +0100 | [diff] [blame] | 477 | |
| 478 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 479 | yang_read_presence(struct lys_module *module, struct lys_node_container *cont, char *value) |
Pavol Vican | b568711 | 2016-02-09 22:35:59 +0100 | [diff] [blame] | 480 | { |
| 481 | if (cont->presence) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 482 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, cont, "presence", "container"); |
Pavol Vican | b568711 | 2016-02-09 22:35:59 +0100 | [diff] [blame] | 483 | free(value); |
| 484 | return EXIT_FAILURE; |
| 485 | } else { |
| 486 | cont->presence = lydict_insert_zc(module->ctx, value); |
| 487 | return EXIT_SUCCESS; |
| 488 | } |
| 489 | } |
| 490 | |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 491 | void * |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 492 | yang_read_when(struct lys_module *module, struct lys_node *node, enum yytokentype type, char *value) |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 493 | { |
| 494 | struct lys_when *retval; |
| 495 | |
| 496 | retval = calloc(1, sizeof *retval); |
| 497 | if (!retval) { |
| 498 | LOGMEM; |
| 499 | free(value); |
| 500 | return NULL; |
| 501 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 502 | retval->cond = transform_schema2json(module, value); |
| 503 | if (!retval->cond || lyxp_syntax_check(retval->cond)) { |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 504 | goto error; |
| 505 | } |
| 506 | switch (type) { |
| 507 | case CONTAINER_KEYWORD: |
| 508 | if (((struct lys_node_container *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 509 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "container"); |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 510 | goto error; |
| 511 | } |
| 512 | ((struct lys_node_container *)node)->when = retval; |
| 513 | break; |
Pavol Vican | db7489e | 2016-08-23 17:23:39 +0200 | [diff] [blame] | 514 | case ANYDATA_KEYWORD: |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 515 | case ANYXML_KEYWORD: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 516 | if (((struct lys_node_anydata *)node)->when) { |
Pavol Vican | db7489e | 2016-08-23 17:23:39 +0200 | [diff] [blame] | 517 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", (type == ANYXML_KEYWORD) ? "anyxml" : "anydata"); |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 518 | goto error; |
| 519 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 520 | ((struct lys_node_anydata *)node)->when = retval; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 521 | break; |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 522 | case CHOICE_KEYWORD: |
| 523 | if (((struct lys_node_choice *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 524 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "choice"); |
Pavol Vican | 1f06ba8 | 2016-02-10 17:39:50 +0100 | [diff] [blame] | 525 | goto error; |
| 526 | } |
| 527 | ((struct lys_node_choice *)node)->when = retval; |
| 528 | break; |
Pavol Vican | bd09813 | 2016-02-11 10:56:49 +0100 | [diff] [blame] | 529 | case CASE_KEYWORD: |
| 530 | if (((struct lys_node_case *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 531 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "case"); |
Pavol Vican | bd09813 | 2016-02-11 10:56:49 +0100 | [diff] [blame] | 532 | goto error; |
| 533 | } |
| 534 | ((struct lys_node_case *)node)->when = retval; |
| 535 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 536 | case LEAF_KEYWORD: |
| 537 | if (((struct lys_node_leaf *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 538 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaf"); |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 539 | goto error; |
| 540 | } |
| 541 | ((struct lys_node_leaf *)node)->when = retval; |
| 542 | break; |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 543 | case LEAF_LIST_KEYWORD: |
| 544 | if (((struct lys_node_leaflist *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 545 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaflist"); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 546 | goto error; |
| 547 | } |
| 548 | ((struct lys_node_leaflist *)node)->when = retval; |
| 549 | break; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 550 | case LIST_KEYWORD: |
| 551 | if (((struct lys_node_list *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 552 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "list"); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 553 | goto error; |
| 554 | } |
| 555 | ((struct lys_node_list *)node)->when = retval; |
| 556 | break; |
Pavol Vican | 14b1856 | 2016-03-01 16:04:29 +0100 | [diff] [blame] | 557 | case USES_KEYWORD: |
| 558 | if (((struct lys_node_uses *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 559 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "uses"); |
Pavol Vican | 14b1856 | 2016-03-01 16:04:29 +0100 | [diff] [blame] | 560 | goto error; |
| 561 | } |
| 562 | ((struct lys_node_uses *)node)->when = retval; |
| 563 | break; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 564 | case AUGMENT_KEYWORD: |
| 565 | if (((struct lys_node_augment *)node)->when) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 566 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "augment"); |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 567 | goto error; |
| 568 | } |
| 569 | ((struct lys_node_augment *)node)->when = retval; |
| 570 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 571 | default: |
| 572 | goto error; |
| 573 | break; |
Pavol Vican | 235dbd4 | 2016-02-10 10:34:19 +0100 | [diff] [blame] | 574 | } |
| 575 | free(value); |
| 576 | return retval; |
| 577 | |
| 578 | error: |
| 579 | free(value); |
| 580 | lys_when_free(module->ctx, retval); |
| 581 | return NULL; |
| 582 | } |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 583 | |
| 584 | void * |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 585 | yang_read_node(struct lys_module *module, struct lys_node *parent, char *value, int nodetype, int sizeof_struct) |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 586 | { |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 587 | struct lys_node *node; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 588 | |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 589 | node = calloc(1, sizeof_struct); |
| 590 | if (!node) { |
Pavol Vican | d1dbfda | 2016-03-21 10:03:58 +0100 | [diff] [blame] | 591 | free(value); |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 592 | LOGMEM; |
| 593 | return NULL; |
| 594 | } |
Pavol Vican | 531a913 | 2016-03-03 10:10:09 +0100 | [diff] [blame] | 595 | if (value) { |
| 596 | node->name = lydict_insert_zc(module->ctx, value); |
| 597 | } |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 598 | node->module = module; |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 599 | node->nodetype = nodetype; |
| 600 | node->prev = node; |
| 601 | |
| 602 | /* insert the node into the schema tree */ |
| 603 | if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, node)) { |
Pavol Vican | 531a913 | 2016-03-03 10:10:09 +0100 | [diff] [blame] | 604 | if (value) { |
| 605 | lydict_remove(module->ctx, node->name); |
| 606 | } |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 607 | free(node); |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 608 | return NULL; |
| 609 | } |
Pavol Vican | 7cadfe7 | 2016-02-11 12:33:34 +0100 | [diff] [blame] | 610 | return node; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Pavol Vican | 8c79399 | 2016-07-15 10:44:57 +0200 | [diff] [blame] | 613 | void * |
| 614 | yang_read_action(struct lys_module *module, struct lys_node *parent, char *value) |
| 615 | { |
| 616 | struct lys_node *node; |
| 617 | |
Michal Vasko | bb17485 | 2016-07-25 11:00:21 +0200 | [diff] [blame] | 618 | if (module->version != 2) { |
| 619 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "action"); |
| 620 | return NULL; |
| 621 | } |
| 622 | |
Pavol Vican | 8c79399 | 2016-07-15 10:44:57 +0200 | [diff] [blame] | 623 | for (node = parent; node; node = lys_parent(node)) { |
| 624 | if (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF) |
Pavol Vican | bbe7782 | 2016-07-15 12:53:07 +0200 | [diff] [blame] | 625 | || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys_size)) { |
Pavol Vican | 8c79399 | 2016-07-15 10:44:57 +0200 | [diff] [blame] | 626 | LOGVAL(LYE_INPAR, LY_VLOG_NONE, NULL, strnodetype(node->nodetype), "action"); |
| 627 | return NULL; |
| 628 | } |
| 629 | } |
| 630 | return yang_read_node(module, parent, value, LYS_ACTION, sizeof(struct lys_node_rpc_action)); |
| 631 | } |
| 632 | |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 633 | int |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 634 | yang_read_default(struct lys_module *module, void *node, char *value, enum yytokentype type) |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 635 | { |
| 636 | int ret; |
| 637 | |
| 638 | switch (type) { |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 639 | case LEAF_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 640 | ret = yang_check_string(module, &((struct lys_node_leaf *) node)->dflt, "default", "leaf", value); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 641 | break; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 642 | case TYPEDEF_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 643 | ret = yang_check_string(module, &((struct lys_tpdf *) node)->dflt, "default", "typedef", value); |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 644 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 645 | default: |
| 646 | free(value); |
| 647 | LOGINT; |
| 648 | ret = EXIT_FAILURE; |
| 649 | break; |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 650 | } |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | int |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 655 | yang_read_units(struct lys_module *module, void *node, char *value, enum yytokentype type) |
Pavol Vican | 096c6db | 2016-02-11 15:08:10 +0100 | [diff] [blame] | 656 | { |
| 657 | int ret; |
| 658 | |
| 659 | switch (type) { |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 660 | case LEAF_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 661 | ret = yang_check_string(module, &((struct lys_node_leaf *) node)->units, "units", "leaf", value); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 662 | break; |
| 663 | case LEAF_LIST_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 664 | ret = yang_check_string(module, &((struct lys_node_leaflist *) node)->units, "units", "leaflist", value); |
Pavol Vican | 339d4ad | 2016-02-12 12:49:22 +0100 | [diff] [blame] | 665 | break; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 666 | case TYPEDEF_KEYWORD: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 667 | ret = yang_check_string(module, &((struct lys_tpdf *) node)->units, "units", "typedef", value); |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 668 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 669 | default: |
| 670 | free(value); |
| 671 | LOGINT; |
| 672 | ret = EXIT_FAILURE; |
| 673 | break; |
Pavol Vican | 8c82fa8 | 2016-02-10 13:13:24 +0100 | [diff] [blame] | 674 | } |
| 675 | return ret; |
| 676 | } |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 677 | |
| 678 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 679 | yang_read_key(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres) |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 680 | { |
| 681 | char *exp, *value; |
| 682 | |
| 683 | exp = value = (char *) list->keys; |
Pavol Vican | bbe7782 | 2016-07-15 12:53:07 +0200 | [diff] [blame] | 684 | list->keys_size = 0; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 685 | while ((value = strpbrk(value, " \t\n"))) { |
| 686 | list->keys_size++; |
| 687 | while (isspace(*value)) { |
| 688 | value++; |
| 689 | } |
| 690 | } |
| 691 | list->keys_size++; |
| 692 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
| 693 | if (!list->keys) { |
| 694 | LOGMEM; |
| 695 | goto error; |
| 696 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 697 | if (unres_schema_add_str(module, unres, list, UNRES_LIST_KEYS, exp) == -1) { |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 698 | goto error; |
| 699 | } |
| 700 | free(exp); |
| 701 | return EXIT_SUCCESS; |
| 702 | |
| 703 | error: |
| 704 | free(exp); |
| 705 | return EXIT_FAILURE; |
| 706 | } |
| 707 | |
| 708 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 709 | yang_fill_unique(struct lys_module *module, struct lys_node_list *list, struct lys_unique *unique, char *value, struct unres_schema *unres) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 710 | { |
| 711 | int i, j; |
| 712 | char *vaux; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 713 | struct unres_list_uniq *unique_info; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 714 | |
| 715 | /* count the number of unique leafs in the value */ |
| 716 | vaux = value; |
| 717 | while ((vaux = strpbrk(vaux, " \t\n"))) { |
| 718 | unique->expr_size++; |
| 719 | while (isspace(*vaux)) { |
| 720 | vaux++; |
| 721 | } |
| 722 | } |
| 723 | unique->expr_size++; |
| 724 | unique->expr = calloc(unique->expr_size, sizeof *unique->expr); |
| 725 | if (!unique->expr) { |
| 726 | LOGMEM; |
| 727 | goto error; |
| 728 | } |
| 729 | |
| 730 | for (i = 0; i < unique->expr_size; i++) { |
| 731 | vaux = strpbrk(value, " \t\n"); |
| 732 | if (!vaux) { |
| 733 | /* the last token, lydict_insert() will count its size on its own */ |
| 734 | vaux = value; |
| 735 | } |
| 736 | |
| 737 | /* store token into unique structure */ |
| 738 | unique->expr[i] = lydict_insert(module->ctx, value, vaux - value); |
| 739 | |
| 740 | /* check that the expression does not repeat */ |
| 741 | for (j = 0; j < i; j++) { |
| 742 | if (ly_strequal(unique->expr[j], unique->expr[i], 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 743 | LOGVAL(LYE_INARG, LY_VLOG_LYS, list, unique->expr[i], "unique"); |
| 744 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The identifier is not unique"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 745 | goto error; |
| 746 | } |
| 747 | } |
| 748 | /* try to resolve leaf */ |
| 749 | if (unres) { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 750 | unique_info = malloc(sizeof *unique_info); |
| 751 | unique_info->list = (struct lys_node *)list; |
| 752 | unique_info->expr = unique->expr[i]; |
| 753 | unique_info->trg_type = &unique->trg_type; |
| 754 | if (unres_schema_add_node(module, unres, unique_info, UNRES_LIST_UNIQ, NULL) == -1) { |
Pavol Vican | 18b1021 | 2016-04-11 15:41:52 +0200 | [diff] [blame] | 755 | goto error; |
| 756 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 757 | } else { |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 758 | if (resolve_unique((struct lys_node *)list, unique->expr[i], &unique->trg_type)) { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 759 | goto error; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /* move to next token */ |
| 764 | value = vaux; |
| 765 | while(isspace(*value)) { |
| 766 | value++; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | return EXIT_SUCCESS; |
| 771 | |
| 772 | error: |
| 773 | return EXIT_FAILURE; |
| 774 | } |
| 775 | |
| 776 | int |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 777 | yang_read_unique(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres) |
| 778 | { |
| 779 | uint8_t k; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 780 | char *str; |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 781 | |
| 782 | for(k=0; k<list->unique_size; k++) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 783 | str = (char *)list->unique[k].expr; |
| 784 | if (yang_fill_unique(module, list, &list->unique[k], str, unres)) { |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 785 | goto error; |
| 786 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 787 | free(str); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 788 | } |
| 789 | return EXIT_SUCCESS; |
| 790 | |
| 791 | error: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 792 | free(str); |
Pavol Vican | 5de3349 | 2016-02-22 14:03:24 +0100 | [diff] [blame] | 793 | return EXIT_FAILURE; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 794 | } |
| 795 | |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 796 | static int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 797 | yang_read_identyref(struct lys_module *module, struct lys_type *type, struct unres_schema *unres) |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 798 | { |
| 799 | const char *value, *tmp; |
| 800 | int rc, ret = EXIT_FAILURE; |
| 801 | |
| 802 | value = tmp = type->info.lref.path; |
| 803 | /* store in the JSON format */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 804 | value = transform_schema2json(module, value); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 805 | if (!value) { |
| 806 | goto end; |
| 807 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 808 | rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 809 | lydict_remove(module->ctx, value); |
| 810 | |
| 811 | if (rc == -1) { |
| 812 | goto end; |
| 813 | } |
| 814 | |
| 815 | ret = EXIT_SUCCESS; |
| 816 | |
| 817 | end: |
| 818 | lydict_remove(module->ctx, tmp); |
| 819 | return ret; |
| 820 | } |
| 821 | |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 822 | int |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 823 | yang_check_type(struct lys_module *module, struct lys_node *parent, struct yang_type *typ, int tpdftype, struct unres_schema *unres) |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 824 | { |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 825 | int i, j, rc; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 826 | int ret = -1; |
| 827 | const char *name, *value; |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 828 | LY_DATA_TYPE base = 0; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 829 | struct lys_node *siter; |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 830 | struct lys_type *dertype; |
| 831 | struct lys_type_enum *enms_sc = NULL; |
| 832 | struct lys_type_bit *bits_sc = NULL; |
| 833 | struct lys_type_bit bit_tmp; |
| 834 | |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 835 | value = transform_schema2json(module, typ->name); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 836 | if (!value) { |
| 837 | goto error; |
| 838 | } |
| 839 | |
| 840 | i = parse_identifier(value); |
| 841 | if (i < 1) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 842 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 843 | lydict_remove(module->ctx, value); |
| 844 | goto error; |
| 845 | } |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 846 | /* module name */ |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 847 | name = value; |
| 848 | if (value[i]) { |
| 849 | typ->type->module_name = lydict_insert(module->ctx, value, i); |
| 850 | name += i; |
| 851 | if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 852 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 853 | lydict_remove(module->ctx, value); |
| 854 | goto error; |
| 855 | } |
| 856 | ++name; |
| 857 | } |
| 858 | |
| 859 | rc = resolve_superior_type(name, typ->type->module_name, module, parent, &typ->type->der); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 860 | if (rc == -1) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 861 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, typ->type->module_name); |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 862 | lydict_remove(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 863 | goto error; |
| 864 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 865 | /* the type could not be resolved or it was resolved to an unresolved typedef or leafref */ |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 866 | } else if (rc == EXIT_FAILURE) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 867 | LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name); |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 868 | lydict_remove(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 869 | ret = EXIT_FAILURE; |
| 870 | goto error; |
| 871 | } |
Radek Krejci | 408ab2f | 2016-06-06 15:27:10 +0200 | [diff] [blame] | 872 | lydict_remove(module->ctx, value); |
Radek Krejci | c13db38 | 2016-08-16 10:52:42 +0200 | [diff] [blame] | 873 | |
| 874 | if (typ->type->base == LY_TYPE_INGRP) { |
| 875 | /* resolved type in grouping, decrease the grouping's nacm number to indicate that one less |
| 876 | * unresolved item left inside the grouping */ |
| 877 | for (siter = parent; siter && (siter->nodetype != LYS_GROUPING); siter = lys_parent(siter)); |
| 878 | if (siter) { |
| 879 | if (!((struct lys_node_grp *)siter)->nacm) { |
| 880 | LOGINT; |
| 881 | goto error; |
| 882 | } |
| 883 | ((struct lys_node_grp *)siter)->nacm--; |
| 884 | } else { |
| 885 | LOGINT; |
| 886 | goto error; |
| 887 | } |
| 888 | } |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 889 | base = typ->base; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 890 | typ->type->base = typ->type->der->type.base; |
| 891 | if (base == 0) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 892 | base = typ->type->der->type.base; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 893 | } |
| 894 | switch (base) { |
| 895 | case LY_TYPE_STRING: |
| 896 | if (typ->type->base == LY_TYPE_BINARY) { |
| 897 | if (typ->type->info.str.pat_count) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 898 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Binary type could not include pattern statement."); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 899 | goto error; |
| 900 | } |
| 901 | typ->type->info.binary.length = typ->type->info.str.length; |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 902 | if (typ->type->info.binary.length && lyp_check_length_range(typ->type->info.binary.length->expr, typ->type)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 903 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.binary.length->expr, "length"); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 904 | goto error; |
| 905 | } |
| 906 | } else if (typ->type->base == LY_TYPE_STRING) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 907 | if (typ->type->info.str.length && lyp_check_length_range(typ->type->info.str.length->expr, typ->type)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 908 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.str.length->expr, "length"); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 909 | goto error; |
| 910 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 911 | } else { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 912 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 913 | goto error; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 914 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 915 | break; |
| 916 | case LY_TYPE_DEC64: |
| 917 | if (typ->type->base == LY_TYPE_DEC64) { |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 918 | if (typ->type->info.dec64.range && lyp_check_length_range(typ->type->info.dec64.range->expr, typ->type)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 919 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.dec64.range->expr, "range"); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 920 | goto error; |
| 921 | } |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 922 | /* mandatory sub-statement(s) check */ |
| 923 | if (!typ->type->info.dec64.dig && !typ->type->der->type.der) { |
| 924 | /* decimal64 type directly derived from built-in type requires fraction-digits */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 925 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 926 | goto error; |
| 927 | } |
| 928 | if (typ->type->info.dec64.dig && typ->type->der->type.der) { |
| 929 | /* type is not directly derived from buit-in type and fraction-digits statement is prohibited */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 930 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 931 | goto error; |
| 932 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 933 | } else if (typ->type->base >= LY_TYPE_INT8 && typ->type->base <=LY_TYPE_UINT64) { |
| 934 | if (typ->type->info.dec64.dig) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 935 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Numerical type could not include fraction statement."); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 936 | goto error; |
| 937 | } |
| 938 | typ->type->info.num.range = typ->type->info.dec64.range; |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 939 | if (typ->type->info.num.range && lyp_check_length_range(typ->type->info.num.range->expr, typ->type)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 940 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.num.range->expr, "range"); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 941 | goto error; |
| 942 | } |
| 943 | } else { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 944 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 945 | goto error; |
| 946 | } |
| 947 | break; |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 948 | case LY_TYPE_ENUM: |
| 949 | if (typ->type->base != LY_TYPE_ENUM) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 950 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 951 | goto error; |
| 952 | } |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 953 | dertype = &typ->type->der->type; |
| 954 | |
| 955 | if (!dertype->der) { |
| 956 | if (!typ->type->info.enums.count) { |
| 957 | /* type is derived directly from buit-in enumeartion type and enum statement is required */ |
| 958 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type"); |
| 959 | goto error; |
| 960 | } |
| 961 | } else { |
| 962 | for (; !dertype->info.enums.count; dertype = &dertype->der->type); |
| 963 | if (module->version < 2 && typ->type->info.enums.count) { |
| 964 | /* type is not directly derived from built-in enumeration type and enum statement is prohibited |
| 965 | * in YANG 1.0, since YANG 1.1 enum statements can be used to restrict the base enumeration type */ |
| 966 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "enum"); |
| 967 | goto error; |
| 968 | } |
| 969 | |
| 970 | /* restricted enumeration type - the name MUST be used in the base type */ |
| 971 | enms_sc = dertype->info.enums.enm; |
| 972 | for(i = 0; i < typ->type->info.enums.count; i++) { |
| 973 | for (j = 0; j < dertype->info.enums.count; j++) { |
| 974 | if (ly_strequal(enms_sc[j].name, typ->type->info.enums.enm[i].name, 1)) { |
| 975 | break; |
| 976 | } |
| 977 | } |
| 978 | if (j == dertype->info.enums.count) { |
| 979 | LOGVAL(LYE_ENUM_INNAME, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].name); |
| 980 | goto error; |
| 981 | } |
| 982 | |
| 983 | if (typ->type->info.enums.enm[i].flags & LYS_AUTOASSIGNED) { |
| 984 | /* automatically assign value from base type */ |
| 985 | typ->type->info.enums.enm[i].value = enms_sc[j].value; |
| 986 | } else { |
| 987 | /* check that the assigned value corresponds to the original |
| 988 | * value of the enum in the base type */ |
| 989 | if (typ->type->info.enums.enm[i].value != enms_sc[j].value) { |
| 990 | /* typ->type->info.enums.enm[i].value - assigned value in restricted enum |
| 991 | * enms_sc[j].value - value assigned to the corresponding enum (detected above) in base type */ |
| 992 | LOGVAL(LYE_ENUM_INVAL, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].value, |
| 993 | typ->type->info.enums.enm[i].name, enms_sc[j].value); |
| 994 | goto error; |
| 995 | } |
| 996 | } |
| 997 | } |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 998 | } |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 999 | break; |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 1000 | case LY_TYPE_BITS: |
| 1001 | if (typ->type->base != LY_TYPE_BITS) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 1002 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name); |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 1003 | goto error; |
| 1004 | } |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 1005 | dertype = &typ->type->der->type; |
| 1006 | |
| 1007 | if (!dertype->der) { |
| 1008 | if (!typ->type->info.bits.count) { |
| 1009 | /* type is derived directly from buit-in bits type and bit statement is required */ |
| 1010 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type"); |
| 1011 | goto error; |
| 1012 | } |
| 1013 | } else { |
| 1014 | for (; !dertype->info.enums.count; dertype = &dertype->der->type); |
| 1015 | if (module->version < 2 && typ->type->info.bits.count) { |
| 1016 | /* type is not directly derived from buit-in bits type and bit statement is prohibited, |
| 1017 | * since YANG 1.1 the bit statements can be used to restrict the base bits type */ |
| 1018 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "bit"); |
| 1019 | goto error; |
| 1020 | } |
| 1021 | |
| 1022 | bits_sc = dertype->info.bits.bit; |
| 1023 | for (i = 0; i < typ->type->info.bits.count; i++) { |
| 1024 | for (j = 0; j < dertype->info.bits.count; j++) { |
| 1025 | if (ly_strequal(bits_sc[j].name, typ->type->info.bits.bit[i].name, 1)) { |
| 1026 | break; |
| 1027 | } |
| 1028 | } |
| 1029 | if (j == dertype->info.bits.count) { |
| 1030 | LOGVAL(LYE_BITS_INNAME, LY_VLOG_NONE, NULL, typ->type->info.bits.bit[i].name); |
| 1031 | goto error; |
| 1032 | } |
| 1033 | |
| 1034 | /* restricted bits type */ |
| 1035 | if (typ->type->info.bits.bit[i].flags & LYS_AUTOASSIGNED) { |
| 1036 | /* automatically assign position from base type */ |
| 1037 | typ->type->info.bits.bit[i].pos = bits_sc[j].pos; |
| 1038 | } else { |
| 1039 | /* check that the assigned position corresponds to the original |
| 1040 | * position of the bit in the base type */ |
| 1041 | if (typ->type->info.bits.bit[i].pos != bits_sc[j].pos) { |
| 1042 | /* typ->type->info.bits.bit[i].pos - assigned position in restricted bits |
| 1043 | * bits_sc[j].pos - position assigned to the corresponding bit (detected above) in base type */ |
| 1044 | LOGVAL(LYE_BITS_INVAL, LY_VLOG_NONE, NULL, typ->type->info.bits.bit[i].pos, |
| 1045 | typ->type->info.bits.bit[i].name, bits_sc[j].pos); |
| 1046 | goto error; |
| 1047 | } |
| 1048 | } |
| 1049 | } |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 1050 | } |
Pavol Vican | 8ad2e0d | 2016-08-09 12:56:19 +0200 | [diff] [blame] | 1051 | |
| 1052 | for (i = typ->type->info.bits.count - 1; i > 0; i--) { |
| 1053 | j = i; |
| 1054 | |
| 1055 | /* keep them ordered by position */ |
| 1056 | while (j && typ->type->info.bits.bit[j - 1].pos > typ->type->info.bits.bit[j].pos) { |
| 1057 | /* switch them */ |
| 1058 | memcpy(&bit_tmp, &typ->type->info.bits.bit[j], sizeof bit_tmp); |
| 1059 | memcpy(&typ->type->info.bits.bit[j], &typ->type->info.bits.bit[j - 1], sizeof bit_tmp); |
| 1060 | memcpy(&typ->type->info.bits.bit[j - 1], &bit_tmp, sizeof bit_tmp); |
| 1061 | j--; |
| 1062 | } |
Pavol Vican | 03a5944 | 2016-03-21 15:23:45 +0100 | [diff] [blame] | 1063 | } |
| 1064 | break; |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 1065 | case LY_TYPE_LEAFREF: |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1066 | if (typ->type->base == LY_TYPE_IDENT && (typ->flags & LYS_TYPE_BASE)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1067 | if (yang_read_identyref(module, typ->type, unres)) { |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 1068 | goto error; |
| 1069 | } |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1070 | } else if (typ->type->base == LY_TYPE_LEAFREF) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1071 | /* flag resolving for later use */ |
| 1072 | if (!tpdftype) { |
| 1073 | for (siter = parent; siter && siter->nodetype != LYS_GROUPING; siter = lys_parent(siter)); |
| 1074 | if (siter) { |
| 1075 | /* just a flag - do not resolve */ |
| 1076 | tpdftype = 1; |
| 1077 | } |
| 1078 | } |
| 1079 | |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1080 | if (typ->type->info.lref.path) { |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1081 | value = typ->type->info.lref.path; |
| 1082 | /* store in the JSON format */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1083 | typ->type->info.lref.path = transform_schema2json(module, value); |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1084 | lydict_remove(module->ctx, value); |
| 1085 | if (!typ->type->info.lref.path) { |
| 1086 | goto error; |
| 1087 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1088 | /* try to resolve leafref path only when this is instantiated |
| 1089 | * leaf, so it is not: |
| 1090 | * - typedef's type, |
| 1091 | * - in grouping definition, |
| 1092 | * - just instantiated in a grouping definition, |
| 1093 | * because in those cases the nodes referenced in path might not be present |
| 1094 | * and it is not a bug. */ |
| 1095 | if (!tpdftype && unres_schema_add_node(module, unres, typ->type, UNRES_TYPE_LEAFREF, parent) == -1) { |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1096 | goto error; |
| 1097 | } |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1098 | } else if (!typ->type->der->type.der) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1099 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type"); |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1100 | goto error; |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 1101 | } else { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1102 | /* copy leafref definition into the derived type */ |
| 1103 | typ->type->info.lref.path = lydict_insert(module->ctx, typ->type->der->type.info.lref.path, 0); |
| 1104 | /* and resolve the path at the place we are (if not in grouping/typedef) */ |
| 1105 | if (!tpdftype && unres_schema_add_node(module, unres, typ->type, UNRES_TYPE_LEAFREF, parent) == -1) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 1106 | goto error; |
| 1107 | } |
Radek Krejci | 742be35 | 2016-07-17 12:18:54 +0200 | [diff] [blame] | 1108 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 1109 | /* add pointer to leafref target, only on leaves (not in typedefs) */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1110 | if (typ->type->info.lref.target && lys_leaf_add_leafref_target(typ->type->info.lref.target, (struct lys_node *)typ->type->parent)) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 1111 | goto error; |
| 1112 | } |
Pavol Vican | 191613a | 2016-02-26 16:21:32 +0100 | [diff] [blame] | 1113 | } |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 1114 | } else { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 1115 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 1116 | goto error; |
| 1117 | } |
| 1118 | break; |
| 1119 | case LY_TYPE_IDENT: |
| 1120 | if (typ->type->der->type.der) { |
| 1121 | /* this is just a derived type with no base specified/required */ |
| 1122 | break; |
| 1123 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1124 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type"); |
Pavol Vican | 1ff0e22 | 2016-02-26 12:27:01 +0100 | [diff] [blame] | 1125 | goto error; |
| 1126 | } |
| 1127 | break; |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1128 | case LY_TYPE_UNION: |
| 1129 | if (typ->type->base != LY_TYPE_UNION) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 1130 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name); |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1131 | goto error; |
| 1132 | } |
| 1133 | if (!typ->type->info.uni.types) { |
| 1134 | if (typ->type->der->type.der) { |
| 1135 | /* this is just a derived type with no additional type specified/required */ |
| 1136 | break; |
| 1137 | } |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1138 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type"); |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1139 | goto error; |
| 1140 | } |
| 1141 | for (i = 0; i < typ->type->info.uni.count; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1142 | if (unres_schema_add_node(module, unres, &typ->type->info.uni.types[i], |
Michal Vasko | 5d63140 | 2016-07-21 13:15:15 +0200 | [diff] [blame] | 1143 | tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) { |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1144 | goto error; |
| 1145 | } |
| 1146 | if (typ->type->info.uni.types[i].base == LY_TYPE_EMPTY) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1147 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", typ->name); |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1148 | goto error; |
| 1149 | } else if (typ->type->info.uni.types[i].base == LY_TYPE_LEAFREF) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1150 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", typ->name); |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1151 | goto error; |
| 1152 | } |
| 1153 | } |
| 1154 | break; |
Pavol Vican | a182796 | 2016-02-29 15:39:42 +0100 | [diff] [blame] | 1155 | |
| 1156 | default: |
| 1157 | if (base >= LY_TYPE_BINARY && base <= LY_TYPE_UINT64) { |
| 1158 | if (typ->type->base != base) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 1159 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name); |
Pavol Vican | a182796 | 2016-02-29 15:39:42 +0100 | [diff] [blame] | 1160 | goto error; |
| 1161 | } |
| 1162 | } else { |
| 1163 | LOGINT; |
| 1164 | goto error; |
| 1165 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1166 | } |
| 1167 | return EXIT_SUCCESS; |
| 1168 | |
| 1169 | error: |
| 1170 | if (typ->type->module_name) { |
| 1171 | lydict_remove(module->ctx, typ->type->module_name); |
| 1172 | typ->type->module_name = NULL; |
| 1173 | } |
Pavol Vican | 8bd72e4 | 2016-08-29 09:53:05 +0200 | [diff] [blame] | 1174 | if (base) { |
| 1175 | typ->type->base = base; |
| 1176 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1177 | return ret; |
| 1178 | } |
| 1179 | |
| 1180 | void * |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1181 | yang_read_type(struct lys_module *module, void *parent, char *value, enum yytokentype type) |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1182 | { |
| 1183 | struct yang_type *typ; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1184 | struct type_deviation *dev; |
| 1185 | struct lys_tpdf *tmp_parent; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1186 | |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 1187 | typ = calloc(1, sizeof *typ); |
| 1188 | if (!typ) { |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1189 | LOGMEM; |
| 1190 | return NULL; |
| 1191 | } |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1192 | |
| 1193 | typ->flags = LY_YANG_STRUCTURE_FLAG; |
| 1194 | switch (type) { |
| 1195 | case LEAF_KEYWORD: |
| 1196 | ((struct lys_node_leaf *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1197 | ((struct lys_node_leaf *)parent)->type.parent = (struct lys_tpdf *)parent; |
| 1198 | typ->type = &((struct lys_node_leaf *)parent)->type; |
| 1199 | break; |
Pavol Vican | a55992a | 2016-03-01 13:37:17 +0100 | [diff] [blame] | 1200 | case LEAF_LIST_KEYWORD: |
| 1201 | ((struct lys_node_leaflist *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1202 | ((struct lys_node_leaflist *)parent)->type.parent = (struct lys_tpdf *)parent; |
| 1203 | typ->type = &((struct lys_node_leaflist *)parent)->type; |
| 1204 | break; |
Pavol Vican | a4f045a | 2016-02-29 15:01:20 +0100 | [diff] [blame] | 1205 | case UNION_KEYWORD: |
| 1206 | ((struct lys_type *)parent)->der = (struct lys_tpdf *)typ; |
| 1207 | typ->type = (struct lys_type *)parent; |
| 1208 | break; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1209 | case TYPEDEF_KEYWORD: |
| 1210 | ((struct lys_tpdf *)parent)->type.der = (struct lys_tpdf *)typ; |
| 1211 | typ->type = &((struct lys_tpdf *)parent)->type; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1212 | break; |
| 1213 | case REPLACE_KEYWORD: |
| 1214 | /* deviation replace type*/ |
| 1215 | dev = (struct type_deviation *)parent; |
| 1216 | if (dev->deviate->type) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1217 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "deviation"); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1218 | goto error; |
| 1219 | } |
| 1220 | /* check target node type */ |
| 1221 | if (dev->target->nodetype == LYS_LEAF) { |
| 1222 | typ->type = &((struct lys_node_leaf *)dev->target)->type; |
| 1223 | } else if (dev->target->nodetype == LYS_LEAFLIST) { |
| 1224 | typ->type = &((struct lys_node_leaflist *)dev->target)->type; |
| 1225 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1226 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "type"); |
| 1227 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"type\" property."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1228 | goto error; |
| 1229 | } |
| 1230 | |
| 1231 | /* remove type and initialize it */ |
| 1232 | lys_type_free(module->ctx, typ->type); |
| 1233 | tmp_parent = typ->type->parent; |
| 1234 | memset(typ->type, 0, sizeof *typ->type); |
| 1235 | typ->type->parent = tmp_parent; |
| 1236 | |
| 1237 | /* replace it with the value specified in deviation */ |
| 1238 | /* HACK for unres */ |
| 1239 | typ->type->der = (struct lys_tpdf *)typ; |
| 1240 | dev->deviate->type = typ->type; |
| 1241 | break; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1242 | default: |
| 1243 | goto error; |
| 1244 | break; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1245 | } |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1246 | typ->name = lydict_insert_zc(module->ctx, value); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1247 | return typ; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1248 | |
| 1249 | error: |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 1250 | free(value); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1251 | free(typ); |
| 1252 | return NULL; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1253 | } |
| 1254 | |
Pavol Vican | be9e383 | 2016-04-28 02:21:37 +0200 | [diff] [blame] | 1255 | void |
| 1256 | yang_delete_type(struct lys_module *module, struct yang_type *stype) |
| 1257 | { |
| 1258 | int i; |
| 1259 | |
| 1260 | stype->type->base = stype->base; |
| 1261 | stype->type->der = NULL; |
| 1262 | lydict_remove(module->ctx, stype->name); |
| 1263 | if (stype->base == LY_TYPE_UNION) { |
| 1264 | for (i = 0; i < stype->type->info.uni.count; i++) { |
| 1265 | if (stype->type->info.uni.types[i].der) { |
| 1266 | yang_delete_type(module, (struct yang_type *)stype->type->info.uni.types[i].der); |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | free(stype); |
| 1271 | } |
| 1272 | |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1273 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1274 | yang_read_length(struct lys_module *module, struct yang_type *typ, char *value) |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1275 | { |
| 1276 | struct lys_restr **length; |
| 1277 | |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1278 | if (typ->base == 0 || typ->base == LY_TYPE_STRING) { |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1279 | length = &typ->type->info.str.length; |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1280 | typ->base = LY_TYPE_STRING; |
| 1281 | } else if (typ->base == LY_TYPE_BINARY) { |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1282 | length = &typ->type->info.binary.length; |
| 1283 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1284 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected length statement."); |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1285 | goto error; |
| 1286 | } |
| 1287 | |
| 1288 | if (*length) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1289 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "length", "type"); |
Pavol Vican | be9e383 | 2016-04-28 02:21:37 +0200 | [diff] [blame] | 1290 | goto error; |
Pavol Vican | 73e7c99 | 2016-02-24 12:18:05 +0100 | [diff] [blame] | 1291 | } |
| 1292 | *length = calloc(1, sizeof **length); |
| 1293 | if (!*length) { |
| 1294 | LOGMEM; |
| 1295 | goto error; |
| 1296 | } |
| 1297 | (*length)->expr = lydict_insert_zc(module->ctx, value); |
| 1298 | return *length; |
| 1299 | |
| 1300 | error: |
| 1301 | free(value); |
| 1302 | return NULL; |
| 1303 | |
| 1304 | } |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1305 | |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1306 | int |
| 1307 | yang_read_pattern(struct lys_module *module, struct lys_restr *pattern, char *value, char modifier) |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1308 | { |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1309 | char *buf; |
| 1310 | size_t len; |
| 1311 | |
Michal Vasko | 0aee5c1 | 2016-06-17 14:27:26 +0200 | [diff] [blame] | 1312 | if (lyp_check_pattern(value, NULL)) { |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1313 | free(value); |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1314 | return EXIT_FAILURE; |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1315 | } |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1316 | |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1317 | len = strlen(value); |
| 1318 | buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */ |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1319 | |
| 1320 | if (!buf) { |
| 1321 | LOGMEM; |
| 1322 | free(value); |
| 1323 | return EXIT_FAILURE; |
| 1324 | } |
| 1325 | |
| 1326 | buf[0] = modifier; |
Radek Krejci | 0d23e7a | 2016-08-04 12:46:17 +0200 | [diff] [blame] | 1327 | strcpy(&buf[1], value); |
| 1328 | free(value); |
| 1329 | |
Pavol Vican | 6eecf30 | 2016-08-10 11:09:05 +0200 | [diff] [blame] | 1330 | pattern->expr = lydict_insert_zc(module->ctx, buf); |
| 1331 | return EXIT_SUCCESS; |
Pavol Vican | 1c203db | 2016-02-24 14:05:23 +0100 | [diff] [blame] | 1332 | } |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1333 | |
| 1334 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1335 | yang_read_range(struct lys_module *module, struct yang_type *typ, char *value) |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1336 | { |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1337 | if (typ->base != 0 && typ->base != LY_TYPE_DEC64) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1338 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected range statement."); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1339 | goto error; |
| 1340 | } |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1341 | typ->base = LY_TYPE_DEC64; |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1342 | if (typ->type->info.dec64.range) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1343 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "range", "type"); |
Pavol Vican | aff5c80 | 2016-02-24 15:56:45 +0100 | [diff] [blame] | 1344 | goto error; |
| 1345 | } |
| 1346 | typ->type->info.dec64.range = calloc(1, sizeof *typ->type->info.dec64.range); |
| 1347 | if (!typ->type->info.dec64.range) { |
| 1348 | LOGMEM; |
| 1349 | goto error; |
| 1350 | } |
| 1351 | typ->type->info.dec64.range->expr = lydict_insert_zc(module->ctx, value); |
| 1352 | return typ->type->info.dec64.range; |
| 1353 | |
| 1354 | error: |
| 1355 | free(value); |
| 1356 | return NULL; |
| 1357 | } |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1358 | |
| 1359 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1360 | yang_read_fraction(struct yang_type *typ, uint32_t value) |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1361 | { |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 1362 | unsigned int i; |
| 1363 | |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 1364 | if (typ->base == 0 || typ->base == LY_TYPE_DEC64) { |
| 1365 | typ->base = LY_TYPE_DEC64; |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1366 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1367 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected fraction-digits statement."); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1368 | goto error; |
| 1369 | } |
| 1370 | if (typ->type->info.dec64.dig) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1371 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "fraction-digits", "type"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1372 | goto error; |
| 1373 | } |
| 1374 | /* range check */ |
| 1375 | if (value < 1 || value > 18) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1376 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", value, "fraction-digits"); |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1377 | goto error; |
| 1378 | } |
| 1379 | typ->type->info.dec64.dig = value; |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 1380 | typ->type->info.dec64.div = 10; |
| 1381 | for (i = 1; i < value; i++) { |
| 1382 | typ->type->info.dec64.div *= 10; |
| 1383 | } |
Pavol Vican | 07ea68d | 2016-02-25 12:01:37 +0100 | [diff] [blame] | 1384 | return EXIT_SUCCESS; |
| 1385 | |
| 1386 | error: |
| 1387 | return EXIT_FAILURE; |
| 1388 | } |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1389 | |
| 1390 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1391 | yang_read_enum(struct lys_module *module, struct yang_type *typ, char *value) |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1392 | { |
| 1393 | struct lys_type_enum *enm; |
| 1394 | int i; |
| 1395 | |
Pavol Vican | c666241 | 2016-08-30 08:06:28 +0200 | [diff] [blame] | 1396 | if (!value[0]) { |
| 1397 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum name"); |
| 1398 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Enum name must not be empty."); |
| 1399 | free(value); |
| 1400 | goto error; |
| 1401 | } |
| 1402 | |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1403 | enm = &typ->type->info.enums.enm[typ->type->info.enums.count]; |
| 1404 | enm->name = lydict_insert_zc(module->ctx, value); |
| 1405 | |
| 1406 | /* the assigned name MUST NOT have any leading or trailing whitespace characters */ |
| 1407 | if (isspace(enm->name[0]) || isspace(enm->name[strlen(enm->name) - 1])) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1408 | LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, enm->name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1409 | goto error; |
| 1410 | } |
| 1411 | |
| 1412 | /* check the name uniqueness */ |
| 1413 | for (i = 0; i < typ->type->info.enums.count; i++) { |
| 1414 | if (!strcmp(typ->type->info.enums.enm[i].name, typ->type->info.enums.enm[typ->type->info.enums.count].name)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1415 | LOGVAL(LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1416 | goto error; |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | typ->type->info.enums.count++; |
| 1421 | return enm; |
| 1422 | |
| 1423 | error: |
| 1424 | typ->type->info.enums.count++; |
| 1425 | return NULL; |
| 1426 | } |
| 1427 | |
| 1428 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1429 | yang_check_enum(struct yang_type *typ, struct lys_type_enum *enm, int64_t *value, int assign) |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1430 | { |
| 1431 | int i, j; |
| 1432 | |
| 1433 | if (!assign) { |
| 1434 | /* assign value automatically */ |
| 1435 | if (*value > INT32_MAX) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1436 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value"); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1437 | goto error; |
| 1438 | } |
| 1439 | enm->value = *value; |
| 1440 | enm->flags |= LYS_AUTOASSIGNED; |
| 1441 | (*value)++; |
| 1442 | } |
| 1443 | |
| 1444 | /* check that the value is unique */ |
| 1445 | j = typ->type->info.enums.count-1; |
| 1446 | for (i = 0; i < j; i++) { |
| 1447 | if (typ->type->info.enums.enm[i].value == typ->type->info.enums.enm[j].value) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1448 | LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL, |
Radek Krejci | e663e01 | 2016-08-01 17:12:34 +0200 | [diff] [blame] | 1449 | typ->type->info.enums.enm[j].value, typ->type->info.enums.enm[j].name, |
| 1450 | typ->type->info.enums.enm[i].name); |
Pavol Vican | 79a763d | 2016-02-25 15:41:27 +0100 | [diff] [blame] | 1451 | goto error; |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | return EXIT_SUCCESS; |
| 1456 | |
| 1457 | error: |
| 1458 | return EXIT_FAILURE; |
| 1459 | } |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1460 | |
| 1461 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1462 | yang_read_bit(struct lys_module *module, struct yang_type *typ, char *value) |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1463 | { |
| 1464 | int i; |
| 1465 | struct lys_type_bit *bit; |
| 1466 | |
| 1467 | bit = &typ->type->info.bits.bit[typ->type->info.bits.count]; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1468 | if (lyp_check_identifier(value, LY_IDENT_SIMPLE, NULL, NULL)) { |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1469 | free(value); |
| 1470 | goto error; |
| 1471 | } |
| 1472 | bit->name = lydict_insert_zc(module->ctx, value); |
| 1473 | |
| 1474 | /* check the name uniqueness */ |
| 1475 | for (i = 0; i < typ->type->info.bits.count; i++) { |
| 1476 | if (!strcmp(typ->type->info.bits.bit[i].name, bit->name)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1477 | LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, bit->name); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1478 | typ->type->info.bits.count++; |
| 1479 | goto error; |
| 1480 | } |
| 1481 | } |
| 1482 | typ->type->info.bits.count++; |
| 1483 | return bit; |
| 1484 | |
| 1485 | error: |
| 1486 | return NULL; |
| 1487 | } |
| 1488 | |
| 1489 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1490 | yang_check_bit(struct yang_type *typ, struct lys_type_bit *bit, int64_t *value, int assign) |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1491 | { |
| 1492 | int i,j; |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1493 | |
| 1494 | if (!assign) { |
| 1495 | /* assign value automatically */ |
| 1496 | if (*value > UINT32_MAX) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1497 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position"); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1498 | goto error; |
| 1499 | } |
| 1500 | bit->pos = (uint32_t)*value; |
| 1501 | bit->flags |= LYS_AUTOASSIGNED; |
| 1502 | (*value)++; |
| 1503 | } |
| 1504 | |
| 1505 | j = typ->type->info.bits.count - 1; |
| 1506 | /* check that the value is unique */ |
| 1507 | for (i = 0; i < j; i++) { |
| 1508 | if (typ->type->info.bits.bit[i].pos == bit->pos) { |
Radek Krejci | e663e01 | 2016-08-01 17:12:34 +0200 | [diff] [blame] | 1509 | LOGVAL(LYE_BITS_DUPVAL, LY_VLOG_NONE, NULL, bit->pos, bit->name, typ->type->info.bits.bit[i].name); |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1510 | goto error; |
| 1511 | } |
| 1512 | } |
| 1513 | |
Pavol Vican | 9887c68 | 2016-02-29 11:32:01 +0100 | [diff] [blame] | 1514 | return EXIT_SUCCESS; |
| 1515 | |
| 1516 | error: |
| 1517 | return EXIT_FAILURE; |
| 1518 | } |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1519 | |
| 1520 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1521 | yang_read_typedef(struct lys_module *module, struct lys_node *parent, char *value) |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1522 | { |
| 1523 | struct lys_tpdf *ret; |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 1524 | struct lys_node *root; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1525 | |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 1526 | root = (parent) ? NULL : lys_main_module(module)->data; |
| 1527 | if (lyp_check_identifier(value, LY_IDENT_TYPE, module, parent) || yang_check_typedef_identif(root, parent, value)) { |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1528 | free(value); |
| 1529 | return NULL; |
| 1530 | } |
Pavol Vican | 810892e | 2016-07-12 16:55:34 +0200 | [diff] [blame] | 1531 | |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1532 | if (!parent) { |
| 1533 | ret = &module->tpdf[module->tpdf_size]; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1534 | module->tpdf_size++; |
Pavol Vican | 21238f5 | 2016-03-01 12:39:52 +0100 | [diff] [blame] | 1535 | } else { |
| 1536 | switch (parent->nodetype) { |
| 1537 | case LYS_GROUPING: |
| 1538 | ret = &((struct lys_node_grp *)parent)->tpdf[((struct lys_node_grp *)parent)->tpdf_size]; |
Pavol Vican | 21238f5 | 2016-03-01 12:39:52 +0100 | [diff] [blame] | 1539 | ((struct lys_node_grp *)parent)->tpdf_size++; |
| 1540 | break; |
Pavol Vican | 535d50e | 2016-03-01 13:05:33 +0100 | [diff] [blame] | 1541 | case LYS_CONTAINER: |
| 1542 | ret = &((struct lys_node_container *)parent)->tpdf[((struct lys_node_container *)parent)->tpdf_size]; |
| 1543 | ((struct lys_node_container *)parent)->tpdf_size++; |
| 1544 | break; |
Pavol Vican | 09f04b8 | 2016-03-01 14:02:28 +0100 | [diff] [blame] | 1545 | case LYS_LIST: |
| 1546 | ret = &((struct lys_node_list *)parent)->tpdf[((struct lys_node_list *)parent)->tpdf_size]; |
| 1547 | ((struct lys_node_list *)parent)->tpdf_size++; |
| 1548 | break; |
Pavol Vican | 52ed67d | 2016-03-02 17:46:15 +0100 | [diff] [blame] | 1549 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1550 | case LYS_ACTION: |
| 1551 | ret = &((struct lys_node_rpc_action *)parent)->tpdf[((struct lys_node_rpc_action *)parent)->tpdf_size]; |
| 1552 | ((struct lys_node_rpc_action *)parent)->tpdf_size++; |
Pavol Vican | 52ed67d | 2016-03-02 17:46:15 +0100 | [diff] [blame] | 1553 | break; |
Pavol Vican | 531a913 | 2016-03-03 10:10:09 +0100 | [diff] [blame] | 1554 | case LYS_INPUT: |
| 1555 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1556 | ret = &((struct lys_node_inout *)parent)->tpdf[((struct lys_node_inout *)parent)->tpdf_size]; |
| 1557 | ((struct lys_node_inout *)parent)->tpdf_size++; |
Pavol Vican | 531a913 | 2016-03-03 10:10:09 +0100 | [diff] [blame] | 1558 | break; |
Pavol Vican | 41267fd | 2016-03-03 10:47:42 +0100 | [diff] [blame] | 1559 | case LYS_NOTIF: |
| 1560 | ret = &((struct lys_node_notif *)parent)->tpdf[((struct lys_node_notif *)parent)->tpdf_size]; |
| 1561 | ((struct lys_node_notif *)parent)->tpdf_size++; |
| 1562 | break; |
Pavol Vican | d1dbfda | 2016-03-21 10:03:58 +0100 | [diff] [blame] | 1563 | default: |
| 1564 | /* another type of nodetype is error*/ |
| 1565 | LOGINT; |
| 1566 | free(value); |
| 1567 | return NULL; |
Pavol Vican | 21238f5 | 2016-03-01 12:39:52 +0100 | [diff] [blame] | 1568 | } |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
Pavol Vican | 7943647 | 2016-04-04 11:22:02 +0200 | [diff] [blame] | 1571 | ret->type.parent = ret; |
Pavol Vican | 0df02b0 | 2016-03-01 10:28:50 +0100 | [diff] [blame] | 1572 | ret->name = lydict_insert_zc(module->ctx, value); |
| 1573 | ret->module = module; |
| 1574 | return ret; |
| 1575 | } |
Pavol Vican | 1003ead | 2016-03-02 12:24:52 +0100 | [diff] [blame] | 1576 | |
| 1577 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1578 | yang_read_refine(struct lys_module *module, struct lys_node_uses *uses, char *value) |
Pavol Vican | 1003ead | 2016-03-02 12:24:52 +0100 | [diff] [blame] | 1579 | { |
| 1580 | struct lys_refine *rfn; |
| 1581 | |
| 1582 | rfn = &uses->refine[uses->refine_size]; |
| 1583 | uses->refine_size++; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1584 | rfn->target_name = transform_schema2json(module, value); |
Pavol Vican | 1003ead | 2016-03-02 12:24:52 +0100 | [diff] [blame] | 1585 | free(value); |
| 1586 | if (!rfn->target_name) { |
| 1587 | return NULL; |
| 1588 | } |
| 1589 | return rfn; |
| 1590 | } |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1591 | |
| 1592 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1593 | yang_read_augment(struct lys_module *module, struct lys_node *parent, char *value) |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1594 | { |
| 1595 | struct lys_node_augment *aug; |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1596 | |
| 1597 | if (parent) { |
| 1598 | aug = &((struct lys_node_uses *)parent)->augment[((struct lys_node_uses *)parent)->augment_size]; |
| 1599 | } else { |
| 1600 | aug = &module->augment[module->augment_size]; |
| 1601 | } |
| 1602 | aug->nodetype = LYS_AUGMENT; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1603 | aug->target_name = transform_schema2json(module, value); |
Pavol Vican | 92fa0dc | 2016-03-02 14:20:39 +0100 | [diff] [blame] | 1604 | free(value); |
| 1605 | if (!aug->target_name) { |
| 1606 | return NULL; |
| 1607 | } |
| 1608 | aug->parent = parent; |
| 1609 | aug->module = module; |
| 1610 | if (parent) { |
| 1611 | ((struct lys_node_uses *)parent)->augment_size++; |
| 1612 | } else { |
| 1613 | module->augment_size++; |
| 1614 | } |
| 1615 | return aug; |
| 1616 | } |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1617 | |
| 1618 | void * |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1619 | yang_read_deviation(struct lys_module *module, char *value) |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1620 | { |
| 1621 | struct lys_node *dev_target = NULL; |
| 1622 | struct lys_deviation *dev; |
| 1623 | struct type_deviation *deviation = NULL; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1624 | int rc; |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1625 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1626 | dev = &module->deviation[module->deviation_size]; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1627 | dev->target_name = transform_schema2json(module, value); |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1628 | free(value); |
| 1629 | if (!dev->target_name) { |
| 1630 | goto error; |
| 1631 | } |
| 1632 | |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1633 | deviation = calloc(1, sizeof *deviation); |
| 1634 | if (!deviation) { |
| 1635 | LOGMEM; |
| 1636 | goto error; |
| 1637 | } |
| 1638 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1639 | /* resolve target node */ |
| 1640 | rc = resolve_augment_schema_nodeid(dev->target_name, NULL, module, (const struct lys_node **)&dev_target); |
| 1641 | if (rc || !dev_target) { |
Michal Vasko | 75c8daf | 2016-05-19 10:56:39 +0200 | [diff] [blame] | 1642 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1643 | goto error; |
| 1644 | } |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 1645 | if (dev_target->module == lys_main_module(module)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1646 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation"); |
| 1647 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Deviating own module is not allowed."); |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1648 | goto error; |
| 1649 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1650 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 1651 | lys_node_module(dev_target)->deviated = 1; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1652 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1653 | /*save pointer to the deviation and deviated target*/ |
| 1654 | deviation->deviation = dev; |
| 1655 | deviation->target = dev_target; |
| 1656 | |
Pavol Vican | 220e5a1 | 2016-03-03 14:19:43 +0100 | [diff] [blame] | 1657 | return deviation; |
| 1658 | |
| 1659 | error: |
| 1660 | free(deviation); |
| 1661 | lydict_remove(module->ctx, dev->target_name); |
| 1662 | return NULL; |
| 1663 | } |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1664 | |
| 1665 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1666 | yang_read_deviate_unsupported(struct type_deviation *dev) |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1667 | { |
| 1668 | int i; |
| 1669 | |
| 1670 | if (dev->deviation->deviate_size) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1671 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation."); |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1672 | return EXIT_FAILURE; |
| 1673 | } |
| 1674 | dev->deviation->deviate[dev->deviation->deviate_size].mod = LY_DEVIATE_NO; |
| 1675 | |
| 1676 | /* you cannot remove a key leaf */ |
| 1677 | if ((dev->target->nodetype == LYS_LEAF) && dev->target->parent && (dev->target->parent->nodetype == LYS_LIST)) { |
| 1678 | for (i = 0; i < ((struct lys_node_list *)dev->target->parent)->keys_size; ++i) { |
| 1679 | if (((struct lys_node_list *)dev->target->parent)->keys[i] == (struct lys_node_leaf *)dev->target) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1680 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "not-supported", "deviation"); |
| 1681 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot remove a list key."); |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1682 | return EXIT_FAILURE; |
| 1683 | } |
| 1684 | } |
| 1685 | } |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 1686 | |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1687 | /* unlink and store the original node */ |
Michal Vasko | d921d68 | 2016-05-19 10:56:51 +0200 | [diff] [blame] | 1688 | lys_node_unlink(dev->target); |
Pavol Vican | 4c90c64 | 2016-03-03 15:06:47 +0100 | [diff] [blame] | 1689 | dev->deviation->orig_node = dev->target; |
| 1690 | |
| 1691 | dev->deviation->deviate_size = 1; |
| 1692 | return EXIT_SUCCESS; |
| 1693 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1694 | |
| 1695 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1696 | yang_read_deviate(struct type_deviation *dev, LYS_DEVIATE_TYPE mod) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1697 | { |
| 1698 | struct unres_schema tmp_unres; |
| 1699 | |
| 1700 | dev->deviation->deviate[dev->deviation->deviate_size].mod = mod; |
| 1701 | dev->deviate = &dev->deviation->deviate[dev->deviation->deviate_size]; |
| 1702 | dev->deviation->deviate_size++; |
| 1703 | if (dev->deviation->deviate[0].mod == LY_DEVIATE_NO) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1704 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "not-supported"); |
| 1705 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1706 | return EXIT_FAILURE; |
| 1707 | } |
| 1708 | |
| 1709 | /* store a shallow copy of the original node */ |
| 1710 | if (!dev->deviation->orig_node) { |
| 1711 | memset(&tmp_unres, 0, sizeof tmp_unres); |
| 1712 | dev->deviation->orig_node = lys_node_dup(dev->target->module, NULL, dev->target, 0, 0, &tmp_unres, 1); |
| 1713 | /* just to be safe */ |
| 1714 | if (tmp_unres.count) { |
| 1715 | LOGINT; |
| 1716 | return EXIT_FAILURE; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | return EXIT_SUCCESS; |
| 1721 | } |
| 1722 | |
| 1723 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1724 | yang_read_deviate_units(struct ly_ctx *ctx, struct type_deviation *dev, char *value) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1725 | { |
| 1726 | const char **stritem; |
| 1727 | |
| 1728 | if (dev->deviate->units) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1729 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "units", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1730 | free(value); |
| 1731 | goto error; |
| 1732 | } |
| 1733 | |
| 1734 | /* check target node type */ |
| 1735 | if (dev->target->nodetype == LYS_LEAFLIST) { |
| 1736 | stritem = &((struct lys_node_leaflist *)dev->target)->units; |
| 1737 | } else if (dev->target->nodetype == LYS_LEAF) { |
| 1738 | stritem = &((struct lys_node_leaf *)dev->target)->units; |
| 1739 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1740 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1741 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"units\" property."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1742 | free(value); |
| 1743 | goto error; |
| 1744 | } |
| 1745 | |
| 1746 | dev->deviate->units = lydict_insert_zc(ctx, value); |
| 1747 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1748 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 1749 | /* check values */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 1750 | if (!ly_strequal(*stritem, dev->deviate->units, 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1751 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->units, "units"); |
| 1752 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1753 | goto error; |
| 1754 | } |
| 1755 | /* remove current units value of the target */ |
| 1756 | lydict_remove(ctx, *stritem); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1757 | } else { |
| 1758 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1759 | /* check that there is no current value */ |
| 1760 | if (*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1761 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1762 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1763 | goto error; |
| 1764 | } |
| 1765 | } else { /* replace */ |
| 1766 | if (!*stritem) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1767 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units"); |
| 1768 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1769 | goto error; |
| 1770 | } |
| 1771 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1772 | /* remove current units value of the target ... */ |
| 1773 | lydict_remove(ctx, *stritem); |
| 1774 | |
| 1775 | /* ... and replace it with the value specified in deviation */ |
| 1776 | *stritem = lydict_insert(ctx, dev->deviate->units, 0); |
| 1777 | } |
| 1778 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1779 | return EXIT_SUCCESS; |
| 1780 | |
| 1781 | error: |
| 1782 | return EXIT_FAILURE; |
| 1783 | } |
| 1784 | |
| 1785 | int |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1786 | yang_read_deviate_must(struct type_deviation *dev, uint8_t c_must) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1787 | { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1788 | /* check target node type */ |
| 1789 | switch (dev->target->nodetype) { |
| 1790 | case LYS_LEAF: |
| 1791 | dev->trg_must = &((struct lys_node_leaf *)dev->target)->must; |
| 1792 | dev->trg_must_size = &((struct lys_node_leaf *)dev->target)->must_size; |
| 1793 | break; |
| 1794 | case LYS_CONTAINER: |
| 1795 | dev->trg_must = &((struct lys_node_container *)dev->target)->must; |
| 1796 | dev->trg_must_size = &((struct lys_node_container *)dev->target)->must_size; |
| 1797 | break; |
| 1798 | case LYS_LEAFLIST: |
| 1799 | dev->trg_must = &((struct lys_node_leaflist *)dev->target)->must; |
| 1800 | dev->trg_must_size = &((struct lys_node_leaflist *)dev->target)->must_size; |
| 1801 | break; |
| 1802 | case LYS_LIST: |
| 1803 | dev->trg_must = &((struct lys_node_list *)dev->target)->must; |
| 1804 | dev->trg_must_size = &((struct lys_node_list *)dev->target)->must_size; |
| 1805 | break; |
| 1806 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1807 | case LYS_ANYDATA: |
| 1808 | dev->trg_must = &((struct lys_node_anydata *)dev->target)->must; |
| 1809 | dev->trg_must_size = &((struct lys_node_anydata *)dev->target)->must_size; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1810 | break; |
| 1811 | default: |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1812 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must"); |
| 1813 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1814 | goto error; |
| 1815 | } |
| 1816 | |
| 1817 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1818 | /* reallocate the must array of the target */ |
| 1819 | dev->deviate->must = ly_realloc(*dev->trg_must, (c_must + *dev->trg_must_size) * sizeof *dev->deviate->must); |
| 1820 | if (!dev->deviate->must) { |
| 1821 | LOGMEM; |
| 1822 | goto error; |
| 1823 | } |
| 1824 | *dev->trg_must = dev->deviate->must; |
| 1825 | dev->deviate->must = &((*dev->trg_must)[*dev->trg_must_size]); |
| 1826 | dev->deviate->must_size = c_must; |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1827 | } else { |
| 1828 | /* LY_DEVIATE_DEL */ |
| 1829 | dev->deviate->must = calloc(c_must, sizeof *dev->deviate->must); |
| 1830 | if (!dev->deviate->must) { |
| 1831 | LOGMEM; |
| 1832 | goto error; |
| 1833 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | return EXIT_SUCCESS; |
| 1837 | |
| 1838 | error: |
| 1839 | return EXIT_FAILURE; |
| 1840 | } |
| 1841 | |
| 1842 | int |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 1843 | yang_read_deviate_unique(struct type_deviation *dev, uint8_t c_uniq) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1844 | { |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1845 | struct lys_node_list *list; |
| 1846 | |
| 1847 | /* check target node type */ |
| 1848 | if (dev->target->nodetype != LYS_LIST) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1849 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique"); |
| 1850 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"unique\" property."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1851 | goto error; |
| 1852 | } |
| 1853 | |
| 1854 | list = (struct lys_node_list *)dev->target; |
| 1855 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1856 | /* reallocate the unique array of the target */ |
| 1857 | dev->deviate->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *dev->deviate->unique); |
| 1858 | if (!dev->deviate->unique) { |
| 1859 | LOGMEM; |
| 1860 | goto error; |
| 1861 | } |
| 1862 | list->unique = dev->deviate->unique; |
| 1863 | dev->deviate->unique = &list->unique[list->unique_size]; |
| 1864 | dev->deviate->unique_size = c_uniq; |
| 1865 | memset(dev->deviate->unique, 0, c_uniq * sizeof *dev->deviate->unique); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1866 | } else { |
| 1867 | /* LY_DEVIATE_DEL */ |
| 1868 | dev->deviate->unique = calloc(c_uniq, sizeof *dev->deviate->unique); |
| 1869 | if (!dev->deviate->unique) { |
| 1870 | LOGMEM; |
| 1871 | goto error; |
| 1872 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | return EXIT_SUCCESS; |
| 1876 | |
| 1877 | error: |
| 1878 | return EXIT_FAILURE; |
| 1879 | } |
| 1880 | |
| 1881 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1882 | yang_read_deviate_default(struct ly_ctx *ctx, struct type_deviation *dev, char *value) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1883 | { |
| 1884 | int rc; |
| 1885 | struct lys_node_choice *choice; |
| 1886 | struct lys_node_leaf *leaf; |
| 1887 | struct lys_node *node; |
| 1888 | |
| 1889 | if (dev->deviate->dflt) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1890 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "default", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1891 | free(value); |
| 1892 | goto error; |
| 1893 | } |
| 1894 | |
| 1895 | dev->deviate->dflt = lydict_insert_zc(ctx, value); |
| 1896 | |
| 1897 | if (dev->target->nodetype == LYS_CHOICE) { |
| 1898 | choice = (struct lys_node_choice *)dev->target; |
| 1899 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1900 | rc = resolve_choice_default_schema_nodeid(dev->deviate->dflt, choice->child, (const struct lys_node **)&node); |
| 1901 | if (rc || !node) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1902 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1903 | goto error; |
| 1904 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1905 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
| 1906 | if (!choice->dflt || (choice->dflt != node)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1907 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default"); |
| 1908 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1909 | goto error; |
| 1910 | } |
| 1911 | choice->dflt = NULL; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1912 | } else { |
| 1913 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1914 | /* check that there is no current value */ |
| 1915 | if (choice->dflt) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1916 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1917 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1918 | goto error; |
Pavol Vican | 321a02e | 2016-04-05 16:11:59 +0200 | [diff] [blame] | 1919 | } else if (choice->flags & LYS_MAND_TRUE) { |
| 1920 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "choice"); |
| 1921 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"default\" statement is forbidden on choices with \"mandatory\"."); |
| 1922 | goto error; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1923 | } |
| 1924 | } else { /* replace*/ |
| 1925 | if (!choice->dflt) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1926 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1927 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1928 | goto error; |
| 1929 | } |
| 1930 | } |
| 1931 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1932 | choice->dflt = node; |
| 1933 | if (!choice->dflt) { |
| 1934 | /* default branch not found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1935 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default"); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1936 | goto error; |
| 1937 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1938 | } |
| 1939 | } else if (dev->target->nodetype == LYS_LEAF) { |
| 1940 | leaf = (struct lys_node_leaf *)dev->target; |
| 1941 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1942 | if (dev->deviate->mod == LY_DEVIATE_DEL) { |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 1943 | if (!leaf->dflt || !ly_strequal(leaf->dflt, dev->deviate->dflt, 1)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1944 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default"); |
| 1945 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1946 | goto error; |
| 1947 | } |
| 1948 | /* remove value */ |
| 1949 | lydict_remove(ctx, leaf->dflt); |
| 1950 | leaf->dflt = NULL; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1951 | } else { |
| 1952 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 1953 | /* check that there is no current value */ |
| 1954 | if (leaf->dflt) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1955 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1956 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1957 | goto error; |
Pavol Vican | fdf81c4 | 2016-04-05 15:55:31 +0200 | [diff] [blame] | 1958 | } else if (leaf->flags & LYS_MAND_TRUE) { |
| 1959 | /* RFC 6020, 7.6.4 - default statement must not with mandatory true */ |
| 1960 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "leaf"); |
| 1961 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"default\" statement is forbidden on leaf with \"mandatory\"."); |
| 1962 | goto error; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1963 | } |
| 1964 | } else { /* replace*/ |
| 1965 | if (!leaf->dflt) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1966 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1967 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 1968 | goto error; |
| 1969 | } |
| 1970 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1971 | /* remove value */ |
| 1972 | lydict_remove(ctx, leaf->dflt); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1973 | |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 1974 | /* set new value */ |
| 1975 | leaf->dflt = lydict_insert(ctx, dev->deviate->dflt, 0); |
| 1976 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1977 | } else { |
| 1978 | /* invalid target for default value */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1979 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default"); |
| 1980 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1981 | goto error; |
| 1982 | } |
| 1983 | |
| 1984 | return EXIT_SUCCESS; |
| 1985 | |
| 1986 | error: |
| 1987 | return EXIT_FAILURE; |
| 1988 | } |
| 1989 | |
| 1990 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1991 | yang_read_deviate_config(struct type_deviation *dev, uint8_t value) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1992 | { |
| 1993 | if (dev->deviate->flags & LYS_CONFIG_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 1994 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "config", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 1995 | goto error; |
| 1996 | } |
| 1997 | |
| 1998 | /* for we deviate from RFC 6020 and allow config property even it is/is not |
| 1999 | * specified in the target explicitly since config property inherits. So we expect |
| 2000 | * that config is specified in every node. But for delete, we check that the value |
| 2001 | * is the same as here in deviation |
| 2002 | */ |
| 2003 | dev->deviate->flags |= value; |
| 2004 | |
| 2005 | /* add and replace are the same in this case */ |
| 2006 | /* remove current config value of the target ... */ |
| 2007 | dev->target->flags &= ~LYS_CONFIG_MASK; |
| 2008 | |
| 2009 | /* ... and replace it with the value specified in deviation */ |
| 2010 | dev->target->flags |= dev->deviate->flags & LYS_CONFIG_MASK; |
| 2011 | |
| 2012 | return EXIT_SUCCESS; |
| 2013 | |
| 2014 | error: |
| 2015 | return EXIT_FAILURE; |
| 2016 | } |
| 2017 | |
| 2018 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2019 | yang_read_deviate_mandatory(struct type_deviation *dev, uint8_t value) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2020 | { |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 2021 | struct lys_node *parent; |
| 2022 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2023 | if (dev->deviate->flags & LYS_MAND_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2024 | LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "mandatory", "deviate"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2025 | goto error; |
| 2026 | } |
| 2027 | |
| 2028 | /* check target node type */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2029 | if (!(dev->target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2030 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 2031 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"mandatory\" property."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2032 | goto error; |
| 2033 | } |
| 2034 | |
| 2035 | dev->deviate->flags |= value; |
| 2036 | |
| 2037 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 2038 | /* check that there is no current value */ |
| 2039 | if (dev->target->flags & LYS_MAND_MASK) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2040 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 2041 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2042 | goto error; |
Pavol Vican | 321a02e | 2016-04-05 16:11:59 +0200 | [diff] [blame] | 2043 | } else { |
| 2044 | if (dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) { |
| 2045 | /* RFC 6020, 7.6.4 - default statement must not with mandatory true */ |
| 2046 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "leaf"); |
| 2047 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\"."); |
| 2048 | goto error; |
| 2049 | } else if (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt) { |
| 2050 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "choice"); |
| 2051 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\"."); |
| 2052 | goto error; |
| 2053 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2054 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2055 | } else { /* replace */ |
| 2056 | if (!(dev->target->flags & LYS_MAND_MASK)) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2057 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory"); |
| 2058 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist."); |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2059 | goto error; |
| 2060 | } |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2061 | } |
| 2062 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2063 | /* remove current mandatory value of the target ... */ |
| 2064 | dev->target->flags &= ~LYS_MAND_MASK; |
| 2065 | |
| 2066 | /* ... and replace it with the value specified in deviation */ |
| 2067 | dev->target->flags |= dev->deviate->flags & LYS_MAND_MASK; |
| 2068 | |
Radek Krejci | e00d231 | 2016-08-12 15:27:49 +0200 | [diff] [blame] | 2069 | /* check for mandatory node in default case, first find the closest parent choice to the changed node */ |
| 2070 | for (parent = dev->target->parent; |
| 2071 | parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION)); |
| 2072 | parent = parent->parent) { |
| 2073 | if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) { |
| 2074 | /* stop also on presence containers */ |
| 2075 | break; |
| 2076 | } |
| 2077 | } |
| 2078 | /* and if it is a choice with the default case, check it for presence of a mandatory node in it */ |
| 2079 | if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) { |
| 2080 | if (lyp_check_mandatory_choice(parent)) { |
| 2081 | goto error; |
| 2082 | } |
| 2083 | } |
| 2084 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2085 | return EXIT_SUCCESS; |
| 2086 | |
| 2087 | error: |
| 2088 | return EXIT_FAILURE; |
| 2089 | } |
| 2090 | |
| 2091 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2092 | yang_read_deviate_minmax(struct type_deviation *dev, uint32_t value, int type) |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2093 | { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2094 | uint32_t *ui32val, *min, *max; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2095 | |
| 2096 | /* check target node type */ |
| 2097 | if (dev->target->nodetype == LYS_LEAFLIST) { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2098 | max = &((struct lys_node_leaflist *)dev->target)->max; |
| 2099 | min = &((struct lys_node_leaflist *)dev->target)->min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2100 | } else if (dev->target->nodetype == LYS_LIST) { |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2101 | max = &((struct lys_node_list *)dev->target)->max; |
| 2102 | min = &((struct lys_node_list *)dev->target)->min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2103 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2104 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 2105 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", (type) ? "max-elements" : "min-elements"); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2106 | goto error; |
| 2107 | } |
| 2108 | |
| 2109 | if (type) { |
| 2110 | dev->deviate->max = value; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2111 | dev->deviate->max_set = 1; |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2112 | ui32val = max; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2113 | } else { |
| 2114 | dev->deviate->min = value; |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2115 | dev->deviate->min_set = 1; |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2116 | ui32val = min; |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | if (dev->deviate->mod == LY_DEVIATE_ADD) { |
| 2120 | /* check that there is no current value */ |
| 2121 | if (*ui32val) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2122 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements"); |
| 2123 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists."); |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2124 | goto error; |
| 2125 | } |
Pavol Vican | 4766aca | 2016-03-07 12:42:36 +0100 | [diff] [blame] | 2126 | } else if (dev->deviate->mod == LY_DEVIATE_RPL) { |
| 2127 | /* unfortunately, there is no way to check reliably that there |
| 2128 | * was a value before, it could have been the default */ |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | /* add (already checked) and replace */ |
| 2132 | /* set new value specified in deviation */ |
| 2133 | *ui32val = value; |
| 2134 | |
Pavol Vican | 09adcc3 | 2016-08-25 10:51:36 +0200 | [diff] [blame] | 2135 | /* check min-elements is smaller than max-elements */ |
| 2136 | if (*max && *min > *max) { |
| 2137 | if (type) { |
| 2138 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"max-elements\".", value); |
| 2139 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\"."); |
| 2140 | } else { |
| 2141 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"min-elements\".", value); |
| 2142 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\"."); |
| 2143 | } |
| 2144 | goto error; |
| 2145 | } |
| 2146 | |
Pavol Vican | 85f1202 | 2016-03-05 16:30:35 +0100 | [diff] [blame] | 2147 | return EXIT_SUCCESS; |
| 2148 | |
| 2149 | error: |
| 2150 | return EXIT_FAILURE; |
| 2151 | } |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2152 | |
| 2153 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2154 | yang_check_deviate_must(struct ly_ctx *ctx, struct type_deviation *dev) |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2155 | { |
| 2156 | int i; |
| 2157 | |
| 2158 | /* find must to delete, we are ok with just matching conditions */ |
| 2159 | for (i = 0; i < *dev->trg_must_size; i++) { |
| 2160 | if (ly_strequal(dev->deviate->must[dev->deviate->must_size - 1].expr, (*dev->trg_must)[i].expr, 1)) { |
| 2161 | /* we have a match, free the must structure ... */ |
| 2162 | lys_restr_free(ctx, &((*dev->trg_must)[i])); |
| 2163 | /* ... and maintain the array */ |
| 2164 | (*dev->trg_must_size)--; |
| 2165 | if (i != *dev->trg_must_size) { |
| 2166 | (*dev->trg_must)[i].expr = (*dev->trg_must)[*dev->trg_must_size].expr; |
| 2167 | (*dev->trg_must)[i].dsc = (*dev->trg_must)[*dev->trg_must_size].dsc; |
| 2168 | (*dev->trg_must)[i].ref = (*dev->trg_must)[*dev->trg_must_size].ref; |
| 2169 | (*dev->trg_must)[i].eapptag = (*dev->trg_must)[*dev->trg_must_size].eapptag; |
| 2170 | (*dev->trg_must)[i].emsg = (*dev->trg_must)[*dev->trg_must_size].emsg; |
| 2171 | } |
| 2172 | if (!(*dev->trg_must_size)) { |
| 2173 | free(*dev->trg_must); |
| 2174 | *dev->trg_must = NULL; |
| 2175 | } else { |
| 2176 | (*dev->trg_must)[*dev->trg_must_size].expr = NULL; |
| 2177 | (*dev->trg_must)[*dev->trg_must_size].dsc = NULL; |
| 2178 | (*dev->trg_must)[*dev->trg_must_size].ref = NULL; |
| 2179 | (*dev->trg_must)[*dev->trg_must_size].eapptag = NULL; |
| 2180 | (*dev->trg_must)[*dev->trg_must_size].emsg = NULL; |
| 2181 | } |
| 2182 | |
| 2183 | i = -1; /* set match flag */ |
| 2184 | break; |
| 2185 | } |
| 2186 | } |
| 2187 | if (i != -1) { |
| 2188 | /* no match found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2189 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->must[dev->deviate->must_size - 1].expr, "must"); |
| 2190 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value does not match any must from the target."); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2191 | return EXIT_FAILURE; |
| 2192 | } |
| 2193 | |
| 2194 | return EXIT_SUCCESS; |
| 2195 | } |
| 2196 | |
| 2197 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2198 | yang_check_deviate_unique(struct lys_module *module, struct type_deviation *dev, char *value) |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2199 | { |
| 2200 | struct lys_node_list *list; |
| 2201 | int i, j; |
| 2202 | |
| 2203 | list = (struct lys_node_list *)dev->target; |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2204 | if (yang_fill_unique(module, list, &dev->deviate->unique[dev->deviate->unique_size], value, NULL)) { |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2205 | dev->deviate->unique_size++; |
| 2206 | goto error; |
| 2207 | } |
| 2208 | |
| 2209 | /* find unique structures to delete */ |
| 2210 | for (i = 0; i < list->unique_size; i++) { |
| 2211 | if (list->unique[i].expr_size != dev->deviate->unique[dev->deviate->unique_size].expr_size) { |
| 2212 | continue; |
| 2213 | } |
| 2214 | |
| 2215 | for (j = 0; j < dev->deviate->unique[dev->deviate->unique_size].expr_size; j++) { |
| 2216 | if (!ly_strequal(list->unique[i].expr[j], dev->deviate->unique[dev->deviate->unique_size].expr[j], 1)) { |
| 2217 | break; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | if (j == dev->deviate->unique[dev->deviate->unique_size].expr_size) { |
| 2222 | /* we have a match, free the unique structure ... */ |
| 2223 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 2224 | lydict_remove(module->ctx, list->unique[i].expr[j]); |
| 2225 | } |
| 2226 | free(list->unique[i].expr); |
| 2227 | /* ... and maintain the array */ |
| 2228 | list->unique_size--; |
| 2229 | if (i != list->unique_size) { |
| 2230 | list->unique[i].expr_size = list->unique[list->unique_size].expr_size; |
| 2231 | list->unique[i].expr = list->unique[list->unique_size].expr; |
| 2232 | } |
| 2233 | |
| 2234 | if (!list->unique_size) { |
| 2235 | free(list->unique); |
| 2236 | list->unique = NULL; |
| 2237 | } else { |
| 2238 | list->unique[list->unique_size].expr_size = 0; |
| 2239 | list->unique[list->unique_size].expr = NULL; |
| 2240 | } |
| 2241 | |
| 2242 | i = -1; /* set match flag */ |
| 2243 | break; |
| 2244 | } |
| 2245 | } |
| 2246 | dev->deviate->unique_size++; |
| 2247 | |
| 2248 | if (i != -1) { |
| 2249 | /* no match found */ |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2250 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "unique"); |
| 2251 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted."); |
Pavol Vican | c1f5a50 | 2016-03-06 16:51:26 +0100 | [diff] [blame] | 2252 | goto error; |
| 2253 | } |
| 2254 | |
| 2255 | free(value); |
| 2256 | return EXIT_SUCCESS; |
| 2257 | |
| 2258 | error: |
| 2259 | free(value); |
| 2260 | return EXIT_FAILURE; |
| 2261 | } |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2262 | |
| 2263 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2264 | yang_check_deviation(struct lys_module *module, struct type_deviation *dev, struct unres_schema *unres) |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2265 | { |
| 2266 | int i, rc; |
| 2267 | |
| 2268 | if (dev->target->nodetype == LYS_LEAF) { |
| 2269 | for(i = 0; i < dev->deviation->deviate_size; ++i) { |
| 2270 | if (dev->deviation->deviate[i].mod != LY_DEVIATE_DEL) { |
| 2271 | if (dev->deviation->deviate[i].dflt || dev->deviation->deviate[i].type) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2272 | rc = unres_schema_add_str(module, unres, &((struct lys_node_leaf *)dev->target)->type, UNRES_TYPE_DFLT, ((struct lys_node_leaf *)dev->target)->dflt); |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2273 | if (rc == -1) { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2274 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Leaf \"%s\" default value no longer matches its type.", dev->deviation->target_name); |
Pavol Vican | e92421d | 2016-03-08 10:12:33 +0100 | [diff] [blame] | 2275 | return EXIT_FAILURE; |
| 2276 | } |
| 2277 | break; |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | } |
| 2282 | return EXIT_SUCCESS; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | int |
| 2286 | yang_fill_include(struct lys_module *module, struct lys_submodule *submodule, char *value, |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2287 | struct lys_include *inc, struct unres_schema *unres) |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2288 | { |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2289 | struct lys_module *trg; |
Pavol Vican | 5587041 | 2016-03-10 12:36:21 +0100 | [diff] [blame] | 2290 | const char *str; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2291 | int rc; |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2292 | int ret = 0; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2293 | |
Pavol Vican | 5587041 | 2016-03-10 12:36:21 +0100 | [diff] [blame] | 2294 | str = lydict_insert_zc(module->ctx, value); |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2295 | trg = (submodule) ? (struct lys_module *)submodule : module; |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2296 | rc = lyp_check_include(module, submodule, str, inc, unres); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2297 | if (!rc) { |
| 2298 | /* success, copy the filled data into the final array */ |
Pavol Vican | e024ab7 | 2016-07-27 14:27:43 +0200 | [diff] [blame] | 2299 | memcpy(&trg->inc[trg->inc_size], inc, sizeof *inc); |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 2300 | trg->inc_size++; |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2301 | } else if (rc == -1) { |
Radek Krejci | 83e3f5b | 2016-06-24 14:55:25 +0200 | [diff] [blame] | 2302 | ret = -1; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2303 | } |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2304 | |
Pavol Vican | 5587041 | 2016-03-10 12:36:21 +0100 | [diff] [blame] | 2305 | lydict_remove(module->ctx, str); |
Radek Krejci | 5b2c8a8 | 2016-06-17 15:36:03 +0200 | [diff] [blame] | 2306 | return ret; |
Pavol Vican | 9b89dda | 2016-03-09 15:36:55 +0100 | [diff] [blame] | 2307 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2308 | |
| 2309 | int |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2310 | yang_use_extension(struct lys_module *module, struct lys_node *data_node, void *actual, char *value) |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2311 | { |
| 2312 | char *prefix; |
| 2313 | char *identif; |
| 2314 | const char *ns = NULL; |
| 2315 | int i; |
| 2316 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2317 | /* check to the same pointer */ |
| 2318 | if (data_node != actual) { |
| 2319 | return EXIT_SUCCESS; |
| 2320 | } |
| 2321 | |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2322 | prefix = strdup(value); |
| 2323 | if (!prefix) { |
| 2324 | LOGMEM; |
| 2325 | goto error; |
| 2326 | } |
| 2327 | /* find prefix anf identificator*/ |
| 2328 | identif = strchr(prefix, ':'); |
Pavol Vican | fbd0278 | 2016-08-29 11:14:45 +0200 | [diff] [blame] | 2329 | if (!identif) { |
| 2330 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, prefix); |
| 2331 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The extension must have prefix."); |
| 2332 | goto error; |
| 2333 | } |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2334 | *identif = '\0'; |
| 2335 | identif++; |
| 2336 | |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2337 | for(i = 0; i < module->imp_size; ++i) { |
| 2338 | if (!strcmp(module->imp[i].prefix, prefix)) { |
| 2339 | ns = module->imp[i].module->ns; |
| 2340 | break; |
| 2341 | } |
| 2342 | } |
Pavol Vican | 1ff1b7b | 2016-04-07 09:51:49 +0200 | [diff] [blame] | 2343 | if (!ns && !strcmp(module->prefix, prefix)) { |
| 2344 | ns = (module->type) ? ((struct lys_submodule *)module)->belongsto->ns : module->ns; |
| 2345 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2346 | if (ns && !strcmp(ns, LY_NSNACM)) { |
| 2347 | if (!strcmp(identif, "default-deny-write")) { |
| 2348 | data_node->nacm |= LYS_NACM_DENYW; |
| 2349 | } else if (!strcmp(identif, "default-deny-all")) { |
| 2350 | data_node->nacm |= LYS_NACM_DENYA; |
| 2351 | } else { |
Pavol Vican | 0adf01d | 2016-03-22 12:29:33 +0100 | [diff] [blame] | 2352 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, identif); |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2353 | goto error; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2354 | } |
| 2355 | } |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2356 | free(prefix); |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2357 | return EXIT_SUCCESS; |
Pavol Vican | a302aa6 | 2016-03-17 10:45:35 +0100 | [diff] [blame] | 2358 | |
| 2359 | error: |
| 2360 | free(prefix); |
| 2361 | return EXIT_FAILURE; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | void |
| 2365 | nacm_inherit(struct lys_module *module) |
| 2366 | { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2367 | struct lys_node *next, *elem, *tmp_node, *tmp_child; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2368 | |
| 2369 | LY_TREE_DFS_BEGIN(module->data, next, elem) { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2370 | tmp_node = NULL; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2371 | if (elem->parent) { |
| 2372 | switch (elem->nodetype) { |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2373 | case LYS_GROUPING: |
| 2374 | /* extension nacm not inherited*/ |
| 2375 | break; |
| 2376 | case LYS_CHOICE: |
| 2377 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2378 | case LYS_ANYDATA: |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2379 | case LYS_USES: |
| 2380 | if (elem->parent->nodetype != LYS_GROUPING) { |
| 2381 | elem->nacm |= elem->parent->nacm; |
| 2382 | } |
| 2383 | break; |
| 2384 | case LYS_CONTAINER: |
| 2385 | case LYS_LIST: |
| 2386 | case LYS_CASE: |
| 2387 | case LYS_NOTIF: |
| 2388 | case LYS_RPC: |
| 2389 | case LYS_INPUT: |
| 2390 | case LYS_OUTPUT: |
| 2391 | case LYS_AUGMENT: |
| 2392 | elem->nacm |= elem->parent->nacm; |
| 2393 | break; |
| 2394 | case LYS_LEAF: |
| 2395 | case LYS_LEAFLIST: |
| 2396 | tmp_node = elem; |
| 2397 | tmp_child = elem->child; |
| 2398 | elem->child = NULL; |
| 2399 | default: |
| 2400 | break; |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | LY_TREE_DFS_END(module->data, next, elem); |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2404 | if (tmp_node) { |
| 2405 | tmp_node->child = tmp_child; |
| 2406 | } |
Pavol Vican | f4717e6 | 2016-03-16 11:30:01 +0100 | [diff] [blame] | 2407 | } |
| 2408 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2409 | |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2410 | int |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2411 | store_flags(struct lys_node *node, uint8_t flags, int config_inherit) |
| 2412 | { |
Michal Vasko | e10d14a | 2016-08-26 15:17:13 +0200 | [diff] [blame] | 2413 | struct lys_node *elem; |
| 2414 | |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2415 | node->flags |= flags; |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2416 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 2417 | if (config_inherit) { |
| 2418 | /* get config flag from parent */ |
| 2419 | if (node->parent) { |
| 2420 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
| 2421 | } else { |
| 2422 | /* default config is true */ |
| 2423 | node->flags |= LYS_CONFIG_W; |
| 2424 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2425 | } |
Michal Vasko | e10d14a | 2016-08-26 15:17:13 +0200 | [diff] [blame] | 2426 | } else { |
| 2427 | /* do we even care about config flags? */ |
| 2428 | for (elem = node; elem && !(elem->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); elem = elem->parent); |
| 2429 | |
| 2430 | if (!elem && (node->flags & LYS_CONFIG_W) && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 2431 | LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config"); |
| 2432 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children."); |
| 2433 | return EXIT_FAILURE; |
| 2434 | } |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2435 | } |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 2436 | |
| 2437 | return EXIT_SUCCESS; |
Pavol Vican | 4fb66c9 | 2016-03-17 10:32:27 +0100 | [diff] [blame] | 2438 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2439 | |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2440 | static int |
| 2441 | yang_parse(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, const char *data, |
| 2442 | unsigned int size, struct lys_array_size *size_arrays, int type_read) |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2443 | { |
| 2444 | YY_BUFFER_STATE bp; |
Pavol Vican | 802af1e | 2016-03-23 20:42:26 +0100 | [diff] [blame] | 2445 | yyscan_t scanner = NULL; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2446 | int ret = EXIT_SUCCESS; |
| 2447 | |
| 2448 | yylex_init(&scanner); |
| 2449 | bp = yy_scan_buffer((char *)data, size, scanner); |
| 2450 | yy_switch_to_buffer(bp, scanner); |
| 2451 | if (yyparse(scanner, module, submodule, unres, size_arrays, type_read)) { |
| 2452 | ret = EXIT_FAILURE; |
| 2453 | } |
| 2454 | yy_delete_buffer(bp, scanner); |
| 2455 | yylex_destroy(scanner); |
| 2456 | return ret; |
| 2457 | } |
| 2458 | |
| 2459 | int |
| 2460 | yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, const char *data, unsigned int size_data) |
| 2461 | { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2462 | struct lys_array_size *size_arrays=NULL; |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2463 | unsigned int size; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2464 | int ret; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2465 | |
| 2466 | size_arrays = calloc(1, sizeof *size_arrays); |
| 2467 | if (!size_arrays) { |
| 2468 | LOGMEM; |
Pavol Vican | f7994fb | 2016-04-05 21:55:53 +0200 | [diff] [blame] | 2469 | return EXIT_FAILURE; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2470 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2471 | size = (size_data) ? size_data : strlen(data) + 2; |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2472 | ret = yang_parse(module, submodule, unres, data, size, size_arrays, LY_READ_ONLY_SIZE); |
| 2473 | if (!ret) { |
| 2474 | ret = yang_parse(module, submodule, unres, data, size, size_arrays, LY_READ_ALL); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2475 | } |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2476 | free(size_arrays->node); |
| 2477 | free(size_arrays); |
Pavol Vican | 1938d88 | 2016-04-10 13:36:31 +0200 | [diff] [blame] | 2478 | return ret; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | struct lys_module * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2482 | yang_read_module(struct ly_ctx *ctx, const char* data, unsigned int size, const char *revision, int implement) |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2483 | { |
| 2484 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2485 | struct lys_module *tmp_module, *module = NULL; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2486 | struct unres_schema *unres = NULL; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2487 | int i; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2488 | |
| 2489 | unres = calloc(1, sizeof *unres); |
| 2490 | if (!unres) { |
| 2491 | LOGMEM; |
| 2492 | goto error; |
| 2493 | } |
| 2494 | |
| 2495 | module = calloc(1, sizeof *module); |
| 2496 | if (!module) { |
| 2497 | LOGMEM; |
Pavol Vican | f7994fb | 2016-04-05 21:55:53 +0200 | [diff] [blame] | 2498 | goto error; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | /* initiale module */ |
Michal Vasko | fe7e5a7 | 2016-05-02 14:49:23 +0200 | [diff] [blame] | 2502 | module->ctx = ctx; |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2503 | module->type = 0; |
| 2504 | module->implemented = (implement ? 1 : 0); |
| 2505 | |
| 2506 | if (yang_parse_mem(module, NULL, unres, data, size)) { |
| 2507 | goto error; |
| 2508 | } |
| 2509 | |
| 2510 | if (module && unres->count && resolve_unres_schema(module, unres)) { |
| 2511 | goto error; |
| 2512 | } |
| 2513 | |
| 2514 | if (revision) { |
| 2515 | /* check revision of the parsed model */ |
| 2516 | if (!module->rev_size || strcmp(revision, module->rev[0].date)) { |
| 2517 | LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", |
| 2518 | module->name, module->rev[0].date, revision); |
| 2519 | goto error; |
| 2520 | } |
| 2521 | } |
| 2522 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2523 | tmp_module = module; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2524 | if (lyp_ctx_add_module(&module)) { |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2525 | goto error; |
| 2526 | } |
| 2527 | |
Pavol Vican | 10ffba5 | 2016-04-04 12:21:22 +0200 | [diff] [blame] | 2528 | if (module == tmp_module) { |
| 2529 | nacm_inherit(module); |
| 2530 | } |
| 2531 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2532 | if (module->augment_size || module->deviation_size) { |
Michal Vasko | 5cec331 | 2016-05-04 08:59:41 +0200 | [diff] [blame] | 2533 | if (!module->implemented) { |
| 2534 | LOGVRB("Module \"%s\" includes augments or deviations, changing conformance to \"implement\".", module->name); |
| 2535 | } |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 2536 | if (lys_module_set_implement(module)) { |
| 2537 | goto error; |
| 2538 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2539 | |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 2540 | if (lys_sub_module_set_dev_aug_target_implement(module)) { |
| 2541 | goto error; |
| 2542 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2543 | for (i = 0; i < module->inc_size; ++i) { |
Radek Krejci | 4dcd339 | 2016-06-22 10:28:40 +0200 | [diff] [blame] | 2544 | if (!module->inc[i].submodule) { |
| 2545 | continue; |
| 2546 | } |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 2547 | if (lys_sub_module_set_dev_aug_target_implement((struct lys_module *)module->inc[i].submodule)) { |
| 2548 | goto error; |
| 2549 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2550 | } |
| 2551 | } |
| 2552 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2553 | unres_schema_free(NULL, &unres); |
| 2554 | LOGVRB("Module \"%s\" successfully parsed.", module->name); |
| 2555 | return module; |
| 2556 | |
| 2557 | error: |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2558 | /* cleanup */ |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2559 | unres_schema_free(module, &unres); |
| 2560 | if (!module || !module->name) { |
| 2561 | free(module); |
| 2562 | LOGERR(ly_errno, "Module parsing failed."); |
| 2563 | return NULL; |
| 2564 | } |
| 2565 | |
| 2566 | LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name); |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2567 | |
| 2568 | lys_sub_module_remove_devs_augs(module); |
| 2569 | lys_free(module, NULL, 1); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2570 | return NULL; |
| 2571 | } |
| 2572 | |
| 2573 | struct lys_submodule * |
Pavol Vican | 974377b | 2016-03-23 00:38:53 +0100 | [diff] [blame] | 2574 | yang_read_submodule(struct lys_module *module, const char *data, unsigned int size, struct unres_schema *unres) |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2575 | { |
| 2576 | struct lys_submodule *submodule; |
| 2577 | |
| 2578 | submodule = calloc(1, sizeof *submodule); |
| 2579 | if (!submodule) { |
| 2580 | LOGMEM; |
| 2581 | goto error; |
| 2582 | } |
| 2583 | |
| 2584 | submodule->ctx = module->ctx; |
| 2585 | submodule->type = 1; |
| 2586 | submodule->belongsto = module; |
| 2587 | |
| 2588 | if (yang_parse_mem(module, submodule, unres, data, size)) { |
| 2589 | goto error; |
| 2590 | } |
| 2591 | |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2592 | LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2593 | return submodule; |
| 2594 | |
| 2595 | error: |
| 2596 | /* cleanup */ |
| 2597 | unres_schema_free((struct lys_module *)submodule, &unres); |
| 2598 | |
| 2599 | if (!submodule || !submodule->name) { |
| 2600 | free(submodule); |
| 2601 | LOGERR(ly_errno, "Submodule parsing failed."); |
| 2602 | return NULL; |
| 2603 | } |
| 2604 | |
| 2605 | LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name); |
| 2606 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2607 | lys_sub_module_remove_devs_augs((struct lys_module *)submodule); |
| 2608 | lys_submodule_module_data_free(submodule); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2609 | lys_submodule_free(submodule, NULL); |
Pavol Vican | 8e7110b | 2016-03-22 17:00:26 +0100 | [diff] [blame] | 2610 | return NULL; |
| 2611 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2612 | |
| 2613 | static int |
| 2614 | count_substring(const char *str, char c) |
| 2615 | { |
| 2616 | const char *tmp = str; |
| 2617 | int count = 0; |
| 2618 | |
| 2619 | while ((tmp = strchr(tmp, c))) { |
| 2620 | tmp++; |
| 2621 | count++; |
| 2622 | } |
| 2623 | return count; |
| 2624 | } |
| 2625 | |
| 2626 | static int |
| 2627 | read_indent(const char *input, int indent, int size, int in_index, int *out_index, char *output) |
| 2628 | { |
| 2629 | int k = 0, j; |
| 2630 | |
| 2631 | while (in_index < size) { |
| 2632 | if (input[in_index] == ' ') { |
| 2633 | k++; |
| 2634 | } else if (input[in_index] == '\t') { |
| 2635 | /* RFC 6020 6.1.3 tab character is treated as 8 space characters */ |
| 2636 | k += 8; |
| 2637 | } else { |
| 2638 | break; |
| 2639 | } |
| 2640 | ++in_index; |
| 2641 | if (k >= indent) { |
| 2642 | for (j = k - indent; j > 0; --j) { |
| 2643 | output[*out_index] = ' '; |
| 2644 | ++(*out_index); |
| 2645 | } |
| 2646 | break; |
| 2647 | } |
| 2648 | } |
| 2649 | return in_index; |
| 2650 | } |
| 2651 | |
| 2652 | char * |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2653 | yang_read_string(const char *input, int size, int indent, int version) |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2654 | { |
| 2655 | int space, count; |
| 2656 | int in_index, out_index; |
| 2657 | char *value; |
| 2658 | char *retval = NULL; |
| 2659 | |
| 2660 | value = malloc(size + 1); |
| 2661 | if (!value) { |
| 2662 | LOGMEM; |
| 2663 | return NULL; |
| 2664 | } |
| 2665 | /* replace special character in escape sequence */ |
| 2666 | in_index = out_index = 0; |
| 2667 | while (in_index < size) { |
| 2668 | if (input[in_index] == '\\') { |
| 2669 | if (input[in_index + 1] == 'n') { |
| 2670 | value[out_index] = '\n'; |
| 2671 | ++in_index; |
| 2672 | } else if (input[in_index + 1] == 't') { |
| 2673 | value[out_index] = '\t'; |
| 2674 | ++in_index; |
| 2675 | } else if (input[in_index + 1] == '\\') { |
| 2676 | value[out_index] = '\\'; |
| 2677 | ++in_index; |
| 2678 | } else if ((in_index + 1) != size && input[in_index + 1] == '"') { |
| 2679 | value[out_index] = '"'; |
| 2680 | ++in_index; |
| 2681 | } else { |
Pavol Vican | 677b013 | 2016-08-09 15:44:58 +0200 | [diff] [blame] | 2682 | if (version < 2) { |
| 2683 | value[out_index] = input[in_index]; |
| 2684 | } else { |
| 2685 | /* YANG 1.1 backslash must not be followed by any other character */ |
| 2686 | LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, input); |
| 2687 | goto error; |
| 2688 | } |
Pavol Vican | 8760bb7 | 2016-04-07 09:44:01 +0200 | [diff] [blame] | 2689 | } |
| 2690 | } else { |
| 2691 | value[out_index] = input[in_index]; |
| 2692 | } |
| 2693 | ++in_index; |
| 2694 | ++out_index; |
| 2695 | } |
| 2696 | value[out_index] = '\0'; |
| 2697 | size = out_index; |
| 2698 | count = count_substring(value, '\t'); |
| 2699 | |
| 2700 | /* extend size of string due to replacing character '\t' with 8 spaces */ |
| 2701 | retval = malloc(size + 1 + 7 * count); |
| 2702 | if (!retval) { |
| 2703 | LOGMEM; |
| 2704 | goto error; |
| 2705 | } |
| 2706 | in_index = out_index = space = 0; |
| 2707 | while (in_index < size) { |
| 2708 | if (value[in_index] == '\n') { |
| 2709 | out_index -= space; |
| 2710 | space = 0; |
| 2711 | retval[out_index] = '\n'; |
| 2712 | ++out_index; |
| 2713 | ++in_index; |
| 2714 | in_index = read_indent(value, indent, size, in_index, &out_index, retval); |
| 2715 | continue; |
| 2716 | } else { |
| 2717 | space = (value[in_index] == ' ' || value[in_index] == '\t') ? space + 1 : 0; |
| 2718 | retval[out_index] = value[in_index]; |
| 2719 | ++out_index; |
| 2720 | } |
| 2721 | ++in_index; |
| 2722 | } |
| 2723 | retval[out_index] = '\0'; |
| 2724 | if (out_index != size) { |
| 2725 | retval = ly_realloc(retval, out_index + 1); |
| 2726 | } |
| 2727 | free(value); |
| 2728 | return retval; |
| 2729 | error: |
| 2730 | free(value); |
| 2731 | return NULL; |
| 2732 | } |