blob: 5205082238b3d84511b65c1ee8d0701d117676f2 [file] [log] [blame]
Pavol Vican021488a2016-01-25 23:56:12 +01001/**
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 Vican9a3a7212016-03-23 10:04:00 +01008 * 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 Vican021488a2016-01-25 23:56:12 +010013 */
14
Pavol Vican5de33492016-02-22 14:03:24 +010015#include <ctype.h>
Pavol Vican021488a2016-01-25 23:56:12 +010016#include "parser_yang.h"
Pavol Vican8e7110b2016-03-22 17:00:26 +010017#include "parser_yang_lex.h"
Pavol Vican6eb14e82016-02-03 12:27:13 +010018#include "parser.h"
Pavol Vicanf37eeaa2016-02-09 20:54:06 +010019#include "xpath.h"
Pavol Vican021488a2016-01-25 23:56:12 +010020
Michal Vaskofe7e5a72016-05-02 14:49:23 +020021static int
Pavol Vican0adf01d2016-03-22 12:29:33 +010022yang_check_string(struct lys_module *module, const char **target, char *what, char *where, char *value)
Pavol Vican2a064652016-02-02 22:54:34 +010023{
Pavol Vicanbf805472016-01-26 14:24:56 +010024 if (*target) {
Pavol Vican0adf01d2016-03-22 12:29:33 +010025 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, what, where);
Pavol Vicanbf805472016-01-26 14:24:56 +010026 free(value);
27 return 1;
28 } else {
Pavol Vican2a064652016-02-02 22:54:34 +010029 *target = lydict_insert_zc(module->ctx, value);
Pavol Vicanbf805472016-01-26 14:24:56 +010030 return 0;
31 }
32}
33
Pavol Vican810892e2016-07-12 16:55:34 +020034static int
35yang_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 Vican810892e2016-07-12 16:55:34 +020041 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 Krejci71d51832016-07-14 15:59:06 +020062 tpdf = ((struct lys_node_rpc_action *)child)->tpdf;
63 size = ((struct lys_node_rpc_action *)child)->tpdf_size;
Pavol Vican810892e2016-07-12 16:55:34 +020064 break;
65 case LYS_INPUT:
66 case LYS_OUTPUT:
Radek Krejci71d51832016-07-14 15:59:06 +020067 tpdf = ((struct lys_node_inout *)child)->tpdf;
68 size = ((struct lys_node_inout *)child)->tpdf_size;
Pavol Vican810892e2016-07-12 16:55:34 +020069 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 Krejcifc824a42016-07-14 15:48:38 +020078 if (size && dup_typedef_check(id, tpdf, size)) {
Pavol Vican810892e2016-07-12 16:55:34 +020079 LOGVAL(LYE_DUPID, LY_VLOG_NONE, NULL, "typedef", id);
80 return EXIT_FAILURE;
81 }
Michal Vasko3767fb22016-07-21 12:10:57 +020082 }
Pavol Vican810892e2016-07-12 16:55:34 +020083 LY_TREE_DFS_END(node, next, child)}
84 } while (root && (node = node->next));
85 return EXIT_SUCCESS;
86}
87
Michal Vaskofe7e5a72016-05-02 14:49:23 +020088int
Pavol Vican5f0316a2016-04-05 21:21:11 +020089yang_read_common(struct lys_module *module, char *value, enum yytokentype type)
Pavol Vican2a064652016-02-02 22:54:34 +010090{
Pavol Vican6eb14e82016-02-03 12:27:13 +010091 int ret = 0;
Pavol Vican021488a2016-01-25 23:56:12 +010092
93 switch (type) {
Pavol Vican2a064652016-02-02 22:54:34 +010094 case MODULE_KEYWORD:
95 module->name = lydict_insert_zc(module->ctx, value);
96 break;
97 case NAMESPACE_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +010098 ret = yang_check_string(module, &module->ns, "namespace", "module", value);
Pavol Vican2a064652016-02-02 22:54:34 +010099 break;
Pavol Vican1ca072c2016-02-03 13:03:56 +0100100 case ORGANIZATION_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100101 ret = yang_check_string(module, &module->org, "organization", "module", value);
Pavol Vican1ca072c2016-02-03 13:03:56 +0100102 break;
103 case CONTACT_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100104 ret = yang_check_string(module, &module->contact, "contact", "module", value);
Pavol Vican1ca072c2016-02-03 13:03:56 +0100105 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200106 default:
107 free(value);
108 LOGINT;
109 ret = EXIT_FAILURE;
110 break;
Pavol Vican2a064652016-02-02 22:54:34 +0100111 }
112
Pavol Vican021488a2016-01-25 23:56:12 +0100113 return ret;
Pavol Vicanbf805472016-01-26 14:24:56 +0100114}
115
Michal Vaskofe7e5a72016-05-02 14:49:23 +0200116int
Pavol Vicand0b64c12016-07-15 09:56:19 +0200117yang_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 Vasko3767fb22016-07-21 12:10:57 +0200122 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "yang version", "module");
123 ret = EXIT_FAILURE;
Pavol Vicand0b64c12016-07-15 09:56:19 +0200124 } 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 Vasko3767fb22016-07-21 12:10:57 +0200146 }
Pavol Vicand0b64c12016-07-15 09:56:19 +0200147 }
148 free(value);
149 return ret;
150}
151
152int
Pavol Vicane024ab72016-07-27 14:27:43 +0200153yang_read_prefix(struct lys_module *module, struct lys_import *imp, char *value)
Pavol Vican2a064652016-02-02 22:54:34 +0100154{
Pavol Vican6eb14e82016-02-03 12:27:13 +0100155 int ret = 0;
Pavol Vicanbf805472016-01-26 14:24:56 +0100156
Pavol Vican0adf01d2016-03-22 12:29:33 +0100157 if (lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) {
Pavol Vican6eb14e82016-02-03 12:27:13 +0100158 free(value);
159 return EXIT_FAILURE;
160 }
Pavol Vicane024ab72016-07-27 14:27:43 +0200161
162 if (imp) {
163 ret = yang_check_string(module, &imp->prefix, "prefix", "import", value);
164 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100165 ret = yang_check_string(module, &module->prefix, "prefix", "module", value);
Pavol Vican2a064652016-02-02 22:54:34 +0100166 }
Pavol Vican6eb14e82016-02-03 12:27:13 +0100167
Pavol Vicanbf805472016-01-26 14:24:56 +0100168 return ret;
169}
Pavol Vican6eb14e82016-02-03 12:27:13 +0100170
Pavol Vican6eb14e82016-02-03 12:27:13 +0100171int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100172yang_fill_import(struct lys_module *module, struct lys_import *imp, char *value)
Pavol Vican6eb14e82016-02-03 12:27:13 +0100173{
Pavol Vican0da132e2016-03-21 12:03:03 +0100174 const char *exp;
Radek Krejci4dcd3392016-06-22 10:28:40 +0200175 int rc;
Pavol Vican6eb14e82016-02-03 12:27:13 +0100176
Pavol Vicane024ab72016-07-27 14:27:43 +0200177 if (!imp->prefix) {
178 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", "import");
179 return EXIT_FAILURE;
180 }
Pavol Vican0da132e2016-03-21 12:03:03 +0100181 exp = lydict_insert_zc(module->ctx, value);
Pavol Vican0adf01d2016-03-22 12:29:33 +0100182 rc = lyp_check_import(module, exp, imp);
Pavol Vican0da132e2016-03-21 12:03:03 +0100183 lydict_remove(module->ctx, exp);
Radek Krejci4dcd3392016-06-22 10:28:40 +0200184 module->imp_size++;
Pavol Vican0da132e2016-03-21 12:03:03 +0100185 if (rc) {
Radek Krejci4dcd3392016-06-22 10:28:40 +0200186 return EXIT_FAILURE;
Pavol Vican6eb14e82016-02-03 12:27:13 +0100187 }
Pavol Vican6eb14e82016-02-03 12:27:13 +0100188
Pavol Vican6eb14e82016-02-03 12:27:13 +0100189 return EXIT_SUCCESS;
Pavol Vican6eb14e82016-02-03 12:27:13 +0100190}
Pavol Vican1ca072c2016-02-03 13:03:56 +0100191
192int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100193yang_read_description(struct lys_module *module, void *node, char *value, char *where)
Pavol Vican1ca072c2016-02-03 13:03:56 +0100194{
195 int ret;
Pavol Vican9bcd7c62016-03-17 19:36:35 +0100196 char *dsc = "description";
Pavol Vican1ca072c2016-02-03 13:03:56 +0100197
198 if (!node) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100199 ret = yang_check_string(module, &module->dsc, dsc, "module", value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100200 } else {
Pavol Vican9bcd7c62016-03-17 19:36:35 +0100201 if (!strcmp("revision", where)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100202 ret = yang_check_string(module, &((struct lys_revision *)node)->dsc, dsc, where, value);
Pavol Vicane024ab72016-07-27 14:27:43 +0200203 } 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 Vican9bcd7c62016-03-17 19:36:35 +0100207 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100208 ret = yang_check_string(module, &((struct lys_node *)node)->dsc, dsc, where, value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100209 }
Pavol Vican1ca072c2016-02-03 13:03:56 +0100210 }
211 return ret;
212}
213
214int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100215yang_read_reference(struct lys_module *module, void *node, char *value, char *where)
Pavol Vican1ca072c2016-02-03 13:03:56 +0100216{
217 int ret;
Pavol Vicanf5fe9662016-03-17 20:00:16 +0100218 char *ref = "reference";
Pavol Vican1ca072c2016-02-03 13:03:56 +0100219
220 if (!node) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100221 ret = yang_check_string(module, &module->ref, "reference", "module", value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100222 } else {
Pavol Vicanf5fe9662016-03-17 20:00:16 +0100223 if (!strcmp("revision", where)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100224 ret = yang_check_string(module, &((struct lys_revision *)node)->ref, ref, where, value);
Pavol Vicane024ab72016-07-27 14:27:43 +0200225 } 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 Vicanf5fe9662016-03-17 20:00:16 +0100229 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100230 ret = yang_check_string(module, &((struct lys_node *)node)->ref, ref, where, value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100231 }
Pavol Vican1ca072c2016-02-03 13:03:56 +0100232 }
233 return ret;
234}
Pavol Vicanbedff692016-02-03 14:29:17 +0100235
236void *
237yang_read_revision(struct lys_module *module, char *value)
238{
239 struct lys_revision *retval;
240
Pavol Vican1eeb1992016-02-09 11:10:45 +0100241 retval = &module->rev[module->rev_size];
Pavol Vicanbedff692016-02-03 14:29:17 +0100242
243 /* first member of array is last revision */
Pavol Vican1eeb1992016-02-09 11:10:45 +0100244 if (module->rev_size && strcmp(module->rev[0].date, value) < 0) {
Pavol Vicanbedff692016-02-03 14:29:17 +0100245 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 Vican1eeb1992016-02-09 11:10:45 +0100255 module->rev_size++;
Pavol Vicanbedff692016-02-03 14:29:17 +0100256 free(value);
257 return retval;
258}
Pavol Vican1eeb1992016-02-09 11:10:45 +0100259
260int
Pavol Vicana1827962016-02-29 15:39:42 +0100261yang_add_elem(struct lys_node_array **node, uint32_t *size)
Pavol Vican1eeb1992016-02-09 11:10:45 +0100262{
Pavol Vican45ccc592016-03-09 18:53:48 +0100263 if (!(*size % LY_ARRAY_SIZE)) {
Pavol Vican1eeb1992016-02-09 11:10:45 +0100264 if (!(*node = ly_realloc(*node, (*size + LY_ARRAY_SIZE) * sizeof **node))) {
265 LOGMEM;
266 return EXIT_FAILURE;
267 } else {
Pavol Vican45ccc592016-03-09 18:53:48 +0100268 memset(*node + *size, 0, LY_ARRAY_SIZE * sizeof **node);
Pavol Vican1eeb1992016-02-09 11:10:45 +0100269 }
270 }
271 (*size)++;
272 return EXIT_SUCCESS;
273}
Pavol Vicane1354e92016-02-09 14:02:09 +0100274
275void *
Pavol Vican0adf01d2016-03-22 12:29:33 +0100276yang_read_feature(struct lys_module *module, char *value)
Pavol Vicane1354e92016-02-09 14:02:09 +0100277{
278 struct lys_feature *retval;
279
280 /* check uniqueness of feature's names */
Pavol Vican0adf01d2016-03-22 12:29:33 +0100281 if (lyp_check_identifier(value, LY_IDENT_FEATURE, module, NULL)) {
Pavol Vicane1354e92016-02-09 14:02:09 +0100282 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
290error:
291 free(value);
292 return NULL;
293}
294
295int
Pavol Vican5f0316a2016-04-05 21:21:11 +0200296yang_read_if_feature(struct lys_module *module, void *ptr, char *value, struct unres_schema *unres, enum yytokentype type)
Pavol Vicane1354e92016-02-09 14:02:09 +0100297{
298 const char *exp;
299 int ret;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100300 struct lys_feature *f;
Pavol Vican4df80542016-08-08 09:37:55 +0200301 struct lys_ident *i;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100302 struct lys_node *n;
Pavol Vican8ff0e2e2016-08-09 09:09:10 +0200303 struct lys_type_enum *e;
304 struct lys_type_bit *b;
Pavol Vicane1354e92016-02-09 14:02:09 +0100305
Michal Vasko97b32be2016-07-25 10:59:53 +0200306 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 Vican0adf01d2016-03-22 12:29:33 +0100312 if (!(exp = transform_schema2json(module, value))) {
Pavol Vicane1354e92016-02-09 14:02:09 +0100313 free(value);
314 return EXIT_FAILURE;
315 }
316 free(value);
317
Pavol Vican8ff0e2e2016-08-09 09:09:10 +0200318 switch (type) {
319 case FEATURE_KEYWORD:
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100320 f = (struct lys_feature *) ptr;
Radek Krejci9ff0a922016-07-14 13:08:05 +0200321 ret = resolve_iffeature_compile(&f->iffeature[f->iffeature_size], exp, (struct lys_node *)f, unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200322 f->iffeature_size++;
Pavol Vican8ff0e2e2016-08-09 09:09:10 +0200323 break;
324 case IDENTITY_KEYWORD:
Pavol Vican4df80542016-08-08 09:37:55 +0200325 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 Vican8ff0e2e2016-08-09 09:09:10 +0200328 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 Vicanf37eeaa2016-02-09 20:54:06 +0100340 n = (struct lys_node *) ptr;
Radek Krejci9ff0a922016-07-14 13:08:05 +0200341 ret = resolve_iffeature_compile(&n->iffeature[n->iffeature_size], exp, n, unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200342 n->iffeature_size++;
Pavol Vican8ff0e2e2016-08-09 09:09:10 +0200343 break;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100344 }
Radek Krejci9ff0a922016-07-14 13:08:05 +0200345 lydict_remove(module->ctx, exp);
Pavol Vicane1354e92016-02-09 14:02:09 +0100346
Radek Krejci9ff0a922016-07-14 13:08:05 +0200347 if (ret) {
Pavol Vicane1354e92016-02-09 14:02:09 +0100348 return EXIT_FAILURE;
349 }
350 return EXIT_SUCCESS;
351}
352
Pavol Vican4fb66c92016-03-17 10:32:27 +0100353int
Radek Krejci4372b4e2016-04-14 17:42:16 +0200354yang_check_flags(uint16_t *flags, uint16_t mask, char *what, char *where, uint16_t value, int shortint)
Pavol Vicane1354e92016-02-09 14:02:09 +0100355{
356 if (*flags & mask) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100357 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, what, where);
Pavol Vicane1354e92016-02-09 14:02:09 +0100358 return EXIT_FAILURE;
359 } else {
Radek Krejci4372b4e2016-04-14 17:42:16 +0200360 if (shortint) {
361 *((uint8_t *)flags) |= (uint8_t)value;
362 } else {
363 *flags |= value;
364 }
Pavol Vicane1354e92016-02-09 14:02:09 +0100365 return EXIT_SUCCESS;
366 }
367}
368
Pavol Vicanbbdef532016-02-09 14:52:12 +0100369void *
370yang_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 Vicand6cda452016-07-13 15:08:29 +0200377 if (dup_identities_check(ret->name, module)) {
378 lydict_remove(module->ctx, ret->name);
379 return NULL;
380 }
Pavol Vicanbbdef532016-02-09 14:52:12 +0100381 module->ident_size++;
382 return ret;
383}
384
385int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100386yang_read_base(struct lys_module *module, struct lys_ident *ident, char *value, struct unres_schema *unres)
Pavol Vicanbbdef532016-02-09 14:52:12 +0100387{
388 const char *exp;
389
Pavol Vican0adf01d2016-03-22 12:29:33 +0100390 exp = transform_schema2json(module, value);
Pavol Vicanbbdef532016-02-09 14:52:12 +0100391 free(value);
392 if (!exp) {
393 return EXIT_FAILURE;
394 }
Pavol Vican4ffd0ab2016-08-27 16:35:04 +0200395
396 /* temporarily decrement identity_size due to resolve base */
397 module->ident_size--;
Pavol Vican0adf01d2016-03-22 12:29:33 +0100398 if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, exp) == -1) {
Pavol Vicanbbdef532016-02-09 14:52:12 +0100399 lydict_remove(module->ctx, exp);
Pavol Vican4ffd0ab2016-08-27 16:35:04 +0200400 /* undo change identity_size */
401 module->ident_size++;
Pavol Vicanbbdef532016-02-09 14:52:12 +0100402 return EXIT_FAILURE;
403 }
Pavol Vican4ffd0ab2016-08-27 16:35:04 +0200404 /* undo change identity_size */
405 module->ident_size++;
Pavol Vican44dde2c2016-02-10 11:18:14 +0100406
Pavol Vicanbbdef532016-02-09 14:52:12 +0100407 lydict_remove(module->ctx, exp);
408 return EXIT_SUCCESS;
409}
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100410
411void *
Pavol Vican5f0316a2016-04-05 21:21:11 +0200412yang_read_must(struct lys_module *module, struct lys_node *node, char *value, enum yytokentype type)
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100413{
414 struct lys_restr *retval;
415
416 switch (type) {
Pavol Vican8c82fa82016-02-10 13:13:24 +0100417 case CONTAINER_KEYWORD:
Pavol Vican096c6db2016-02-11 15:08:10 +0100418 retval = &((struct lys_node_container *)node)->must[((struct lys_node_container *)node)->must_size++];
Pavol Vican8c82fa82016-02-10 13:13:24 +0100419 break;
Pavol Vicandb7489e2016-08-23 17:23:39 +0200420 case ANYDATA_KEYWORD:
Pavol Vican8c82fa82016-02-10 13:13:24 +0100421 case ANYXML_KEYWORD:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200422 retval = &((struct lys_node_anydata *)node)->must[((struct lys_node_anydata *)node)->must_size++];
Pavol Vican096c6db2016-02-11 15:08:10 +0100423 break;
424 case LEAF_KEYWORD:
425 retval = &((struct lys_node_leaf *)node)->must[((struct lys_node_leaf *)node)->must_size++];
Pavol Vican8c82fa82016-02-10 13:13:24 +0100426 break;
Pavol Vican339d4ad2016-02-12 12:49:22 +0100427 case LEAF_LIST_KEYWORD:
428 retval = &((struct lys_node_leaflist *)node)->must[((struct lys_node_leaflist *)node)->must_size++];
429 break;
Pavol Vican5de33492016-02-22 14:03:24 +0100430 case LIST_KEYWORD:
431 retval = &((struct lys_node_list *)node)->must[((struct lys_node_list *)node)->must_size++];
432 break;
Pavol Vican1003ead2016-03-02 12:24:52 +0100433 case REFINE_KEYWORD:
434 retval = &((struct lys_refine *)node)->must[((struct lys_refine *)node)->must_size++];
435 break;
Pavol Vican85f12022016-03-05 16:30:35 +0100436 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 Vicanc1f5a502016-03-06 16:51:26 +0100440 case DELETE_KEYWORD:
441 retval = &((struct type_deviation *)node)->deviate->must[((struct type_deviation *)node)->deviate->must_size++];
442 break;
Pavol Vican9b14b492016-08-09 14:55:10 +0200443 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 Vican5f0316a2016-04-05 21:21:11 +0200449 default:
450 goto error;
451 break;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100452 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100453 retval->expr = transform_schema2json(module, value);
454 if (!retval->expr || lyxp_syntax_check(retval->expr)) {
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100455 goto error;
456 }
457 free(value);
458 return retval;
459
460error:
461 free(value);
462 return NULL;
463}
464
465int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100466yang_read_message(struct lys_module *module,struct lys_restr *save,char *value, char *what, int message)
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100467{
468 int ret;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100469
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100470 if (message==ERROR_APP_TAG_KEYWORD) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100471 ret = yang_check_string(module, &save->eapptag, "error_app_tag", what, value);
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100472 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100473 ret = yang_check_string(module, &save->emsg, "error_app_tag", what, value);
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100474 }
475 return ret;
476}
Pavol Vicanb5687112016-02-09 22:35:59 +0100477
478int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100479yang_read_presence(struct lys_module *module, struct lys_node_container *cont, char *value)
Pavol Vicanb5687112016-02-09 22:35:59 +0100480{
481 if (cont->presence) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100482 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, cont, "presence", "container");
Pavol Vicanb5687112016-02-09 22:35:59 +0100483 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 Vican235dbd42016-02-10 10:34:19 +0100491void *
Pavol Vican5f0316a2016-04-05 21:21:11 +0200492yang_read_when(struct lys_module *module, struct lys_node *node, enum yytokentype type, char *value)
Pavol Vican235dbd42016-02-10 10:34:19 +0100493{
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 Vican0adf01d2016-03-22 12:29:33 +0100502 retval->cond = transform_schema2json(module, value);
503 if (!retval->cond || lyxp_syntax_check(retval->cond)) {
Pavol Vican235dbd42016-02-10 10:34:19 +0100504 goto error;
505 }
506 switch (type) {
507 case CONTAINER_KEYWORD:
508 if (((struct lys_node_container *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100509 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "container");
Pavol Vican235dbd42016-02-10 10:34:19 +0100510 goto error;
511 }
512 ((struct lys_node_container *)node)->when = retval;
513 break;
Pavol Vicandb7489e2016-08-23 17:23:39 +0200514 case ANYDATA_KEYWORD:
Pavol Vican1f06ba82016-02-10 17:39:50 +0100515 case ANYXML_KEYWORD:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200516 if (((struct lys_node_anydata *)node)->when) {
Pavol Vicandb7489e2016-08-23 17:23:39 +0200517 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", (type == ANYXML_KEYWORD) ? "anyxml" : "anydata");
Pavol Vican8c82fa82016-02-10 13:13:24 +0100518 goto error;
519 }
Radek Krejcibf2abff2016-08-23 15:51:52 +0200520 ((struct lys_node_anydata *)node)->when = retval;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100521 break;
Pavol Vican1f06ba82016-02-10 17:39:50 +0100522 case CHOICE_KEYWORD:
523 if (((struct lys_node_choice *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100524 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "choice");
Pavol Vican1f06ba82016-02-10 17:39:50 +0100525 goto error;
526 }
527 ((struct lys_node_choice *)node)->when = retval;
528 break;
Pavol Vicanbd098132016-02-11 10:56:49 +0100529 case CASE_KEYWORD:
530 if (((struct lys_node_case *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100531 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "case");
Pavol Vicanbd098132016-02-11 10:56:49 +0100532 goto error;
533 }
534 ((struct lys_node_case *)node)->when = retval;
535 break;
Pavol Vican096c6db2016-02-11 15:08:10 +0100536 case LEAF_KEYWORD:
537 if (((struct lys_node_leaf *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100538 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaf");
Pavol Vican096c6db2016-02-11 15:08:10 +0100539 goto error;
540 }
541 ((struct lys_node_leaf *)node)->when = retval;
542 break;
Pavol Vican339d4ad2016-02-12 12:49:22 +0100543 case LEAF_LIST_KEYWORD:
544 if (((struct lys_node_leaflist *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100545 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaflist");
Pavol Vican339d4ad2016-02-12 12:49:22 +0100546 goto error;
547 }
548 ((struct lys_node_leaflist *)node)->when = retval;
549 break;
Pavol Vican5de33492016-02-22 14:03:24 +0100550 case LIST_KEYWORD:
551 if (((struct lys_node_list *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100552 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "list");
Pavol Vican5de33492016-02-22 14:03:24 +0100553 goto error;
554 }
555 ((struct lys_node_list *)node)->when = retval;
556 break;
Pavol Vican14b18562016-03-01 16:04:29 +0100557 case USES_KEYWORD:
558 if (((struct lys_node_uses *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100559 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "uses");
Pavol Vican14b18562016-03-01 16:04:29 +0100560 goto error;
561 }
562 ((struct lys_node_uses *)node)->when = retval;
563 break;
Pavol Vican92fa0dc2016-03-02 14:20:39 +0100564 case AUGMENT_KEYWORD:
565 if (((struct lys_node_augment *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100566 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "augment");
Pavol Vican92fa0dc2016-03-02 14:20:39 +0100567 goto error;
568 }
569 ((struct lys_node_augment *)node)->when = retval;
570 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200571 default:
572 goto error;
573 break;
Pavol Vican235dbd42016-02-10 10:34:19 +0100574 }
575 free(value);
576 return retval;
577
578error:
579 free(value);
580 lys_when_free(module->ctx, retval);
581 return NULL;
582}
Pavol Vican8c82fa82016-02-10 13:13:24 +0100583
584void *
Pavol Vican7cadfe72016-02-11 12:33:34 +0100585yang_read_node(struct lys_module *module, struct lys_node *parent, char *value, int nodetype, int sizeof_struct)
Pavol Vican8c82fa82016-02-10 13:13:24 +0100586{
Pavol Vican7cadfe72016-02-11 12:33:34 +0100587 struct lys_node *node;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100588
Pavol Vican7cadfe72016-02-11 12:33:34 +0100589 node = calloc(1, sizeof_struct);
590 if (!node) {
Pavol Vicand1dbfda2016-03-21 10:03:58 +0100591 free(value);
Pavol Vican8c82fa82016-02-10 13:13:24 +0100592 LOGMEM;
593 return NULL;
594 }
Pavol Vican531a9132016-03-03 10:10:09 +0100595 if (value) {
596 node->name = lydict_insert_zc(module->ctx, value);
597 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100598 node->module = module;
Pavol Vican7cadfe72016-02-11 12:33:34 +0100599 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 Vican531a9132016-03-03 10:10:09 +0100604 if (value) {
605 lydict_remove(module->ctx, node->name);
606 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100607 free(node);
Pavol Vican8c82fa82016-02-10 13:13:24 +0100608 return NULL;
609 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100610 return node;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100611}
612
Pavol Vican8c793992016-07-15 10:44:57 +0200613void *
614yang_read_action(struct lys_module *module, struct lys_node *parent, char *value)
615{
616 struct lys_node *node;
617
Michal Vaskobb174852016-07-25 11:00:21 +0200618 if (module->version != 2) {
619 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "action");
620 return NULL;
621 }
622
Pavol Vican8c793992016-07-15 10:44:57 +0200623 for (node = parent; node; node = lys_parent(node)) {
624 if (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)
Pavol Vicanbbe77822016-07-15 12:53:07 +0200625 || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys_size)) {
Pavol Vican8c793992016-07-15 10:44:57 +0200626 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 Vican8c82fa82016-02-10 13:13:24 +0100633int
Pavol Vican5f0316a2016-04-05 21:21:11 +0200634yang_read_default(struct lys_module *module, void *node, char *value, enum yytokentype type)
Pavol Vican096c6db2016-02-11 15:08:10 +0100635{
636 int ret;
637
638 switch (type) {
Pavol Vican339d4ad2016-02-12 12:49:22 +0100639 case LEAF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100640 ret = yang_check_string(module, &((struct lys_node_leaf *) node)->dflt, "default", "leaf", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100641 break;
Pavol Vican0df02b02016-03-01 10:28:50 +0100642 case TYPEDEF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100643 ret = yang_check_string(module, &((struct lys_tpdf *) node)->dflt, "default", "typedef", value);
Pavol Vican0df02b02016-03-01 10:28:50 +0100644 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200645 default:
646 free(value);
647 LOGINT;
648 ret = EXIT_FAILURE;
649 break;
Pavol Vican096c6db2016-02-11 15:08:10 +0100650 }
651 return ret;
652}
653
654int
Pavol Vican5f0316a2016-04-05 21:21:11 +0200655yang_read_units(struct lys_module *module, void *node, char *value, enum yytokentype type)
Pavol Vican096c6db2016-02-11 15:08:10 +0100656{
657 int ret;
658
659 switch (type) {
Pavol Vican339d4ad2016-02-12 12:49:22 +0100660 case LEAF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100661 ret = yang_check_string(module, &((struct lys_node_leaf *) node)->units, "units", "leaf", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100662 break;
663 case LEAF_LIST_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100664 ret = yang_check_string(module, &((struct lys_node_leaflist *) node)->units, "units", "leaflist", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100665 break;
Pavol Vican0df02b02016-03-01 10:28:50 +0100666 case TYPEDEF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100667 ret = yang_check_string(module, &((struct lys_tpdf *) node)->units, "units", "typedef", value);
Pavol Vican0df02b02016-03-01 10:28:50 +0100668 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200669 default:
670 free(value);
671 LOGINT;
672 ret = EXIT_FAILURE;
673 break;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100674 }
675 return ret;
676}
Pavol Vican5de33492016-02-22 14:03:24 +0100677
678int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100679yang_read_key(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres)
Pavol Vican5de33492016-02-22 14:03:24 +0100680{
681 char *exp, *value;
682
683 exp = value = (char *) list->keys;
Pavol Vicanbbe77822016-07-15 12:53:07 +0200684 list->keys_size = 0;
Pavol Vican5de33492016-02-22 14:03:24 +0100685 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 Vican0adf01d2016-03-22 12:29:33 +0100697 if (unres_schema_add_str(module, unres, list, UNRES_LIST_KEYS, exp) == -1) {
Pavol Vican5de33492016-02-22 14:03:24 +0100698 goto error;
699 }
700 free(exp);
701 return EXIT_SUCCESS;
702
703error:
704 free(exp);
705 return EXIT_FAILURE;
706}
707
708int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100709yang_fill_unique(struct lys_module *module, struct lys_node_list *list, struct lys_unique *unique, char *value, struct unres_schema *unres)
Pavol Vican85f12022016-03-05 16:30:35 +0100710{
711 int i, j;
712 char *vaux;
Radek Krejcid09d1a52016-08-11 14:05:45 +0200713 struct unres_list_uniq *unique_info;
Pavol Vican85f12022016-03-05 16:30:35 +0100714
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 Vican0adf01d2016-03-22 12:29:33 +0100743 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 Vican85f12022016-03-05 16:30:35 +0100745 goto error;
746 }
747 }
748 /* try to resolve leaf */
749 if (unres) {
Radek Krejcid09d1a52016-08-11 14:05:45 +0200750 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 Vican18b10212016-04-11 15:41:52 +0200755 goto error;
756 }
Pavol Vican85f12022016-03-05 16:30:35 +0100757 } else {
Radek Krejcid09d1a52016-08-11 14:05:45 +0200758 if (resolve_unique((struct lys_node *)list, unique->expr[i], &unique->trg_type)) {
Pavol Vican85f12022016-03-05 16:30:35 +0100759 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
772error:
773 return EXIT_FAILURE;
774}
775
776int
Pavol Vican5de33492016-02-22 14:03:24 +0100777yang_read_unique(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres)
778{
779 uint8_t k;
Pavol Vican0adf01d2016-03-22 12:29:33 +0100780 char *str;
Pavol Vican5de33492016-02-22 14:03:24 +0100781
782 for(k=0; k<list->unique_size; k++) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100783 str = (char *)list->unique[k].expr;
784 if (yang_fill_unique(module, list, &list->unique[k], str, unres)) {
Pavol Vican5de33492016-02-22 14:03:24 +0100785 goto error;
786 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100787 free(str);
Pavol Vican5de33492016-02-22 14:03:24 +0100788 }
789 return EXIT_SUCCESS;
790
791error:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100792 free(str);
Pavol Vican5de33492016-02-22 14:03:24 +0100793 return EXIT_FAILURE;
Pavol Vican73e7c992016-02-24 12:18:05 +0100794}
795
Pavol Vican1ff0e222016-02-26 12:27:01 +0100796static int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100797yang_read_identyref(struct lys_module *module, struct lys_type *type, struct unres_schema *unres)
Pavol Vican1ff0e222016-02-26 12:27:01 +0100798{
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 Vican0adf01d2016-03-22 12:29:33 +0100804 value = transform_schema2json(module, value);
Pavol Vican1ff0e222016-02-26 12:27:01 +0100805 if (!value) {
806 goto end;
807 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100808 rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value);
Pavol Vican1ff0e222016-02-26 12:27:01 +0100809 lydict_remove(module->ctx, value);
810
811 if (rc == -1) {
812 goto end;
813 }
814
815 ret = EXIT_SUCCESS;
816
817end:
818 lydict_remove(module->ctx, tmp);
819 return ret;
820}
821
Pavol Vican73e7c992016-02-24 12:18:05 +0100822int
Radek Krejci3a5501d2016-07-18 22:03:34 +0200823yang_check_type(struct lys_module *module, struct lys_node *parent, struct yang_type *typ, int tpdftype, struct unres_schema *unres)
Pavol Vican73e7c992016-02-24 12:18:05 +0100824{
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200825 int i, j, rc;
Pavol Vican73e7c992016-02-24 12:18:05 +0100826 int ret = -1;
827 const char *name, *value;
Pavol Vican8bd72e42016-08-29 09:53:05 +0200828 LY_DATA_TYPE base = 0;
Radek Krejci3a5501d2016-07-18 22:03:34 +0200829 struct lys_node *siter;
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200830 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 Vican0adf01d2016-03-22 12:29:33 +0100835 value = transform_schema2json(module, typ->name);
Pavol Vican73e7c992016-02-24 12:18:05 +0100836 if (!value) {
837 goto error;
838 }
839
840 i = parse_identifier(value);
841 if (i < 1) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100842 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]);
Pavol Vican73e7c992016-02-24 12:18:05 +0100843 lydict_remove(module->ctx, value);
844 goto error;
845 }
Radek Krejci408ab2f2016-06-06 15:27:10 +0200846 /* module name */
Pavol Vican73e7c992016-02-24 12:18:05 +0100847 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 Vican0adf01d2016-03-22 12:29:33 +0100852 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name);
Pavol Vican73e7c992016-02-24 12:18:05 +0100853 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 Vican73e7c992016-02-24 12:18:05 +0100860 if (rc == -1) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100861 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, typ->type->module_name);
Radek Krejci408ab2f2016-06-06 15:27:10 +0200862 lydict_remove(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +0100863 goto error;
864
Michal Vasko01c6fd22016-05-20 11:43:05 +0200865 /* the type could not be resolved or it was resolved to an unresolved typedef or leafref */
Pavol Vican73e7c992016-02-24 12:18:05 +0100866 } else if (rc == EXIT_FAILURE) {
Michal Vasko01c6fd22016-05-20 11:43:05 +0200867 LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name);
Radek Krejci408ab2f2016-06-06 15:27:10 +0200868 lydict_remove(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +0100869 ret = EXIT_FAILURE;
870 goto error;
871 }
Radek Krejci408ab2f2016-06-06 15:27:10 +0200872 lydict_remove(module->ctx, value);
Radek Krejcic13db382016-08-16 10:52:42 +0200873
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 Vican8bd72e42016-08-29 09:53:05 +0200889 base = typ->base;
Pavol Vican73e7c992016-02-24 12:18:05 +0100890 typ->type->base = typ->type->der->type.base;
891 if (base == 0) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100892 base = typ->type->der->type.base;
Pavol Vican73e7c992016-02-24 12:18:05 +0100893 }
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 Vican0adf01d2016-03-22 12:29:33 +0100898 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Binary type could not include pattern statement.");
Pavol Vican73e7c992016-02-24 12:18:05 +0100899 goto error;
900 }
901 typ->type->info.binary.length = typ->type->info.str.length;
Pavol Vican07ea68d2016-02-25 12:01:37 +0100902 if (typ->type->info.binary.length && lyp_check_length_range(typ->type->info.binary.length->expr, typ->type)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100903 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.binary.length->expr, "length");
Pavol Vican73e7c992016-02-24 12:18:05 +0100904 goto error;
905 }
906 } else if (typ->type->base == LY_TYPE_STRING) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100907 if (typ->type->info.str.length && lyp_check_length_range(typ->type->info.str.length->expr, typ->type)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100908 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.str.length->expr, "length");
Pavol Vican73e7c992016-02-24 12:18:05 +0100909 goto error;
910 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100911 } else {
Pavol Vican933aa5a2016-04-09 21:05:46 +0200912 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicanaff5c802016-02-24 15:56:45 +0100913 goto error;
Pavol Vican73e7c992016-02-24 12:18:05 +0100914 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100915 break;
916 case LY_TYPE_DEC64:
917 if (typ->type->base == LY_TYPE_DEC64) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100918 if (typ->type->info.dec64.range && lyp_check_length_range(typ->type->info.dec64.range->expr, typ->type)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100919 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.dec64.range->expr, "range");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100920 goto error;
921 }
Pavol Vican07ea68d2016-02-25 12:01:37 +0100922 /* 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 Vican0adf01d2016-03-22 12:29:33 +0100925 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type");
Pavol Vican07ea68d2016-02-25 12:01:37 +0100926 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 Vican0adf01d2016-03-22 12:29:33 +0100930 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits");
Pavol Vican07ea68d2016-02-25 12:01:37 +0100931 goto error;
932 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100933 } else if (typ->type->base >= LY_TYPE_INT8 && typ->type->base <=LY_TYPE_UINT64) {
934 if (typ->type->info.dec64.dig) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100935 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Numerical type could not include fraction statement.");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100936 goto error;
937 }
938 typ->type->info.num.range = typ->type->info.dec64.range;
Pavol Vican07ea68d2016-02-25 12:01:37 +0100939 if (typ->type->info.num.range && lyp_check_length_range(typ->type->info.num.range->expr, typ->type)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100940 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.num.range->expr, "range");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100941 goto error;
942 }
943 } else {
Pavol Vican933aa5a2016-04-09 21:05:46 +0200944 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicanaff5c802016-02-24 15:56:45 +0100945 goto error;
946 }
947 break;
Pavol Vican79a763d2016-02-25 15:41:27 +0100948 case LY_TYPE_ENUM:
949 if (typ->type->base != LY_TYPE_ENUM) {
Pavol Vican933aa5a2016-04-09 21:05:46 +0200950 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vican79a763d2016-02-25 15:41:27 +0100951 goto error;
952 }
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200953 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 Vican79a763d2016-02-25 15:41:27 +0100998 }
Pavol Vican1ff0e222016-02-26 12:27:01 +0100999 break;
Pavol Vican03a59442016-03-21 15:23:45 +01001000 case LY_TYPE_BITS:
1001 if (typ->type->base != LY_TYPE_BITS) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001002 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vican03a59442016-03-21 15:23:45 +01001003 goto error;
1004 }
Pavol Vican8ad2e0d2016-08-09 12:56:19 +02001005 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 Vican03a59442016-03-21 15:23:45 +01001050 }
Pavol Vican8ad2e0d2016-08-09 12:56:19 +02001051
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 Vican03a59442016-03-21 15:23:45 +01001063 }
1064 break;
Pavol Vican1ff0e222016-02-26 12:27:01 +01001065 case LY_TYPE_LEAFREF:
Pavol Vican6b072512016-04-04 10:50:21 +02001066 if (typ->type->base == LY_TYPE_IDENT && (typ->flags & LYS_TYPE_BASE)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001067 if (yang_read_identyref(module, typ->type, unres)) {
Pavol Vican1ff0e222016-02-26 12:27:01 +01001068 goto error;
1069 }
Pavol Vican191613a2016-02-26 16:21:32 +01001070 } else if (typ->type->base == LY_TYPE_LEAFREF) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001071 /* 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 Vican6b072512016-04-04 10:50:21 +02001080 if (typ->type->info.lref.path) {
Pavol Vican191613a2016-02-26 16:21:32 +01001081 value = typ->type->info.lref.path;
1082 /* store in the JSON format */
Pavol Vican0adf01d2016-03-22 12:29:33 +01001083 typ->type->info.lref.path = transform_schema2json(module, value);
Pavol Vican191613a2016-02-26 16:21:32 +01001084 lydict_remove(module->ctx, value);
1085 if (!typ->type->info.lref.path) {
1086 goto error;
1087 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001088 /* 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 Vican191613a2016-02-26 16:21:32 +01001096 goto error;
1097 }
Pavol Vican6b072512016-04-04 10:50:21 +02001098 } else if (!typ->type->der->type.der) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001099 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type");
Pavol Vican191613a2016-02-26 16:21:32 +01001100 goto error;
Michal Vasko01c6fd22016-05-20 11:43:05 +02001101 } else {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001102 /* 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 Vasko01c6fd22016-05-20 11:43:05 +02001106 goto error;
1107 }
Radek Krejci742be352016-07-17 12:18:54 +02001108
Michal Vasko01c6fd22016-05-20 11:43:05 +02001109 /* add pointer to leafref target, only on leaves (not in typedefs) */
Radek Krejci3a5501d2016-07-18 22:03:34 +02001110 if (typ->type->info.lref.target && lys_leaf_add_leafref_target(typ->type->info.lref.target, (struct lys_node *)typ->type->parent)) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02001111 goto error;
1112 }
Pavol Vican191613a2016-02-26 16:21:32 +01001113 }
Pavol Vican1ff0e222016-02-26 12:27:01 +01001114 } else {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001115 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vican1ff0e222016-02-26 12:27:01 +01001116 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 Vican0adf01d2016-03-22 12:29:33 +01001124 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type");
Pavol Vican1ff0e222016-02-26 12:27:01 +01001125 goto error;
1126 }
1127 break;
Pavol Vicana4f045a2016-02-29 15:01:20 +01001128 case LY_TYPE_UNION:
1129 if (typ->type->base != LY_TYPE_UNION) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001130 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicana4f045a2016-02-29 15:01:20 +01001131 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 Vican0adf01d2016-03-22 12:29:33 +01001138 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type");
Pavol Vicana4f045a2016-02-29 15:01:20 +01001139 goto error;
1140 }
1141 for (i = 0; i < typ->type->info.uni.count; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001142 if (unres_schema_add_node(module, unres, &typ->type->info.uni.types[i],
Michal Vasko5d631402016-07-21 13:15:15 +02001143 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Pavol Vicana4f045a2016-02-29 15:01:20 +01001144 goto error;
1145 }
1146 if (typ->type->info.uni.types[i].base == LY_TYPE_EMPTY) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001147 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", typ->name);
Pavol Vicana4f045a2016-02-29 15:01:20 +01001148 goto error;
1149 } else if (typ->type->info.uni.types[i].base == LY_TYPE_LEAFREF) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001150 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", typ->name);
Pavol Vicana4f045a2016-02-29 15:01:20 +01001151 goto error;
1152 }
1153 }
1154 break;
Pavol Vicana1827962016-02-29 15:39:42 +01001155
1156 default:
1157 if (base >= LY_TYPE_BINARY && base <= LY_TYPE_UINT64) {
1158 if (typ->type->base != base) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001159 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicana1827962016-02-29 15:39:42 +01001160 goto error;
1161 }
1162 } else {
1163 LOGINT;
1164 goto error;
1165 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001166 }
1167 return EXIT_SUCCESS;
1168
1169error:
1170 if (typ->type->module_name) {
1171 lydict_remove(module->ctx, typ->type->module_name);
1172 typ->type->module_name = NULL;
1173 }
Pavol Vican8bd72e42016-08-29 09:53:05 +02001174 if (base) {
1175 typ->type->base = base;
1176 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001177 return ret;
1178}
1179
1180void *
Pavol Vican5f0316a2016-04-05 21:21:11 +02001181yang_read_type(struct lys_module *module, void *parent, char *value, enum yytokentype type)
Pavol Vican73e7c992016-02-24 12:18:05 +01001182{
1183 struct yang_type *typ;
Pavol Vican4766aca2016-03-07 12:42:36 +01001184 struct type_deviation *dev;
1185 struct lys_tpdf *tmp_parent;
Pavol Vican73e7c992016-02-24 12:18:05 +01001186
Pavol Vicand01d8ae2016-03-01 10:45:59 +01001187 typ = calloc(1, sizeof *typ);
1188 if (!typ) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001189 LOGMEM;
1190 return NULL;
1191 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001192
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 Vicana55992a2016-03-01 13:37:17 +01001200 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 Vicana4f045a2016-02-29 15:01:20 +01001205 case UNION_KEYWORD:
1206 ((struct lys_type *)parent)->der = (struct lys_tpdf *)typ;
1207 typ->type = (struct lys_type *)parent;
1208 break;
Pavol Vican0df02b02016-03-01 10:28:50 +01001209 case TYPEDEF_KEYWORD:
1210 ((struct lys_tpdf *)parent)->type.der = (struct lys_tpdf *)typ;
1211 typ->type = &((struct lys_tpdf *)parent)->type;
Pavol Vican4766aca2016-03-07 12:42:36 +01001212 break;
1213 case REPLACE_KEYWORD:
1214 /* deviation replace type*/
1215 dev = (struct type_deviation *)parent;
1216 if (dev->deviate->type) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001217 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "deviation");
Pavol Vican4766aca2016-03-07 12:42:36 +01001218 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 Vican0adf01d2016-03-22 12:29:33 +01001226 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "type");
1227 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"type\" property.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001228 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 Vican5f0316a2016-04-05 21:21:11 +02001242 default:
1243 goto error;
1244 break;
Pavol Vican73e7c992016-02-24 12:18:05 +01001245 }
Pavol Vican5f0316a2016-04-05 21:21:11 +02001246 typ->name = lydict_insert_zc(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +01001247 return typ;
Pavol Vican4766aca2016-03-07 12:42:36 +01001248
1249error:
Pavol Vican5f0316a2016-04-05 21:21:11 +02001250 free(value);
Pavol Vican4766aca2016-03-07 12:42:36 +01001251 free(typ);
1252 return NULL;
Pavol Vican73e7c992016-02-24 12:18:05 +01001253}
1254
Pavol Vicanbe9e3832016-04-28 02:21:37 +02001255void
1256yang_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 Vican73e7c992016-02-24 12:18:05 +01001273void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001274yang_read_length(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vican73e7c992016-02-24 12:18:05 +01001275{
1276 struct lys_restr **length;
1277
Pavol Vican6b072512016-04-04 10:50:21 +02001278 if (typ->base == 0 || typ->base == LY_TYPE_STRING) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001279 length = &typ->type->info.str.length;
Pavol Vican6b072512016-04-04 10:50:21 +02001280 typ->base = LY_TYPE_STRING;
1281 } else if (typ->base == LY_TYPE_BINARY) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001282 length = &typ->type->info.binary.length;
1283 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001284 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected length statement.");
Pavol Vican73e7c992016-02-24 12:18:05 +01001285 goto error;
1286 }
1287
1288 if (*length) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001289 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "length", "type");
Pavol Vicanbe9e3832016-04-28 02:21:37 +02001290 goto error;
Pavol Vican73e7c992016-02-24 12:18:05 +01001291 }
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
1300error:
1301 free(value);
1302 return NULL;
1303
1304}
Pavol Vican1c203db2016-02-24 14:05:23 +01001305
Pavol Vican6eecf302016-08-10 11:09:05 +02001306int
1307yang_read_pattern(struct lys_module *module, struct lys_restr *pattern, char *value, char modifier)
Pavol Vican1c203db2016-02-24 14:05:23 +01001308{
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001309 char *buf;
1310 size_t len;
1311
Michal Vasko0aee5c12016-06-17 14:27:26 +02001312 if (lyp_check_pattern(value, NULL)) {
Pavol Vican1c203db2016-02-24 14:05:23 +01001313 free(value);
Pavol Vican6eecf302016-08-10 11:09:05 +02001314 return EXIT_FAILURE;
Pavol Vican1c203db2016-02-24 14:05:23 +01001315 }
Pavol Vican1c203db2016-02-24 14:05:23 +01001316
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001317 len = strlen(value);
1318 buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */
Pavol Vican6eecf302016-08-10 11:09:05 +02001319
1320 if (!buf) {
1321 LOGMEM;
1322 free(value);
1323 return EXIT_FAILURE;
1324 }
1325
1326 buf[0] = modifier;
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001327 strcpy(&buf[1], value);
1328 free(value);
1329
Pavol Vican6eecf302016-08-10 11:09:05 +02001330 pattern->expr = lydict_insert_zc(module->ctx, buf);
1331 return EXIT_SUCCESS;
Pavol Vican1c203db2016-02-24 14:05:23 +01001332}
Pavol Vicanaff5c802016-02-24 15:56:45 +01001333
1334void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001335yang_read_range(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vicanaff5c802016-02-24 15:56:45 +01001336{
Pavol Vican6b072512016-04-04 10:50:21 +02001337 if (typ->base != 0 && typ->base != LY_TYPE_DEC64) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001338 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected range statement.");
Pavol Vicanaff5c802016-02-24 15:56:45 +01001339 goto error;
1340 }
Pavol Vican6b072512016-04-04 10:50:21 +02001341 typ->base = LY_TYPE_DEC64;
Pavol Vicanaff5c802016-02-24 15:56:45 +01001342 if (typ->type->info.dec64.range) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001343 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "range", "type");
Pavol Vicanaff5c802016-02-24 15:56:45 +01001344 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
1354error:
1355 free(value);
1356 return NULL;
1357}
Pavol Vican07ea68d2016-02-25 12:01:37 +01001358
1359int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001360yang_read_fraction(struct yang_type *typ, uint32_t value)
Pavol Vican07ea68d2016-02-25 12:01:37 +01001361{
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001362 unsigned int i;
1363
Pavol Vican6b072512016-04-04 10:50:21 +02001364 if (typ->base == 0 || typ->base == LY_TYPE_DEC64) {
1365 typ->base = LY_TYPE_DEC64;
Pavol Vican07ea68d2016-02-25 12:01:37 +01001366 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001367 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected fraction-digits statement.");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001368 goto error;
1369 }
1370 if (typ->type->info.dec64.dig) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001371 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "fraction-digits", "type");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001372 goto error;
1373 }
1374 /* range check */
1375 if (value < 1 || value > 18) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001376 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", value, "fraction-digits");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001377 goto error;
1378 }
1379 typ->type->info.dec64.dig = value;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001380 typ->type->info.dec64.div = 10;
1381 for (i = 1; i < value; i++) {
1382 typ->type->info.dec64.div *= 10;
1383 }
Pavol Vican07ea68d2016-02-25 12:01:37 +01001384 return EXIT_SUCCESS;
1385
1386error:
1387 return EXIT_FAILURE;
1388}
Pavol Vican79a763d2016-02-25 15:41:27 +01001389
1390void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001391yang_read_enum(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vican79a763d2016-02-25 15:41:27 +01001392{
1393 struct lys_type_enum *enm;
1394 int i;
1395
Pavol Vicanc6662412016-08-30 08:06:28 +02001396 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 Vican79a763d2016-02-25 15:41:27 +01001403 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 Vican0adf01d2016-03-22 12:29:33 +01001408 LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, enm->name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001409 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 Vican0adf01d2016-03-22 12:29:33 +01001415 LOGVAL(LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001416 goto error;
1417 }
1418 }
1419
1420 typ->type->info.enums.count++;
1421 return enm;
1422
1423error:
1424 typ->type->info.enums.count++;
1425 return NULL;
1426}
1427
1428int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001429yang_check_enum(struct yang_type *typ, struct lys_type_enum *enm, int64_t *value, int assign)
Pavol Vican79a763d2016-02-25 15:41:27 +01001430{
1431 int i, j;
1432
1433 if (!assign) {
1434 /* assign value automatically */
1435 if (*value > INT32_MAX) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001436 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value");
Pavol Vican79a763d2016-02-25 15:41:27 +01001437 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 Vican0adf01d2016-03-22 12:29:33 +01001448 LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL,
Radek Krejcie663e012016-08-01 17:12:34 +02001449 typ->type->info.enums.enm[j].value, typ->type->info.enums.enm[j].name,
1450 typ->type->info.enums.enm[i].name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001451 goto error;
1452 }
1453 }
1454
1455 return EXIT_SUCCESS;
1456
1457error:
1458 return EXIT_FAILURE;
1459}
Pavol Vican9887c682016-02-29 11:32:01 +01001460
1461void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001462yang_read_bit(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vican9887c682016-02-29 11:32:01 +01001463{
1464 int i;
1465 struct lys_type_bit *bit;
1466
1467 bit = &typ->type->info.bits.bit[typ->type->info.bits.count];
Pavol Vican0adf01d2016-03-22 12:29:33 +01001468 if (lyp_check_identifier(value, LY_IDENT_SIMPLE, NULL, NULL)) {
Pavol Vican9887c682016-02-29 11:32:01 +01001469 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 Vican0adf01d2016-03-22 12:29:33 +01001477 LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, bit->name);
Pavol Vican9887c682016-02-29 11:32:01 +01001478 typ->type->info.bits.count++;
1479 goto error;
1480 }
1481 }
1482 typ->type->info.bits.count++;
1483 return bit;
1484
1485error:
1486 return NULL;
1487}
1488
1489int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001490yang_check_bit(struct yang_type *typ, struct lys_type_bit *bit, int64_t *value, int assign)
Pavol Vican9887c682016-02-29 11:32:01 +01001491{
1492 int i,j;
Pavol Vican9887c682016-02-29 11:32:01 +01001493
1494 if (!assign) {
1495 /* assign value automatically */
1496 if (*value > UINT32_MAX) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001497 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position");
Pavol Vican9887c682016-02-29 11:32:01 +01001498 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 Krejcie663e012016-08-01 17:12:34 +02001509 LOGVAL(LYE_BITS_DUPVAL, LY_VLOG_NONE, NULL, bit->pos, bit->name, typ->type->info.bits.bit[i].name);
Pavol Vican9887c682016-02-29 11:32:01 +01001510 goto error;
1511 }
1512 }
1513
Pavol Vican9887c682016-02-29 11:32:01 +01001514 return EXIT_SUCCESS;
1515
1516error:
1517 return EXIT_FAILURE;
1518}
Pavol Vican0df02b02016-03-01 10:28:50 +01001519
1520void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001521yang_read_typedef(struct lys_module *module, struct lys_node *parent, char *value)
Pavol Vican0df02b02016-03-01 10:28:50 +01001522{
1523 struct lys_tpdf *ret;
Pavol Vican810892e2016-07-12 16:55:34 +02001524 struct lys_node *root;
Pavol Vican0df02b02016-03-01 10:28:50 +01001525
Pavol Vican810892e2016-07-12 16:55:34 +02001526 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 Vican0df02b02016-03-01 10:28:50 +01001528 free(value);
1529 return NULL;
1530 }
Pavol Vican810892e2016-07-12 16:55:34 +02001531
Pavol Vican0df02b02016-03-01 10:28:50 +01001532 if (!parent) {
1533 ret = &module->tpdf[module->tpdf_size];
Pavol Vican0df02b02016-03-01 10:28:50 +01001534 module->tpdf_size++;
Pavol Vican21238f52016-03-01 12:39:52 +01001535 } 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 Vican21238f52016-03-01 12:39:52 +01001539 ((struct lys_node_grp *)parent)->tpdf_size++;
1540 break;
Pavol Vican535d50e2016-03-01 13:05:33 +01001541 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 Vican09f04b82016-03-01 14:02:28 +01001545 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 Vican52ed67d2016-03-02 17:46:15 +01001549 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02001550 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 Vican52ed67d2016-03-02 17:46:15 +01001553 break;
Pavol Vican531a9132016-03-03 10:10:09 +01001554 case LYS_INPUT:
1555 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02001556 ret = &((struct lys_node_inout *)parent)->tpdf[((struct lys_node_inout *)parent)->tpdf_size];
1557 ((struct lys_node_inout *)parent)->tpdf_size++;
Pavol Vican531a9132016-03-03 10:10:09 +01001558 break;
Pavol Vican41267fd2016-03-03 10:47:42 +01001559 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 Vicand1dbfda2016-03-21 10:03:58 +01001563 default:
1564 /* another type of nodetype is error*/
1565 LOGINT;
1566 free(value);
1567 return NULL;
Pavol Vican21238f52016-03-01 12:39:52 +01001568 }
Pavol Vican0df02b02016-03-01 10:28:50 +01001569 }
1570
Pavol Vican79436472016-04-04 11:22:02 +02001571 ret->type.parent = ret;
Pavol Vican0df02b02016-03-01 10:28:50 +01001572 ret->name = lydict_insert_zc(module->ctx, value);
1573 ret->module = module;
1574 return ret;
1575}
Pavol Vican1003ead2016-03-02 12:24:52 +01001576
1577void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001578yang_read_refine(struct lys_module *module, struct lys_node_uses *uses, char *value)
Pavol Vican1003ead2016-03-02 12:24:52 +01001579{
1580 struct lys_refine *rfn;
1581
1582 rfn = &uses->refine[uses->refine_size];
1583 uses->refine_size++;
Pavol Vican0adf01d2016-03-22 12:29:33 +01001584 rfn->target_name = transform_schema2json(module, value);
Pavol Vican1003ead2016-03-02 12:24:52 +01001585 free(value);
1586 if (!rfn->target_name) {
1587 return NULL;
1588 }
1589 return rfn;
1590}
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001591
1592void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001593yang_read_augment(struct lys_module *module, struct lys_node *parent, char *value)
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001594{
1595 struct lys_node_augment *aug;
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001596
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 Vican0adf01d2016-03-22 12:29:33 +01001603 aug->target_name = transform_schema2json(module, value);
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001604 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 Vican220e5a12016-03-03 14:19:43 +01001617
1618void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001619yang_read_deviation(struct lys_module *module, char *value)
Pavol Vican220e5a12016-03-03 14:19:43 +01001620{
1621 struct lys_node *dev_target = NULL;
1622 struct lys_deviation *dev;
1623 struct type_deviation *deviation = NULL;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001624 int rc;
Pavol Vican220e5a12016-03-03 14:19:43 +01001625
Pavol Vican220e5a12016-03-03 14:19:43 +01001626 dev = &module->deviation[module->deviation_size];
Pavol Vican0adf01d2016-03-22 12:29:33 +01001627 dev->target_name = transform_schema2json(module, value);
Pavol Vican220e5a12016-03-03 14:19:43 +01001628 free(value);
1629 if (!dev->target_name) {
1630 goto error;
1631 }
1632
Pavol Vican974377b2016-03-23 00:38:53 +01001633 deviation = calloc(1, sizeof *deviation);
1634 if (!deviation) {
1635 LOGMEM;
1636 goto error;
1637 }
1638
Pavol Vican220e5a12016-03-03 14:19:43 +01001639 /* 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 Vasko75c8daf2016-05-19 10:56:39 +02001642 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation");
Pavol Vican220e5a12016-03-03 14:19:43 +01001643 goto error;
1644 }
Radek Krejcic4283442016-04-22 09:19:27 +02001645 if (dev_target->module == lys_main_module(module)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001646 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 Vican220e5a12016-03-03 14:19:43 +01001648 goto error;
1649 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001650
Michal Vasko89563fc2016-07-28 16:19:35 +02001651 lys_node_module(dev_target)->deviated = 1;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001652
Pavol Vican220e5a12016-03-03 14:19:43 +01001653 /*save pointer to the deviation and deviated target*/
1654 deviation->deviation = dev;
1655 deviation->target = dev_target;
1656
Pavol Vican220e5a12016-03-03 14:19:43 +01001657 return deviation;
1658
1659error:
1660 free(deviation);
1661 lydict_remove(module->ctx, dev->target_name);
1662 return NULL;
1663}
Pavol Vican4c90c642016-03-03 15:06:47 +01001664
1665int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001666yang_read_deviate_unsupported(struct type_deviation *dev)
Pavol Vican4c90c642016-03-03 15:06:47 +01001667{
1668 int i;
1669
1670 if (dev->deviation->deviate_size) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001671 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
Pavol Vican4c90c642016-03-03 15:06:47 +01001672 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 Vican0adf01d2016-03-22 12:29:33 +01001680 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 Vican4c90c642016-03-03 15:06:47 +01001682 return EXIT_FAILURE;
1683 }
1684 }
1685 }
Michal Vaskofe7e5a72016-05-02 14:49:23 +02001686
Pavol Vican4c90c642016-03-03 15:06:47 +01001687 /* unlink and store the original node */
Michal Vaskod921d682016-05-19 10:56:51 +02001688 lys_node_unlink(dev->target);
Pavol Vican4c90c642016-03-03 15:06:47 +01001689 dev->deviation->orig_node = dev->target;
1690
1691 dev->deviation->deviate_size = 1;
1692 return EXIT_SUCCESS;
1693}
Pavol Vican85f12022016-03-05 16:30:35 +01001694
1695int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001696yang_read_deviate(struct type_deviation *dev, LYS_DEVIATE_TYPE mod)
Pavol Vican85f12022016-03-05 16:30:35 +01001697{
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 Vican0adf01d2016-03-22 12:29:33 +01001704 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 Vican85f12022016-03-05 16:30:35 +01001706 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
1723int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001724yang_read_deviate_units(struct ly_ctx *ctx, struct type_deviation *dev, char *value)
Pavol Vican85f12022016-03-05 16:30:35 +01001725{
1726 const char **stritem;
1727
1728 if (dev->deviate->units) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001729 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "units", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001730 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 Vican0adf01d2016-03-22 12:29:33 +01001740 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1741 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"units\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001742 free(value);
1743 goto error;
1744 }
1745
1746 dev->deviate->units = lydict_insert_zc(ctx, value);
1747
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001748 if (dev->deviate->mod == LY_DEVIATE_DEL) {
1749 /* check values */
Michal Vaskob42b6972016-06-06 14:21:30 +02001750 if (!ly_strequal(*stritem, dev->deviate->units, 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001751 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 Vicanc1f5a502016-03-06 16:51:26 +01001753 goto error;
1754 }
1755 /* remove current units value of the target */
1756 lydict_remove(ctx, *stritem);
Pavol Vican4766aca2016-03-07 12:42:36 +01001757 } else {
1758 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1759 /* check that there is no current value */
1760 if (*stritem) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001761 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1762 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001763 goto error;
1764 }
1765 } else { /* replace */
1766 if (!*stritem) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001767 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1768 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001769 goto error;
1770 }
1771 }
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001772 /* 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 Vican85f12022016-03-05 16:30:35 +01001779 return EXIT_SUCCESS;
1780
1781error:
1782 return EXIT_FAILURE;
1783}
1784
1785int
Pavol Vican974377b2016-03-23 00:38:53 +01001786yang_read_deviate_must(struct type_deviation *dev, uint8_t c_must)
Pavol Vican85f12022016-03-05 16:30:35 +01001787{
Pavol Vican85f12022016-03-05 16:30:35 +01001788 /* 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 Krejcibf2abff2016-08-23 15:51:52 +02001807 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 Vican85f12022016-03-05 16:30:35 +01001810 break;
1811 default:
Pavol Vican0adf01d2016-03-22 12:29:33 +01001812 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must");
1813 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001814 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 Vicanc1f5a502016-03-06 16:51:26 +01001827 } 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 Vican85f12022016-03-05 16:30:35 +01001834 }
1835
1836 return EXIT_SUCCESS;
1837
1838error:
1839 return EXIT_FAILURE;
1840}
1841
1842int
Pavol Vican974377b2016-03-23 00:38:53 +01001843yang_read_deviate_unique(struct type_deviation *dev, uint8_t c_uniq)
Pavol Vican85f12022016-03-05 16:30:35 +01001844{
Pavol Vican85f12022016-03-05 16:30:35 +01001845 struct lys_node_list *list;
1846
1847 /* check target node type */
1848 if (dev->target->nodetype != LYS_LIST) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001849 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique");
1850 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"unique\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001851 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 Vicanc1f5a502016-03-06 16:51:26 +01001866 } 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 Vican85f12022016-03-05 16:30:35 +01001873 }
1874
1875 return EXIT_SUCCESS;
1876
1877error:
1878 return EXIT_FAILURE;
1879}
1880
1881int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001882yang_read_deviate_default(struct ly_ctx *ctx, struct type_deviation *dev, char *value)
Pavol Vican85f12022016-03-05 16:30:35 +01001883{
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 Vican0adf01d2016-03-22 12:29:33 +01001890 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "default", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001891 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 Vican85f12022016-03-05 16:30:35 +01001900 rc = resolve_choice_default_schema_nodeid(dev->deviate->dflt, choice->child, (const struct lys_node **)&node);
1901 if (rc || !node) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001902 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default");
Pavol Vican85f12022016-03-05 16:30:35 +01001903 goto error;
1904 }
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001905 if (dev->deviate->mod == LY_DEVIATE_DEL) {
1906 if (!choice->dflt || (choice->dflt != node)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001907 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 Vicanc1f5a502016-03-06 16:51:26 +01001909 goto error;
1910 }
1911 choice->dflt = NULL;
Pavol Vican4766aca2016-03-07 12:42:36 +01001912 } else {
1913 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1914 /* check that there is no current value */
1915 if (choice->dflt) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001916 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1917 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001918 goto error;
Pavol Vican321a02e2016-04-05 16:11:59 +02001919 } 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 Vican4766aca2016-03-07 12:42:36 +01001923 }
1924 } else { /* replace*/
1925 if (!choice->dflt) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001926 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1927 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001928 goto error;
1929 }
1930 }
1931
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001932 choice->dflt = node;
1933 if (!choice->dflt) {
1934 /* default branch not found */
Pavol Vican0adf01d2016-03-22 12:29:33 +01001935 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001936 goto error;
1937 }
Pavol Vican85f12022016-03-05 16:30:35 +01001938 }
1939 } else if (dev->target->nodetype == LYS_LEAF) {
1940 leaf = (struct lys_node_leaf *)dev->target;
1941
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001942 if (dev->deviate->mod == LY_DEVIATE_DEL) {
Michal Vaskob42b6972016-06-06 14:21:30 +02001943 if (!leaf->dflt || !ly_strequal(leaf->dflt, dev->deviate->dflt, 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001944 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 Vicanc1f5a502016-03-06 16:51:26 +01001946 goto error;
1947 }
1948 /* remove value */
1949 lydict_remove(ctx, leaf->dflt);
1950 leaf->dflt = NULL;
Pavol Vican4766aca2016-03-07 12:42:36 +01001951 } else {
1952 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1953 /* check that there is no current value */
1954 if (leaf->dflt) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001955 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1956 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001957 goto error;
Pavol Vicanfdf81c42016-04-05 15:55:31 +02001958 } 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 Vican4766aca2016-03-07 12:42:36 +01001963 }
1964 } else { /* replace*/
1965 if (!leaf->dflt) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001966 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1967 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001968 goto error;
1969 }
1970 }
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001971 /* remove value */
1972 lydict_remove(ctx, leaf->dflt);
Pavol Vican85f12022016-03-05 16:30:35 +01001973
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001974 /* set new value */
1975 leaf->dflt = lydict_insert(ctx, dev->deviate->dflt, 0);
1976 }
Pavol Vican85f12022016-03-05 16:30:35 +01001977 } else {
1978 /* invalid target for default value */
Pavol Vican0adf01d2016-03-22 12:29:33 +01001979 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1980 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001981 goto error;
1982 }
1983
1984 return EXIT_SUCCESS;
1985
1986error:
1987 return EXIT_FAILURE;
1988}
1989
1990int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001991yang_read_deviate_config(struct type_deviation *dev, uint8_t value)
Pavol Vican85f12022016-03-05 16:30:35 +01001992{
1993 if (dev->deviate->flags & LYS_CONFIG_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001994 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "config", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001995 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
2014error:
2015 return EXIT_FAILURE;
2016}
2017
2018int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002019yang_read_deviate_mandatory(struct type_deviation *dev, uint8_t value)
Pavol Vican85f12022016-03-05 16:30:35 +01002020{
Radek Krejcie00d2312016-08-12 15:27:49 +02002021 struct lys_node *parent;
2022
Pavol Vican85f12022016-03-05 16:30:35 +01002023 if (dev->deviate->flags & LYS_MAND_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002024 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "mandatory", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01002025 goto error;
2026 }
2027
2028 /* check target node type */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002029 if (!(dev->target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002030 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
2031 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"mandatory\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01002032 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 Vican0adf01d2016-03-22 12:29:33 +01002040 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
2041 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican85f12022016-03-05 16:30:35 +01002042 goto error;
Pavol Vican321a02e2016-04-05 16:11:59 +02002043 } 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 Vican85f12022016-03-05 16:30:35 +01002054 }
Pavol Vican4766aca2016-03-07 12:42:36 +01002055 } else { /* replace */
2056 if (!(dev->target->flags & LYS_MAND_MASK)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002057 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
2058 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01002059 goto error;
2060 }
Pavol Vican85f12022016-03-05 16:30:35 +01002061 }
2062
Pavol Vican85f12022016-03-05 16:30:35 +01002063 /* 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 Krejcie00d2312016-08-12 15:27:49 +02002069 /* 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 Vican85f12022016-03-05 16:30:35 +01002085 return EXIT_SUCCESS;
2086
2087error:
2088 return EXIT_FAILURE;
2089}
2090
2091int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002092yang_read_deviate_minmax(struct type_deviation *dev, uint32_t value, int type)
Pavol Vican85f12022016-03-05 16:30:35 +01002093{
Pavol Vican09adcc32016-08-25 10:51:36 +02002094 uint32_t *ui32val, *min, *max;
Pavol Vican85f12022016-03-05 16:30:35 +01002095
2096 /* check target node type */
2097 if (dev->target->nodetype == LYS_LEAFLIST) {
Pavol Vican09adcc32016-08-25 10:51:36 +02002098 max = &((struct lys_node_leaflist *)dev->target)->max;
2099 min = &((struct lys_node_leaflist *)dev->target)->min;
Pavol Vican85f12022016-03-05 16:30:35 +01002100 } else if (dev->target->nodetype == LYS_LIST) {
Pavol Vican09adcc32016-08-25 10:51:36 +02002101 max = &((struct lys_node_list *)dev->target)->max;
2102 min = &((struct lys_node_list *)dev->target)->min;
Pavol Vican85f12022016-03-05 16:30:35 +01002103 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002104 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 Vican85f12022016-03-05 16:30:35 +01002106 goto error;
2107 }
2108
2109 if (type) {
2110 dev->deviate->max = value;
Pavol Vican4766aca2016-03-07 12:42:36 +01002111 dev->deviate->max_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02002112 ui32val = max;
Pavol Vican85f12022016-03-05 16:30:35 +01002113 } else {
2114 dev->deviate->min = value;
Pavol Vican4766aca2016-03-07 12:42:36 +01002115 dev->deviate->min_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02002116 ui32val = min;
Pavol Vican85f12022016-03-05 16:30:35 +01002117 }
2118
2119 if (dev->deviate->mod == LY_DEVIATE_ADD) {
2120 /* check that there is no current value */
2121 if (*ui32val) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002122 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 Vican85f12022016-03-05 16:30:35 +01002124 goto error;
2125 }
Pavol Vican4766aca2016-03-07 12:42:36 +01002126 } 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 Vican85f12022016-03-05 16:30:35 +01002129 }
2130
2131 /* add (already checked) and replace */
2132 /* set new value specified in deviation */
2133 *ui32val = value;
2134
Pavol Vican09adcc32016-08-25 10:51:36 +02002135 /* 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 Vican85f12022016-03-05 16:30:35 +01002147 return EXIT_SUCCESS;
2148
2149error:
2150 return EXIT_FAILURE;
2151}
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002152
2153int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002154yang_check_deviate_must(struct ly_ctx *ctx, struct type_deviation *dev)
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002155{
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 Vican0adf01d2016-03-22 12:29:33 +01002189 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 Vicanc1f5a502016-03-06 16:51:26 +01002191 return EXIT_FAILURE;
2192 }
2193
2194 return EXIT_SUCCESS;
2195}
2196
2197int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002198yang_check_deviate_unique(struct lys_module *module, struct type_deviation *dev, char *value)
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002199{
2200 struct lys_node_list *list;
2201 int i, j;
2202
2203 list = (struct lys_node_list *)dev->target;
Pavol Vican0adf01d2016-03-22 12:29:33 +01002204 if (yang_fill_unique(module, list, &dev->deviate->unique[dev->deviate->unique_size], value, NULL)) {
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002205 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 Vican0adf01d2016-03-22 12:29:33 +01002250 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 Vicanc1f5a502016-03-06 16:51:26 +01002252 goto error;
2253 }
2254
2255 free(value);
2256 return EXIT_SUCCESS;
2257
2258error:
2259 free(value);
2260 return EXIT_FAILURE;
2261}
Pavol Vicane92421d2016-03-08 10:12:33 +01002262
2263int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002264yang_check_deviation(struct lys_module *module, struct type_deviation *dev, struct unres_schema *unres)
Pavol Vicane92421d2016-03-08 10:12:33 +01002265{
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 Vican0adf01d2016-03-22 12:29:33 +01002272 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 Vicane92421d2016-03-08 10:12:33 +01002273 if (rc == -1) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002274 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Leaf \"%s\" default value no longer matches its type.", dev->deviation->target_name);
Pavol Vicane92421d2016-03-08 10:12:33 +01002275 return EXIT_FAILURE;
2276 }
2277 break;
2278 }
2279 }
2280 }
2281 }
2282 return EXIT_SUCCESS;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002283}
2284
2285int
2286yang_fill_include(struct lys_module *module, struct lys_submodule *submodule, char *value,
Pavol Vicane024ab72016-07-27 14:27:43 +02002287 struct lys_include *inc, struct unres_schema *unres)
Pavol Vican9b89dda2016-03-09 15:36:55 +01002288{
Pavol Vican9b89dda2016-03-09 15:36:55 +01002289 struct lys_module *trg;
Pavol Vican55870412016-03-10 12:36:21 +01002290 const char *str;
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002291 int rc;
Radek Krejci83e3f5b2016-06-24 14:55:25 +02002292 int ret = 0;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002293
Pavol Vican55870412016-03-10 12:36:21 +01002294 str = lydict_insert_zc(module->ctx, value);
Pavol Vican9b89dda2016-03-09 15:36:55 +01002295 trg = (submodule) ? (struct lys_module *)submodule : module;
Pavol Vicane024ab72016-07-27 14:27:43 +02002296 rc = lyp_check_include(module, submodule, str, inc, unres);
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002297 if (!rc) {
2298 /* success, copy the filled data into the final array */
Pavol Vicane024ab72016-07-27 14:27:43 +02002299 memcpy(&trg->inc[trg->inc_size], inc, sizeof *inc);
Radek Krejci4dcd3392016-06-22 10:28:40 +02002300 trg->inc_size++;
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002301 } else if (rc == -1) {
Radek Krejci83e3f5b2016-06-24 14:55:25 +02002302 ret = -1;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002303 }
Pavol Vican9b89dda2016-03-09 15:36:55 +01002304
Pavol Vican55870412016-03-10 12:36:21 +01002305 lydict_remove(module->ctx, str);
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002306 return ret;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002307}
Pavol Vicanf4717e62016-03-16 11:30:01 +01002308
2309int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002310yang_use_extension(struct lys_module *module, struct lys_node *data_node, void *actual, char *value)
Pavol Vicanf4717e62016-03-16 11:30:01 +01002311{
2312 char *prefix;
2313 char *identif;
2314 const char *ns = NULL;
2315 int i;
2316
Pavol Vicanf4717e62016-03-16 11:30:01 +01002317 /* check to the same pointer */
2318 if (data_node != actual) {
2319 return EXIT_SUCCESS;
2320 }
2321
Pavol Vicana302aa62016-03-17 10:45:35 +01002322 prefix = strdup(value);
2323 if (!prefix) {
2324 LOGMEM;
2325 goto error;
2326 }
2327 /* find prefix anf identificator*/
2328 identif = strchr(prefix, ':');
Pavol Vicanfbd02782016-08-29 11:14:45 +02002329 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 Vicana302aa62016-03-17 10:45:35 +01002334 *identif = '\0';
2335 identif++;
2336
Pavol Vicanf4717e62016-03-16 11:30:01 +01002337 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 Vican1ff1b7b2016-04-07 09:51:49 +02002343 if (!ns && !strcmp(module->prefix, prefix)) {
2344 ns = (module->type) ? ((struct lys_submodule *)module)->belongsto->ns : module->ns;
2345 }
Pavol Vicanf4717e62016-03-16 11:30:01 +01002346 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 Vican0adf01d2016-03-22 12:29:33 +01002352 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, identif);
Pavol Vicana302aa62016-03-17 10:45:35 +01002353 goto error;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002354 }
2355 }
Pavol Vicana302aa62016-03-17 10:45:35 +01002356 free(prefix);
Pavol Vicanf4717e62016-03-16 11:30:01 +01002357 return EXIT_SUCCESS;
Pavol Vicana302aa62016-03-17 10:45:35 +01002358
2359error:
2360 free(prefix);
2361 return EXIT_FAILURE;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002362}
2363
2364void
2365nacm_inherit(struct lys_module *module)
2366{
Pavol Vican10ffba52016-04-04 12:21:22 +02002367 struct lys_node *next, *elem, *tmp_node, *tmp_child;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002368
2369 LY_TREE_DFS_BEGIN(module->data, next, elem) {
Pavol Vican10ffba52016-04-04 12:21:22 +02002370 tmp_node = NULL;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002371 if (elem->parent) {
2372 switch (elem->nodetype) {
Pavol Vican10ffba52016-04-04 12:21:22 +02002373 case LYS_GROUPING:
2374 /* extension nacm not inherited*/
2375 break;
2376 case LYS_CHOICE:
2377 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002378 case LYS_ANYDATA:
Pavol Vican10ffba52016-04-04 12:21:22 +02002379 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 Vicanf4717e62016-03-16 11:30:01 +01002401 }
2402 }
2403 LY_TREE_DFS_END(module->data, next, elem);
Pavol Vican10ffba52016-04-04 12:21:22 +02002404 if (tmp_node) {
2405 tmp_node->child = tmp_child;
2406 }
Pavol Vicanf4717e62016-03-16 11:30:01 +01002407 }
2408}
Pavol Vican4fb66c92016-03-17 10:32:27 +01002409
Michal Vaskoe1e351e2016-08-25 12:13:39 +02002410int
Pavol Vican4fb66c92016-03-17 10:32:27 +01002411store_flags(struct lys_node *node, uint8_t flags, int config_inherit)
2412{
Michal Vaskoe10d14a2016-08-26 15:17:13 +02002413 struct lys_node *elem;
2414
Pavol Vican4fb66c92016-03-17 10:32:27 +01002415 node->flags |= flags;
Michal Vaskoe1e351e2016-08-25 12:13:39 +02002416 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 Vican4fb66c92016-03-17 10:32:27 +01002425 }
Michal Vaskoe10d14a2016-08-26 15:17:13 +02002426 } 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 Vican4fb66c92016-03-17 10:32:27 +01002435 }
Michal Vaskoe1e351e2016-08-25 12:13:39 +02002436
2437 return EXIT_SUCCESS;
Pavol Vican4fb66c92016-03-17 10:32:27 +01002438}
Pavol Vican8e7110b2016-03-22 17:00:26 +01002439
Pavol Vican1938d882016-04-10 13:36:31 +02002440static int
2441yang_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 Vican8e7110b2016-03-22 17:00:26 +01002443{
2444 YY_BUFFER_STATE bp;
Pavol Vican802af1e2016-03-23 20:42:26 +01002445 yyscan_t scanner = NULL;
Pavol Vican1938d882016-04-10 13:36:31 +02002446 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
2459int
2460yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, const char *data, unsigned int size_data)
2461{
Pavol Vican8e7110b2016-03-22 17:00:26 +01002462 struct lys_array_size *size_arrays=NULL;
Pavol Vican974377b2016-03-23 00:38:53 +01002463 unsigned int size;
Pavol Vican1938d882016-04-10 13:36:31 +02002464 int ret;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002465
2466 size_arrays = calloc(1, sizeof *size_arrays);
2467 if (!size_arrays) {
2468 LOGMEM;
Pavol Vicanf7994fb2016-04-05 21:55:53 +02002469 return EXIT_FAILURE;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002470 }
Pavol Vican8e7110b2016-03-22 17:00:26 +01002471 size = (size_data) ? size_data : strlen(data) + 2;
Pavol Vican1938d882016-04-10 13:36:31 +02002472 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 Vican8e7110b2016-03-22 17:00:26 +01002475 }
Pavol Vican8e7110b2016-03-22 17:00:26 +01002476 free(size_arrays->node);
2477 free(size_arrays);
Pavol Vican1938d882016-04-10 13:36:31 +02002478 return ret;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002479}
2480
2481struct lys_module *
Pavol Vican974377b2016-03-23 00:38:53 +01002482yang_read_module(struct ly_ctx *ctx, const char* data, unsigned int size, const char *revision, int implement)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002483{
2484
Pavol Vican10ffba52016-04-04 12:21:22 +02002485 struct lys_module *tmp_module, *module = NULL;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002486 struct unres_schema *unres = NULL;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002487 int i;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002488
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 Vicanf7994fb2016-04-05 21:55:53 +02002498 goto error;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002499 }
2500
2501 /* initiale module */
Michal Vaskofe7e5a72016-05-02 14:49:23 +02002502 module->ctx = ctx;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002503 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 Vican10ffba52016-04-04 12:21:22 +02002523 tmp_module = module;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002524 if (lyp_ctx_add_module(&module)) {
Pavol Vican8e7110b2016-03-22 17:00:26 +01002525 goto error;
2526 }
2527
Pavol Vican10ffba52016-04-04 12:21:22 +02002528 if (module == tmp_module) {
2529 nacm_inherit(module);
2530 }
2531
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002532 if (module->augment_size || module->deviation_size) {
Michal Vasko5cec3312016-05-04 08:59:41 +02002533 if (!module->implemented) {
2534 LOGVRB("Module \"%s\" includes augments or deviations, changing conformance to \"implement\".", module->name);
2535 }
Michal Vasko26055752016-05-03 11:36:31 +02002536 if (lys_module_set_implement(module)) {
2537 goto error;
2538 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002539
Michal Vasko26055752016-05-03 11:36:31 +02002540 if (lys_sub_module_set_dev_aug_target_implement(module)) {
2541 goto error;
2542 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002543 for (i = 0; i < module->inc_size; ++i) {
Radek Krejci4dcd3392016-06-22 10:28:40 +02002544 if (!module->inc[i].submodule) {
2545 continue;
2546 }
Michal Vasko26055752016-05-03 11:36:31 +02002547 if (lys_sub_module_set_dev_aug_target_implement((struct lys_module *)module->inc[i].submodule)) {
2548 goto error;
2549 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002550 }
2551 }
2552
Pavol Vican8e7110b2016-03-22 17:00:26 +01002553 unres_schema_free(NULL, &unres);
2554 LOGVRB("Module \"%s\" successfully parsed.", module->name);
2555 return module;
2556
2557error:
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002558 /* cleanup */
Pavol Vican8e7110b2016-03-22 17:00:26 +01002559 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 Vasko9eb6dd02016-05-02 14:52:40 +02002567
2568 lys_sub_module_remove_devs_augs(module);
2569 lys_free(module, NULL, 1);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002570 return NULL;
2571}
2572
2573struct lys_submodule *
Pavol Vican974377b2016-03-23 00:38:53 +01002574yang_read_submodule(struct lys_module *module, const char *data, unsigned int size, struct unres_schema *unres)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002575{
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 Vican8e7110b2016-03-22 17:00:26 +01002592 LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002593 return submodule;
2594
2595error:
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 Vasko9eb6dd02016-05-02 14:52:40 +02002607 lys_sub_module_remove_devs_augs((struct lys_module *)submodule);
2608 lys_submodule_module_data_free(submodule);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002609 lys_submodule_free(submodule, NULL);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002610 return NULL;
2611}
Pavol Vican8760bb72016-04-07 09:44:01 +02002612
2613static int
2614count_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
2626static int
2627read_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
2652char *
Pavol Vican677b0132016-08-09 15:44:58 +02002653yang_read_string(const char *input, int size, int indent, int version)
Pavol Vican8760bb72016-04-07 09:44:01 +02002654{
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 Vican677b0132016-08-09 15:44:58 +02002682 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 Vican8760bb72016-04-07 09:44:01 +02002689 }
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;
2729error:
2730 free(value);
2731 return NULL;
2732}