blob: bad06704da2ec7797bfc453fe842eed4c2237d40 [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 Vican0adf01d2016-03-22 12:29:33 +0100395 if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, exp) == -1) {
Pavol Vicanbbdef532016-02-09 14:52:12 +0100396 lydict_remove(module->ctx, exp);
397 return EXIT_FAILURE;
398 }
Pavol Vican44dde2c2016-02-10 11:18:14 +0100399
Pavol Vicanbbdef532016-02-09 14:52:12 +0100400 lydict_remove(module->ctx, exp);
401 return EXIT_SUCCESS;
402}
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100403
404void *
Pavol Vican5f0316a2016-04-05 21:21:11 +0200405yang_read_must(struct lys_module *module, struct lys_node *node, char *value, enum yytokentype type)
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100406{
407 struct lys_restr *retval;
408
409 switch (type) {
Pavol Vican8c82fa82016-02-10 13:13:24 +0100410 case CONTAINER_KEYWORD:
Pavol Vican096c6db2016-02-11 15:08:10 +0100411 retval = &((struct lys_node_container *)node)->must[((struct lys_node_container *)node)->must_size++];
Pavol Vican8c82fa82016-02-10 13:13:24 +0100412 break;
413 case ANYXML_KEYWORD:
Pavol Vican096c6db2016-02-11 15:08:10 +0100414 retval = &((struct lys_node_anyxml *)node)->must[((struct lys_node_anyxml *)node)->must_size++];
415 break;
416 case LEAF_KEYWORD:
417 retval = &((struct lys_node_leaf *)node)->must[((struct lys_node_leaf *)node)->must_size++];
Pavol Vican8c82fa82016-02-10 13:13:24 +0100418 break;
Pavol Vican339d4ad2016-02-12 12:49:22 +0100419 case LEAF_LIST_KEYWORD:
420 retval = &((struct lys_node_leaflist *)node)->must[((struct lys_node_leaflist *)node)->must_size++];
421 break;
Pavol Vican5de33492016-02-22 14:03:24 +0100422 case LIST_KEYWORD:
423 retval = &((struct lys_node_list *)node)->must[((struct lys_node_list *)node)->must_size++];
424 break;
Pavol Vican1003ead2016-03-02 12:24:52 +0100425 case REFINE_KEYWORD:
426 retval = &((struct lys_refine *)node)->must[((struct lys_refine *)node)->must_size++];
427 break;
Pavol Vican85f12022016-03-05 16:30:35 +0100428 case ADD_KEYWORD:
429 retval = &(*((struct type_deviation *)node)->trg_must)[(*((struct type_deviation *)node)->trg_must_size)++];
430 memset(retval, 0, sizeof *retval);
431 break;
Pavol Vicanc1f5a502016-03-06 16:51:26 +0100432 case DELETE_KEYWORD:
433 retval = &((struct type_deviation *)node)->deviate->must[((struct type_deviation *)node)->deviate->must_size++];
434 break;
Pavol Vican9b14b492016-08-09 14:55:10 +0200435 case NOTIFICATION_KEYWORD:
436 retval = &((struct lys_node_notif *)node)->must[((struct lys_node_notif *)node)->must_size++];
437 break;
438 case INPUT_KEYWORD:
439 retval = &((struct lys_node_inout *)node)->must[((struct lys_node_inout *)node)->must_size++];
440 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200441 default:
442 goto error;
443 break;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100444 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100445 retval->expr = transform_schema2json(module, value);
446 if (!retval->expr || lyxp_syntax_check(retval->expr)) {
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100447 goto error;
448 }
449 free(value);
450 return retval;
451
452error:
453 free(value);
454 return NULL;
455}
456
457int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100458yang_read_message(struct lys_module *module,struct lys_restr *save,char *value, char *what, int message)
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100459{
460 int ret;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100461
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100462 if (message==ERROR_APP_TAG_KEYWORD) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100463 ret = yang_check_string(module, &save->eapptag, "error_app_tag", what, value);
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100464 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100465 ret = yang_check_string(module, &save->emsg, "error_app_tag", what, value);
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100466 }
467 return ret;
468}
Pavol Vicanb5687112016-02-09 22:35:59 +0100469
470int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100471yang_read_presence(struct lys_module *module, struct lys_node_container *cont, char *value)
Pavol Vicanb5687112016-02-09 22:35:59 +0100472{
473 if (cont->presence) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100474 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, cont, "presence", "container");
Pavol Vicanb5687112016-02-09 22:35:59 +0100475 free(value);
476 return EXIT_FAILURE;
477 } else {
478 cont->presence = lydict_insert_zc(module->ctx, value);
479 return EXIT_SUCCESS;
480 }
481}
482
Pavol Vican235dbd42016-02-10 10:34:19 +0100483void *
Pavol Vican5f0316a2016-04-05 21:21:11 +0200484yang_read_when(struct lys_module *module, struct lys_node *node, enum yytokentype type, char *value)
Pavol Vican235dbd42016-02-10 10:34:19 +0100485{
486 struct lys_when *retval;
487
488 retval = calloc(1, sizeof *retval);
489 if (!retval) {
490 LOGMEM;
491 free(value);
492 return NULL;
493 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100494 retval->cond = transform_schema2json(module, value);
495 if (!retval->cond || lyxp_syntax_check(retval->cond)) {
Pavol Vican235dbd42016-02-10 10:34:19 +0100496 goto error;
497 }
498 switch (type) {
499 case CONTAINER_KEYWORD:
500 if (((struct lys_node_container *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100501 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "container");
Pavol Vican235dbd42016-02-10 10:34:19 +0100502 goto error;
503 }
504 ((struct lys_node_container *)node)->when = retval;
505 break;
Pavol Vican1f06ba82016-02-10 17:39:50 +0100506 case ANYXML_KEYWORD:
Pavol Vican8c82fa82016-02-10 13:13:24 +0100507 if (((struct lys_node_anyxml *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100508 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "anyxml");
Pavol Vican8c82fa82016-02-10 13:13:24 +0100509 goto error;
510 }
511 ((struct lys_node_anyxml *)node)->when = retval;
512 break;
Pavol Vican1f06ba82016-02-10 17:39:50 +0100513 case CHOICE_KEYWORD:
514 if (((struct lys_node_choice *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100515 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "choice");
Pavol Vican1f06ba82016-02-10 17:39:50 +0100516 goto error;
517 }
518 ((struct lys_node_choice *)node)->when = retval;
519 break;
Pavol Vicanbd098132016-02-11 10:56:49 +0100520 case CASE_KEYWORD:
521 if (((struct lys_node_case *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100522 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "case");
Pavol Vicanbd098132016-02-11 10:56:49 +0100523 goto error;
524 }
525 ((struct lys_node_case *)node)->when = retval;
526 break;
Pavol Vican096c6db2016-02-11 15:08:10 +0100527 case LEAF_KEYWORD:
528 if (((struct lys_node_leaf *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100529 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaf");
Pavol Vican096c6db2016-02-11 15:08:10 +0100530 goto error;
531 }
532 ((struct lys_node_leaf *)node)->when = retval;
533 break;
Pavol Vican339d4ad2016-02-12 12:49:22 +0100534 case LEAF_LIST_KEYWORD:
535 if (((struct lys_node_leaflist *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100536 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaflist");
Pavol Vican339d4ad2016-02-12 12:49:22 +0100537 goto error;
538 }
539 ((struct lys_node_leaflist *)node)->when = retval;
540 break;
Pavol Vican5de33492016-02-22 14:03:24 +0100541 case LIST_KEYWORD:
542 if (((struct lys_node_list *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100543 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "list");
Pavol Vican5de33492016-02-22 14:03:24 +0100544 goto error;
545 }
546 ((struct lys_node_list *)node)->when = retval;
547 break;
Pavol Vican14b18562016-03-01 16:04:29 +0100548 case USES_KEYWORD:
549 if (((struct lys_node_uses *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100550 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "uses");
Pavol Vican14b18562016-03-01 16:04:29 +0100551 goto error;
552 }
553 ((struct lys_node_uses *)node)->when = retval;
554 break;
Pavol Vican92fa0dc2016-03-02 14:20:39 +0100555 case AUGMENT_KEYWORD:
556 if (((struct lys_node_augment *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100557 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "augment");
Pavol Vican92fa0dc2016-03-02 14:20:39 +0100558 goto error;
559 }
560 ((struct lys_node_augment *)node)->when = retval;
561 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200562 default:
563 goto error;
564 break;
Pavol Vican235dbd42016-02-10 10:34:19 +0100565 }
566 free(value);
567 return retval;
568
569error:
570 free(value);
571 lys_when_free(module->ctx, retval);
572 return NULL;
573}
Pavol Vican8c82fa82016-02-10 13:13:24 +0100574
575void *
Pavol Vican7cadfe72016-02-11 12:33:34 +0100576yang_read_node(struct lys_module *module, struct lys_node *parent, char *value, int nodetype, int sizeof_struct)
Pavol Vican8c82fa82016-02-10 13:13:24 +0100577{
Pavol Vican7cadfe72016-02-11 12:33:34 +0100578 struct lys_node *node;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100579
Pavol Vican7cadfe72016-02-11 12:33:34 +0100580 node = calloc(1, sizeof_struct);
581 if (!node) {
Pavol Vicand1dbfda2016-03-21 10:03:58 +0100582 free(value);
Pavol Vican8c82fa82016-02-10 13:13:24 +0100583 LOGMEM;
584 return NULL;
585 }
Pavol Vican531a9132016-03-03 10:10:09 +0100586 if (value) {
587 node->name = lydict_insert_zc(module->ctx, value);
588 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100589 node->module = module;
Pavol Vican7cadfe72016-02-11 12:33:34 +0100590 node->nodetype = nodetype;
591 node->prev = node;
592
593 /* insert the node into the schema tree */
594 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, node)) {
Pavol Vican531a9132016-03-03 10:10:09 +0100595 if (value) {
596 lydict_remove(module->ctx, node->name);
597 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100598 free(node);
Pavol Vican8c82fa82016-02-10 13:13:24 +0100599 return NULL;
600 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100601 return node;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100602}
603
Pavol Vican8c793992016-07-15 10:44:57 +0200604void *
605yang_read_action(struct lys_module *module, struct lys_node *parent, char *value)
606{
607 struct lys_node *node;
608
Michal Vaskobb174852016-07-25 11:00:21 +0200609 if (module->version != 2) {
610 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "action");
611 return NULL;
612 }
613
Pavol Vican8c793992016-07-15 10:44:57 +0200614 for (node = parent; node; node = lys_parent(node)) {
615 if (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)
Pavol Vicanbbe77822016-07-15 12:53:07 +0200616 || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys_size)) {
Pavol Vican8c793992016-07-15 10:44:57 +0200617 LOGVAL(LYE_INPAR, LY_VLOG_NONE, NULL, strnodetype(node->nodetype), "action");
618 return NULL;
619 }
620 }
621 return yang_read_node(module, parent, value, LYS_ACTION, sizeof(struct lys_node_rpc_action));
622}
623
Pavol Vican8c82fa82016-02-10 13:13:24 +0100624int
Pavol Vican5f0316a2016-04-05 21:21:11 +0200625yang_read_default(struct lys_module *module, void *node, char *value, enum yytokentype type)
Pavol Vican096c6db2016-02-11 15:08:10 +0100626{
627 int ret;
628
629 switch (type) {
Pavol Vican339d4ad2016-02-12 12:49:22 +0100630 case LEAF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100631 ret = yang_check_string(module, &((struct lys_node_leaf *) node)->dflt, "default", "leaf", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100632 break;
Pavol Vican0df02b02016-03-01 10:28:50 +0100633 case TYPEDEF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100634 ret = yang_check_string(module, &((struct lys_tpdf *) node)->dflt, "default", "typedef", value);
Pavol Vican0df02b02016-03-01 10:28:50 +0100635 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200636 default:
637 free(value);
638 LOGINT;
639 ret = EXIT_FAILURE;
640 break;
Pavol Vican096c6db2016-02-11 15:08:10 +0100641 }
642 return ret;
643}
644
645int
Pavol Vican5f0316a2016-04-05 21:21:11 +0200646yang_read_units(struct lys_module *module, void *node, char *value, enum yytokentype type)
Pavol Vican096c6db2016-02-11 15:08:10 +0100647{
648 int ret;
649
650 switch (type) {
Pavol Vican339d4ad2016-02-12 12:49:22 +0100651 case LEAF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100652 ret = yang_check_string(module, &((struct lys_node_leaf *) node)->units, "units", "leaf", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100653 break;
654 case LEAF_LIST_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100655 ret = yang_check_string(module, &((struct lys_node_leaflist *) node)->units, "units", "leaflist", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100656 break;
Pavol Vican0df02b02016-03-01 10:28:50 +0100657 case TYPEDEF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100658 ret = yang_check_string(module, &((struct lys_tpdf *) node)->units, "units", "typedef", value);
Pavol Vican0df02b02016-03-01 10:28:50 +0100659 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200660 default:
661 free(value);
662 LOGINT;
663 ret = EXIT_FAILURE;
664 break;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100665 }
666 return ret;
667}
Pavol Vican5de33492016-02-22 14:03:24 +0100668
669int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100670yang_read_key(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres)
Pavol Vican5de33492016-02-22 14:03:24 +0100671{
672 char *exp, *value;
673
674 exp = value = (char *) list->keys;
Pavol Vicanbbe77822016-07-15 12:53:07 +0200675 list->keys_size = 0;
Pavol Vican5de33492016-02-22 14:03:24 +0100676 while ((value = strpbrk(value, " \t\n"))) {
677 list->keys_size++;
678 while (isspace(*value)) {
679 value++;
680 }
681 }
682 list->keys_size++;
683 list->keys = calloc(list->keys_size, sizeof *list->keys);
684 if (!list->keys) {
685 LOGMEM;
686 goto error;
687 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100688 if (unres_schema_add_str(module, unres, list, UNRES_LIST_KEYS, exp) == -1) {
Pavol Vican5de33492016-02-22 14:03:24 +0100689 goto error;
690 }
691 free(exp);
692 return EXIT_SUCCESS;
693
694error:
695 free(exp);
696 return EXIT_FAILURE;
697}
698
699int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100700yang_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 +0100701{
702 int i, j;
703 char *vaux;
Radek Krejcid09d1a52016-08-11 14:05:45 +0200704 struct unres_list_uniq *unique_info;
Pavol Vican85f12022016-03-05 16:30:35 +0100705
706 /* count the number of unique leafs in the value */
707 vaux = value;
708 while ((vaux = strpbrk(vaux, " \t\n"))) {
709 unique->expr_size++;
710 while (isspace(*vaux)) {
711 vaux++;
712 }
713 }
714 unique->expr_size++;
715 unique->expr = calloc(unique->expr_size, sizeof *unique->expr);
716 if (!unique->expr) {
717 LOGMEM;
718 goto error;
719 }
720
721 for (i = 0; i < unique->expr_size; i++) {
722 vaux = strpbrk(value, " \t\n");
723 if (!vaux) {
724 /* the last token, lydict_insert() will count its size on its own */
725 vaux = value;
726 }
727
728 /* store token into unique structure */
729 unique->expr[i] = lydict_insert(module->ctx, value, vaux - value);
730
731 /* check that the expression does not repeat */
732 for (j = 0; j < i; j++) {
733 if (ly_strequal(unique->expr[j], unique->expr[i], 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100734 LOGVAL(LYE_INARG, LY_VLOG_LYS, list, unique->expr[i], "unique");
735 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The identifier is not unique");
Pavol Vican85f12022016-03-05 16:30:35 +0100736 goto error;
737 }
738 }
739 /* try to resolve leaf */
740 if (unres) {
Radek Krejcid09d1a52016-08-11 14:05:45 +0200741 unique_info = malloc(sizeof *unique_info);
742 unique_info->list = (struct lys_node *)list;
743 unique_info->expr = unique->expr[i];
744 unique_info->trg_type = &unique->trg_type;
745 if (unres_schema_add_node(module, unres, unique_info, UNRES_LIST_UNIQ, NULL) == -1) {
Pavol Vican18b10212016-04-11 15:41:52 +0200746 goto error;
747 }
Pavol Vican85f12022016-03-05 16:30:35 +0100748 } else {
Radek Krejcid09d1a52016-08-11 14:05:45 +0200749 if (resolve_unique((struct lys_node *)list, unique->expr[i], &unique->trg_type)) {
Pavol Vican85f12022016-03-05 16:30:35 +0100750 goto error;
751 }
752 }
753
754 /* move to next token */
755 value = vaux;
756 while(isspace(*value)) {
757 value++;
758 }
759 }
760
761 return EXIT_SUCCESS;
762
763error:
764 return EXIT_FAILURE;
765}
766
767int
Pavol Vican5de33492016-02-22 14:03:24 +0100768yang_read_unique(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres)
769{
770 uint8_t k;
Pavol Vican0adf01d2016-03-22 12:29:33 +0100771 char *str;
Pavol Vican5de33492016-02-22 14:03:24 +0100772
773 for(k=0; k<list->unique_size; k++) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100774 str = (char *)list->unique[k].expr;
775 if (yang_fill_unique(module, list, &list->unique[k], str, unres)) {
Pavol Vican5de33492016-02-22 14:03:24 +0100776 goto error;
777 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100778 free(str);
Pavol Vican5de33492016-02-22 14:03:24 +0100779 }
780 return EXIT_SUCCESS;
781
782error:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100783 free(str);
Pavol Vican5de33492016-02-22 14:03:24 +0100784 return EXIT_FAILURE;
Pavol Vican73e7c992016-02-24 12:18:05 +0100785}
786
Pavol Vican1ff0e222016-02-26 12:27:01 +0100787static int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100788yang_read_identyref(struct lys_module *module, struct lys_type *type, struct unres_schema *unres)
Pavol Vican1ff0e222016-02-26 12:27:01 +0100789{
790 const char *value, *tmp;
791 int rc, ret = EXIT_FAILURE;
792
793 value = tmp = type->info.lref.path;
794 /* store in the JSON format */
Pavol Vican0adf01d2016-03-22 12:29:33 +0100795 value = transform_schema2json(module, value);
Pavol Vican1ff0e222016-02-26 12:27:01 +0100796 if (!value) {
797 goto end;
798 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100799 rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value);
Pavol Vican1ff0e222016-02-26 12:27:01 +0100800 lydict_remove(module->ctx, value);
801
802 if (rc == -1) {
803 goto end;
804 }
805
806 ret = EXIT_SUCCESS;
807
808end:
809 lydict_remove(module->ctx, tmp);
810 return ret;
811}
812
Pavol Vican73e7c992016-02-24 12:18:05 +0100813int
Radek Krejci3a5501d2016-07-18 22:03:34 +0200814yang_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 +0100815{
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200816 int i, j, rc;
Pavol Vican73e7c992016-02-24 12:18:05 +0100817 int ret = -1;
818 const char *name, *value;
819 LY_DATA_TYPE base;
Radek Krejci3a5501d2016-07-18 22:03:34 +0200820 struct lys_node *siter;
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200821 struct lys_type *dertype;
822 struct lys_type_enum *enms_sc = NULL;
823 struct lys_type_bit *bits_sc = NULL;
824 struct lys_type_bit bit_tmp;
825
Pavol Vican73e7c992016-02-24 12:18:05 +0100826
Pavol Vican6b072512016-04-04 10:50:21 +0200827 base = typ->base;
Pavol Vican0adf01d2016-03-22 12:29:33 +0100828 value = transform_schema2json(module, typ->name);
Pavol Vican73e7c992016-02-24 12:18:05 +0100829 if (!value) {
830 goto error;
831 }
832
833 i = parse_identifier(value);
834 if (i < 1) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100835 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]);
Pavol Vican73e7c992016-02-24 12:18:05 +0100836 lydict_remove(module->ctx, value);
837 goto error;
838 }
Radek Krejci408ab2f2016-06-06 15:27:10 +0200839 /* module name */
Pavol Vican73e7c992016-02-24 12:18:05 +0100840 name = value;
841 if (value[i]) {
842 typ->type->module_name = lydict_insert(module->ctx, value, i);
843 name += i;
844 if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100845 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name);
Pavol Vican73e7c992016-02-24 12:18:05 +0100846 lydict_remove(module->ctx, value);
847 goto error;
848 }
849 ++name;
850 }
851
852 rc = resolve_superior_type(name, typ->type->module_name, module, parent, &typ->type->der);
Pavol Vican73e7c992016-02-24 12:18:05 +0100853 if (rc == -1) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100854 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, typ->type->module_name);
Radek Krejci408ab2f2016-06-06 15:27:10 +0200855 lydict_remove(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +0100856 goto error;
857
Michal Vasko01c6fd22016-05-20 11:43:05 +0200858 /* the type could not be resolved or it was resolved to an unresolved typedef or leafref */
Pavol Vican73e7c992016-02-24 12:18:05 +0100859 } else if (rc == EXIT_FAILURE) {
Michal Vasko01c6fd22016-05-20 11:43:05 +0200860 LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name);
Radek Krejci408ab2f2016-06-06 15:27:10 +0200861 lydict_remove(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +0100862 ret = EXIT_FAILURE;
863 goto error;
864 }
Radek Krejci408ab2f2016-06-06 15:27:10 +0200865 lydict_remove(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +0100866 typ->type->base = typ->type->der->type.base;
867 if (base == 0) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100868 base = typ->type->der->type.base;
Pavol Vican73e7c992016-02-24 12:18:05 +0100869 }
870 switch (base) {
871 case LY_TYPE_STRING:
872 if (typ->type->base == LY_TYPE_BINARY) {
873 if (typ->type->info.str.pat_count) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100874 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Binary type could not include pattern statement.");
Pavol Vican73e7c992016-02-24 12:18:05 +0100875 goto error;
876 }
877 typ->type->info.binary.length = typ->type->info.str.length;
Pavol Vican07ea68d2016-02-25 12:01:37 +0100878 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 +0100879 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.binary.length->expr, "length");
Pavol Vican73e7c992016-02-24 12:18:05 +0100880 goto error;
881 }
882 } else if (typ->type->base == LY_TYPE_STRING) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100883 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 +0100884 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.str.length->expr, "length");
Pavol Vican73e7c992016-02-24 12:18:05 +0100885 goto error;
886 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100887 } else {
Pavol Vican933aa5a2016-04-09 21:05:46 +0200888 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicanaff5c802016-02-24 15:56:45 +0100889 goto error;
Pavol Vican73e7c992016-02-24 12:18:05 +0100890 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100891 break;
892 case LY_TYPE_DEC64:
893 if (typ->type->base == LY_TYPE_DEC64) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100894 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 +0100895 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.dec64.range->expr, "range");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100896 goto error;
897 }
Pavol Vican07ea68d2016-02-25 12:01:37 +0100898 /* mandatory sub-statement(s) check */
899 if (!typ->type->info.dec64.dig && !typ->type->der->type.der) {
900 /* decimal64 type directly derived from built-in type requires fraction-digits */
Pavol Vican0adf01d2016-03-22 12:29:33 +0100901 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type");
Pavol Vican07ea68d2016-02-25 12:01:37 +0100902 goto error;
903 }
904 if (typ->type->info.dec64.dig && typ->type->der->type.der) {
905 /* type is not directly derived from buit-in type and fraction-digits statement is prohibited */
Pavol Vican0adf01d2016-03-22 12:29:33 +0100906 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits");
Pavol Vican07ea68d2016-02-25 12:01:37 +0100907 goto error;
908 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100909 } else if (typ->type->base >= LY_TYPE_INT8 && typ->type->base <=LY_TYPE_UINT64) {
910 if (typ->type->info.dec64.dig) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100911 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Numerical type could not include fraction statement.");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100912 goto error;
913 }
914 typ->type->info.num.range = typ->type->info.dec64.range;
Pavol Vican07ea68d2016-02-25 12:01:37 +0100915 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 +0100916 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.num.range->expr, "range");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100917 goto error;
918 }
919 } else {
Pavol Vican933aa5a2016-04-09 21:05:46 +0200920 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicanaff5c802016-02-24 15:56:45 +0100921 goto error;
922 }
923 break;
Pavol Vican79a763d2016-02-25 15:41:27 +0100924 case LY_TYPE_ENUM:
925 if (typ->type->base != LY_TYPE_ENUM) {
Pavol Vican933aa5a2016-04-09 21:05:46 +0200926 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vican79a763d2016-02-25 15:41:27 +0100927 goto error;
928 }
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200929 dertype = &typ->type->der->type;
930
931 if (!dertype->der) {
932 if (!typ->type->info.enums.count) {
933 /* type is derived directly from buit-in enumeartion type and enum statement is required */
934 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type");
935 goto error;
936 }
937 } else {
938 for (; !dertype->info.enums.count; dertype = &dertype->der->type);
939 if (module->version < 2 && typ->type->info.enums.count) {
940 /* type is not directly derived from built-in enumeration type and enum statement is prohibited
941 * in YANG 1.0, since YANG 1.1 enum statements can be used to restrict the base enumeration type */
942 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "enum");
943 goto error;
944 }
945
946 /* restricted enumeration type - the name MUST be used in the base type */
947 enms_sc = dertype->info.enums.enm;
948 for(i = 0; i < typ->type->info.enums.count; i++) {
949 for (j = 0; j < dertype->info.enums.count; j++) {
950 if (ly_strequal(enms_sc[j].name, typ->type->info.enums.enm[i].name, 1)) {
951 break;
952 }
953 }
954 if (j == dertype->info.enums.count) {
955 LOGVAL(LYE_ENUM_INNAME, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].name);
956 goto error;
957 }
958
959 if (typ->type->info.enums.enm[i].flags & LYS_AUTOASSIGNED) {
960 /* automatically assign value from base type */
961 typ->type->info.enums.enm[i].value = enms_sc[j].value;
962 } else {
963 /* check that the assigned value corresponds to the original
964 * value of the enum in the base type */
965 if (typ->type->info.enums.enm[i].value != enms_sc[j].value) {
966 /* typ->type->info.enums.enm[i].value - assigned value in restricted enum
967 * enms_sc[j].value - value assigned to the corresponding enum (detected above) in base type */
968 LOGVAL(LYE_ENUM_INVAL, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].value,
969 typ->type->info.enums.enm[i].name, enms_sc[j].value);
970 goto error;
971 }
972 }
973 }
Pavol Vican79a763d2016-02-25 15:41:27 +0100974 }
Pavol Vican1ff0e222016-02-26 12:27:01 +0100975 break;
Pavol Vican03a59442016-03-21 15:23:45 +0100976 case LY_TYPE_BITS:
977 if (typ->type->base != LY_TYPE_BITS) {
Pavol Vican933aa5a2016-04-09 21:05:46 +0200978 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vican03a59442016-03-21 15:23:45 +0100979 goto error;
980 }
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200981 dertype = &typ->type->der->type;
982
983 if (!dertype->der) {
984 if (!typ->type->info.bits.count) {
985 /* type is derived directly from buit-in bits type and bit statement is required */
986 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type");
987 goto error;
988 }
989 } else {
990 for (; !dertype->info.enums.count; dertype = &dertype->der->type);
991 if (module->version < 2 && typ->type->info.bits.count) {
992 /* type is not directly derived from buit-in bits type and bit statement is prohibited,
993 * since YANG 1.1 the bit statements can be used to restrict the base bits type */
994 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "bit");
995 goto error;
996 }
997
998 bits_sc = dertype->info.bits.bit;
999 for (i = 0; i < typ->type->info.bits.count; i++) {
1000 for (j = 0; j < dertype->info.bits.count; j++) {
1001 if (ly_strequal(bits_sc[j].name, typ->type->info.bits.bit[i].name, 1)) {
1002 break;
1003 }
1004 }
1005 if (j == dertype->info.bits.count) {
1006 LOGVAL(LYE_BITS_INNAME, LY_VLOG_NONE, NULL, typ->type->info.bits.bit[i].name);
1007 goto error;
1008 }
1009
1010 /* restricted bits type */
1011 if (typ->type->info.bits.bit[i].flags & LYS_AUTOASSIGNED) {
1012 /* automatically assign position from base type */
1013 typ->type->info.bits.bit[i].pos = bits_sc[j].pos;
1014 } else {
1015 /* check that the assigned position corresponds to the original
1016 * position of the bit in the base type */
1017 if (typ->type->info.bits.bit[i].pos != bits_sc[j].pos) {
1018 /* typ->type->info.bits.bit[i].pos - assigned position in restricted bits
1019 * bits_sc[j].pos - position assigned to the corresponding bit (detected above) in base type */
1020 LOGVAL(LYE_BITS_INVAL, LY_VLOG_NONE, NULL, typ->type->info.bits.bit[i].pos,
1021 typ->type->info.bits.bit[i].name, bits_sc[j].pos);
1022 goto error;
1023 }
1024 }
1025 }
Pavol Vican03a59442016-03-21 15:23:45 +01001026 }
Pavol Vican8ad2e0d2016-08-09 12:56:19 +02001027
1028 for (i = typ->type->info.bits.count - 1; i > 0; i--) {
1029 j = i;
1030
1031 /* keep them ordered by position */
1032 while (j && typ->type->info.bits.bit[j - 1].pos > typ->type->info.bits.bit[j].pos) {
1033 /* switch them */
1034 memcpy(&bit_tmp, &typ->type->info.bits.bit[j], sizeof bit_tmp);
1035 memcpy(&typ->type->info.bits.bit[j], &typ->type->info.bits.bit[j - 1], sizeof bit_tmp);
1036 memcpy(&typ->type->info.bits.bit[j - 1], &bit_tmp, sizeof bit_tmp);
1037 j--;
1038 }
Pavol Vican03a59442016-03-21 15:23:45 +01001039 }
1040 break;
Pavol Vican1ff0e222016-02-26 12:27:01 +01001041 case LY_TYPE_LEAFREF:
Pavol Vican6b072512016-04-04 10:50:21 +02001042 if (typ->type->base == LY_TYPE_IDENT && (typ->flags & LYS_TYPE_BASE)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001043 if (yang_read_identyref(module, typ->type, unres)) {
Pavol Vican1ff0e222016-02-26 12:27:01 +01001044 goto error;
1045 }
Pavol Vican191613a2016-02-26 16:21:32 +01001046 } else if (typ->type->base == LY_TYPE_LEAFREF) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001047 /* flag resolving for later use */
1048 if (!tpdftype) {
1049 for (siter = parent; siter && siter->nodetype != LYS_GROUPING; siter = lys_parent(siter));
1050 if (siter) {
1051 /* just a flag - do not resolve */
1052 tpdftype = 1;
1053 }
1054 }
1055
Pavol Vican6b072512016-04-04 10:50:21 +02001056 if (typ->type->info.lref.path) {
Pavol Vican191613a2016-02-26 16:21:32 +01001057 value = typ->type->info.lref.path;
1058 /* store in the JSON format */
Pavol Vican0adf01d2016-03-22 12:29:33 +01001059 typ->type->info.lref.path = transform_schema2json(module, value);
Pavol Vican191613a2016-02-26 16:21:32 +01001060 lydict_remove(module->ctx, value);
1061 if (!typ->type->info.lref.path) {
1062 goto error;
1063 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001064 /* try to resolve leafref path only when this is instantiated
1065 * leaf, so it is not:
1066 * - typedef's type,
1067 * - in grouping definition,
1068 * - just instantiated in a grouping definition,
1069 * because in those cases the nodes referenced in path might not be present
1070 * and it is not a bug. */
1071 if (!tpdftype && unres_schema_add_node(module, unres, typ->type, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vican191613a2016-02-26 16:21:32 +01001072 goto error;
1073 }
Pavol Vican6b072512016-04-04 10:50:21 +02001074 } else if (!typ->type->der->type.der) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001075 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type");
Pavol Vican191613a2016-02-26 16:21:32 +01001076 goto error;
Michal Vasko01c6fd22016-05-20 11:43:05 +02001077 } else {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001078 /* copy leafref definition into the derived type */
1079 typ->type->info.lref.path = lydict_insert(module->ctx, typ->type->der->type.info.lref.path, 0);
1080 /* and resolve the path at the place we are (if not in grouping/typedef) */
1081 if (!tpdftype && unres_schema_add_node(module, unres, typ->type, UNRES_TYPE_LEAFREF, parent) == -1) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02001082 goto error;
1083 }
Radek Krejci742be352016-07-17 12:18:54 +02001084
Michal Vasko01c6fd22016-05-20 11:43:05 +02001085 /* add pointer to leafref target, only on leaves (not in typedefs) */
Radek Krejci3a5501d2016-07-18 22:03:34 +02001086 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 +02001087 goto error;
1088 }
Pavol Vican191613a2016-02-26 16:21:32 +01001089 }
Pavol Vican1ff0e222016-02-26 12:27:01 +01001090 } else {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001091 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vican1ff0e222016-02-26 12:27:01 +01001092 goto error;
1093 }
1094 break;
1095 case LY_TYPE_IDENT:
1096 if (typ->type->der->type.der) {
1097 /* this is just a derived type with no base specified/required */
1098 break;
1099 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001100 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type");
Pavol Vican1ff0e222016-02-26 12:27:01 +01001101 goto error;
1102 }
1103 break;
Pavol Vicana4f045a2016-02-29 15:01:20 +01001104 case LY_TYPE_UNION:
1105 if (typ->type->base != LY_TYPE_UNION) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001106 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicana4f045a2016-02-29 15:01:20 +01001107 goto error;
1108 }
1109 if (!typ->type->info.uni.types) {
1110 if (typ->type->der->type.der) {
1111 /* this is just a derived type with no additional type specified/required */
1112 break;
1113 }
Pavol Vican0adf01d2016-03-22 12:29:33 +01001114 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type");
Pavol Vicana4f045a2016-02-29 15:01:20 +01001115 goto error;
1116 }
1117 for (i = 0; i < typ->type->info.uni.count; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001118 if (unres_schema_add_node(module, unres, &typ->type->info.uni.types[i],
Michal Vasko5d631402016-07-21 13:15:15 +02001119 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Pavol Vicana4f045a2016-02-29 15:01:20 +01001120 goto error;
1121 }
1122 if (typ->type->info.uni.types[i].base == LY_TYPE_EMPTY) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001123 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", typ->name);
Pavol Vicana4f045a2016-02-29 15:01:20 +01001124 goto error;
1125 } else if (typ->type->info.uni.types[i].base == LY_TYPE_LEAFREF) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001126 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", typ->name);
Pavol Vicana4f045a2016-02-29 15:01:20 +01001127 goto error;
1128 }
1129 }
1130 break;
Pavol Vicana1827962016-02-29 15:39:42 +01001131
1132 default:
1133 if (base >= LY_TYPE_BINARY && base <= LY_TYPE_UINT64) {
1134 if (typ->type->base != base) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001135 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicana1827962016-02-29 15:39:42 +01001136 goto error;
1137 }
1138 } else {
1139 LOGINT;
1140 goto error;
1141 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001142 }
1143 return EXIT_SUCCESS;
1144
1145error:
1146 if (typ->type->module_name) {
1147 lydict_remove(module->ctx, typ->type->module_name);
1148 typ->type->module_name = NULL;
1149 }
1150 return ret;
1151}
1152
1153void *
Pavol Vican5f0316a2016-04-05 21:21:11 +02001154yang_read_type(struct lys_module *module, void *parent, char *value, enum yytokentype type)
Pavol Vican73e7c992016-02-24 12:18:05 +01001155{
1156 struct yang_type *typ;
Pavol Vican4766aca2016-03-07 12:42:36 +01001157 struct type_deviation *dev;
1158 struct lys_tpdf *tmp_parent;
Pavol Vican73e7c992016-02-24 12:18:05 +01001159
Pavol Vicand01d8ae2016-03-01 10:45:59 +01001160 typ = calloc(1, sizeof *typ);
1161 if (!typ) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001162 LOGMEM;
1163 return NULL;
1164 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001165
1166 typ->flags = LY_YANG_STRUCTURE_FLAG;
1167 switch (type) {
1168 case LEAF_KEYWORD:
1169 ((struct lys_node_leaf *)parent)->type.der = (struct lys_tpdf *)typ;
1170 ((struct lys_node_leaf *)parent)->type.parent = (struct lys_tpdf *)parent;
1171 typ->type = &((struct lys_node_leaf *)parent)->type;
1172 break;
Pavol Vicana55992a2016-03-01 13:37:17 +01001173 case LEAF_LIST_KEYWORD:
1174 ((struct lys_node_leaflist *)parent)->type.der = (struct lys_tpdf *)typ;
1175 ((struct lys_node_leaflist *)parent)->type.parent = (struct lys_tpdf *)parent;
1176 typ->type = &((struct lys_node_leaflist *)parent)->type;
1177 break;
Pavol Vicana4f045a2016-02-29 15:01:20 +01001178 case UNION_KEYWORD:
1179 ((struct lys_type *)parent)->der = (struct lys_tpdf *)typ;
1180 typ->type = (struct lys_type *)parent;
1181 break;
Pavol Vican0df02b02016-03-01 10:28:50 +01001182 case TYPEDEF_KEYWORD:
1183 ((struct lys_tpdf *)parent)->type.der = (struct lys_tpdf *)typ;
1184 typ->type = &((struct lys_tpdf *)parent)->type;
Pavol Vican4766aca2016-03-07 12:42:36 +01001185 break;
1186 case REPLACE_KEYWORD:
1187 /* deviation replace type*/
1188 dev = (struct type_deviation *)parent;
1189 if (dev->deviate->type) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001190 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "deviation");
Pavol Vican4766aca2016-03-07 12:42:36 +01001191 goto error;
1192 }
1193 /* check target node type */
1194 if (dev->target->nodetype == LYS_LEAF) {
1195 typ->type = &((struct lys_node_leaf *)dev->target)->type;
1196 } else if (dev->target->nodetype == LYS_LEAFLIST) {
1197 typ->type = &((struct lys_node_leaflist *)dev->target)->type;
1198 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001199 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "type");
1200 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"type\" property.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001201 goto error;
1202 }
1203
1204 /* remove type and initialize it */
1205 lys_type_free(module->ctx, typ->type);
1206 tmp_parent = typ->type->parent;
1207 memset(typ->type, 0, sizeof *typ->type);
1208 typ->type->parent = tmp_parent;
1209
1210 /* replace it with the value specified in deviation */
1211 /* HACK for unres */
1212 typ->type->der = (struct lys_tpdf *)typ;
1213 dev->deviate->type = typ->type;
1214 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +02001215 default:
1216 goto error;
1217 break;
Pavol Vican73e7c992016-02-24 12:18:05 +01001218 }
Pavol Vican5f0316a2016-04-05 21:21:11 +02001219 typ->name = lydict_insert_zc(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +01001220 return typ;
Pavol Vican4766aca2016-03-07 12:42:36 +01001221
1222error:
Pavol Vican5f0316a2016-04-05 21:21:11 +02001223 free(value);
Pavol Vican4766aca2016-03-07 12:42:36 +01001224 free(typ);
1225 return NULL;
Pavol Vican73e7c992016-02-24 12:18:05 +01001226}
1227
Pavol Vicanbe9e3832016-04-28 02:21:37 +02001228void
1229yang_delete_type(struct lys_module *module, struct yang_type *stype)
1230{
1231 int i;
1232
1233 stype->type->base = stype->base;
1234 stype->type->der = NULL;
1235 lydict_remove(module->ctx, stype->name);
1236 if (stype->base == LY_TYPE_UNION) {
1237 for (i = 0; i < stype->type->info.uni.count; i++) {
1238 if (stype->type->info.uni.types[i].der) {
1239 yang_delete_type(module, (struct yang_type *)stype->type->info.uni.types[i].der);
1240 }
1241 }
1242 }
1243 free(stype);
1244}
1245
Pavol Vican73e7c992016-02-24 12:18:05 +01001246void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001247yang_read_length(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vican73e7c992016-02-24 12:18:05 +01001248{
1249 struct lys_restr **length;
1250
Pavol Vican6b072512016-04-04 10:50:21 +02001251 if (typ->base == 0 || typ->base == LY_TYPE_STRING) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001252 length = &typ->type->info.str.length;
Pavol Vican6b072512016-04-04 10:50:21 +02001253 typ->base = LY_TYPE_STRING;
1254 } else if (typ->base == LY_TYPE_BINARY) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001255 length = &typ->type->info.binary.length;
1256 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001257 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected length statement.");
Pavol Vican73e7c992016-02-24 12:18:05 +01001258 goto error;
1259 }
1260
1261 if (*length) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001262 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "length", "type");
Pavol Vicanbe9e3832016-04-28 02:21:37 +02001263 goto error;
Pavol Vican73e7c992016-02-24 12:18:05 +01001264 }
1265 *length = calloc(1, sizeof **length);
1266 if (!*length) {
1267 LOGMEM;
1268 goto error;
1269 }
1270 (*length)->expr = lydict_insert_zc(module->ctx, value);
1271 return *length;
1272
1273error:
1274 free(value);
1275 return NULL;
1276
1277}
Pavol Vican1c203db2016-02-24 14:05:23 +01001278
Pavol Vican6eecf302016-08-10 11:09:05 +02001279int
1280yang_read_pattern(struct lys_module *module, struct lys_restr *pattern, char *value, char modifier)
Pavol Vican1c203db2016-02-24 14:05:23 +01001281{
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001282 char *buf;
1283 size_t len;
1284
Michal Vasko0aee5c12016-06-17 14:27:26 +02001285 if (lyp_check_pattern(value, NULL)) {
Pavol Vican1c203db2016-02-24 14:05:23 +01001286 free(value);
Pavol Vican6eecf302016-08-10 11:09:05 +02001287 return EXIT_FAILURE;
Pavol Vican1c203db2016-02-24 14:05:23 +01001288 }
Pavol Vican1c203db2016-02-24 14:05:23 +01001289
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001290 len = strlen(value);
1291 buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */
Pavol Vican6eecf302016-08-10 11:09:05 +02001292
1293 if (!buf) {
1294 LOGMEM;
1295 free(value);
1296 return EXIT_FAILURE;
1297 }
1298
1299 buf[0] = modifier;
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001300 strcpy(&buf[1], value);
1301 free(value);
1302
Pavol Vican6eecf302016-08-10 11:09:05 +02001303 pattern->expr = lydict_insert_zc(module->ctx, buf);
1304 return EXIT_SUCCESS;
Pavol Vican1c203db2016-02-24 14:05:23 +01001305}
Pavol Vicanaff5c802016-02-24 15:56:45 +01001306
1307void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001308yang_read_range(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vicanaff5c802016-02-24 15:56:45 +01001309{
Pavol Vican6b072512016-04-04 10:50:21 +02001310 if (typ->base != 0 && typ->base != LY_TYPE_DEC64) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001311 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected range statement.");
Pavol Vicanaff5c802016-02-24 15:56:45 +01001312 goto error;
1313 }
Pavol Vican6b072512016-04-04 10:50:21 +02001314 typ->base = LY_TYPE_DEC64;
Pavol Vicanaff5c802016-02-24 15:56:45 +01001315 if (typ->type->info.dec64.range) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001316 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "range", "type");
Pavol Vicanaff5c802016-02-24 15:56:45 +01001317 goto error;
1318 }
1319 typ->type->info.dec64.range = calloc(1, sizeof *typ->type->info.dec64.range);
1320 if (!typ->type->info.dec64.range) {
1321 LOGMEM;
1322 goto error;
1323 }
1324 typ->type->info.dec64.range->expr = lydict_insert_zc(module->ctx, value);
1325 return typ->type->info.dec64.range;
1326
1327error:
1328 free(value);
1329 return NULL;
1330}
Pavol Vican07ea68d2016-02-25 12:01:37 +01001331
1332int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001333yang_read_fraction(struct yang_type *typ, uint32_t value)
Pavol Vican07ea68d2016-02-25 12:01:37 +01001334{
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001335 unsigned int i;
1336
Pavol Vican6b072512016-04-04 10:50:21 +02001337 if (typ->base == 0 || typ->base == LY_TYPE_DEC64) {
1338 typ->base = LY_TYPE_DEC64;
Pavol Vican07ea68d2016-02-25 12:01:37 +01001339 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001340 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected fraction-digits statement.");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001341 goto error;
1342 }
1343 if (typ->type->info.dec64.dig) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001344 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "fraction-digits", "type");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001345 goto error;
1346 }
1347 /* range check */
1348 if (value < 1 || value > 18) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001349 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", value, "fraction-digits");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001350 goto error;
1351 }
1352 typ->type->info.dec64.dig = value;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001353 typ->type->info.dec64.div = 10;
1354 for (i = 1; i < value; i++) {
1355 typ->type->info.dec64.div *= 10;
1356 }
Pavol Vican07ea68d2016-02-25 12:01:37 +01001357 return EXIT_SUCCESS;
1358
1359error:
1360 return EXIT_FAILURE;
1361}
Pavol Vican79a763d2016-02-25 15:41:27 +01001362
1363void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001364yang_read_enum(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vican79a763d2016-02-25 15:41:27 +01001365{
1366 struct lys_type_enum *enm;
1367 int i;
1368
1369 enm = &typ->type->info.enums.enm[typ->type->info.enums.count];
1370 enm->name = lydict_insert_zc(module->ctx, value);
1371
1372 /* the assigned name MUST NOT have any leading or trailing whitespace characters */
1373 if (isspace(enm->name[0]) || isspace(enm->name[strlen(enm->name) - 1])) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001374 LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, enm->name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001375 goto error;
1376 }
1377
1378 /* check the name uniqueness */
1379 for (i = 0; i < typ->type->info.enums.count; i++) {
1380 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 +01001381 LOGVAL(LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001382 goto error;
1383 }
1384 }
1385
1386 typ->type->info.enums.count++;
1387 return enm;
1388
1389error:
1390 typ->type->info.enums.count++;
1391 return NULL;
1392}
1393
1394int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001395yang_check_enum(struct yang_type *typ, struct lys_type_enum *enm, int64_t *value, int assign)
Pavol Vican79a763d2016-02-25 15:41:27 +01001396{
1397 int i, j;
1398
1399 if (!assign) {
1400 /* assign value automatically */
1401 if (*value > INT32_MAX) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001402 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value");
Pavol Vican79a763d2016-02-25 15:41:27 +01001403 goto error;
1404 }
1405 enm->value = *value;
1406 enm->flags |= LYS_AUTOASSIGNED;
1407 (*value)++;
1408 }
1409
1410 /* check that the value is unique */
1411 j = typ->type->info.enums.count-1;
1412 for (i = 0; i < j; i++) {
1413 if (typ->type->info.enums.enm[i].value == typ->type->info.enums.enm[j].value) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001414 LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL,
Radek Krejcie663e012016-08-01 17:12:34 +02001415 typ->type->info.enums.enm[j].value, typ->type->info.enums.enm[j].name,
1416 typ->type->info.enums.enm[i].name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001417 goto error;
1418 }
1419 }
1420
1421 return EXIT_SUCCESS;
1422
1423error:
1424 return EXIT_FAILURE;
1425}
Pavol Vican9887c682016-02-29 11:32:01 +01001426
1427void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001428yang_read_bit(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vican9887c682016-02-29 11:32:01 +01001429{
1430 int i;
1431 struct lys_type_bit *bit;
1432
1433 bit = &typ->type->info.bits.bit[typ->type->info.bits.count];
Pavol Vican0adf01d2016-03-22 12:29:33 +01001434 if (lyp_check_identifier(value, LY_IDENT_SIMPLE, NULL, NULL)) {
Pavol Vican9887c682016-02-29 11:32:01 +01001435 free(value);
1436 goto error;
1437 }
1438 bit->name = lydict_insert_zc(module->ctx, value);
1439
1440 /* check the name uniqueness */
1441 for (i = 0; i < typ->type->info.bits.count; i++) {
1442 if (!strcmp(typ->type->info.bits.bit[i].name, bit->name)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001443 LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, bit->name);
Pavol Vican9887c682016-02-29 11:32:01 +01001444 typ->type->info.bits.count++;
1445 goto error;
1446 }
1447 }
1448 typ->type->info.bits.count++;
1449 return bit;
1450
1451error:
1452 return NULL;
1453}
1454
1455int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001456yang_check_bit(struct yang_type *typ, struct lys_type_bit *bit, int64_t *value, int assign)
Pavol Vican9887c682016-02-29 11:32:01 +01001457{
1458 int i,j;
Pavol Vican9887c682016-02-29 11:32:01 +01001459
1460 if (!assign) {
1461 /* assign value automatically */
1462 if (*value > UINT32_MAX) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001463 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position");
Pavol Vican9887c682016-02-29 11:32:01 +01001464 goto error;
1465 }
1466 bit->pos = (uint32_t)*value;
1467 bit->flags |= LYS_AUTOASSIGNED;
1468 (*value)++;
1469 }
1470
1471 j = typ->type->info.bits.count - 1;
1472 /* check that the value is unique */
1473 for (i = 0; i < j; i++) {
1474 if (typ->type->info.bits.bit[i].pos == bit->pos) {
Radek Krejcie663e012016-08-01 17:12:34 +02001475 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 +01001476 goto error;
1477 }
1478 }
1479
Pavol Vican9887c682016-02-29 11:32:01 +01001480 return EXIT_SUCCESS;
1481
1482error:
1483 return EXIT_FAILURE;
1484}
Pavol Vican0df02b02016-03-01 10:28:50 +01001485
1486void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001487yang_read_typedef(struct lys_module *module, struct lys_node *parent, char *value)
Pavol Vican0df02b02016-03-01 10:28:50 +01001488{
1489 struct lys_tpdf *ret;
Pavol Vican810892e2016-07-12 16:55:34 +02001490 struct lys_node *root;
Pavol Vican0df02b02016-03-01 10:28:50 +01001491
Pavol Vican810892e2016-07-12 16:55:34 +02001492 root = (parent) ? NULL : lys_main_module(module)->data;
1493 if (lyp_check_identifier(value, LY_IDENT_TYPE, module, parent) || yang_check_typedef_identif(root, parent, value)) {
Pavol Vican0df02b02016-03-01 10:28:50 +01001494 free(value);
1495 return NULL;
1496 }
Pavol Vican810892e2016-07-12 16:55:34 +02001497
Pavol Vican0df02b02016-03-01 10:28:50 +01001498 if (!parent) {
1499 ret = &module->tpdf[module->tpdf_size];
Pavol Vican0df02b02016-03-01 10:28:50 +01001500 module->tpdf_size++;
Pavol Vican21238f52016-03-01 12:39:52 +01001501 } else {
1502 switch (parent->nodetype) {
1503 case LYS_GROUPING:
1504 ret = &((struct lys_node_grp *)parent)->tpdf[((struct lys_node_grp *)parent)->tpdf_size];
Pavol Vican21238f52016-03-01 12:39:52 +01001505 ((struct lys_node_grp *)parent)->tpdf_size++;
1506 break;
Pavol Vican535d50e2016-03-01 13:05:33 +01001507 case LYS_CONTAINER:
1508 ret = &((struct lys_node_container *)parent)->tpdf[((struct lys_node_container *)parent)->tpdf_size];
1509 ((struct lys_node_container *)parent)->tpdf_size++;
1510 break;
Pavol Vican09f04b82016-03-01 14:02:28 +01001511 case LYS_LIST:
1512 ret = &((struct lys_node_list *)parent)->tpdf[((struct lys_node_list *)parent)->tpdf_size];
1513 ((struct lys_node_list *)parent)->tpdf_size++;
1514 break;
Pavol Vican52ed67d2016-03-02 17:46:15 +01001515 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02001516 case LYS_ACTION:
1517 ret = &((struct lys_node_rpc_action *)parent)->tpdf[((struct lys_node_rpc_action *)parent)->tpdf_size];
1518 ((struct lys_node_rpc_action *)parent)->tpdf_size++;
Pavol Vican52ed67d2016-03-02 17:46:15 +01001519 break;
Pavol Vican531a9132016-03-03 10:10:09 +01001520 case LYS_INPUT:
1521 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02001522 ret = &((struct lys_node_inout *)parent)->tpdf[((struct lys_node_inout *)parent)->tpdf_size];
1523 ((struct lys_node_inout *)parent)->tpdf_size++;
Pavol Vican531a9132016-03-03 10:10:09 +01001524 break;
Pavol Vican41267fd2016-03-03 10:47:42 +01001525 case LYS_NOTIF:
1526 ret = &((struct lys_node_notif *)parent)->tpdf[((struct lys_node_notif *)parent)->tpdf_size];
1527 ((struct lys_node_notif *)parent)->tpdf_size++;
1528 break;
Pavol Vicand1dbfda2016-03-21 10:03:58 +01001529 default:
1530 /* another type of nodetype is error*/
1531 LOGINT;
1532 free(value);
1533 return NULL;
Pavol Vican21238f52016-03-01 12:39:52 +01001534 }
Pavol Vican0df02b02016-03-01 10:28:50 +01001535 }
1536
Pavol Vican79436472016-04-04 11:22:02 +02001537 ret->type.parent = ret;
Pavol Vican0df02b02016-03-01 10:28:50 +01001538 ret->name = lydict_insert_zc(module->ctx, value);
1539 ret->module = module;
1540 return ret;
1541}
Pavol Vican1003ead2016-03-02 12:24:52 +01001542
1543void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001544yang_read_refine(struct lys_module *module, struct lys_node_uses *uses, char *value)
Pavol Vican1003ead2016-03-02 12:24:52 +01001545{
1546 struct lys_refine *rfn;
1547
1548 rfn = &uses->refine[uses->refine_size];
1549 uses->refine_size++;
Pavol Vican0adf01d2016-03-22 12:29:33 +01001550 rfn->target_name = transform_schema2json(module, value);
Pavol Vican1003ead2016-03-02 12:24:52 +01001551 free(value);
1552 if (!rfn->target_name) {
1553 return NULL;
1554 }
1555 return rfn;
1556}
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001557
1558void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001559yang_read_augment(struct lys_module *module, struct lys_node *parent, char *value)
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001560{
1561 struct lys_node_augment *aug;
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001562
1563 if (parent) {
1564 aug = &((struct lys_node_uses *)parent)->augment[((struct lys_node_uses *)parent)->augment_size];
1565 } else {
1566 aug = &module->augment[module->augment_size];
1567 }
1568 aug->nodetype = LYS_AUGMENT;
Pavol Vican0adf01d2016-03-22 12:29:33 +01001569 aug->target_name = transform_schema2json(module, value);
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001570 free(value);
1571 if (!aug->target_name) {
1572 return NULL;
1573 }
1574 aug->parent = parent;
1575 aug->module = module;
1576 if (parent) {
1577 ((struct lys_node_uses *)parent)->augment_size++;
1578 } else {
1579 module->augment_size++;
1580 }
1581 return aug;
1582}
Pavol Vican220e5a12016-03-03 14:19:43 +01001583
1584void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001585yang_read_deviation(struct lys_module *module, char *value)
Pavol Vican220e5a12016-03-03 14:19:43 +01001586{
1587 struct lys_node *dev_target = NULL;
1588 struct lys_deviation *dev;
1589 struct type_deviation *deviation = NULL;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001590 int rc;
Pavol Vican220e5a12016-03-03 14:19:43 +01001591
Pavol Vican220e5a12016-03-03 14:19:43 +01001592 dev = &module->deviation[module->deviation_size];
Pavol Vican0adf01d2016-03-22 12:29:33 +01001593 dev->target_name = transform_schema2json(module, value);
Pavol Vican220e5a12016-03-03 14:19:43 +01001594 free(value);
1595 if (!dev->target_name) {
1596 goto error;
1597 }
1598
Pavol Vican974377b2016-03-23 00:38:53 +01001599 deviation = calloc(1, sizeof *deviation);
1600 if (!deviation) {
1601 LOGMEM;
1602 goto error;
1603 }
1604
Pavol Vican220e5a12016-03-03 14:19:43 +01001605 /* resolve target node */
1606 rc = resolve_augment_schema_nodeid(dev->target_name, NULL, module, (const struct lys_node **)&dev_target);
1607 if (rc || !dev_target) {
Michal Vasko75c8daf2016-05-19 10:56:39 +02001608 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation");
Pavol Vican220e5a12016-03-03 14:19:43 +01001609 goto error;
1610 }
Radek Krejcic4283442016-04-22 09:19:27 +02001611 if (dev_target->module == lys_main_module(module)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001612 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation");
1613 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Deviating own module is not allowed.");
Pavol Vican220e5a12016-03-03 14:19:43 +01001614 goto error;
1615 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001616
Michal Vasko89563fc2016-07-28 16:19:35 +02001617 lys_node_module(dev_target)->deviated = 1;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001618
Pavol Vican220e5a12016-03-03 14:19:43 +01001619 /*save pointer to the deviation and deviated target*/
1620 deviation->deviation = dev;
1621 deviation->target = dev_target;
1622
Pavol Vican220e5a12016-03-03 14:19:43 +01001623 return deviation;
1624
1625error:
1626 free(deviation);
1627 lydict_remove(module->ctx, dev->target_name);
1628 return NULL;
1629}
Pavol Vican4c90c642016-03-03 15:06:47 +01001630
1631int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001632yang_read_deviate_unsupported(struct type_deviation *dev)
Pavol Vican4c90c642016-03-03 15:06:47 +01001633{
1634 int i;
1635
1636 if (dev->deviation->deviate_size) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001637 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
Pavol Vican4c90c642016-03-03 15:06:47 +01001638 return EXIT_FAILURE;
1639 }
1640 dev->deviation->deviate[dev->deviation->deviate_size].mod = LY_DEVIATE_NO;
1641
1642 /* you cannot remove a key leaf */
1643 if ((dev->target->nodetype == LYS_LEAF) && dev->target->parent && (dev->target->parent->nodetype == LYS_LIST)) {
1644 for (i = 0; i < ((struct lys_node_list *)dev->target->parent)->keys_size; ++i) {
1645 if (((struct lys_node_list *)dev->target->parent)->keys[i] == (struct lys_node_leaf *)dev->target) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001646 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "not-supported", "deviation");
1647 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot remove a list key.");
Pavol Vican4c90c642016-03-03 15:06:47 +01001648 return EXIT_FAILURE;
1649 }
1650 }
1651 }
Michal Vaskofe7e5a72016-05-02 14:49:23 +02001652
Pavol Vican4c90c642016-03-03 15:06:47 +01001653 /* unlink and store the original node */
Michal Vaskod921d682016-05-19 10:56:51 +02001654 lys_node_unlink(dev->target);
Pavol Vican4c90c642016-03-03 15:06:47 +01001655 dev->deviation->orig_node = dev->target;
1656
1657 dev->deviation->deviate_size = 1;
1658 return EXIT_SUCCESS;
1659}
Pavol Vican85f12022016-03-05 16:30:35 +01001660
1661int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001662yang_read_deviate(struct type_deviation *dev, LYS_DEVIATE_TYPE mod)
Pavol Vican85f12022016-03-05 16:30:35 +01001663{
1664 struct unres_schema tmp_unres;
1665
1666 dev->deviation->deviate[dev->deviation->deviate_size].mod = mod;
1667 dev->deviate = &dev->deviation->deviate[dev->deviation->deviate_size];
1668 dev->deviation->deviate_size++;
1669 if (dev->deviation->deviate[0].mod == LY_DEVIATE_NO) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001670 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "not-supported");
1671 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
Pavol Vican85f12022016-03-05 16:30:35 +01001672 return EXIT_FAILURE;
1673 }
1674
1675 /* store a shallow copy of the original node */
1676 if (!dev->deviation->orig_node) {
1677 memset(&tmp_unres, 0, sizeof tmp_unres);
1678 dev->deviation->orig_node = lys_node_dup(dev->target->module, NULL, dev->target, 0, 0, &tmp_unres, 1);
1679 /* just to be safe */
1680 if (tmp_unres.count) {
1681 LOGINT;
1682 return EXIT_FAILURE;
1683 }
1684 }
1685
1686 return EXIT_SUCCESS;
1687}
1688
1689int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001690yang_read_deviate_units(struct ly_ctx *ctx, struct type_deviation *dev, char *value)
Pavol Vican85f12022016-03-05 16:30:35 +01001691{
1692 const char **stritem;
1693
1694 if (dev->deviate->units) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001695 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "units", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001696 free(value);
1697 goto error;
1698 }
1699
1700 /* check target node type */
1701 if (dev->target->nodetype == LYS_LEAFLIST) {
1702 stritem = &((struct lys_node_leaflist *)dev->target)->units;
1703 } else if (dev->target->nodetype == LYS_LEAF) {
1704 stritem = &((struct lys_node_leaf *)dev->target)->units;
1705 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001706 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1707 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"units\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001708 free(value);
1709 goto error;
1710 }
1711
1712 dev->deviate->units = lydict_insert_zc(ctx, value);
1713
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001714 if (dev->deviate->mod == LY_DEVIATE_DEL) {
1715 /* check values */
Michal Vaskob42b6972016-06-06 14:21:30 +02001716 if (!ly_strequal(*stritem, dev->deviate->units, 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001717 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->units, "units");
1718 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001719 goto error;
1720 }
1721 /* remove current units value of the target */
1722 lydict_remove(ctx, *stritem);
Pavol Vican4766aca2016-03-07 12:42:36 +01001723 } else {
1724 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1725 /* check that there is no current value */
1726 if (*stritem) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001727 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1728 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001729 goto error;
1730 }
1731 } else { /* replace */
1732 if (!*stritem) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001733 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1734 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001735 goto error;
1736 }
1737 }
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001738 /* remove current units value of the target ... */
1739 lydict_remove(ctx, *stritem);
1740
1741 /* ... and replace it with the value specified in deviation */
1742 *stritem = lydict_insert(ctx, dev->deviate->units, 0);
1743 }
1744
Pavol Vican85f12022016-03-05 16:30:35 +01001745 return EXIT_SUCCESS;
1746
1747error:
1748 return EXIT_FAILURE;
1749}
1750
1751int
Pavol Vican974377b2016-03-23 00:38:53 +01001752yang_read_deviate_must(struct type_deviation *dev, uint8_t c_must)
Pavol Vican85f12022016-03-05 16:30:35 +01001753{
Pavol Vican85f12022016-03-05 16:30:35 +01001754 /* check target node type */
1755 switch (dev->target->nodetype) {
1756 case LYS_LEAF:
1757 dev->trg_must = &((struct lys_node_leaf *)dev->target)->must;
1758 dev->trg_must_size = &((struct lys_node_leaf *)dev->target)->must_size;
1759 break;
1760 case LYS_CONTAINER:
1761 dev->trg_must = &((struct lys_node_container *)dev->target)->must;
1762 dev->trg_must_size = &((struct lys_node_container *)dev->target)->must_size;
1763 break;
1764 case LYS_LEAFLIST:
1765 dev->trg_must = &((struct lys_node_leaflist *)dev->target)->must;
1766 dev->trg_must_size = &((struct lys_node_leaflist *)dev->target)->must_size;
1767 break;
1768 case LYS_LIST:
1769 dev->trg_must = &((struct lys_node_list *)dev->target)->must;
1770 dev->trg_must_size = &((struct lys_node_list *)dev->target)->must_size;
1771 break;
1772 case LYS_ANYXML:
1773 dev->trg_must = &((struct lys_node_anyxml *)dev->target)->must;
1774 dev->trg_must_size = &((struct lys_node_anyxml *)dev->target)->must_size;
1775 break;
1776 default:
Pavol Vican0adf01d2016-03-22 12:29:33 +01001777 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must");
1778 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001779 goto error;
1780 }
1781
1782 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1783 /* reallocate the must array of the target */
1784 dev->deviate->must = ly_realloc(*dev->trg_must, (c_must + *dev->trg_must_size) * sizeof *dev->deviate->must);
1785 if (!dev->deviate->must) {
1786 LOGMEM;
1787 goto error;
1788 }
1789 *dev->trg_must = dev->deviate->must;
1790 dev->deviate->must = &((*dev->trg_must)[*dev->trg_must_size]);
1791 dev->deviate->must_size = c_must;
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001792 } else {
1793 /* LY_DEVIATE_DEL */
1794 dev->deviate->must = calloc(c_must, sizeof *dev->deviate->must);
1795 if (!dev->deviate->must) {
1796 LOGMEM;
1797 goto error;
1798 }
Pavol Vican85f12022016-03-05 16:30:35 +01001799 }
1800
1801 return EXIT_SUCCESS;
1802
1803error:
1804 return EXIT_FAILURE;
1805}
1806
1807int
Pavol Vican974377b2016-03-23 00:38:53 +01001808yang_read_deviate_unique(struct type_deviation *dev, uint8_t c_uniq)
Pavol Vican85f12022016-03-05 16:30:35 +01001809{
Pavol Vican85f12022016-03-05 16:30:35 +01001810 struct lys_node_list *list;
1811
1812 /* check target node type */
1813 if (dev->target->nodetype != LYS_LIST) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001814 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique");
1815 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"unique\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001816 goto error;
1817 }
1818
1819 list = (struct lys_node_list *)dev->target;
1820 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1821 /* reallocate the unique array of the target */
1822 dev->deviate->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *dev->deviate->unique);
1823 if (!dev->deviate->unique) {
1824 LOGMEM;
1825 goto error;
1826 }
1827 list->unique = dev->deviate->unique;
1828 dev->deviate->unique = &list->unique[list->unique_size];
1829 dev->deviate->unique_size = c_uniq;
1830 memset(dev->deviate->unique, 0, c_uniq * sizeof *dev->deviate->unique);
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001831 } else {
1832 /* LY_DEVIATE_DEL */
1833 dev->deviate->unique = calloc(c_uniq, sizeof *dev->deviate->unique);
1834 if (!dev->deviate->unique) {
1835 LOGMEM;
1836 goto error;
1837 }
Pavol Vican85f12022016-03-05 16:30:35 +01001838 }
1839
1840 return EXIT_SUCCESS;
1841
1842error:
1843 return EXIT_FAILURE;
1844}
1845
1846int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001847yang_read_deviate_default(struct ly_ctx *ctx, struct type_deviation *dev, char *value)
Pavol Vican85f12022016-03-05 16:30:35 +01001848{
1849 int rc;
1850 struct lys_node_choice *choice;
1851 struct lys_node_leaf *leaf;
1852 struct lys_node *node;
1853
1854 if (dev->deviate->dflt) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001855 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "default", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001856 free(value);
1857 goto error;
1858 }
1859
1860 dev->deviate->dflt = lydict_insert_zc(ctx, value);
1861
1862 if (dev->target->nodetype == LYS_CHOICE) {
1863 choice = (struct lys_node_choice *)dev->target;
1864
Pavol Vican85f12022016-03-05 16:30:35 +01001865 rc = resolve_choice_default_schema_nodeid(dev->deviate->dflt, choice->child, (const struct lys_node **)&node);
1866 if (rc || !node) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001867 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default");
Pavol Vican85f12022016-03-05 16:30:35 +01001868 goto error;
1869 }
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001870 if (dev->deviate->mod == LY_DEVIATE_DEL) {
1871 if (!choice->dflt || (choice->dflt != node)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001872 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default");
1873 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001874 goto error;
1875 }
1876 choice->dflt = NULL;
Pavol Vican4766aca2016-03-07 12:42:36 +01001877 } else {
1878 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1879 /* check that there is no current value */
1880 if (choice->dflt) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001881 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1882 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001883 goto error;
Pavol Vican321a02e2016-04-05 16:11:59 +02001884 } else if (choice->flags & LYS_MAND_TRUE) {
1885 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "choice");
1886 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"default\" statement is forbidden on choices with \"mandatory\".");
1887 goto error;
Pavol Vican4766aca2016-03-07 12:42:36 +01001888 }
1889 } else { /* replace*/
1890 if (!choice->dflt) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001891 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1892 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001893 goto error;
1894 }
1895 }
1896
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001897 choice->dflt = node;
1898 if (!choice->dflt) {
1899 /* default branch not found */
Pavol Vican0adf01d2016-03-22 12:29:33 +01001900 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001901 goto error;
1902 }
Pavol Vican85f12022016-03-05 16:30:35 +01001903 }
1904 } else if (dev->target->nodetype == LYS_LEAF) {
1905 leaf = (struct lys_node_leaf *)dev->target;
1906
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001907 if (dev->deviate->mod == LY_DEVIATE_DEL) {
Michal Vaskob42b6972016-06-06 14:21:30 +02001908 if (!leaf->dflt || !ly_strequal(leaf->dflt, dev->deviate->dflt, 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001909 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->dflt, "default");
1910 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001911 goto error;
1912 }
1913 /* remove value */
1914 lydict_remove(ctx, leaf->dflt);
1915 leaf->dflt = NULL;
Pavol Vican4766aca2016-03-07 12:42:36 +01001916 } else {
1917 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1918 /* check that there is no current value */
1919 if (leaf->dflt) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001920 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1921 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001922 goto error;
Pavol Vicanfdf81c42016-04-05 15:55:31 +02001923 } else if (leaf->flags & LYS_MAND_TRUE) {
1924 /* RFC 6020, 7.6.4 - default statement must not with mandatory true */
1925 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "leaf");
1926 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"default\" statement is forbidden on leaf with \"mandatory\".");
1927 goto error;
Pavol Vican4766aca2016-03-07 12:42:36 +01001928 }
1929 } else { /* replace*/
1930 if (!leaf->dflt) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001931 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1932 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001933 goto error;
1934 }
1935 }
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001936 /* remove value */
1937 lydict_remove(ctx, leaf->dflt);
Pavol Vican85f12022016-03-05 16:30:35 +01001938
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001939 /* set new value */
1940 leaf->dflt = lydict_insert(ctx, dev->deviate->dflt, 0);
1941 }
Pavol Vican85f12022016-03-05 16:30:35 +01001942 } else {
1943 /* invalid target for default value */
Pavol Vican0adf01d2016-03-22 12:29:33 +01001944 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1945 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001946 goto error;
1947 }
1948
1949 return EXIT_SUCCESS;
1950
1951error:
1952 return EXIT_FAILURE;
1953}
1954
1955int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001956yang_read_deviate_config(struct type_deviation *dev, uint8_t value)
Pavol Vican85f12022016-03-05 16:30:35 +01001957{
1958 if (dev->deviate->flags & LYS_CONFIG_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001959 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "config", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001960 goto error;
1961 }
1962
1963 /* for we deviate from RFC 6020 and allow config property even it is/is not
1964 * specified in the target explicitly since config property inherits. So we expect
1965 * that config is specified in every node. But for delete, we check that the value
1966 * is the same as here in deviation
1967 */
1968 dev->deviate->flags |= value;
1969
1970 /* add and replace are the same in this case */
1971 /* remove current config value of the target ... */
1972 dev->target->flags &= ~LYS_CONFIG_MASK;
1973
1974 /* ... and replace it with the value specified in deviation */
1975 dev->target->flags |= dev->deviate->flags & LYS_CONFIG_MASK;
1976
1977 return EXIT_SUCCESS;
1978
1979error:
1980 return EXIT_FAILURE;
1981}
1982
1983int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001984yang_read_deviate_mandatory(struct type_deviation *dev, uint8_t value)
Pavol Vican85f12022016-03-05 16:30:35 +01001985{
Radek Krejcie00d2312016-08-12 15:27:49 +02001986 struct lys_node *parent;
1987
Pavol Vican85f12022016-03-05 16:30:35 +01001988 if (dev->deviate->flags & LYS_MAND_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001989 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "mandatory", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001990 goto error;
1991 }
1992
1993 /* check target node type */
1994 if (!(dev->target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYXML))) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001995 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
1996 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"mandatory\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001997 goto error;
1998 }
1999
2000 dev->deviate->flags |= value;
2001
2002 if (dev->deviate->mod == LY_DEVIATE_ADD) {
2003 /* check that there is no current value */
2004 if (dev->target->flags & LYS_MAND_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002005 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
2006 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican85f12022016-03-05 16:30:35 +01002007 goto error;
Pavol Vican321a02e2016-04-05 16:11:59 +02002008 } else {
2009 if (dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) {
2010 /* RFC 6020, 7.6.4 - default statement must not with mandatory true */
2011 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "leaf");
2012 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\".");
2013 goto error;
2014 } else if (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt) {
2015 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "choice");
2016 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\".");
2017 goto error;
2018 }
Pavol Vican85f12022016-03-05 16:30:35 +01002019 }
Pavol Vican4766aca2016-03-07 12:42:36 +01002020 } else { /* replace */
2021 if (!(dev->target->flags & LYS_MAND_MASK)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002022 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
2023 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01002024 goto error;
2025 }
Pavol Vican85f12022016-03-05 16:30:35 +01002026 }
2027
Pavol Vican85f12022016-03-05 16:30:35 +01002028 /* remove current mandatory value of the target ... */
2029 dev->target->flags &= ~LYS_MAND_MASK;
2030
2031 /* ... and replace it with the value specified in deviation */
2032 dev->target->flags |= dev->deviate->flags & LYS_MAND_MASK;
2033
Radek Krejcie00d2312016-08-12 15:27:49 +02002034 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
2035 for (parent = dev->target->parent;
2036 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION));
2037 parent = parent->parent) {
2038 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
2039 /* stop also on presence containers */
2040 break;
2041 }
2042 }
2043 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
2044 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
2045 if (lyp_check_mandatory_choice(parent)) {
2046 goto error;
2047 }
2048 }
2049
Pavol Vican85f12022016-03-05 16:30:35 +01002050 return EXIT_SUCCESS;
2051
2052error:
2053 return EXIT_FAILURE;
2054}
2055
2056int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002057yang_read_deviate_minmax(struct type_deviation *dev, uint32_t value, int type)
Pavol Vican85f12022016-03-05 16:30:35 +01002058{
2059 uint32_t *ui32val;
2060
2061 /* check target node type */
2062 if (dev->target->nodetype == LYS_LEAFLIST) {
2063 if (type) {
2064 ui32val = &((struct lys_node_leaflist *)dev->target)->max;
2065 } else {
2066 ui32val = &((struct lys_node_leaflist *)dev->target)->min;
2067 }
2068 } else if (dev->target->nodetype == LYS_LIST) {
2069 if (type) {
2070 ui32val = &((struct lys_node_list *)dev->target)->max;
2071 } else {
2072 ui32val = &((struct lys_node_list *)dev->target)->min;
2073 }
2074 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002075 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements");
2076 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 +01002077 goto error;
2078 }
2079
2080 if (type) {
2081 dev->deviate->max = value;
Pavol Vican4766aca2016-03-07 12:42:36 +01002082 dev->deviate->max_set = 1;
Pavol Vican85f12022016-03-05 16:30:35 +01002083 } else {
2084 dev->deviate->min = value;
Pavol Vican4766aca2016-03-07 12:42:36 +01002085 dev->deviate->min_set = 1;
Pavol Vican85f12022016-03-05 16:30:35 +01002086 }
2087
2088 if (dev->deviate->mod == LY_DEVIATE_ADD) {
2089 /* check that there is no current value */
2090 if (*ui32val) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002091 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements");
2092 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican85f12022016-03-05 16:30:35 +01002093 goto error;
2094 }
Pavol Vican4766aca2016-03-07 12:42:36 +01002095 } else if (dev->deviate->mod == LY_DEVIATE_RPL) {
2096 /* unfortunately, there is no way to check reliably that there
2097 * was a value before, it could have been the default */
Pavol Vican85f12022016-03-05 16:30:35 +01002098 }
2099
2100 /* add (already checked) and replace */
2101 /* set new value specified in deviation */
2102 *ui32val = value;
2103
2104 return EXIT_SUCCESS;
2105
2106error:
2107 return EXIT_FAILURE;
2108}
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002109
2110int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002111yang_check_deviate_must(struct ly_ctx *ctx, struct type_deviation *dev)
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002112{
2113 int i;
2114
2115 /* find must to delete, we are ok with just matching conditions */
2116 for (i = 0; i < *dev->trg_must_size; i++) {
2117 if (ly_strequal(dev->deviate->must[dev->deviate->must_size - 1].expr, (*dev->trg_must)[i].expr, 1)) {
2118 /* we have a match, free the must structure ... */
2119 lys_restr_free(ctx, &((*dev->trg_must)[i]));
2120 /* ... and maintain the array */
2121 (*dev->trg_must_size)--;
2122 if (i != *dev->trg_must_size) {
2123 (*dev->trg_must)[i].expr = (*dev->trg_must)[*dev->trg_must_size].expr;
2124 (*dev->trg_must)[i].dsc = (*dev->trg_must)[*dev->trg_must_size].dsc;
2125 (*dev->trg_must)[i].ref = (*dev->trg_must)[*dev->trg_must_size].ref;
2126 (*dev->trg_must)[i].eapptag = (*dev->trg_must)[*dev->trg_must_size].eapptag;
2127 (*dev->trg_must)[i].emsg = (*dev->trg_must)[*dev->trg_must_size].emsg;
2128 }
2129 if (!(*dev->trg_must_size)) {
2130 free(*dev->trg_must);
2131 *dev->trg_must = NULL;
2132 } else {
2133 (*dev->trg_must)[*dev->trg_must_size].expr = NULL;
2134 (*dev->trg_must)[*dev->trg_must_size].dsc = NULL;
2135 (*dev->trg_must)[*dev->trg_must_size].ref = NULL;
2136 (*dev->trg_must)[*dev->trg_must_size].eapptag = NULL;
2137 (*dev->trg_must)[*dev->trg_must_size].emsg = NULL;
2138 }
2139
2140 i = -1; /* set match flag */
2141 break;
2142 }
2143 }
2144 if (i != -1) {
2145 /* no match found */
Pavol Vican0adf01d2016-03-22 12:29:33 +01002146 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->must[dev->deviate->must_size - 1].expr, "must");
2147 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value does not match any must from the target.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002148 return EXIT_FAILURE;
2149 }
2150
2151 return EXIT_SUCCESS;
2152}
2153
2154int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002155yang_check_deviate_unique(struct lys_module *module, struct type_deviation *dev, char *value)
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002156{
2157 struct lys_node_list *list;
2158 int i, j;
2159
2160 list = (struct lys_node_list *)dev->target;
Pavol Vican0adf01d2016-03-22 12:29:33 +01002161 if (yang_fill_unique(module, list, &dev->deviate->unique[dev->deviate->unique_size], value, NULL)) {
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002162 dev->deviate->unique_size++;
2163 goto error;
2164 }
2165
2166 /* find unique structures to delete */
2167 for (i = 0; i < list->unique_size; i++) {
2168 if (list->unique[i].expr_size != dev->deviate->unique[dev->deviate->unique_size].expr_size) {
2169 continue;
2170 }
2171
2172 for (j = 0; j < dev->deviate->unique[dev->deviate->unique_size].expr_size; j++) {
2173 if (!ly_strequal(list->unique[i].expr[j], dev->deviate->unique[dev->deviate->unique_size].expr[j], 1)) {
2174 break;
2175 }
2176 }
2177
2178 if (j == dev->deviate->unique[dev->deviate->unique_size].expr_size) {
2179 /* we have a match, free the unique structure ... */
2180 for (j = 0; j < list->unique[i].expr_size; j++) {
2181 lydict_remove(module->ctx, list->unique[i].expr[j]);
2182 }
2183 free(list->unique[i].expr);
2184 /* ... and maintain the array */
2185 list->unique_size--;
2186 if (i != list->unique_size) {
2187 list->unique[i].expr_size = list->unique[list->unique_size].expr_size;
2188 list->unique[i].expr = list->unique[list->unique_size].expr;
2189 }
2190
2191 if (!list->unique_size) {
2192 free(list->unique);
2193 list->unique = NULL;
2194 } else {
2195 list->unique[list->unique_size].expr_size = 0;
2196 list->unique[list->unique_size].expr = NULL;
2197 }
2198
2199 i = -1; /* set match flag */
2200 break;
2201 }
2202 }
2203 dev->deviate->unique_size++;
2204
2205 if (i != -1) {
2206 /* no match found */
Pavol Vican0adf01d2016-03-22 12:29:33 +01002207 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "unique");
2208 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002209 goto error;
2210 }
2211
2212 free(value);
2213 return EXIT_SUCCESS;
2214
2215error:
2216 free(value);
2217 return EXIT_FAILURE;
2218}
Pavol Vicane92421d2016-03-08 10:12:33 +01002219
2220int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002221yang_check_deviation(struct lys_module *module, struct type_deviation *dev, struct unres_schema *unres)
Pavol Vicane92421d2016-03-08 10:12:33 +01002222{
2223 int i, rc;
2224
2225 if (dev->target->nodetype == LYS_LEAF) {
2226 for(i = 0; i < dev->deviation->deviate_size; ++i) {
2227 if (dev->deviation->deviate[i].mod != LY_DEVIATE_DEL) {
2228 if (dev->deviation->deviate[i].dflt || dev->deviation->deviate[i].type) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002229 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 +01002230 if (rc == -1) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002231 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 +01002232 return EXIT_FAILURE;
2233 }
2234 break;
2235 }
2236 }
2237 }
2238 }
2239 return EXIT_SUCCESS;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002240}
2241
2242int
2243yang_fill_include(struct lys_module *module, struct lys_submodule *submodule, char *value,
Pavol Vicane024ab72016-07-27 14:27:43 +02002244 struct lys_include *inc, struct unres_schema *unres)
Pavol Vican9b89dda2016-03-09 15:36:55 +01002245{
Pavol Vican9b89dda2016-03-09 15:36:55 +01002246 struct lys_module *trg;
Pavol Vican55870412016-03-10 12:36:21 +01002247 const char *str;
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002248 int rc;
Radek Krejci83e3f5b2016-06-24 14:55:25 +02002249 int ret = 0;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002250
Pavol Vican55870412016-03-10 12:36:21 +01002251 str = lydict_insert_zc(module->ctx, value);
Pavol Vican9b89dda2016-03-09 15:36:55 +01002252 trg = (submodule) ? (struct lys_module *)submodule : module;
Pavol Vicane024ab72016-07-27 14:27:43 +02002253 rc = lyp_check_include(module, submodule, str, inc, unres);
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002254 if (!rc) {
2255 /* success, copy the filled data into the final array */
Pavol Vicane024ab72016-07-27 14:27:43 +02002256 memcpy(&trg->inc[trg->inc_size], inc, sizeof *inc);
Radek Krejci4dcd3392016-06-22 10:28:40 +02002257 trg->inc_size++;
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002258 } else if (rc == -1) {
Radek Krejci83e3f5b2016-06-24 14:55:25 +02002259 ret = -1;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002260 }
Pavol Vican9b89dda2016-03-09 15:36:55 +01002261
Pavol Vican55870412016-03-10 12:36:21 +01002262 lydict_remove(module->ctx, str);
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002263 return ret;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002264}
Pavol Vicanf4717e62016-03-16 11:30:01 +01002265
2266int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002267yang_use_extension(struct lys_module *module, struct lys_node *data_node, void *actual, char *value)
Pavol Vicanf4717e62016-03-16 11:30:01 +01002268{
2269 char *prefix;
2270 char *identif;
2271 const char *ns = NULL;
2272 int i;
2273
Pavol Vicanf4717e62016-03-16 11:30:01 +01002274 /* check to the same pointer */
2275 if (data_node != actual) {
2276 return EXIT_SUCCESS;
2277 }
2278
Pavol Vicana302aa62016-03-17 10:45:35 +01002279 prefix = strdup(value);
2280 if (!prefix) {
2281 LOGMEM;
2282 goto error;
2283 }
2284 /* find prefix anf identificator*/
2285 identif = strchr(prefix, ':');
2286 *identif = '\0';
2287 identif++;
2288
Pavol Vicanf4717e62016-03-16 11:30:01 +01002289 for(i = 0; i < module->imp_size; ++i) {
2290 if (!strcmp(module->imp[i].prefix, prefix)) {
2291 ns = module->imp[i].module->ns;
2292 break;
2293 }
2294 }
Pavol Vican1ff1b7b2016-04-07 09:51:49 +02002295 if (!ns && !strcmp(module->prefix, prefix)) {
2296 ns = (module->type) ? ((struct lys_submodule *)module)->belongsto->ns : module->ns;
2297 }
Pavol Vicanf4717e62016-03-16 11:30:01 +01002298 if (ns && !strcmp(ns, LY_NSNACM)) {
2299 if (!strcmp(identif, "default-deny-write")) {
2300 data_node->nacm |= LYS_NACM_DENYW;
2301 } else if (!strcmp(identif, "default-deny-all")) {
2302 data_node->nacm |= LYS_NACM_DENYA;
2303 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002304 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, identif);
Pavol Vicana302aa62016-03-17 10:45:35 +01002305 goto error;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002306 }
2307 }
Pavol Vicana302aa62016-03-17 10:45:35 +01002308 free(prefix);
Pavol Vicanf4717e62016-03-16 11:30:01 +01002309 return EXIT_SUCCESS;
Pavol Vicana302aa62016-03-17 10:45:35 +01002310
2311error:
2312 free(prefix);
2313 return EXIT_FAILURE;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002314}
2315
2316void
2317nacm_inherit(struct lys_module *module)
2318{
Pavol Vican10ffba52016-04-04 12:21:22 +02002319 struct lys_node *next, *elem, *tmp_node, *tmp_child;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002320
2321 LY_TREE_DFS_BEGIN(module->data, next, elem) {
Pavol Vican10ffba52016-04-04 12:21:22 +02002322 tmp_node = NULL;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002323 if (elem->parent) {
2324 switch (elem->nodetype) {
Pavol Vican10ffba52016-04-04 12:21:22 +02002325 case LYS_GROUPING:
2326 /* extension nacm not inherited*/
2327 break;
2328 case LYS_CHOICE:
2329 case LYS_ANYXML:
2330 case LYS_USES:
2331 if (elem->parent->nodetype != LYS_GROUPING) {
2332 elem->nacm |= elem->parent->nacm;
2333 }
2334 break;
2335 case LYS_CONTAINER:
2336 case LYS_LIST:
2337 case LYS_CASE:
2338 case LYS_NOTIF:
2339 case LYS_RPC:
2340 case LYS_INPUT:
2341 case LYS_OUTPUT:
2342 case LYS_AUGMENT:
2343 elem->nacm |= elem->parent->nacm;
2344 break;
2345 case LYS_LEAF:
2346 case LYS_LEAFLIST:
2347 tmp_node = elem;
2348 tmp_child = elem->child;
2349 elem->child = NULL;
2350 default:
2351 break;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002352 }
2353 }
2354 LY_TREE_DFS_END(module->data, next, elem);
Pavol Vican10ffba52016-04-04 12:21:22 +02002355 if (tmp_node) {
2356 tmp_node->child = tmp_child;
2357 }
Pavol Vicanf4717e62016-03-16 11:30:01 +01002358 }
2359}
Pavol Vican4fb66c92016-03-17 10:32:27 +01002360
2361void
2362store_flags(struct lys_node *node, uint8_t flags, int config_inherit)
2363{
2364 node->flags |= flags;
2365 if (!(node->flags & LYS_CONFIG_MASK) && config_inherit) {
2366 /* get config flag from parent */
2367 if (node->parent) {
2368 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
2369 } else {
2370 /* default config is true */
2371 node->flags |= LYS_CONFIG_W;
2372 }
2373 }
2374}
Pavol Vican8e7110b2016-03-22 17:00:26 +01002375
Pavol Vican1938d882016-04-10 13:36:31 +02002376static int
2377yang_parse(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, const char *data,
2378 unsigned int size, struct lys_array_size *size_arrays, int type_read)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002379{
2380 YY_BUFFER_STATE bp;
Pavol Vican802af1e2016-03-23 20:42:26 +01002381 yyscan_t scanner = NULL;
Pavol Vican1938d882016-04-10 13:36:31 +02002382 int ret = EXIT_SUCCESS;
2383
2384 yylex_init(&scanner);
2385 bp = yy_scan_buffer((char *)data, size, scanner);
2386 yy_switch_to_buffer(bp, scanner);
2387 if (yyparse(scanner, module, submodule, unres, size_arrays, type_read)) {
2388 ret = EXIT_FAILURE;
2389 }
2390 yy_delete_buffer(bp, scanner);
2391 yylex_destroy(scanner);
2392 return ret;
2393}
2394
2395int
2396yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, const char *data, unsigned int size_data)
2397{
Pavol Vican8e7110b2016-03-22 17:00:26 +01002398 struct lys_array_size *size_arrays=NULL;
Pavol Vican974377b2016-03-23 00:38:53 +01002399 unsigned int size;
Pavol Vican1938d882016-04-10 13:36:31 +02002400 int ret;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002401
2402 size_arrays = calloc(1, sizeof *size_arrays);
2403 if (!size_arrays) {
2404 LOGMEM;
Pavol Vicanf7994fb2016-04-05 21:55:53 +02002405 return EXIT_FAILURE;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002406 }
Pavol Vican8e7110b2016-03-22 17:00:26 +01002407 size = (size_data) ? size_data : strlen(data) + 2;
Pavol Vican1938d882016-04-10 13:36:31 +02002408 ret = yang_parse(module, submodule, unres, data, size, size_arrays, LY_READ_ONLY_SIZE);
2409 if (!ret) {
2410 ret = yang_parse(module, submodule, unres, data, size, size_arrays, LY_READ_ALL);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002411 }
Pavol Vican8e7110b2016-03-22 17:00:26 +01002412 free(size_arrays->node);
2413 free(size_arrays);
Pavol Vican1938d882016-04-10 13:36:31 +02002414 return ret;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002415}
2416
2417struct lys_module *
Pavol Vican974377b2016-03-23 00:38:53 +01002418yang_read_module(struct ly_ctx *ctx, const char* data, unsigned int size, const char *revision, int implement)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002419{
2420
Pavol Vican10ffba52016-04-04 12:21:22 +02002421 struct lys_module *tmp_module, *module = NULL;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002422 struct unres_schema *unres = NULL;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002423 int i;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002424
2425 unres = calloc(1, sizeof *unres);
2426 if (!unres) {
2427 LOGMEM;
2428 goto error;
2429 }
2430
2431 module = calloc(1, sizeof *module);
2432 if (!module) {
2433 LOGMEM;
Pavol Vicanf7994fb2016-04-05 21:55:53 +02002434 goto error;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002435 }
2436
2437 /* initiale module */
Michal Vaskofe7e5a72016-05-02 14:49:23 +02002438 module->ctx = ctx;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002439 module->type = 0;
2440 module->implemented = (implement ? 1 : 0);
2441
2442 if (yang_parse_mem(module, NULL, unres, data, size)) {
2443 goto error;
2444 }
2445
2446 if (module && unres->count && resolve_unres_schema(module, unres)) {
2447 goto error;
2448 }
2449
2450 if (revision) {
2451 /* check revision of the parsed model */
2452 if (!module->rev_size || strcmp(revision, module->rev[0].date)) {
2453 LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").",
2454 module->name, module->rev[0].date, revision);
2455 goto error;
2456 }
2457 }
2458
Pavol Vican10ffba52016-04-04 12:21:22 +02002459 tmp_module = module;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002460 if (lyp_ctx_add_module(&module)) {
Pavol Vican8e7110b2016-03-22 17:00:26 +01002461 goto error;
2462 }
2463
Pavol Vican10ffba52016-04-04 12:21:22 +02002464 if (module == tmp_module) {
2465 nacm_inherit(module);
2466 }
2467
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002468 if (module->augment_size || module->deviation_size) {
Michal Vasko5cec3312016-05-04 08:59:41 +02002469 if (!module->implemented) {
2470 LOGVRB("Module \"%s\" includes augments or deviations, changing conformance to \"implement\".", module->name);
2471 }
Michal Vasko26055752016-05-03 11:36:31 +02002472 if (lys_module_set_implement(module)) {
2473 goto error;
2474 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002475
Michal Vasko26055752016-05-03 11:36:31 +02002476 if (lys_sub_module_set_dev_aug_target_implement(module)) {
2477 goto error;
2478 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002479 for (i = 0; i < module->inc_size; ++i) {
Radek Krejci4dcd3392016-06-22 10:28:40 +02002480 if (!module->inc[i].submodule) {
2481 continue;
2482 }
Michal Vasko26055752016-05-03 11:36:31 +02002483 if (lys_sub_module_set_dev_aug_target_implement((struct lys_module *)module->inc[i].submodule)) {
2484 goto error;
2485 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002486 }
2487 }
2488
Pavol Vican8e7110b2016-03-22 17:00:26 +01002489 unres_schema_free(NULL, &unres);
2490 LOGVRB("Module \"%s\" successfully parsed.", module->name);
2491 return module;
2492
2493error:
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002494 /* cleanup */
Pavol Vican8e7110b2016-03-22 17:00:26 +01002495 unres_schema_free(module, &unres);
2496 if (!module || !module->name) {
2497 free(module);
2498 LOGERR(ly_errno, "Module parsing failed.");
2499 return NULL;
2500 }
2501
2502 LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002503
2504 lys_sub_module_remove_devs_augs(module);
2505 lys_free(module, NULL, 1);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002506 return NULL;
2507}
2508
2509struct lys_submodule *
Pavol Vican974377b2016-03-23 00:38:53 +01002510yang_read_submodule(struct lys_module *module, const char *data, unsigned int size, struct unres_schema *unres)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002511{
2512 struct lys_submodule *submodule;
2513
2514 submodule = calloc(1, sizeof *submodule);
2515 if (!submodule) {
2516 LOGMEM;
2517 goto error;
2518 }
2519
2520 submodule->ctx = module->ctx;
2521 submodule->type = 1;
2522 submodule->belongsto = module;
2523
2524 if (yang_parse_mem(module, submodule, unres, data, size)) {
2525 goto error;
2526 }
2527
Pavol Vican8e7110b2016-03-22 17:00:26 +01002528 LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002529 return submodule;
2530
2531error:
2532 /* cleanup */
2533 unres_schema_free((struct lys_module *)submodule, &unres);
2534
2535 if (!submodule || !submodule->name) {
2536 free(submodule);
2537 LOGERR(ly_errno, "Submodule parsing failed.");
2538 return NULL;
2539 }
2540
2541 LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name);
2542
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002543 lys_sub_module_remove_devs_augs((struct lys_module *)submodule);
2544 lys_submodule_module_data_free(submodule);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002545 lys_submodule_free(submodule, NULL);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002546 return NULL;
2547}
Pavol Vican8760bb72016-04-07 09:44:01 +02002548
2549static int
2550count_substring(const char *str, char c)
2551{
2552 const char *tmp = str;
2553 int count = 0;
2554
2555 while ((tmp = strchr(tmp, c))) {
2556 tmp++;
2557 count++;
2558 }
2559 return count;
2560}
2561
2562static int
2563read_indent(const char *input, int indent, int size, int in_index, int *out_index, char *output)
2564{
2565 int k = 0, j;
2566
2567 while (in_index < size) {
2568 if (input[in_index] == ' ') {
2569 k++;
2570 } else if (input[in_index] == '\t') {
2571 /* RFC 6020 6.1.3 tab character is treated as 8 space characters */
2572 k += 8;
2573 } else {
2574 break;
2575 }
2576 ++in_index;
2577 if (k >= indent) {
2578 for (j = k - indent; j > 0; --j) {
2579 output[*out_index] = ' ';
2580 ++(*out_index);
2581 }
2582 break;
2583 }
2584 }
2585 return in_index;
2586}
2587
2588char *
Pavol Vican677b0132016-08-09 15:44:58 +02002589yang_read_string(const char *input, int size, int indent, int version)
Pavol Vican8760bb72016-04-07 09:44:01 +02002590{
2591 int space, count;
2592 int in_index, out_index;
2593 char *value;
2594 char *retval = NULL;
2595
2596 value = malloc(size + 1);
2597 if (!value) {
2598 LOGMEM;
2599 return NULL;
2600 }
2601 /* replace special character in escape sequence */
2602 in_index = out_index = 0;
2603 while (in_index < size) {
2604 if (input[in_index] == '\\') {
2605 if (input[in_index + 1] == 'n') {
2606 value[out_index] = '\n';
2607 ++in_index;
2608 } else if (input[in_index + 1] == 't') {
2609 value[out_index] = '\t';
2610 ++in_index;
2611 } else if (input[in_index + 1] == '\\') {
2612 value[out_index] = '\\';
2613 ++in_index;
2614 } else if ((in_index + 1) != size && input[in_index + 1] == '"') {
2615 value[out_index] = '"';
2616 ++in_index;
2617 } else {
Pavol Vican677b0132016-08-09 15:44:58 +02002618 if (version < 2) {
2619 value[out_index] = input[in_index];
2620 } else {
2621 /* YANG 1.1 backslash must not be followed by any other character */
2622 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, input);
2623 goto error;
2624 }
Pavol Vican8760bb72016-04-07 09:44:01 +02002625 }
2626 } else {
2627 value[out_index] = input[in_index];
2628 }
2629 ++in_index;
2630 ++out_index;
2631 }
2632 value[out_index] = '\0';
2633 size = out_index;
2634 count = count_substring(value, '\t');
2635
2636 /* extend size of string due to replacing character '\t' with 8 spaces */
2637 retval = malloc(size + 1 + 7 * count);
2638 if (!retval) {
2639 LOGMEM;
2640 goto error;
2641 }
2642 in_index = out_index = space = 0;
2643 while (in_index < size) {
2644 if (value[in_index] == '\n') {
2645 out_index -= space;
2646 space = 0;
2647 retval[out_index] = '\n';
2648 ++out_index;
2649 ++in_index;
2650 in_index = read_indent(value, indent, size, in_index, &out_index, retval);
2651 continue;
2652 } else {
2653 space = (value[in_index] == ' ' || value[in_index] == '\t') ? space + 1 : 0;
2654 retval[out_index] = value[in_index];
2655 ++out_index;
2656 }
2657 ++in_index;
2658 }
2659 retval[out_index] = '\0';
2660 if (out_index != size) {
2661 retval = ly_realloc(retval, out_index + 1);
2662 }
2663 free(value);
2664 return retval;
2665error:
2666 free(value);
2667 return NULL;
2668}