blob: 16c04b028f8ffa06547a60886df9af0340469531 [file] [log] [blame]
Pavol Vican021488a2016-01-25 23:56:12 +01001/**
2 * @file parser_yang.c
3 * @author Pavol Vican
4 * @brief YANG parser for libyang
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Pavol Vican9a3a7212016-03-23 10:04:00 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
Pavol Vican021488a2016-01-25 23:56:12 +010013 */
14
Pavol Vican5de33492016-02-22 14:03:24 +010015#include <ctype.h>
Pavol Vican021488a2016-01-25 23:56:12 +010016#include "parser_yang.h"
Pavol Vican8e7110b2016-03-22 17:00:26 +010017#include "parser_yang_lex.h"
Pavol Vican6eb14e82016-02-03 12:27:13 +010018#include "parser.h"
Pavol Vicanf37eeaa2016-02-09 20:54:06 +010019#include "xpath.h"
Pavol Vican021488a2016-01-25 23:56:12 +010020
Michal Vaskofe7e5a72016-05-02 14:49:23 +020021static int
Pavol Vican0adf01d2016-03-22 12:29:33 +010022yang_check_string(struct lys_module *module, const char **target, char *what, char *where, char *value)
Pavol Vican2a064652016-02-02 22:54:34 +010023{
Pavol Vicanbf805472016-01-26 14:24:56 +010024 if (*target) {
Pavol Vican0adf01d2016-03-22 12:29:33 +010025 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, what, where);
Pavol Vicanbf805472016-01-26 14:24:56 +010026 free(value);
27 return 1;
28 } else {
Pavol Vican2a064652016-02-02 22:54:34 +010029 *target = lydict_insert_zc(module->ctx, value);
Pavol Vicanbf805472016-01-26 14:24:56 +010030 return 0;
31 }
32}
33
Pavol Vican810892e2016-07-12 16:55:34 +020034static int
35yang_check_typedef_identif(struct lys_node *root, struct lys_node *node, char *id)
36{
37 struct lys_node *child, *next;
38 int size;
39 struct lys_tpdf *tpdf;
40
Pavol Vican810892e2016-07-12 16:55:34 +020041 if (root) {
42 node = root;
43 }
44
45 do {
46 LY_TREE_DFS_BEGIN(node, next, child) {
47 if (child->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_GROUPING | LYS_RPC | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
48 switch (child->nodetype) {
49 case LYS_CONTAINER:
50 tpdf = ((struct lys_node_container *)child)->tpdf;
51 size = ((struct lys_node_container *)child)->tpdf_size;
52 break;
53 case LYS_LIST:
54 tpdf = ((struct lys_node_list *)child)->tpdf;
55 size = ((struct lys_node_list *)child)->tpdf_size;
56 break;
57 case LYS_GROUPING:
58 tpdf = ((struct lys_node_grp *)child)->tpdf;
59 size = ((struct lys_node_grp *)child)->tpdf_size;
60 break;
61 case LYS_RPC:
Radek Krejci71d51832016-07-14 15:59:06 +020062 tpdf = ((struct lys_node_rpc_action *)child)->tpdf;
63 size = ((struct lys_node_rpc_action *)child)->tpdf_size;
Pavol Vican810892e2016-07-12 16:55:34 +020064 break;
65 case LYS_INPUT:
66 case LYS_OUTPUT:
Radek Krejci71d51832016-07-14 15:59:06 +020067 tpdf = ((struct lys_node_inout *)child)->tpdf;
68 size = ((struct lys_node_inout *)child)->tpdf_size;
Pavol Vican810892e2016-07-12 16:55:34 +020069 break;
70 case LYS_NOTIF:
71 tpdf = ((struct lys_node_notif *)child)->tpdf;
72 size = ((struct lys_node_notif *)child)->tpdf_size;
73 break;
74 default:
75 size = 0;
76 break;
77 }
Radek Krejcifc824a42016-07-14 15:48:38 +020078 if (size && dup_typedef_check(id, tpdf, size)) {
Pavol Vican810892e2016-07-12 16:55:34 +020079 LOGVAL(LYE_DUPID, LY_VLOG_NONE, NULL, "typedef", id);
80 return EXIT_FAILURE;
81 }
Michal Vasko3767fb22016-07-21 12:10:57 +020082 }
Pavol Vican810892e2016-07-12 16:55:34 +020083 LY_TREE_DFS_END(node, next, child)}
84 } while (root && (node = node->next));
85 return EXIT_SUCCESS;
86}
87
Michal Vaskofe7e5a72016-05-02 14:49:23 +020088int
Pavol Vican5f0316a2016-04-05 21:21:11 +020089yang_read_common(struct lys_module *module, char *value, enum yytokentype type)
Pavol Vican2a064652016-02-02 22:54:34 +010090{
Pavol Vican6eb14e82016-02-03 12:27:13 +010091 int ret = 0;
Pavol Vican021488a2016-01-25 23:56:12 +010092
93 switch (type) {
Pavol Vican2a064652016-02-02 22:54:34 +010094 case MODULE_KEYWORD:
95 module->name = lydict_insert_zc(module->ctx, value);
96 break;
97 case NAMESPACE_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +010098 ret = yang_check_string(module, &module->ns, "namespace", "module", value);
Pavol Vican2a064652016-02-02 22:54:34 +010099 break;
Pavol Vican1ca072c2016-02-03 13:03:56 +0100100 case ORGANIZATION_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100101 ret = yang_check_string(module, &module->org, "organization", "module", value);
Pavol Vican1ca072c2016-02-03 13:03:56 +0100102 break;
103 case CONTACT_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100104 ret = yang_check_string(module, &module->contact, "contact", "module", value);
Pavol Vican1ca072c2016-02-03 13:03:56 +0100105 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200106 default:
107 free(value);
108 LOGINT;
109 ret = EXIT_FAILURE;
110 break;
Pavol Vican2a064652016-02-02 22:54:34 +0100111 }
112
Pavol Vican021488a2016-01-25 23:56:12 +0100113 return ret;
Pavol Vicanbf805472016-01-26 14:24:56 +0100114}
115
Michal Vaskofe7e5a72016-05-02 14:49:23 +0200116int
Pavol Vicand0b64c12016-07-15 09:56:19 +0200117yang_check_version(struct lys_module *module, struct lys_submodule *submodule, char *value, int repeat)
118{
119 int ret = EXIT_SUCCESS;
120
121 if (repeat) {
Michal Vasko3767fb22016-07-21 12:10:57 +0200122 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "yang version", "module");
123 ret = EXIT_FAILURE;
Pavol Vicand0b64c12016-07-15 09:56:19 +0200124 } else {
125 if (!strcmp(value, "1")) {
126 if (submodule) {
127 if (module->version > 1) {
128 LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
129 ret = EXIT_FAILURE;
130 }
131 } else {
132 module->version = 1;
133 }
134 } else if (!strcmp(value, "1.1")) {
135 if (submodule) {
136 if (module->version != 2) {
137 LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
138 ret = EXIT_FAILURE;
139 }
140 } else {
141 module->version = 2;
142 }
143 } else {
144 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "yang-version");
145 ret = EXIT_FAILURE;
Michal Vasko3767fb22016-07-21 12:10:57 +0200146 }
Pavol Vicand0b64c12016-07-15 09:56:19 +0200147 }
148 free(value);
149 return ret;
150}
151
152int
Pavol Vicane024ab72016-07-27 14:27:43 +0200153yang_read_prefix(struct lys_module *module, struct lys_import *imp, char *value)
Pavol Vican2a064652016-02-02 22:54:34 +0100154{
Pavol Vican6eb14e82016-02-03 12:27:13 +0100155 int ret = 0;
Pavol Vicanbf805472016-01-26 14:24:56 +0100156
Pavol Vican0adf01d2016-03-22 12:29:33 +0100157 if (lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) {
Pavol Vican6eb14e82016-02-03 12:27:13 +0100158 free(value);
159 return EXIT_FAILURE;
160 }
Pavol Vicane024ab72016-07-27 14:27:43 +0200161
162 if (imp) {
163 ret = yang_check_string(module, &imp->prefix, "prefix", "import", value);
164 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100165 ret = yang_check_string(module, &module->prefix, "prefix", "module", value);
Pavol Vican2a064652016-02-02 22:54:34 +0100166 }
Pavol Vican6eb14e82016-02-03 12:27:13 +0100167
Pavol Vicanbf805472016-01-26 14:24:56 +0100168 return ret;
169}
Pavol Vican6eb14e82016-02-03 12:27:13 +0100170
Pavol Vican6eb14e82016-02-03 12:27:13 +0100171int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100172yang_fill_import(struct lys_module *module, struct lys_import *imp, char *value)
Pavol Vican6eb14e82016-02-03 12:27:13 +0100173{
Pavol Vican0da132e2016-03-21 12:03:03 +0100174 const char *exp;
Radek Krejci4dcd3392016-06-22 10:28:40 +0200175 int rc;
Pavol Vican6eb14e82016-02-03 12:27:13 +0100176
Pavol Vicane024ab72016-07-27 14:27:43 +0200177 if (!imp->prefix) {
178 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", "import");
179 return EXIT_FAILURE;
180 }
Pavol Vican0da132e2016-03-21 12:03:03 +0100181 exp = lydict_insert_zc(module->ctx, value);
Pavol Vican0adf01d2016-03-22 12:29:33 +0100182 rc = lyp_check_import(module, exp, imp);
Pavol Vican0da132e2016-03-21 12:03:03 +0100183 lydict_remove(module->ctx, exp);
Radek Krejci4dcd3392016-06-22 10:28:40 +0200184 module->imp_size++;
Pavol Vican0da132e2016-03-21 12:03:03 +0100185 if (rc) {
Radek Krejci4dcd3392016-06-22 10:28:40 +0200186 return EXIT_FAILURE;
Pavol Vican6eb14e82016-02-03 12:27:13 +0100187 }
Pavol Vican6eb14e82016-02-03 12:27:13 +0100188
Pavol Vican6eb14e82016-02-03 12:27:13 +0100189 return EXIT_SUCCESS;
Pavol Vican6eb14e82016-02-03 12:27:13 +0100190}
Pavol Vican1ca072c2016-02-03 13:03:56 +0100191
192int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100193yang_read_description(struct lys_module *module, void *node, char *value, char *where)
Pavol Vican1ca072c2016-02-03 13:03:56 +0100194{
195 int ret;
Pavol Vican9bcd7c62016-03-17 19:36:35 +0100196 char *dsc = "description";
Pavol Vican1ca072c2016-02-03 13:03:56 +0100197
198 if (!node) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100199 ret = yang_check_string(module, &module->dsc, dsc, "module", value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100200 } else {
Pavol Vican9bcd7c62016-03-17 19:36:35 +0100201 if (!strcmp("revision", where)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100202 ret = yang_check_string(module, &((struct lys_revision *)node)->dsc, dsc, where, value);
Pavol Vicane024ab72016-07-27 14:27:43 +0200203 } else if (!strcmp("import", where)){
204 ret = yang_check_string(module, &((struct lys_import *)node)->dsc, dsc, where, value);
205 } else if (!strcmp("include", where)){
206 ret = yang_check_string(module, &((struct lys_include *)node)->dsc, dsc, where, value);
Pavol Vican9bcd7c62016-03-17 19:36:35 +0100207 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100208 ret = yang_check_string(module, &((struct lys_node *)node)->dsc, dsc, where, value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100209 }
Pavol Vican1ca072c2016-02-03 13:03:56 +0100210 }
211 return ret;
212}
213
214int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100215yang_read_reference(struct lys_module *module, void *node, char *value, char *where)
Pavol Vican1ca072c2016-02-03 13:03:56 +0100216{
217 int ret;
Pavol Vicanf5fe9662016-03-17 20:00:16 +0100218 char *ref = "reference";
Pavol Vican1ca072c2016-02-03 13:03:56 +0100219
220 if (!node) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100221 ret = yang_check_string(module, &module->ref, "reference", "module", value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100222 } else {
Pavol Vicanf5fe9662016-03-17 20:00:16 +0100223 if (!strcmp("revision", where)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100224 ret = yang_check_string(module, &((struct lys_revision *)node)->ref, ref, where, value);
Pavol Vicane024ab72016-07-27 14:27:43 +0200225 } else if (!strcmp("import", where)){
226 ret = yang_check_string(module, &((struct lys_import *)node)->ref, ref, where, value);
227 } else if (!strcmp("include", where)){
228 ret = yang_check_string(module, &((struct lys_include *)node)->ref, ref, where, value);
Pavol Vicanf5fe9662016-03-17 20:00:16 +0100229 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100230 ret = yang_check_string(module, &((struct lys_node *)node)->ref, ref, where, value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100231 }
Pavol Vican1ca072c2016-02-03 13:03:56 +0100232 }
233 return ret;
234}
Pavol Vicanbedff692016-02-03 14:29:17 +0100235
236void *
237yang_read_revision(struct lys_module *module, char *value)
238{
239 struct lys_revision *retval;
240
Pavol Vican1eeb1992016-02-09 11:10:45 +0100241 retval = &module->rev[module->rev_size];
Pavol Vicanbedff692016-02-03 14:29:17 +0100242
243 /* first member of array is last revision */
Pavol Vican1eeb1992016-02-09 11:10:45 +0100244 if (module->rev_size && strcmp(module->rev[0].date, value) < 0) {
Pavol Vicanbedff692016-02-03 14:29:17 +0100245 memcpy(retval->date, module->rev[0].date, LY_REV_SIZE);
246 memcpy(module->rev[0].date, value, LY_REV_SIZE);
247 retval->dsc = module->rev[0].dsc;
248 retval->ref = module->rev[0].ref;
249 retval = module->rev;
250 retval->dsc = NULL;
251 retval->ref = NULL;
252 } else {
253 memcpy(retval->date, value, LY_REV_SIZE);
254 }
Pavol Vican1eeb1992016-02-09 11:10:45 +0100255 module->rev_size++;
Pavol Vicanbedff692016-02-03 14:29:17 +0100256 free(value);
257 return retval;
258}
Pavol Vican1eeb1992016-02-09 11:10:45 +0100259
260int
Pavol Vicana1827962016-02-29 15:39:42 +0100261yang_add_elem(struct lys_node_array **node, uint32_t *size)
Pavol Vican1eeb1992016-02-09 11:10:45 +0100262{
Pavol Vican45ccc592016-03-09 18:53:48 +0100263 if (!(*size % LY_ARRAY_SIZE)) {
Pavol Vican1eeb1992016-02-09 11:10:45 +0100264 if (!(*node = ly_realloc(*node, (*size + LY_ARRAY_SIZE) * sizeof **node))) {
265 LOGMEM;
266 return EXIT_FAILURE;
267 } else {
Pavol Vican45ccc592016-03-09 18:53:48 +0100268 memset(*node + *size, 0, LY_ARRAY_SIZE * sizeof **node);
Pavol Vican1eeb1992016-02-09 11:10:45 +0100269 }
270 }
271 (*size)++;
272 return EXIT_SUCCESS;
273}
Pavol Vicane1354e92016-02-09 14:02:09 +0100274
275void *
Pavol Vican0adf01d2016-03-22 12:29:33 +0100276yang_read_feature(struct lys_module *module, char *value)
Pavol Vicane1354e92016-02-09 14:02:09 +0100277{
278 struct lys_feature *retval;
279
280 /* check uniqueness of feature's names */
Pavol Vican0adf01d2016-03-22 12:29:33 +0100281 if (lyp_check_identifier(value, LY_IDENT_FEATURE, module, NULL)) {
Pavol Vicane1354e92016-02-09 14:02:09 +0100282 goto error;
283 }
284 retval = &module->features[module->features_size];
285 retval->name = lydict_insert_zc(module->ctx, value);
286 retval->module = module;
287 module->features_size++;
288 return retval;
289
290error:
291 free(value);
292 return NULL;
293}
294
295int
Pavol Vican5f0316a2016-04-05 21:21:11 +0200296yang_read_if_feature(struct lys_module *module, void *ptr, char *value, struct unres_schema *unres, enum yytokentype type)
Pavol Vicane1354e92016-02-09 14:02:09 +0100297{
298 const char *exp;
299 int ret;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100300 struct lys_feature *f;
Pavol Vican4df80542016-08-08 09:37:55 +0200301 struct lys_ident *i;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100302 struct lys_node *n;
Pavol Vican8ff0e2e2016-08-09 09:09:10 +0200303 struct lys_type_enum *e;
304 struct lys_type_bit *b;
Pavol Vicane1354e92016-02-09 14:02:09 +0100305
Michal Vasko97b32be2016-07-25 10:59:53 +0200306 if ((module->version != 2) && ((value[0] == '(') || strchr(value, ' '))) {
307 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
308 free(value);
309 return EXIT_FAILURE;
310 }
311
Pavol Vican0adf01d2016-03-22 12:29:33 +0100312 if (!(exp = transform_schema2json(module, value))) {
Pavol Vicane1354e92016-02-09 14:02:09 +0100313 free(value);
314 return EXIT_FAILURE;
315 }
316 free(value);
317
Pavol Vican8ff0e2e2016-08-09 09:09:10 +0200318 switch (type) {
319 case FEATURE_KEYWORD:
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100320 f = (struct lys_feature *) ptr;
Radek Krejci9ff0a922016-07-14 13:08:05 +0200321 ret = resolve_iffeature_compile(&f->iffeature[f->iffeature_size], exp, (struct lys_node *)f, unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200322 f->iffeature_size++;
Pavol Vican8ff0e2e2016-08-09 09:09:10 +0200323 break;
324 case IDENTITY_KEYWORD:
Pavol Vican4df80542016-08-08 09:37:55 +0200325 i = (struct lys_ident *) ptr;
326 ret = resolve_iffeature_compile(&i->iffeature[i->iffeature_size], exp, (struct lys_node *)i, unres);
327 i->iffeature_size++;
Pavol Vican8ff0e2e2016-08-09 09:09:10 +0200328 break;
329 case ENUM_KEYWORD:
330 e = &((struct yang_type *)ptr)->type->info.enums.enm[((struct yang_type *)ptr)->type->info.enums.count - 1];
331 ret = resolve_iffeature_compile(&e->iffeature[e->iffeature_size], exp, (struct lys_node *)((struct yang_type *)ptr)->type->parent, unres);
332 e->iffeature_size++;
333 break;
334 case BIT_KEYWORD:
335 b = &((struct yang_type *)ptr)->type->info.bits.bit[((struct yang_type *)ptr)->type->info.bits.count - 1];
336 ret = resolve_iffeature_compile(&b->iffeature[b->iffeature_size], exp, (struct lys_node *)((struct yang_type *)ptr)->type->parent, unres);
337 b->iffeature_size++;
338 break;
339 default:
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100340 n = (struct lys_node *) ptr;
Radek Krejci9ff0a922016-07-14 13:08:05 +0200341 ret = resolve_iffeature_compile(&n->iffeature[n->iffeature_size], exp, n, unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200342 n->iffeature_size++;
Pavol Vican8ff0e2e2016-08-09 09:09:10 +0200343 break;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100344 }
Radek Krejci9ff0a922016-07-14 13:08:05 +0200345 lydict_remove(module->ctx, exp);
Pavol Vicane1354e92016-02-09 14:02:09 +0100346
Radek Krejci9ff0a922016-07-14 13:08:05 +0200347 if (ret) {
Pavol Vicane1354e92016-02-09 14:02:09 +0100348 return EXIT_FAILURE;
349 }
350 return EXIT_SUCCESS;
351}
352
Pavol Vican4fb66c92016-03-17 10:32:27 +0100353int
Radek Krejci4372b4e2016-04-14 17:42:16 +0200354yang_check_flags(uint16_t *flags, uint16_t mask, char *what, char *where, uint16_t value, int shortint)
Pavol Vicane1354e92016-02-09 14:02:09 +0100355{
356 if (*flags & mask) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100357 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, what, where);
Pavol Vicane1354e92016-02-09 14:02:09 +0100358 return EXIT_FAILURE;
359 } else {
Radek Krejci4372b4e2016-04-14 17:42:16 +0200360 if (shortint) {
361 *((uint8_t *)flags) |= (uint8_t)value;
362 } else {
363 *flags |= value;
364 }
Pavol Vicane1354e92016-02-09 14:02:09 +0100365 return EXIT_SUCCESS;
366 }
367}
368
Pavol Vicanbbdef532016-02-09 14:52:12 +0100369void *
370yang_read_identity(struct lys_module *module, char *value)
371{
372 struct lys_ident *ret;
373
374 ret = &module->ident[module->ident_size];
375 ret->name = lydict_insert_zc(module->ctx, value);
376 ret->module = module;
Pavol Vicand6cda452016-07-13 15:08:29 +0200377 if (dup_identities_check(ret->name, module)) {
378 lydict_remove(module->ctx, ret->name);
379 return NULL;
380 }
Pavol Vicanbbdef532016-02-09 14:52:12 +0100381 module->ident_size++;
382 return ret;
383}
384
385int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100386yang_read_base(struct lys_module *module, struct lys_ident *ident, char *value, struct unres_schema *unres)
Pavol Vicanbbdef532016-02-09 14:52:12 +0100387{
388 const char *exp;
389
Pavol Vican0adf01d2016-03-22 12:29:33 +0100390 exp = transform_schema2json(module, value);
Pavol Vicanbbdef532016-02-09 14:52:12 +0100391 free(value);
392 if (!exp) {
393 return EXIT_FAILURE;
394 }
Pavol Vican4ffd0ab2016-08-27 16:35:04 +0200395
396 /* temporarily decrement identity_size due to resolve base */
397 module->ident_size--;
Pavol Vican0adf01d2016-03-22 12:29:33 +0100398 if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, exp) == -1) {
Pavol Vicanbbdef532016-02-09 14:52:12 +0100399 lydict_remove(module->ctx, exp);
Pavol Vican4ffd0ab2016-08-27 16:35:04 +0200400 /* undo change identity_size */
401 module->ident_size++;
Pavol Vicanbbdef532016-02-09 14:52:12 +0100402 return EXIT_FAILURE;
403 }
Pavol Vican4ffd0ab2016-08-27 16:35:04 +0200404 /* undo change identity_size */
405 module->ident_size++;
Pavol Vican44dde2c2016-02-10 11:18:14 +0100406
Pavol Vicanbbdef532016-02-09 14:52:12 +0100407 lydict_remove(module->ctx, exp);
408 return EXIT_SUCCESS;
409}
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100410
411void *
Pavol Vican5f0316a2016-04-05 21:21:11 +0200412yang_read_must(struct lys_module *module, struct lys_node *node, char *value, enum yytokentype type)
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100413{
414 struct lys_restr *retval;
415
416 switch (type) {
Pavol Vican8c82fa82016-02-10 13:13:24 +0100417 case CONTAINER_KEYWORD:
Pavol Vican096c6db2016-02-11 15:08:10 +0100418 retval = &((struct lys_node_container *)node)->must[((struct lys_node_container *)node)->must_size++];
Pavol Vican8c82fa82016-02-10 13:13:24 +0100419 break;
Pavol Vicandb7489e2016-08-23 17:23:39 +0200420 case ANYDATA_KEYWORD:
Pavol Vican8c82fa82016-02-10 13:13:24 +0100421 case ANYXML_KEYWORD:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200422 retval = &((struct lys_node_anydata *)node)->must[((struct lys_node_anydata *)node)->must_size++];
Pavol Vican096c6db2016-02-11 15:08:10 +0100423 break;
424 case LEAF_KEYWORD:
425 retval = &((struct lys_node_leaf *)node)->must[((struct lys_node_leaf *)node)->must_size++];
Pavol Vican8c82fa82016-02-10 13:13:24 +0100426 break;
Pavol Vican339d4ad2016-02-12 12:49:22 +0100427 case LEAF_LIST_KEYWORD:
428 retval = &((struct lys_node_leaflist *)node)->must[((struct lys_node_leaflist *)node)->must_size++];
429 break;
Pavol Vican5de33492016-02-22 14:03:24 +0100430 case LIST_KEYWORD:
431 retval = &((struct lys_node_list *)node)->must[((struct lys_node_list *)node)->must_size++];
432 break;
Pavol Vican1003ead2016-03-02 12:24:52 +0100433 case REFINE_KEYWORD:
434 retval = &((struct lys_refine *)node)->must[((struct lys_refine *)node)->must_size++];
435 break;
Pavol Vican85f12022016-03-05 16:30:35 +0100436 case ADD_KEYWORD:
437 retval = &(*((struct type_deviation *)node)->trg_must)[(*((struct type_deviation *)node)->trg_must_size)++];
438 memset(retval, 0, sizeof *retval);
439 break;
Pavol Vicanc1f5a502016-03-06 16:51:26 +0100440 case DELETE_KEYWORD:
441 retval = &((struct type_deviation *)node)->deviate->must[((struct type_deviation *)node)->deviate->must_size++];
442 break;
Pavol Vican9b14b492016-08-09 14:55:10 +0200443 case NOTIFICATION_KEYWORD:
444 retval = &((struct lys_node_notif *)node)->must[((struct lys_node_notif *)node)->must_size++];
445 break;
446 case INPUT_KEYWORD:
447 retval = &((struct lys_node_inout *)node)->must[((struct lys_node_inout *)node)->must_size++];
448 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200449 default:
450 goto error;
451 break;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100452 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100453 retval->expr = transform_schema2json(module, value);
Michal Vasko508a50d2016-09-07 14:50:33 +0200454 if (!retval->expr) {
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100455 goto error;
456 }
457 free(value);
458 return retval;
459
460error:
461 free(value);
462 return NULL;
463}
464
465int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100466yang_read_message(struct lys_module *module,struct lys_restr *save,char *value, char *what, int message)
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100467{
468 int ret;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100469
Pavol Vicandde090a2016-08-30 15:12:14 +0200470 if (message == ERROR_APP_TAG_KEYWORD) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100471 ret = yang_check_string(module, &save->eapptag, "error_app_tag", what, value);
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100472 } else {
Pavol Vicandde090a2016-08-30 15:12:14 +0200473 ret = yang_check_string(module, &save->emsg, "error_message", what, value);
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100474 }
475 return ret;
476}
Pavol Vicanb5687112016-02-09 22:35:59 +0100477
478int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100479yang_read_presence(struct lys_module *module, struct lys_node_container *cont, char *value)
Pavol Vicanb5687112016-02-09 22:35:59 +0100480{
481 if (cont->presence) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100482 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, cont, "presence", "container");
Pavol Vicanb5687112016-02-09 22:35:59 +0100483 free(value);
484 return EXIT_FAILURE;
485 } else {
486 cont->presence = lydict_insert_zc(module->ctx, value);
487 return EXIT_SUCCESS;
488 }
489}
490
Pavol Vican235dbd42016-02-10 10:34:19 +0100491void *
Pavol Vican5f0316a2016-04-05 21:21:11 +0200492yang_read_when(struct lys_module *module, struct lys_node *node, enum yytokentype type, char *value)
Pavol Vican235dbd42016-02-10 10:34:19 +0100493{
494 struct lys_when *retval;
495
496 retval = calloc(1, sizeof *retval);
497 if (!retval) {
498 LOGMEM;
499 free(value);
500 return NULL;
501 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100502 retval->cond = transform_schema2json(module, value);
Michal Vasko508a50d2016-09-07 14:50:33 +0200503 if (!retval->cond) {
Pavol Vican235dbd42016-02-10 10:34:19 +0100504 goto error;
505 }
506 switch (type) {
507 case CONTAINER_KEYWORD:
508 if (((struct lys_node_container *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100509 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "container");
Pavol Vican235dbd42016-02-10 10:34:19 +0100510 goto error;
511 }
512 ((struct lys_node_container *)node)->when = retval;
513 break;
Pavol Vicandb7489e2016-08-23 17:23:39 +0200514 case ANYDATA_KEYWORD:
Pavol Vican1f06ba82016-02-10 17:39:50 +0100515 case ANYXML_KEYWORD:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200516 if (((struct lys_node_anydata *)node)->when) {
Pavol Vicandb7489e2016-08-23 17:23:39 +0200517 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", (type == ANYXML_KEYWORD) ? "anyxml" : "anydata");
Pavol Vican8c82fa82016-02-10 13:13:24 +0100518 goto error;
519 }
Radek Krejcibf2abff2016-08-23 15:51:52 +0200520 ((struct lys_node_anydata *)node)->when = retval;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100521 break;
Pavol Vican1f06ba82016-02-10 17:39:50 +0100522 case CHOICE_KEYWORD:
523 if (((struct lys_node_choice *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100524 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "choice");
Pavol Vican1f06ba82016-02-10 17:39:50 +0100525 goto error;
526 }
527 ((struct lys_node_choice *)node)->when = retval;
528 break;
Pavol Vicanbd098132016-02-11 10:56:49 +0100529 case CASE_KEYWORD:
530 if (((struct lys_node_case *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100531 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "case");
Pavol Vicanbd098132016-02-11 10:56:49 +0100532 goto error;
533 }
534 ((struct lys_node_case *)node)->when = retval;
535 break;
Pavol Vican096c6db2016-02-11 15:08:10 +0100536 case LEAF_KEYWORD:
537 if (((struct lys_node_leaf *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100538 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaf");
Pavol Vican096c6db2016-02-11 15:08:10 +0100539 goto error;
540 }
541 ((struct lys_node_leaf *)node)->when = retval;
542 break;
Pavol Vican339d4ad2016-02-12 12:49:22 +0100543 case LEAF_LIST_KEYWORD:
544 if (((struct lys_node_leaflist *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100545 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaflist");
Pavol Vican339d4ad2016-02-12 12:49:22 +0100546 goto error;
547 }
548 ((struct lys_node_leaflist *)node)->when = retval;
549 break;
Pavol Vican5de33492016-02-22 14:03:24 +0100550 case LIST_KEYWORD:
551 if (((struct lys_node_list *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100552 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "list");
Pavol Vican5de33492016-02-22 14:03:24 +0100553 goto error;
554 }
555 ((struct lys_node_list *)node)->when = retval;
556 break;
Pavol Vican14b18562016-03-01 16:04:29 +0100557 case USES_KEYWORD:
558 if (((struct lys_node_uses *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100559 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "uses");
Pavol Vican14b18562016-03-01 16:04:29 +0100560 goto error;
561 }
562 ((struct lys_node_uses *)node)->when = retval;
563 break;
Pavol Vican92fa0dc2016-03-02 14:20:39 +0100564 case AUGMENT_KEYWORD:
565 if (((struct lys_node_augment *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100566 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "augment");
Pavol Vican92fa0dc2016-03-02 14:20:39 +0100567 goto error;
568 }
569 ((struct lys_node_augment *)node)->when = retval;
570 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200571 default:
572 goto error;
573 break;
Pavol Vican235dbd42016-02-10 10:34:19 +0100574 }
575 free(value);
576 return retval;
577
578error:
579 free(value);
580 lys_when_free(module->ctx, retval);
581 return NULL;
582}
Pavol Vican8c82fa82016-02-10 13:13:24 +0100583
584void *
Pavol Vican7cadfe72016-02-11 12:33:34 +0100585yang_read_node(struct lys_module *module, struct lys_node *parent, char *value, int nodetype, int sizeof_struct)
Pavol Vican8c82fa82016-02-10 13:13:24 +0100586{
Pavol Vican7cadfe72016-02-11 12:33:34 +0100587 struct lys_node *node;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100588
Pavol Vican7cadfe72016-02-11 12:33:34 +0100589 node = calloc(1, sizeof_struct);
590 if (!node) {
Pavol Vicand1dbfda2016-03-21 10:03:58 +0100591 free(value);
Pavol Vican8c82fa82016-02-10 13:13:24 +0100592 LOGMEM;
593 return NULL;
594 }
Pavol Vican531a9132016-03-03 10:10:09 +0100595 if (value) {
596 node->name = lydict_insert_zc(module->ctx, value);
597 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100598 node->module = module;
Pavol Vican7cadfe72016-02-11 12:33:34 +0100599 node->nodetype = nodetype;
600 node->prev = node;
601
602 /* insert the node into the schema tree */
603 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, node)) {
Pavol Vican531a9132016-03-03 10:10:09 +0100604 if (value) {
605 lydict_remove(module->ctx, node->name);
606 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100607 free(node);
Pavol Vican8c82fa82016-02-10 13:13:24 +0100608 return NULL;
609 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100610 return node;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100611}
612
Pavol Vican8c793992016-07-15 10:44:57 +0200613void *
614yang_read_action(struct lys_module *module, struct lys_node *parent, char *value)
615{
616 struct lys_node *node;
617
Michal Vaskobb174852016-07-25 11:00:21 +0200618 if (module->version != 2) {
619 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "action");
620 return NULL;
621 }
622
Pavol Vican8c793992016-07-15 10:44:57 +0200623 for (node = parent; node; node = lys_parent(node)) {
624 if (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)
Pavol Vicanbbe77822016-07-15 12:53:07 +0200625 || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys_size)) {
Pavol Vican8c793992016-07-15 10:44:57 +0200626 LOGVAL(LYE_INPAR, LY_VLOG_NONE, NULL, strnodetype(node->nodetype), "action");
627 return NULL;
628 }
629 }
630 return yang_read_node(module, parent, value, LYS_ACTION, sizeof(struct lys_node_rpc_action));
631}
632
Pavol Vican8c82fa82016-02-10 13:13:24 +0100633int
Pavol Vican5f0316a2016-04-05 21:21:11 +0200634yang_read_default(struct lys_module *module, void *node, char *value, enum yytokentype type)
Pavol Vican096c6db2016-02-11 15:08:10 +0100635{
636 int ret;
637
638 switch (type) {
Pavol Vican339d4ad2016-02-12 12:49:22 +0100639 case LEAF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100640 ret = yang_check_string(module, &((struct lys_node_leaf *) node)->dflt, "default", "leaf", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100641 break;
Pavol Vican0df02b02016-03-01 10:28:50 +0100642 case TYPEDEF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100643 ret = yang_check_string(module, &((struct lys_tpdf *) node)->dflt, "default", "typedef", value);
Pavol Vican0df02b02016-03-01 10:28:50 +0100644 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200645 default:
646 free(value);
647 LOGINT;
648 ret = EXIT_FAILURE;
649 break;
Pavol Vican096c6db2016-02-11 15:08:10 +0100650 }
651 return ret;
652}
653
654int
Pavol Vican5f0316a2016-04-05 21:21:11 +0200655yang_read_units(struct lys_module *module, void *node, char *value, enum yytokentype type)
Pavol Vican096c6db2016-02-11 15:08:10 +0100656{
657 int ret;
658
659 switch (type) {
Pavol Vican339d4ad2016-02-12 12:49:22 +0100660 case LEAF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100661 ret = yang_check_string(module, &((struct lys_node_leaf *) node)->units, "units", "leaf", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100662 break;
663 case LEAF_LIST_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100664 ret = yang_check_string(module, &((struct lys_node_leaflist *) node)->units, "units", "leaflist", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100665 break;
Pavol Vican0df02b02016-03-01 10:28:50 +0100666 case TYPEDEF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100667 ret = yang_check_string(module, &((struct lys_tpdf *) node)->units, "units", "typedef", value);
Pavol Vican0df02b02016-03-01 10:28:50 +0100668 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200669 default:
670 free(value);
671 LOGINT;
672 ret = EXIT_FAILURE;
673 break;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100674 }
675 return ret;
676}
Pavol Vican5de33492016-02-22 14:03:24 +0100677
678int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100679yang_read_key(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres)
Pavol Vican5de33492016-02-22 14:03:24 +0100680{
681 char *exp, *value;
682
683 exp = value = (char *) list->keys;
Pavol Vicanbbe77822016-07-15 12:53:07 +0200684 list->keys_size = 0;
Pavol Vican5de33492016-02-22 14:03:24 +0100685 while ((value = strpbrk(value, " \t\n"))) {
686 list->keys_size++;
687 while (isspace(*value)) {
688 value++;
689 }
690 }
691 list->keys_size++;
692 list->keys = calloc(list->keys_size, sizeof *list->keys);
693 if (!list->keys) {
694 LOGMEM;
695 goto error;
696 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100697 if (unres_schema_add_str(module, unres, list, UNRES_LIST_KEYS, exp) == -1) {
Pavol Vican5de33492016-02-22 14:03:24 +0100698 goto error;
699 }
700 free(exp);
701 return EXIT_SUCCESS;
702
703error:
704 free(exp);
705 return EXIT_FAILURE;
706}
707
708int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100709yang_fill_unique(struct lys_module *module, struct lys_node_list *list, struct lys_unique *unique, char *value, struct unres_schema *unres)
Pavol Vican85f12022016-03-05 16:30:35 +0100710{
711 int i, j;
712 char *vaux;
Radek Krejcid09d1a52016-08-11 14:05:45 +0200713 struct unres_list_uniq *unique_info;
Pavol Vican85f12022016-03-05 16:30:35 +0100714
715 /* count the number of unique leafs in the value */
716 vaux = value;
717 while ((vaux = strpbrk(vaux, " \t\n"))) {
718 unique->expr_size++;
719 while (isspace(*vaux)) {
720 vaux++;
721 }
722 }
723 unique->expr_size++;
724 unique->expr = calloc(unique->expr_size, sizeof *unique->expr);
725 if (!unique->expr) {
726 LOGMEM;
727 goto error;
728 }
729
730 for (i = 0; i < unique->expr_size; i++) {
731 vaux = strpbrk(value, " \t\n");
732 if (!vaux) {
733 /* the last token, lydict_insert() will count its size on its own */
734 vaux = value;
735 }
736
737 /* store token into unique structure */
738 unique->expr[i] = lydict_insert(module->ctx, value, vaux - value);
739
740 /* check that the expression does not repeat */
741 for (j = 0; j < i; j++) {
742 if (ly_strequal(unique->expr[j], unique->expr[i], 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100743 LOGVAL(LYE_INARG, LY_VLOG_LYS, list, unique->expr[i], "unique");
744 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The identifier is not unique");
Pavol Vican85f12022016-03-05 16:30:35 +0100745 goto error;
746 }
747 }
748 /* try to resolve leaf */
749 if (unres) {
Radek Krejcid09d1a52016-08-11 14:05:45 +0200750 unique_info = malloc(sizeof *unique_info);
751 unique_info->list = (struct lys_node *)list;
752 unique_info->expr = unique->expr[i];
753 unique_info->trg_type = &unique->trg_type;
754 if (unres_schema_add_node(module, unres, unique_info, UNRES_LIST_UNIQ, NULL) == -1) {
Pavol Vican18b10212016-04-11 15:41:52 +0200755 goto error;
756 }
Pavol Vican85f12022016-03-05 16:30:35 +0100757 } else {
Radek Krejcid09d1a52016-08-11 14:05:45 +0200758 if (resolve_unique((struct lys_node *)list, unique->expr[i], &unique->trg_type)) {
Pavol Vican85f12022016-03-05 16:30:35 +0100759 goto error;
760 }
761 }
762
763 /* move to next token */
764 value = vaux;
765 while(isspace(*value)) {
766 value++;
767 }
768 }
769
770 return EXIT_SUCCESS;
771
772error:
773 return EXIT_FAILURE;
774}
775
776int
Pavol Vican5de33492016-02-22 14:03:24 +0100777yang_read_unique(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres)
778{
779 uint8_t k;
Pavol Vican0adf01d2016-03-22 12:29:33 +0100780 char *str;
Pavol Vican5de33492016-02-22 14:03:24 +0100781
782 for(k=0; k<list->unique_size; k++) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100783 str = (char *)list->unique[k].expr;
784 if (yang_fill_unique(module, list, &list->unique[k], str, unres)) {
Pavol Vican5de33492016-02-22 14:03:24 +0100785 goto error;
786 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100787 free(str);
Pavol Vican5de33492016-02-22 14:03:24 +0100788 }
789 return EXIT_SUCCESS;
790
791error:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100792 free(str);
Pavol Vican5de33492016-02-22 14:03:24 +0100793 return EXIT_FAILURE;
Pavol Vican73e7c992016-02-24 12:18:05 +0100794}
795
Pavol Vican07f220f2016-09-02 13:04:37 +0200796int
Pavol Vican81344ac2016-09-02 14:23:06 +0200797yang_read_leafref_path(struct lys_module *module, struct yang_type *stype, char *value)
798{
799 if (stype->base && (stype->base != LY_TYPE_LEAFREF)) {
800 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance");
801 goto error;
802 }
803 if (stype->type->info.lref.path) {
804 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "path", "type");
805 goto error;
806 }
807 stype->type->info.lref.path = lydict_insert_zc(module->ctx, value);
808 stype->base = LY_TYPE_LEAFREF;
809 return EXIT_SUCCESS;
810
811error:
812 free(value);
813 return EXIT_FAILURE;
814}
815
816int
817yang_read_require_instance(struct yang_type *stype, int req)
818{
819 if (stype->base && (stype->base != LY_TYPE_LEAFREF)) {
820 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance");
821 return EXIT_FAILURE;
822 }
823 if (stype->type->info.lref.req) {
824 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "require-instance", "type");
825 return EXIT_FAILURE;
826 }
827 stype->type->info.lref.req = req;
828 stype->base = LY_TYPE_LEAFREF;
829 return EXIT_SUCCESS;
830}
831
832int
Pavol Vican07f220f2016-09-02 13:04:37 +0200833yang_read_identyref(struct lys_module *module, struct yang_type *stype, char *expr, struct unres_schema *unres)
Pavol Vican1ff0e222016-02-26 12:27:01 +0100834{
Pavol Vican07f220f2016-09-02 13:04:37 +0200835 const char *value;
836 int rc;
Pavol Vican1ff0e222016-02-26 12:27:01 +0100837
Pavol Vican07f220f2016-09-02 13:04:37 +0200838 if (stype->base && stype->base != LY_TYPE_IDENT) {
839 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "base");
840 return EXIT_FAILURE;
Pavol Vican1ff0e222016-02-26 12:27:01 +0100841 }
Pavol Vican07f220f2016-09-02 13:04:37 +0200842
843 stype->base = LY_TYPE_IDENT;
844 /* store in the JSON format */
845 value = transform_schema2json(module, expr);
846 free(expr);
847
848 if (!value) {
849 return EXIT_FAILURE;
850 }
851 rc = unres_schema_add_str(module, unres, stype->type, UNRES_TYPE_IDENTREF, value);
Pavol Vican1ff0e222016-02-26 12:27:01 +0100852 lydict_remove(module->ctx, value);
853
854 if (rc == -1) {
Pavol Vican07f220f2016-09-02 13:04:37 +0200855 return EXIT_FAILURE;
Pavol Vican1ff0e222016-02-26 12:27:01 +0100856 }
857
Pavol Vican07f220f2016-09-02 13:04:37 +0200858 return EXIT_SUCCESS;
Pavol Vican1ff0e222016-02-26 12:27:01 +0100859}
860
Pavol Vican73e7c992016-02-24 12:18:05 +0100861int
Radek Krejci3a5501d2016-07-18 22:03:34 +0200862yang_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 +0100863{
Pavol Vican81344ac2016-09-02 14:23:06 +0200864 int i, j, rc, ret = -1;
865 int8_t req;
Pavol Vican73e7c992016-02-24 12:18:05 +0100866 const char *name, *value;
Pavol Vican8bd72e42016-08-29 09:53:05 +0200867 LY_DATA_TYPE base = 0;
Radek Krejci3a5501d2016-07-18 22:03:34 +0200868 struct lys_node *siter;
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200869 struct lys_type *dertype;
870 struct lys_type_enum *enms_sc = NULL;
871 struct lys_type_bit *bits_sc = NULL;
872 struct lys_type_bit bit_tmp;
873
Pavol Vican0adf01d2016-03-22 12:29:33 +0100874 value = transform_schema2json(module, typ->name);
Pavol Vican73e7c992016-02-24 12:18:05 +0100875 if (!value) {
876 goto error;
877 }
878
879 i = parse_identifier(value);
880 if (i < 1) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100881 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]);
Pavol Vican73e7c992016-02-24 12:18:05 +0100882 lydict_remove(module->ctx, value);
883 goto error;
884 }
Radek Krejci408ab2f2016-06-06 15:27:10 +0200885 /* module name */
Pavol Vican73e7c992016-02-24 12:18:05 +0100886 name = value;
887 if (value[i]) {
888 typ->type->module_name = lydict_insert(module->ctx, value, i);
889 name += i;
890 if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100891 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name);
Pavol Vican73e7c992016-02-24 12:18:05 +0100892 lydict_remove(module->ctx, value);
893 goto error;
894 }
895 ++name;
896 }
897
898 rc = resolve_superior_type(name, typ->type->module_name, module, parent, &typ->type->der);
Pavol Vican73e7c992016-02-24 12:18:05 +0100899 if (rc == -1) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100900 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, typ->type->module_name);
Radek Krejci408ab2f2016-06-06 15:27:10 +0200901 lydict_remove(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +0100902 goto error;
903
Michal Vasko01c6fd22016-05-20 11:43:05 +0200904 /* the type could not be resolved or it was resolved to an unresolved typedef or leafref */
Pavol Vican73e7c992016-02-24 12:18:05 +0100905 } else if (rc == EXIT_FAILURE) {
Michal Vasko01c6fd22016-05-20 11:43:05 +0200906 LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name);
Radek Krejci408ab2f2016-06-06 15:27:10 +0200907 lydict_remove(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +0100908 ret = EXIT_FAILURE;
909 goto error;
910 }
Radek Krejci408ab2f2016-06-06 15:27:10 +0200911 lydict_remove(module->ctx, value);
Radek Krejcic13db382016-08-16 10:52:42 +0200912
913 if (typ->type->base == LY_TYPE_INGRP) {
914 /* resolved type in grouping, decrease the grouping's nacm number to indicate that one less
915 * unresolved item left inside the grouping */
916 for (siter = parent; siter && (siter->nodetype != LYS_GROUPING); siter = lys_parent(siter));
917 if (siter) {
918 if (!((struct lys_node_grp *)siter)->nacm) {
919 LOGINT;
920 goto error;
921 }
922 ((struct lys_node_grp *)siter)->nacm--;
923 } else {
924 LOGINT;
925 goto error;
926 }
927 }
Pavol Vicanfdab9f92016-09-07 15:23:27 +0200928
929 /* check status */
930 if (lyp_check_status(typ->type->parent->flags, typ->type->parent->module, typ->type->parent->name,
931 typ->type->der->flags, typ->type->der->module, typ->type->der->name, parent)) {
932 goto error;
933 }
934
Pavol Vican8bd72e42016-08-29 09:53:05 +0200935 base = typ->base;
Pavol Vican73e7c992016-02-24 12:18:05 +0100936 typ->type->base = typ->type->der->type.base;
937 if (base == 0) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100938 base = typ->type->der->type.base;
Pavol Vican73e7c992016-02-24 12:18:05 +0100939 }
940 switch (base) {
941 case LY_TYPE_STRING:
942 if (typ->type->base == LY_TYPE_BINARY) {
943 if (typ->type->info.str.pat_count) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100944 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Binary type could not include pattern statement.");
Pavol Vican73e7c992016-02-24 12:18:05 +0100945 goto error;
946 }
947 typ->type->info.binary.length = typ->type->info.str.length;
Pavol Vican07ea68d2016-02-25 12:01:37 +0100948 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 +0100949 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.binary.length->expr, "length");
Pavol Vican73e7c992016-02-24 12:18:05 +0100950 goto error;
951 }
952 } else if (typ->type->base == LY_TYPE_STRING) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100953 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 +0100954 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.str.length->expr, "length");
Pavol Vican73e7c992016-02-24 12:18:05 +0100955 goto error;
956 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100957 } else {
Pavol Vican933aa5a2016-04-09 21:05:46 +0200958 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicanaff5c802016-02-24 15:56:45 +0100959 goto error;
Pavol Vican73e7c992016-02-24 12:18:05 +0100960 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100961 break;
962 case LY_TYPE_DEC64:
963 if (typ->type->base == LY_TYPE_DEC64) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100964 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 +0100965 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.dec64.range->expr, "range");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100966 goto error;
967 }
Pavol Vican07ea68d2016-02-25 12:01:37 +0100968 /* mandatory sub-statement(s) check */
969 if (!typ->type->info.dec64.dig && !typ->type->der->type.der) {
970 /* decimal64 type directly derived from built-in type requires fraction-digits */
Pavol Vican0adf01d2016-03-22 12:29:33 +0100971 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type");
Pavol Vican07ea68d2016-02-25 12:01:37 +0100972 goto error;
973 }
974 if (typ->type->info.dec64.dig && typ->type->der->type.der) {
975 /* type is not directly derived from buit-in type and fraction-digits statement is prohibited */
Pavol Vican0adf01d2016-03-22 12:29:33 +0100976 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits");
Pavol Vican07ea68d2016-02-25 12:01:37 +0100977 goto error;
978 }
Radek Krejcib51d5932016-09-08 14:02:52 +0200979
980 /* copy fraction-digits specification from parent type for easier internal use */
981 if (typ->type->der->type.der) {
982 typ->type->info.dec64.dig = typ->type->der->type.info.dec64.dig;
983 typ->type->info.dec64.div = typ->type->der->type.info.dec64.div;
984 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100985 } else if (typ->type->base >= LY_TYPE_INT8 && typ->type->base <=LY_TYPE_UINT64) {
986 if (typ->type->info.dec64.dig) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100987 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Numerical type could not include fraction statement.");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100988 goto error;
989 }
990 typ->type->info.num.range = typ->type->info.dec64.range;
Pavol Vican07ea68d2016-02-25 12:01:37 +0100991 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 +0100992 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, typ->type->info.num.range->expr, "range");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100993 goto error;
994 }
995 } else {
Pavol Vican933aa5a2016-04-09 21:05:46 +0200996 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicanaff5c802016-02-24 15:56:45 +0100997 goto error;
998 }
999 break;
Pavol Vican79a763d2016-02-25 15:41:27 +01001000 case LY_TYPE_ENUM:
1001 if (typ->type->base != LY_TYPE_ENUM) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001002 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001003 goto error;
1004 }
Pavol Vican8ad2e0d2016-08-09 12:56:19 +02001005 dertype = &typ->type->der->type;
1006
1007 if (!dertype->der) {
1008 if (!typ->type->info.enums.count) {
1009 /* type is derived directly from buit-in enumeartion type and enum statement is required */
1010 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type");
1011 goto error;
1012 }
1013 } else {
1014 for (; !dertype->info.enums.count; dertype = &dertype->der->type);
1015 if (module->version < 2 && typ->type->info.enums.count) {
1016 /* type is not directly derived from built-in enumeration type and enum statement is prohibited
1017 * in YANG 1.0, since YANG 1.1 enum statements can be used to restrict the base enumeration type */
1018 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "enum");
1019 goto error;
1020 }
1021
1022 /* restricted enumeration type - the name MUST be used in the base type */
1023 enms_sc = dertype->info.enums.enm;
1024 for(i = 0; i < typ->type->info.enums.count; i++) {
1025 for (j = 0; j < dertype->info.enums.count; j++) {
1026 if (ly_strequal(enms_sc[j].name, typ->type->info.enums.enm[i].name, 1)) {
1027 break;
1028 }
1029 }
1030 if (j == dertype->info.enums.count) {
1031 LOGVAL(LYE_ENUM_INNAME, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].name);
1032 goto error;
1033 }
1034
1035 if (typ->type->info.enums.enm[i].flags & LYS_AUTOASSIGNED) {
1036 /* automatically assign value from base type */
1037 typ->type->info.enums.enm[i].value = enms_sc[j].value;
1038 } else {
1039 /* check that the assigned value corresponds to the original
1040 * value of the enum in the base type */
1041 if (typ->type->info.enums.enm[i].value != enms_sc[j].value) {
1042 /* typ->type->info.enums.enm[i].value - assigned value in restricted enum
1043 * enms_sc[j].value - value assigned to the corresponding enum (detected above) in base type */
1044 LOGVAL(LYE_ENUM_INVAL, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].value,
1045 typ->type->info.enums.enm[i].name, enms_sc[j].value);
1046 goto error;
1047 }
1048 }
1049 }
Pavol Vican79a763d2016-02-25 15:41:27 +01001050 }
Pavol Vican1ff0e222016-02-26 12:27:01 +01001051 break;
Pavol Vican03a59442016-03-21 15:23:45 +01001052 case LY_TYPE_BITS:
1053 if (typ->type->base != LY_TYPE_BITS) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001054 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vican03a59442016-03-21 15:23:45 +01001055 goto error;
1056 }
Pavol Vican8ad2e0d2016-08-09 12:56:19 +02001057 dertype = &typ->type->der->type;
1058
1059 if (!dertype->der) {
1060 if (!typ->type->info.bits.count) {
1061 /* type is derived directly from buit-in bits type and bit statement is required */
1062 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type");
1063 goto error;
1064 }
1065 } else {
1066 for (; !dertype->info.enums.count; dertype = &dertype->der->type);
1067 if (module->version < 2 && typ->type->info.bits.count) {
1068 /* type is not directly derived from buit-in bits type and bit statement is prohibited,
1069 * since YANG 1.1 the bit statements can be used to restrict the base bits type */
1070 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "bit");
1071 goto error;
1072 }
1073
1074 bits_sc = dertype->info.bits.bit;
1075 for (i = 0; i < typ->type->info.bits.count; i++) {
1076 for (j = 0; j < dertype->info.bits.count; j++) {
1077 if (ly_strequal(bits_sc[j].name, typ->type->info.bits.bit[i].name, 1)) {
1078 break;
1079 }
1080 }
1081 if (j == dertype->info.bits.count) {
1082 LOGVAL(LYE_BITS_INNAME, LY_VLOG_NONE, NULL, typ->type->info.bits.bit[i].name);
1083 goto error;
1084 }
1085
1086 /* restricted bits type */
1087 if (typ->type->info.bits.bit[i].flags & LYS_AUTOASSIGNED) {
1088 /* automatically assign position from base type */
1089 typ->type->info.bits.bit[i].pos = bits_sc[j].pos;
1090 } else {
1091 /* check that the assigned position corresponds to the original
1092 * position of the bit in the base type */
1093 if (typ->type->info.bits.bit[i].pos != bits_sc[j].pos) {
1094 /* typ->type->info.bits.bit[i].pos - assigned position in restricted bits
1095 * bits_sc[j].pos - position assigned to the corresponding bit (detected above) in base type */
1096 LOGVAL(LYE_BITS_INVAL, LY_VLOG_NONE, NULL, typ->type->info.bits.bit[i].pos,
1097 typ->type->info.bits.bit[i].name, bits_sc[j].pos);
1098 goto error;
1099 }
1100 }
1101 }
Pavol Vican03a59442016-03-21 15:23:45 +01001102 }
Pavol Vican8ad2e0d2016-08-09 12:56:19 +02001103
1104 for (i = typ->type->info.bits.count - 1; i > 0; i--) {
1105 j = i;
1106
1107 /* keep them ordered by position */
1108 while (j && typ->type->info.bits.bit[j - 1].pos > typ->type->info.bits.bit[j].pos) {
1109 /* switch them */
1110 memcpy(&bit_tmp, &typ->type->info.bits.bit[j], sizeof bit_tmp);
1111 memcpy(&typ->type->info.bits.bit[j], &typ->type->info.bits.bit[j - 1], sizeof bit_tmp);
1112 memcpy(&typ->type->info.bits.bit[j - 1], &bit_tmp, sizeof bit_tmp);
1113 j--;
1114 }
Pavol Vican03a59442016-03-21 15:23:45 +01001115 }
1116 break;
Pavol Vican1ff0e222016-02-26 12:27:01 +01001117 case LY_TYPE_LEAFREF:
Pavol Vican81344ac2016-09-02 14:23:06 +02001118 if (typ->type->base == LY_TYPE_INST) {
1119 if (typ->type->info.lref.path) {
1120 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "path");
1121 goto error;
1122 }
1123 if ((req = typ->type->info.lref.req)) {
1124 typ->type->info.inst.req = req;
1125 }
1126 } else if (typ->type->base == LY_TYPE_LEAFREF) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001127 /* flag resolving for later use */
1128 if (!tpdftype) {
1129 for (siter = parent; siter && siter->nodetype != LYS_GROUPING; siter = lys_parent(siter));
1130 if (siter) {
1131 /* just a flag - do not resolve */
1132 tpdftype = 1;
1133 }
1134 }
1135
Pavol Vican6b072512016-04-04 10:50:21 +02001136 if (typ->type->info.lref.path) {
Pavol Vican894ee0f2016-08-30 15:29:46 +02001137 if (typ->type->der->type.der) {
1138 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "path");
1139 goto error;
1140 }
Pavol Vican191613a2016-02-26 16:21:32 +01001141 value = typ->type->info.lref.path;
1142 /* store in the JSON format */
Pavol Vican0adf01d2016-03-22 12:29:33 +01001143 typ->type->info.lref.path = transform_schema2json(module, value);
Pavol Vican191613a2016-02-26 16:21:32 +01001144 lydict_remove(module->ctx, value);
1145 if (!typ->type->info.lref.path) {
1146 goto error;
1147 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001148 /* try to resolve leafref path only when this is instantiated
1149 * leaf, so it is not:
1150 * - typedef's type,
1151 * - in grouping definition,
1152 * - just instantiated in a grouping definition,
1153 * because in those cases the nodes referenced in path might not be present
1154 * and it is not a bug. */
1155 if (!tpdftype && unres_schema_add_node(module, unres, typ->type, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vican191613a2016-02-26 16:21:32 +01001156 goto error;
1157 }
Pavol Vican6b072512016-04-04 10:50:21 +02001158 } else if (!typ->type->der->type.der) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001159 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type");
Pavol Vican191613a2016-02-26 16:21:32 +01001160 goto error;
Michal Vasko01c6fd22016-05-20 11:43:05 +02001161 } else {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001162 /* copy leafref definition into the derived type */
1163 typ->type->info.lref.path = lydict_insert(module->ctx, typ->type->der->type.info.lref.path, 0);
1164 /* and resolve the path at the place we are (if not in grouping/typedef) */
1165 if (!tpdftype && unres_schema_add_node(module, unres, typ->type, UNRES_TYPE_LEAFREF, parent) == -1) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02001166 goto error;
1167 }
Radek Krejci742be352016-07-17 12:18:54 +02001168
Michal Vasko01c6fd22016-05-20 11:43:05 +02001169 /* add pointer to leafref target, only on leaves (not in typedefs) */
Radek Krejci3a5501d2016-07-18 22:03:34 +02001170 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 +02001171 goto error;
1172 }
Pavol Vican191613a2016-02-26 16:21:32 +01001173 }
Pavol Vican1ff0e222016-02-26 12:27:01 +01001174 } else {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001175 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vican1ff0e222016-02-26 12:27:01 +01001176 goto error;
1177 }
1178 break;
1179 case LY_TYPE_IDENT:
Pavol Vican07f220f2016-09-02 13:04:37 +02001180 if (typ->type->base != LY_TYPE_IDENT) {
1181 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vican1ff0e222016-02-26 12:27:01 +01001182 goto error;
1183 }
Pavol Vican07f220f2016-09-02 13:04:37 +02001184 if (typ->type->der->type.der) {
1185 if (typ->type->info.ident.ref) {
1186 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "base");
1187 goto error;
1188 }
1189 } else {
1190 if (!typ->type->info.ident.ref) {
1191 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type");
1192 goto error;
1193 }
1194 }
Pavol Vican1ff0e222016-02-26 12:27:01 +01001195 break;
Pavol Vicana4f045a2016-02-29 15:01:20 +01001196 case LY_TYPE_UNION:
1197 if (typ->type->base != LY_TYPE_UNION) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001198 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicana4f045a2016-02-29 15:01:20 +01001199 goto error;
1200 }
1201 if (!typ->type->info.uni.types) {
1202 if (typ->type->der->type.der) {
1203 /* this is just a derived type with no additional type specified/required */
1204 break;
1205 }
Pavol Vican0adf01d2016-03-22 12:29:33 +01001206 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type");
Pavol Vicana4f045a2016-02-29 15:01:20 +01001207 goto error;
1208 }
1209 for (i = 0; i < typ->type->info.uni.count; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001210 if (unres_schema_add_node(module, unres, &typ->type->info.uni.types[i],
Michal Vasko5d631402016-07-21 13:15:15 +02001211 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Pavol Vicana4f045a2016-02-29 15:01:20 +01001212 goto error;
1213 }
Pavol Vican8e00e7a2016-09-20 11:07:48 +02001214 if (module->version < 2) {
1215 if (typ->type->info.uni.types[i].base == LY_TYPE_EMPTY) {
1216 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", typ->name);
1217 goto error;
1218 } else if (typ->type->info.uni.types[i].base == LY_TYPE_LEAFREF) {
1219 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", typ->name);
1220 goto error;
1221 }
Pavol Vicana4f045a2016-02-29 15:01:20 +01001222 }
1223 }
1224 break;
Pavol Vicana1827962016-02-29 15:39:42 +01001225
1226 default:
1227 if (base >= LY_TYPE_BINARY && base <= LY_TYPE_UINT64) {
1228 if (typ->type->base != base) {
Pavol Vican933aa5a2016-04-09 21:05:46 +02001229 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", typ->type->parent->name);
Pavol Vicana1827962016-02-29 15:39:42 +01001230 goto error;
1231 }
1232 } else {
1233 LOGINT;
1234 goto error;
1235 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001236 }
1237 return EXIT_SUCCESS;
1238
1239error:
1240 if (typ->type->module_name) {
1241 lydict_remove(module->ctx, typ->type->module_name);
1242 typ->type->module_name = NULL;
1243 }
Pavol Vican8bd72e42016-08-29 09:53:05 +02001244 if (base) {
1245 typ->type->base = base;
1246 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001247 return ret;
1248}
1249
1250void *
Pavol Vican5f0316a2016-04-05 21:21:11 +02001251yang_read_type(struct lys_module *module, void *parent, char *value, enum yytokentype type)
Pavol Vican73e7c992016-02-24 12:18:05 +01001252{
1253 struct yang_type *typ;
Pavol Vican4766aca2016-03-07 12:42:36 +01001254 struct type_deviation *dev;
1255 struct lys_tpdf *tmp_parent;
Pavol Vican73e7c992016-02-24 12:18:05 +01001256
Pavol Vicand01d8ae2016-03-01 10:45:59 +01001257 typ = calloc(1, sizeof *typ);
1258 if (!typ) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001259 LOGMEM;
1260 return NULL;
1261 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001262
1263 typ->flags = LY_YANG_STRUCTURE_FLAG;
1264 switch (type) {
1265 case LEAF_KEYWORD:
1266 ((struct lys_node_leaf *)parent)->type.der = (struct lys_tpdf *)typ;
1267 ((struct lys_node_leaf *)parent)->type.parent = (struct lys_tpdf *)parent;
1268 typ->type = &((struct lys_node_leaf *)parent)->type;
1269 break;
Pavol Vicana55992a2016-03-01 13:37:17 +01001270 case LEAF_LIST_KEYWORD:
1271 ((struct lys_node_leaflist *)parent)->type.der = (struct lys_tpdf *)typ;
1272 ((struct lys_node_leaflist *)parent)->type.parent = (struct lys_tpdf *)parent;
1273 typ->type = &((struct lys_node_leaflist *)parent)->type;
1274 break;
Pavol Vicana4f045a2016-02-29 15:01:20 +01001275 case UNION_KEYWORD:
1276 ((struct lys_type *)parent)->der = (struct lys_tpdf *)typ;
1277 typ->type = (struct lys_type *)parent;
1278 break;
Pavol Vican0df02b02016-03-01 10:28:50 +01001279 case TYPEDEF_KEYWORD:
1280 ((struct lys_tpdf *)parent)->type.der = (struct lys_tpdf *)typ;
1281 typ->type = &((struct lys_tpdf *)parent)->type;
Pavol Vican4766aca2016-03-07 12:42:36 +01001282 break;
1283 case REPLACE_KEYWORD:
1284 /* deviation replace type*/
1285 dev = (struct type_deviation *)parent;
1286 if (dev->deviate->type) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001287 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "deviation");
Pavol Vican4766aca2016-03-07 12:42:36 +01001288 goto error;
1289 }
1290 /* check target node type */
1291 if (dev->target->nodetype == LYS_LEAF) {
1292 typ->type = &((struct lys_node_leaf *)dev->target)->type;
1293 } else if (dev->target->nodetype == LYS_LEAFLIST) {
1294 typ->type = &((struct lys_node_leaflist *)dev->target)->type;
1295 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001296 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "type");
1297 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"type\" property.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001298 goto error;
1299 }
1300
1301 /* remove type and initialize it */
1302 lys_type_free(module->ctx, typ->type);
1303 tmp_parent = typ->type->parent;
1304 memset(typ->type, 0, sizeof *typ->type);
1305 typ->type->parent = tmp_parent;
1306
1307 /* replace it with the value specified in deviation */
1308 /* HACK for unres */
1309 typ->type->der = (struct lys_tpdf *)typ;
1310 dev->deviate->type = typ->type;
1311 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +02001312 default:
1313 goto error;
1314 break;
Pavol Vican73e7c992016-02-24 12:18:05 +01001315 }
Pavol Vican5f0316a2016-04-05 21:21:11 +02001316 typ->name = lydict_insert_zc(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +01001317 return typ;
Pavol Vican4766aca2016-03-07 12:42:36 +01001318
1319error:
Pavol Vican5f0316a2016-04-05 21:21:11 +02001320 free(value);
Pavol Vican4766aca2016-03-07 12:42:36 +01001321 free(typ);
1322 return NULL;
Pavol Vican73e7c992016-02-24 12:18:05 +01001323}
1324
Pavol Vicanbe9e3832016-04-28 02:21:37 +02001325void
1326yang_delete_type(struct lys_module *module, struct yang_type *stype)
1327{
1328 int i;
1329
1330 stype->type->base = stype->base;
1331 stype->type->der = NULL;
1332 lydict_remove(module->ctx, stype->name);
1333 if (stype->base == LY_TYPE_UNION) {
1334 for (i = 0; i < stype->type->info.uni.count; i++) {
1335 if (stype->type->info.uni.types[i].der) {
1336 yang_delete_type(module, (struct yang_type *)stype->type->info.uni.types[i].der);
1337 }
1338 }
1339 }
1340 free(stype);
1341}
1342
Pavol Vican73e7c992016-02-24 12:18:05 +01001343void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001344yang_read_length(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vican73e7c992016-02-24 12:18:05 +01001345{
1346 struct lys_restr **length;
1347
Pavol Vican6b072512016-04-04 10:50:21 +02001348 if (typ->base == 0 || typ->base == LY_TYPE_STRING) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001349 length = &typ->type->info.str.length;
Pavol Vican6b072512016-04-04 10:50:21 +02001350 typ->base = LY_TYPE_STRING;
1351 } else if (typ->base == LY_TYPE_BINARY) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001352 length = &typ->type->info.binary.length;
1353 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001354 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected length statement.");
Pavol Vican73e7c992016-02-24 12:18:05 +01001355 goto error;
1356 }
1357
1358 if (*length) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001359 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "length", "type");
Pavol Vicanbe9e3832016-04-28 02:21:37 +02001360 goto error;
Pavol Vican73e7c992016-02-24 12:18:05 +01001361 }
1362 *length = calloc(1, sizeof **length);
1363 if (!*length) {
1364 LOGMEM;
1365 goto error;
1366 }
1367 (*length)->expr = lydict_insert_zc(module->ctx, value);
1368 return *length;
1369
1370error:
1371 free(value);
1372 return NULL;
1373
1374}
Pavol Vican1c203db2016-02-24 14:05:23 +01001375
Pavol Vican6eecf302016-08-10 11:09:05 +02001376int
1377yang_read_pattern(struct lys_module *module, struct lys_restr *pattern, char *value, char modifier)
Pavol Vican1c203db2016-02-24 14:05:23 +01001378{
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001379 char *buf;
1380 size_t len;
1381
Michal Vasko0aee5c12016-06-17 14:27:26 +02001382 if (lyp_check_pattern(value, NULL)) {
Pavol Vican1c203db2016-02-24 14:05:23 +01001383 free(value);
Pavol Vican6eecf302016-08-10 11:09:05 +02001384 return EXIT_FAILURE;
Pavol Vican1c203db2016-02-24 14:05:23 +01001385 }
Pavol Vican1c203db2016-02-24 14:05:23 +01001386
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001387 len = strlen(value);
1388 buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */
Pavol Vican6eecf302016-08-10 11:09:05 +02001389
1390 if (!buf) {
1391 LOGMEM;
1392 free(value);
1393 return EXIT_FAILURE;
1394 }
1395
1396 buf[0] = modifier;
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001397 strcpy(&buf[1], value);
1398 free(value);
1399
Pavol Vican6eecf302016-08-10 11:09:05 +02001400 pattern->expr = lydict_insert_zc(module->ctx, buf);
1401 return EXIT_SUCCESS;
Pavol Vican1c203db2016-02-24 14:05:23 +01001402}
Pavol Vicanaff5c802016-02-24 15:56:45 +01001403
1404void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001405yang_read_range(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vicanaff5c802016-02-24 15:56:45 +01001406{
Pavol Vican6b072512016-04-04 10:50:21 +02001407 if (typ->base != 0 && typ->base != LY_TYPE_DEC64) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001408 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected range statement.");
Pavol Vicanaff5c802016-02-24 15:56:45 +01001409 goto error;
1410 }
Pavol Vican6b072512016-04-04 10:50:21 +02001411 typ->base = LY_TYPE_DEC64;
Pavol Vicanaff5c802016-02-24 15:56:45 +01001412 if (typ->type->info.dec64.range) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001413 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "range", "type");
Pavol Vicanaff5c802016-02-24 15:56:45 +01001414 goto error;
1415 }
1416 typ->type->info.dec64.range = calloc(1, sizeof *typ->type->info.dec64.range);
1417 if (!typ->type->info.dec64.range) {
1418 LOGMEM;
1419 goto error;
1420 }
1421 typ->type->info.dec64.range->expr = lydict_insert_zc(module->ctx, value);
1422 return typ->type->info.dec64.range;
1423
1424error:
1425 free(value);
1426 return NULL;
1427}
Pavol Vican07ea68d2016-02-25 12:01:37 +01001428
1429int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001430yang_read_fraction(struct yang_type *typ, uint32_t value)
Pavol Vican07ea68d2016-02-25 12:01:37 +01001431{
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001432 unsigned int i;
1433
Pavol Vican6b072512016-04-04 10:50:21 +02001434 if (typ->base == 0 || typ->base == LY_TYPE_DEC64) {
1435 typ->base = LY_TYPE_DEC64;
Pavol Vican07ea68d2016-02-25 12:01:37 +01001436 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001437 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected fraction-digits statement.");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001438 goto error;
1439 }
1440 if (typ->type->info.dec64.dig) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001441 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "fraction-digits", "type");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001442 goto error;
1443 }
1444 /* range check */
1445 if (value < 1 || value > 18) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001446 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", value, "fraction-digits");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001447 goto error;
1448 }
1449 typ->type->info.dec64.dig = value;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001450 typ->type->info.dec64.div = 10;
1451 for (i = 1; i < value; i++) {
1452 typ->type->info.dec64.div *= 10;
1453 }
Pavol Vican07ea68d2016-02-25 12:01:37 +01001454 return EXIT_SUCCESS;
1455
1456error:
1457 return EXIT_FAILURE;
1458}
Pavol Vican79a763d2016-02-25 15:41:27 +01001459
1460void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001461yang_read_enum(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vican79a763d2016-02-25 15:41:27 +01001462{
1463 struct lys_type_enum *enm;
1464 int i;
1465
Pavol Vicanc6662412016-08-30 08:06:28 +02001466 if (!value[0]) {
1467 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum name");
1468 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Enum name must not be empty.");
1469 free(value);
1470 goto error;
1471 }
1472
Pavol Vican79a763d2016-02-25 15:41:27 +01001473 enm = &typ->type->info.enums.enm[typ->type->info.enums.count];
1474 enm->name = lydict_insert_zc(module->ctx, value);
1475
1476 /* the assigned name MUST NOT have any leading or trailing whitespace characters */
1477 if (isspace(enm->name[0]) || isspace(enm->name[strlen(enm->name) - 1])) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001478 LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, enm->name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001479 goto error;
1480 }
1481
1482 /* check the name uniqueness */
1483 for (i = 0; i < typ->type->info.enums.count; i++) {
1484 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 +01001485 LOGVAL(LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001486 goto error;
1487 }
1488 }
1489
1490 typ->type->info.enums.count++;
1491 return enm;
1492
1493error:
1494 typ->type->info.enums.count++;
1495 return NULL;
1496}
1497
1498int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001499yang_check_enum(struct yang_type *typ, struct lys_type_enum *enm, int64_t *value, int assign)
Pavol Vican79a763d2016-02-25 15:41:27 +01001500{
1501 int i, j;
1502
1503 if (!assign) {
1504 /* assign value automatically */
1505 if (*value > INT32_MAX) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001506 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value");
Pavol Vican79a763d2016-02-25 15:41:27 +01001507 goto error;
1508 }
1509 enm->value = *value;
1510 enm->flags |= LYS_AUTOASSIGNED;
1511 (*value)++;
Pavol Vican5de389c2016-08-30 08:55:15 +02001512 } else if (typ->type->info.enums.enm == enm) {
1513 /* change value, which is assigned automatically, if first enum has value. */
1514 *value = typ->type->info.enums.enm[0].value;
1515 (*value)++;
Pavol Vican79a763d2016-02-25 15:41:27 +01001516 }
1517
1518 /* check that the value is unique */
1519 j = typ->type->info.enums.count-1;
1520 for (i = 0; i < j; i++) {
1521 if (typ->type->info.enums.enm[i].value == typ->type->info.enums.enm[j].value) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001522 LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL,
Radek Krejcie663e012016-08-01 17:12:34 +02001523 typ->type->info.enums.enm[j].value, typ->type->info.enums.enm[j].name,
1524 typ->type->info.enums.enm[i].name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001525 goto error;
1526 }
1527 }
1528
1529 return EXIT_SUCCESS;
1530
1531error:
1532 return EXIT_FAILURE;
1533}
Pavol Vican9887c682016-02-29 11:32:01 +01001534
1535void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001536yang_read_bit(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vican9887c682016-02-29 11:32:01 +01001537{
1538 int i;
1539 struct lys_type_bit *bit;
1540
1541 bit = &typ->type->info.bits.bit[typ->type->info.bits.count];
Pavol Vican0adf01d2016-03-22 12:29:33 +01001542 if (lyp_check_identifier(value, LY_IDENT_SIMPLE, NULL, NULL)) {
Pavol Vican9887c682016-02-29 11:32:01 +01001543 free(value);
1544 goto error;
1545 }
1546 bit->name = lydict_insert_zc(module->ctx, value);
1547
1548 /* check the name uniqueness */
1549 for (i = 0; i < typ->type->info.bits.count; i++) {
1550 if (!strcmp(typ->type->info.bits.bit[i].name, bit->name)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001551 LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, bit->name);
Pavol Vican9887c682016-02-29 11:32:01 +01001552 typ->type->info.bits.count++;
1553 goto error;
1554 }
1555 }
1556 typ->type->info.bits.count++;
1557 return bit;
1558
1559error:
1560 return NULL;
1561}
1562
1563int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001564yang_check_bit(struct yang_type *typ, struct lys_type_bit *bit, int64_t *value, int assign)
Pavol Vican9887c682016-02-29 11:32:01 +01001565{
1566 int i,j;
Pavol Vican9887c682016-02-29 11:32:01 +01001567
1568 if (!assign) {
1569 /* assign value automatically */
1570 if (*value > UINT32_MAX) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001571 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position");
Pavol Vican9887c682016-02-29 11:32:01 +01001572 goto error;
1573 }
1574 bit->pos = (uint32_t)*value;
1575 bit->flags |= LYS_AUTOASSIGNED;
1576 (*value)++;
1577 }
1578
1579 j = typ->type->info.bits.count - 1;
1580 /* check that the value is unique */
1581 for (i = 0; i < j; i++) {
1582 if (typ->type->info.bits.bit[i].pos == bit->pos) {
Radek Krejcie663e012016-08-01 17:12:34 +02001583 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 +01001584 goto error;
1585 }
1586 }
1587
Pavol Vican9887c682016-02-29 11:32:01 +01001588 return EXIT_SUCCESS;
1589
1590error:
1591 return EXIT_FAILURE;
1592}
Pavol Vican0df02b02016-03-01 10:28:50 +01001593
1594void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001595yang_read_typedef(struct lys_module *module, struct lys_node *parent, char *value)
Pavol Vican0df02b02016-03-01 10:28:50 +01001596{
1597 struct lys_tpdf *ret;
Pavol Vican810892e2016-07-12 16:55:34 +02001598 struct lys_node *root;
Pavol Vican0df02b02016-03-01 10:28:50 +01001599
Pavol Vican810892e2016-07-12 16:55:34 +02001600 root = (parent) ? NULL : lys_main_module(module)->data;
1601 if (lyp_check_identifier(value, LY_IDENT_TYPE, module, parent) || yang_check_typedef_identif(root, parent, value)) {
Pavol Vican0df02b02016-03-01 10:28:50 +01001602 free(value);
1603 return NULL;
1604 }
Pavol Vican810892e2016-07-12 16:55:34 +02001605
Pavol Vican0df02b02016-03-01 10:28:50 +01001606 if (!parent) {
1607 ret = &module->tpdf[module->tpdf_size];
Pavol Vican0df02b02016-03-01 10:28:50 +01001608 module->tpdf_size++;
Pavol Vican21238f52016-03-01 12:39:52 +01001609 } else {
1610 switch (parent->nodetype) {
1611 case LYS_GROUPING:
1612 ret = &((struct lys_node_grp *)parent)->tpdf[((struct lys_node_grp *)parent)->tpdf_size];
Pavol Vican21238f52016-03-01 12:39:52 +01001613 ((struct lys_node_grp *)parent)->tpdf_size++;
1614 break;
Pavol Vican535d50e2016-03-01 13:05:33 +01001615 case LYS_CONTAINER:
1616 ret = &((struct lys_node_container *)parent)->tpdf[((struct lys_node_container *)parent)->tpdf_size];
1617 ((struct lys_node_container *)parent)->tpdf_size++;
1618 break;
Pavol Vican09f04b82016-03-01 14:02:28 +01001619 case LYS_LIST:
1620 ret = &((struct lys_node_list *)parent)->tpdf[((struct lys_node_list *)parent)->tpdf_size];
1621 ((struct lys_node_list *)parent)->tpdf_size++;
1622 break;
Pavol Vican52ed67d2016-03-02 17:46:15 +01001623 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02001624 case LYS_ACTION:
1625 ret = &((struct lys_node_rpc_action *)parent)->tpdf[((struct lys_node_rpc_action *)parent)->tpdf_size];
1626 ((struct lys_node_rpc_action *)parent)->tpdf_size++;
Pavol Vican52ed67d2016-03-02 17:46:15 +01001627 break;
Pavol Vican531a9132016-03-03 10:10:09 +01001628 case LYS_INPUT:
1629 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02001630 ret = &((struct lys_node_inout *)parent)->tpdf[((struct lys_node_inout *)parent)->tpdf_size];
1631 ((struct lys_node_inout *)parent)->tpdf_size++;
Pavol Vican531a9132016-03-03 10:10:09 +01001632 break;
Pavol Vican41267fd2016-03-03 10:47:42 +01001633 case LYS_NOTIF:
1634 ret = &((struct lys_node_notif *)parent)->tpdf[((struct lys_node_notif *)parent)->tpdf_size];
1635 ((struct lys_node_notif *)parent)->tpdf_size++;
1636 break;
Pavol Vicand1dbfda2016-03-21 10:03:58 +01001637 default:
1638 /* another type of nodetype is error*/
1639 LOGINT;
1640 free(value);
1641 return NULL;
Pavol Vican21238f52016-03-01 12:39:52 +01001642 }
Pavol Vican0df02b02016-03-01 10:28:50 +01001643 }
1644
Pavol Vican79436472016-04-04 11:22:02 +02001645 ret->type.parent = ret;
Pavol Vican0df02b02016-03-01 10:28:50 +01001646 ret->name = lydict_insert_zc(module->ctx, value);
1647 ret->module = module;
1648 return ret;
1649}
Pavol Vican1003ead2016-03-02 12:24:52 +01001650
1651void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001652yang_read_refine(struct lys_module *module, struct lys_node_uses *uses, char *value)
Pavol Vican1003ead2016-03-02 12:24:52 +01001653{
1654 struct lys_refine *rfn;
1655
1656 rfn = &uses->refine[uses->refine_size];
1657 uses->refine_size++;
Pavol Vican0adf01d2016-03-22 12:29:33 +01001658 rfn->target_name = transform_schema2json(module, value);
Pavol Vican1003ead2016-03-02 12:24:52 +01001659 free(value);
1660 if (!rfn->target_name) {
1661 return NULL;
1662 }
1663 return rfn;
1664}
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001665
1666void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001667yang_read_augment(struct lys_module *module, struct lys_node *parent, char *value)
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001668{
1669 struct lys_node_augment *aug;
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001670
1671 if (parent) {
1672 aug = &((struct lys_node_uses *)parent)->augment[((struct lys_node_uses *)parent)->augment_size];
1673 } else {
1674 aug = &module->augment[module->augment_size];
1675 }
1676 aug->nodetype = LYS_AUGMENT;
Pavol Vican0adf01d2016-03-22 12:29:33 +01001677 aug->target_name = transform_schema2json(module, value);
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001678 free(value);
1679 if (!aug->target_name) {
1680 return NULL;
1681 }
1682 aug->parent = parent;
1683 aug->module = module;
1684 if (parent) {
1685 ((struct lys_node_uses *)parent)->augment_size++;
1686 } else {
1687 module->augment_size++;
1688 }
1689 return aug;
1690}
Pavol Vican220e5a12016-03-03 14:19:43 +01001691
1692void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001693yang_read_deviation(struct lys_module *module, char *value)
Pavol Vican220e5a12016-03-03 14:19:43 +01001694{
Radek Krejci27fe55e2016-09-13 17:13:35 +02001695 struct lys_node *dev_target = NULL, *parent;
Pavol Vican220e5a12016-03-03 14:19:43 +01001696 struct lys_deviation *dev;
1697 struct type_deviation *deviation = NULL;
Radek Krejci27fe55e2016-09-13 17:13:35 +02001698 struct lys_module *mod;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001699 int rc;
Pavol Vican220e5a12016-03-03 14:19:43 +01001700
Pavol Vican220e5a12016-03-03 14:19:43 +01001701 dev = &module->deviation[module->deviation_size];
Pavol Vican0adf01d2016-03-22 12:29:33 +01001702 dev->target_name = transform_schema2json(module, value);
Pavol Vican220e5a12016-03-03 14:19:43 +01001703 free(value);
1704 if (!dev->target_name) {
1705 goto error;
1706 }
1707
Pavol Vican974377b2016-03-23 00:38:53 +01001708 deviation = calloc(1, sizeof *deviation);
1709 if (!deviation) {
1710 LOGMEM;
1711 goto error;
1712 }
1713
Pavol Vican220e5a12016-03-03 14:19:43 +01001714 /* resolve target node */
1715 rc = resolve_augment_schema_nodeid(dev->target_name, NULL, module, (const struct lys_node **)&dev_target);
1716 if (rc || !dev_target) {
Michal Vasko75c8daf2016-05-19 10:56:39 +02001717 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation");
Pavol Vican220e5a12016-03-03 14:19:43 +01001718 goto error;
1719 }
Radek Krejcic4283442016-04-22 09:19:27 +02001720 if (dev_target->module == lys_main_module(module)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001721 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation");
1722 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Deviating own module is not allowed.");
Pavol Vican220e5a12016-03-03 14:19:43 +01001723 goto error;
1724 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001725
Radek Krejci27fe55e2016-09-13 17:13:35 +02001726 /* mark all the affected modules as deviated and implemented */
1727 for(parent = dev_target; parent; parent = lys_parent(parent)) {
1728 mod = lys_node_module(parent);
1729 if (module != mod) {
1730 mod->deviated = 1;
1731 lys_set_implemented(mod);
1732 }
1733 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001734
Pavol Vican220e5a12016-03-03 14:19:43 +01001735 /*save pointer to the deviation and deviated target*/
1736 deviation->deviation = dev;
1737 deviation->target = dev_target;
1738
Pavol Vican38321d02016-08-16 14:56:02 +02001739 deviation->dflt_check = ly_set_new();
1740 if (!deviation->dflt_check) {
1741 LOGMEM;
1742 goto error;
1743 }
1744
Pavol Vican220e5a12016-03-03 14:19:43 +01001745 return deviation;
1746
1747error:
1748 free(deviation);
1749 lydict_remove(module->ctx, dev->target_name);
1750 return NULL;
1751}
Pavol Vican4c90c642016-03-03 15:06:47 +01001752
1753int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001754yang_read_deviate_unsupported(struct type_deviation *dev)
Pavol Vican4c90c642016-03-03 15:06:47 +01001755{
1756 int i;
1757
1758 if (dev->deviation->deviate_size) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001759 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
Pavol Vican4c90c642016-03-03 15:06:47 +01001760 return EXIT_FAILURE;
1761 }
1762 dev->deviation->deviate[dev->deviation->deviate_size].mod = LY_DEVIATE_NO;
1763
1764 /* you cannot remove a key leaf */
1765 if ((dev->target->nodetype == LYS_LEAF) && dev->target->parent && (dev->target->parent->nodetype == LYS_LIST)) {
1766 for (i = 0; i < ((struct lys_node_list *)dev->target->parent)->keys_size; ++i) {
1767 if (((struct lys_node_list *)dev->target->parent)->keys[i] == (struct lys_node_leaf *)dev->target) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001768 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "not-supported", "deviation");
1769 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot remove a list key.");
Pavol Vican4c90c642016-03-03 15:06:47 +01001770 return EXIT_FAILURE;
1771 }
1772 }
1773 }
Michal Vaskofe7e5a72016-05-02 14:49:23 +02001774
Pavol Vican4c90c642016-03-03 15:06:47 +01001775 /* unlink and store the original node */
Michal Vaskod921d682016-05-19 10:56:51 +02001776 lys_node_unlink(dev->target);
Pavol Vican4c90c642016-03-03 15:06:47 +01001777 dev->deviation->orig_node = dev->target;
1778
1779 dev->deviation->deviate_size = 1;
1780 return EXIT_SUCCESS;
1781}
Pavol Vican85f12022016-03-05 16:30:35 +01001782
1783int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001784yang_read_deviate(struct type_deviation *dev, LYS_DEVIATE_TYPE mod)
Pavol Vican85f12022016-03-05 16:30:35 +01001785{
1786 struct unres_schema tmp_unres;
1787
1788 dev->deviation->deviate[dev->deviation->deviate_size].mod = mod;
1789 dev->deviate = &dev->deviation->deviate[dev->deviation->deviate_size];
1790 dev->deviation->deviate_size++;
1791 if (dev->deviation->deviate[0].mod == LY_DEVIATE_NO) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001792 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "not-supported");
1793 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
Pavol Vican85f12022016-03-05 16:30:35 +01001794 return EXIT_FAILURE;
1795 }
1796
1797 /* store a shallow copy of the original node */
1798 if (!dev->deviation->orig_node) {
1799 memset(&tmp_unres, 0, sizeof tmp_unres);
1800 dev->deviation->orig_node = lys_node_dup(dev->target->module, NULL, dev->target, 0, 0, &tmp_unres, 1);
1801 /* just to be safe */
1802 if (tmp_unres.count) {
1803 LOGINT;
1804 return EXIT_FAILURE;
1805 }
1806 }
1807
1808 return EXIT_SUCCESS;
1809}
1810
1811int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001812yang_read_deviate_units(struct ly_ctx *ctx, struct type_deviation *dev, char *value)
Pavol Vican85f12022016-03-05 16:30:35 +01001813{
1814 const char **stritem;
1815
1816 if (dev->deviate->units) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001817 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "units", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001818 free(value);
1819 goto error;
1820 }
1821
1822 /* check target node type */
1823 if (dev->target->nodetype == LYS_LEAFLIST) {
1824 stritem = &((struct lys_node_leaflist *)dev->target)->units;
1825 } else if (dev->target->nodetype == LYS_LEAF) {
1826 stritem = &((struct lys_node_leaf *)dev->target)->units;
1827 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001828 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1829 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"units\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001830 free(value);
1831 goto error;
1832 }
1833
1834 dev->deviate->units = lydict_insert_zc(ctx, value);
1835
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001836 if (dev->deviate->mod == LY_DEVIATE_DEL) {
1837 /* check values */
Michal Vaskob42b6972016-06-06 14:21:30 +02001838 if (!ly_strequal(*stritem, dev->deviate->units, 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001839 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->units, "units");
1840 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001841 goto error;
1842 }
1843 /* remove current units value of the target */
1844 lydict_remove(ctx, *stritem);
Pavol Vican4766aca2016-03-07 12:42:36 +01001845 } else {
1846 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1847 /* check that there is no current value */
1848 if (*stritem) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001849 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1850 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001851 goto error;
1852 }
1853 } else { /* replace */
1854 if (!*stritem) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001855 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1856 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001857 goto error;
1858 }
1859 }
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001860 /* remove current units value of the target ... */
1861 lydict_remove(ctx, *stritem);
1862
1863 /* ... and replace it with the value specified in deviation */
1864 *stritem = lydict_insert(ctx, dev->deviate->units, 0);
1865 }
1866
Pavol Vican85f12022016-03-05 16:30:35 +01001867 return EXIT_SUCCESS;
1868
1869error:
1870 return EXIT_FAILURE;
1871}
1872
1873int
Pavol Vican974377b2016-03-23 00:38:53 +01001874yang_read_deviate_must(struct type_deviation *dev, uint8_t c_must)
Pavol Vican85f12022016-03-05 16:30:35 +01001875{
Pavol Vican85f12022016-03-05 16:30:35 +01001876 /* check target node type */
1877 switch (dev->target->nodetype) {
1878 case LYS_LEAF:
1879 dev->trg_must = &((struct lys_node_leaf *)dev->target)->must;
1880 dev->trg_must_size = &((struct lys_node_leaf *)dev->target)->must_size;
1881 break;
1882 case LYS_CONTAINER:
1883 dev->trg_must = &((struct lys_node_container *)dev->target)->must;
1884 dev->trg_must_size = &((struct lys_node_container *)dev->target)->must_size;
1885 break;
1886 case LYS_LEAFLIST:
1887 dev->trg_must = &((struct lys_node_leaflist *)dev->target)->must;
1888 dev->trg_must_size = &((struct lys_node_leaflist *)dev->target)->must_size;
1889 break;
1890 case LYS_LIST:
1891 dev->trg_must = &((struct lys_node_list *)dev->target)->must;
1892 dev->trg_must_size = &((struct lys_node_list *)dev->target)->must_size;
1893 break;
1894 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001895 case LYS_ANYDATA:
1896 dev->trg_must = &((struct lys_node_anydata *)dev->target)->must;
1897 dev->trg_must_size = &((struct lys_node_anydata *)dev->target)->must_size;
Pavol Vican85f12022016-03-05 16:30:35 +01001898 break;
1899 default:
Pavol Vican0adf01d2016-03-22 12:29:33 +01001900 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must");
1901 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001902 goto error;
1903 }
1904
Michal Vasko508a50d2016-09-07 14:50:33 +02001905 /* flag will be checked again, clear it for now */
1906 dev->target->flags &= ~LYS_XPATH_DEP;
1907
Pavol Vican85f12022016-03-05 16:30:35 +01001908 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1909 /* reallocate the must array of the target */
1910 dev->deviate->must = ly_realloc(*dev->trg_must, (c_must + *dev->trg_must_size) * sizeof *dev->deviate->must);
1911 if (!dev->deviate->must) {
1912 LOGMEM;
1913 goto error;
1914 }
1915 *dev->trg_must = dev->deviate->must;
1916 dev->deviate->must = &((*dev->trg_must)[*dev->trg_must_size]);
1917 dev->deviate->must_size = c_must;
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001918 } else {
1919 /* LY_DEVIATE_DEL */
1920 dev->deviate->must = calloc(c_must, sizeof *dev->deviate->must);
1921 if (!dev->deviate->must) {
1922 LOGMEM;
1923 goto error;
1924 }
Pavol Vican85f12022016-03-05 16:30:35 +01001925 }
1926
1927 return EXIT_SUCCESS;
1928
1929error:
1930 return EXIT_FAILURE;
1931}
1932
1933int
Pavol Vican974377b2016-03-23 00:38:53 +01001934yang_read_deviate_unique(struct type_deviation *dev, uint8_t c_uniq)
Pavol Vican85f12022016-03-05 16:30:35 +01001935{
Pavol Vican85f12022016-03-05 16:30:35 +01001936 struct lys_node_list *list;
1937
1938 /* check target node type */
1939 if (dev->target->nodetype != LYS_LIST) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001940 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique");
1941 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"unique\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001942 goto error;
1943 }
1944
1945 list = (struct lys_node_list *)dev->target;
1946 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1947 /* reallocate the unique array of the target */
1948 dev->deviate->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *dev->deviate->unique);
1949 if (!dev->deviate->unique) {
1950 LOGMEM;
1951 goto error;
1952 }
1953 list->unique = dev->deviate->unique;
1954 dev->deviate->unique = &list->unique[list->unique_size];
1955 dev->deviate->unique_size = c_uniq;
1956 memset(dev->deviate->unique, 0, c_uniq * sizeof *dev->deviate->unique);
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001957 } else {
1958 /* LY_DEVIATE_DEL */
1959 dev->deviate->unique = calloc(c_uniq, sizeof *dev->deviate->unique);
1960 if (!dev->deviate->unique) {
1961 LOGMEM;
1962 goto error;
1963 }
Pavol Vican85f12022016-03-05 16:30:35 +01001964 }
1965
1966 return EXIT_SUCCESS;
1967
1968error:
1969 return EXIT_FAILURE;
1970}
1971
1972int
Pavol Vican38321d02016-08-16 14:56:02 +02001973yang_read_deviate_default(struct lys_module *module, struct type_deviation *dev, uint8_t c_dflt)
Pavol Vican85f12022016-03-05 16:30:35 +01001974{
Pavol Vican38321d02016-08-16 14:56:02 +02001975 int i;
1976 struct lys_node_leaflist *llist;
Pavol Vican85f12022016-03-05 16:30:35 +01001977
Pavol Vican38321d02016-08-16 14:56:02 +02001978 /* check target node type */
1979 if (module->version < 2 && dev->target->nodetype == LYS_LEAFLIST) {
1980 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1981 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
1982 goto error;
1983 } else if (c_dflt > 1 && dev->target->nodetype != LYS_LEAFLIST) { /* from YANG 1.1 */
1984 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1985 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow multiple \"default\" properties.");
1986 goto error;
1987 } else if (!(dev->target->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
1988 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1989 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001990 goto error;
1991 }
1992
Pavol Vican38321d02016-08-16 14:56:02 +02001993 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1994 /* check that there is no current value */
1995 if ((dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) ||
1996 (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt)) {
1997 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1998 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican85f12022016-03-05 16:30:35 +01001999 goto error;
2000 }
Pavol Vican4766aca2016-03-07 12:42:36 +01002001
Pavol Vican38321d02016-08-16 14:56:02 +02002002 /* check collision with mandatory/min-elements */
2003 if ((dev->target->flags & LYS_MAND_TRUE) ||
2004 (dev->target->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)dev->target)->min)) {
2005 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "deviation");
2006 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
2007 "Adding the \"default\" statement is forbidden on %s statement.",
2008 (dev->target->flags & LYS_MAND_TRUE) ? "nodes with the \"mandatory\"" : "leaflists with non-zero \"min-elements\"");
2009 goto error;
Pavol Vican85f12022016-03-05 16:30:35 +01002010 }
Pavol Vican38321d02016-08-16 14:56:02 +02002011 } else if (dev->deviate->mod == LY_DEVIATE_RPL) {
2012 /* check that there was a value before */
2013 if (((dev->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && !((struct lys_node_leaf *)dev->target)->dflt) ||
2014 (dev->target->nodetype == LYS_CHOICE && !((struct lys_node_choice *)dev->target)->dflt)) {
2015 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
2016 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
2017 goto error;
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002018 }
Pavol Vican38321d02016-08-16 14:56:02 +02002019 }
2020
2021 if (dev->target->nodetype == LYS_LEAFLIST) {
2022 /* reallocate default list in the target */
2023 llist = (struct lys_node_leaflist *)dev->target;
2024 if (dev->deviate->mod == LY_DEVIATE_ADD) {
2025 /* reallocate (enlarge) the unique array of the target */
2026 llist->dflt = ly_realloc(llist->dflt, (c_dflt + llist->dflt_size) * sizeof *dev->deviate->dflt);
2027 } else if (dev->deviate->mod == LY_DEVIATE_RPL) {
2028 /* reallocate (replace) the unique array of the target */
2029 for (i = 0; i < llist->dflt_size; i++) {
2030 lydict_remove(llist->module->ctx, llist->dflt[i]);
2031 }
2032 llist->dflt = ly_realloc(llist->dflt, c_dflt * sizeof *dev->deviate->dflt);
2033 llist->dflt_size = 0;
2034 }
2035 }
2036
2037 dev->deviate->dflt = calloc(c_dflt, sizeof *dev->deviate->dflt);
2038 if (!dev->deviate->dflt) {
2039 LOGMEM;
Pavol Vican85f12022016-03-05 16:30:35 +01002040 goto error;
2041 }
2042
2043 return EXIT_SUCCESS;
2044
2045error:
2046 return EXIT_FAILURE;
2047}
2048
2049int
Pavol Vican38321d02016-08-16 14:56:02 +02002050yang_fill_deviate_default(struct ly_ctx *ctx, struct type_deviation *dev, char *exp)
2051{
2052 struct lys_node *node;
2053 struct lys_node_choice *choice;
2054 struct lys_node_leaf *leaf;
2055 struct lys_node_leaflist *llist;
2056 int rc, i;
2057 unsigned int u;
2058 const char *value;
2059
2060 value = lydict_insert_zc(ctx, exp);
2061 u = strlen(value);
2062 dev->deviate->dflt[dev->deviate->dflt_size++] = value;
2063
2064 if (dev->target->nodetype == LYS_CHOICE) {
2065 choice = (struct lys_node_choice *)dev->target;
2066 rc = resolve_choice_default_schema_nodeid(value, choice->child, (const struct lys_node **)&node);
2067 if (rc || !node) {
2068 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2069 goto error;
2070 }
2071 if (dev->deviate->mod == LY_DEVIATE_DEL) {
2072 if (!choice->dflt || (choice->dflt != node)) {
2073 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2074 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
2075 goto error;
2076 }
2077 } else { /* add or replace */
2078 choice->dflt = node;
2079 if (!choice->dflt) {
2080 /* default branch not found */
2081 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2082 goto error;
2083 }
2084 }
2085 } else if (dev->target->nodetype == LYS_LEAF) {
2086 leaf = (struct lys_node_leaf *)dev->target;
2087 if (dev->deviate->mod == LY_DEVIATE_DEL) {
2088 if (!leaf->dflt || !ly_strequal(leaf->dflt, value, 1)) {
2089 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2090 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
2091 goto error;
2092 }
2093 /* remove value */
2094 lydict_remove(ctx, leaf->dflt);
2095 leaf->dflt = NULL;
2096 } else { /* add (already checked) and replace */
2097 /* remove value */
2098 lydict_remove(ctx, leaf->dflt);
2099
2100 /* set new value */
2101 leaf->dflt = lydict_insert(ctx, value, u);
2102
2103 /* remember to check it later (it may not fit now, but the type can be deviated too) */
2104 ly_set_add(dev->dflt_check, dev->target, 0);
2105 }
2106 } else { /* LYS_LEAFLIST */
2107 llist = (struct lys_node_leaflist *)dev->target;
2108 if (dev->deviate->mod == LY_DEVIATE_DEL) {
2109 /* find and remove the value in target list */
2110 for (i = 0; i < llist->dflt_size; i++) {
2111 if (llist->dflt[i] && ly_strequal(llist->dflt[i], value, 1)) {
2112 /* match, remove the value */
2113 lydict_remove(llist->module->ctx, llist->dflt[i]);
2114 llist->dflt[i] = NULL;
2115 break;
2116 }
2117 }
2118 if (i == llist->dflt_size) {
2119 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2120 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The default value to delete not found in the target node.");
2121 goto error;
2122 }
2123 } else {
2124 /* add or replace, anyway we place items into the deviate's list
2125 which propagates to the target */
2126 /* we just want to check that the value isn't already in the list */
2127 for (i = 0; i < llist->dflt_size; i++) {
2128 if (ly_strequal(llist->dflt[i], value, 1)) {
2129 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2130 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
2131 goto error;
2132 }
2133 }
2134 /* store it in target node */
2135 llist->dflt[llist->dflt_size++] = lydict_insert(ctx, value, u);
2136
2137 /* remember to check it later (it may not fit now, but the type can be deviated too) */
2138 ly_set_add(dev->dflt_check, dev->target, 0);
2139 }
2140 }
2141
2142 return EXIT_SUCCESS;
2143error:
2144 return EXIT_FAILURE;
2145}
2146
2147
2148int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002149yang_read_deviate_config(struct type_deviation *dev, uint8_t value)
Pavol Vican85f12022016-03-05 16:30:35 +01002150{
2151 if (dev->deviate->flags & LYS_CONFIG_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002152 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "config", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01002153 goto error;
2154 }
2155
2156 /* for we deviate from RFC 6020 and allow config property even it is/is not
2157 * specified in the target explicitly since config property inherits. So we expect
2158 * that config is specified in every node. But for delete, we check that the value
2159 * is the same as here in deviation
2160 */
2161 dev->deviate->flags |= value;
2162
2163 /* add and replace are the same in this case */
2164 /* remove current config value of the target ... */
2165 dev->target->flags &= ~LYS_CONFIG_MASK;
2166
2167 /* ... and replace it with the value specified in deviation */
2168 dev->target->flags |= dev->deviate->flags & LYS_CONFIG_MASK;
2169
2170 return EXIT_SUCCESS;
2171
2172error:
2173 return EXIT_FAILURE;
2174}
2175
2176int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002177yang_read_deviate_mandatory(struct type_deviation *dev, uint8_t value)
Pavol Vican85f12022016-03-05 16:30:35 +01002178{
Radek Krejcie00d2312016-08-12 15:27:49 +02002179 struct lys_node *parent;
2180
Pavol Vican85f12022016-03-05 16:30:35 +01002181 if (dev->deviate->flags & LYS_MAND_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002182 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "mandatory", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01002183 goto error;
2184 }
2185
2186 /* check target node type */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002187 if (!(dev->target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002188 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
2189 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"mandatory\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01002190 goto error;
2191 }
2192
2193 dev->deviate->flags |= value;
2194
2195 if (dev->deviate->mod == LY_DEVIATE_ADD) {
2196 /* check that there is no current value */
2197 if (dev->target->flags & LYS_MAND_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002198 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
2199 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican85f12022016-03-05 16:30:35 +01002200 goto error;
Pavol Vican321a02e2016-04-05 16:11:59 +02002201 } else {
2202 if (dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) {
2203 /* RFC 6020, 7.6.4 - default statement must not with mandatory true */
2204 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "leaf");
2205 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\".");
2206 goto error;
2207 } else if (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt) {
2208 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "choice");
2209 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\".");
2210 goto error;
2211 }
Pavol Vican85f12022016-03-05 16:30:35 +01002212 }
Pavol Vican4766aca2016-03-07 12:42:36 +01002213 } else { /* replace */
2214 if (!(dev->target->flags & LYS_MAND_MASK)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002215 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
2216 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01002217 goto error;
2218 }
Pavol Vican85f12022016-03-05 16:30:35 +01002219 }
2220
Pavol Vican85f12022016-03-05 16:30:35 +01002221 /* remove current mandatory value of the target ... */
2222 dev->target->flags &= ~LYS_MAND_MASK;
2223
2224 /* ... and replace it with the value specified in deviation */
2225 dev->target->flags |= dev->deviate->flags & LYS_MAND_MASK;
2226
Radek Krejcie00d2312016-08-12 15:27:49 +02002227 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
2228 for (parent = dev->target->parent;
2229 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION));
2230 parent = parent->parent) {
2231 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
2232 /* stop also on presence containers */
2233 break;
2234 }
2235 }
2236 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
2237 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
2238 if (lyp_check_mandatory_choice(parent)) {
2239 goto error;
2240 }
2241 }
2242
Pavol Vican85f12022016-03-05 16:30:35 +01002243 return EXIT_SUCCESS;
2244
2245error:
2246 return EXIT_FAILURE;
2247}
2248
2249int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002250yang_read_deviate_minmax(struct type_deviation *dev, uint32_t value, int type)
Pavol Vican85f12022016-03-05 16:30:35 +01002251{
Pavol Vican09adcc32016-08-25 10:51:36 +02002252 uint32_t *ui32val, *min, *max;
Pavol Vican85f12022016-03-05 16:30:35 +01002253
2254 /* check target node type */
2255 if (dev->target->nodetype == LYS_LEAFLIST) {
Pavol Vican09adcc32016-08-25 10:51:36 +02002256 max = &((struct lys_node_leaflist *)dev->target)->max;
2257 min = &((struct lys_node_leaflist *)dev->target)->min;
Pavol Vican85f12022016-03-05 16:30:35 +01002258 } else if (dev->target->nodetype == LYS_LIST) {
Pavol Vican09adcc32016-08-25 10:51:36 +02002259 max = &((struct lys_node_list *)dev->target)->max;
2260 min = &((struct lys_node_list *)dev->target)->min;
Pavol Vican85f12022016-03-05 16:30:35 +01002261 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002262 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements");
2263 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 +01002264 goto error;
2265 }
2266
2267 if (type) {
2268 dev->deviate->max = value;
Pavol Vican4766aca2016-03-07 12:42:36 +01002269 dev->deviate->max_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02002270 ui32val = max;
Pavol Vican85f12022016-03-05 16:30:35 +01002271 } else {
2272 dev->deviate->min = value;
Pavol Vican4766aca2016-03-07 12:42:36 +01002273 dev->deviate->min_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02002274 ui32val = min;
Pavol Vican85f12022016-03-05 16:30:35 +01002275 }
2276
2277 if (dev->deviate->mod == LY_DEVIATE_ADD) {
2278 /* check that there is no current value */
2279 if (*ui32val) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002280 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements");
2281 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican85f12022016-03-05 16:30:35 +01002282 goto error;
2283 }
Pavol Vican4766aca2016-03-07 12:42:36 +01002284 } else if (dev->deviate->mod == LY_DEVIATE_RPL) {
2285 /* unfortunately, there is no way to check reliably that there
2286 * was a value before, it could have been the default */
Pavol Vican85f12022016-03-05 16:30:35 +01002287 }
2288
2289 /* add (already checked) and replace */
2290 /* set new value specified in deviation */
2291 *ui32val = value;
2292
Pavol Vican09adcc32016-08-25 10:51:36 +02002293 /* check min-elements is smaller than max-elements */
2294 if (*max && *min > *max) {
2295 if (type) {
2296 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"max-elements\".", value);
2297 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\".");
2298 } else {
2299 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"min-elements\".", value);
2300 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
2301 }
2302 goto error;
2303 }
2304
Pavol Vican85f12022016-03-05 16:30:35 +01002305 return EXIT_SUCCESS;
2306
2307error:
2308 return EXIT_FAILURE;
2309}
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002310
2311int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002312yang_check_deviate_must(struct ly_ctx *ctx, struct type_deviation *dev)
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002313{
2314 int i;
2315
2316 /* find must to delete, we are ok with just matching conditions */
2317 for (i = 0; i < *dev->trg_must_size; i++) {
2318 if (ly_strequal(dev->deviate->must[dev->deviate->must_size - 1].expr, (*dev->trg_must)[i].expr, 1)) {
2319 /* we have a match, free the must structure ... */
2320 lys_restr_free(ctx, &((*dev->trg_must)[i]));
2321 /* ... and maintain the array */
2322 (*dev->trg_must_size)--;
2323 if (i != *dev->trg_must_size) {
2324 (*dev->trg_must)[i].expr = (*dev->trg_must)[*dev->trg_must_size].expr;
2325 (*dev->trg_must)[i].dsc = (*dev->trg_must)[*dev->trg_must_size].dsc;
2326 (*dev->trg_must)[i].ref = (*dev->trg_must)[*dev->trg_must_size].ref;
2327 (*dev->trg_must)[i].eapptag = (*dev->trg_must)[*dev->trg_must_size].eapptag;
2328 (*dev->trg_must)[i].emsg = (*dev->trg_must)[*dev->trg_must_size].emsg;
2329 }
2330 if (!(*dev->trg_must_size)) {
2331 free(*dev->trg_must);
2332 *dev->trg_must = NULL;
2333 } else {
2334 (*dev->trg_must)[*dev->trg_must_size].expr = NULL;
2335 (*dev->trg_must)[*dev->trg_must_size].dsc = NULL;
2336 (*dev->trg_must)[*dev->trg_must_size].ref = NULL;
2337 (*dev->trg_must)[*dev->trg_must_size].eapptag = NULL;
2338 (*dev->trg_must)[*dev->trg_must_size].emsg = NULL;
2339 }
2340
2341 i = -1; /* set match flag */
2342 break;
2343 }
2344 }
2345 if (i != -1) {
2346 /* no match found */
Pavol Vican0adf01d2016-03-22 12:29:33 +01002347 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->must[dev->deviate->must_size - 1].expr, "must");
2348 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value does not match any must from the target.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002349 return EXIT_FAILURE;
2350 }
2351
2352 return EXIT_SUCCESS;
2353}
2354
2355int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002356yang_check_deviate_unique(struct lys_module *module, struct type_deviation *dev, char *value)
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002357{
2358 struct lys_node_list *list;
2359 int i, j;
2360
2361 list = (struct lys_node_list *)dev->target;
Pavol Vican0adf01d2016-03-22 12:29:33 +01002362 if (yang_fill_unique(module, list, &dev->deviate->unique[dev->deviate->unique_size], value, NULL)) {
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002363 dev->deviate->unique_size++;
2364 goto error;
2365 }
2366
2367 /* find unique structures to delete */
2368 for (i = 0; i < list->unique_size; i++) {
2369 if (list->unique[i].expr_size != dev->deviate->unique[dev->deviate->unique_size].expr_size) {
2370 continue;
2371 }
2372
2373 for (j = 0; j < dev->deviate->unique[dev->deviate->unique_size].expr_size; j++) {
2374 if (!ly_strequal(list->unique[i].expr[j], dev->deviate->unique[dev->deviate->unique_size].expr[j], 1)) {
2375 break;
2376 }
2377 }
2378
2379 if (j == dev->deviate->unique[dev->deviate->unique_size].expr_size) {
2380 /* we have a match, free the unique structure ... */
2381 for (j = 0; j < list->unique[i].expr_size; j++) {
2382 lydict_remove(module->ctx, list->unique[i].expr[j]);
2383 }
2384 free(list->unique[i].expr);
2385 /* ... and maintain the array */
2386 list->unique_size--;
2387 if (i != list->unique_size) {
2388 list->unique[i].expr_size = list->unique[list->unique_size].expr_size;
2389 list->unique[i].expr = list->unique[list->unique_size].expr;
2390 }
2391
2392 if (!list->unique_size) {
2393 free(list->unique);
2394 list->unique = NULL;
2395 } else {
2396 list->unique[list->unique_size].expr_size = 0;
2397 list->unique[list->unique_size].expr = NULL;
2398 }
2399
2400 i = -1; /* set match flag */
2401 break;
2402 }
2403 }
2404 dev->deviate->unique_size++;
2405
2406 if (i != -1) {
2407 /* no match found */
Pavol Vican0adf01d2016-03-22 12:29:33 +01002408 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "unique");
2409 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002410 goto error;
2411 }
2412
2413 free(value);
2414 return EXIT_SUCCESS;
2415
2416error:
2417 free(value);
2418 return EXIT_FAILURE;
2419}
Pavol Vicane92421d2016-03-08 10:12:33 +01002420
2421int
Pavol Vican38321d02016-08-16 14:56:02 +02002422yang_check_deviation(struct lys_module *module, struct ly_set *dflt_check, struct unres_schema *unres)
Pavol Vicane92421d2016-03-08 10:12:33 +01002423{
2424 int i, rc;
Pavol Vican38321d02016-08-16 14:56:02 +02002425 unsigned int u;
2426 const char *value, *target_name;
2427 struct lys_node_leaflist *llist;
2428 struct lys_node_leaf *leaf;
Pavol Vicane92421d2016-03-08 10:12:33 +01002429
Pavol Vican38321d02016-08-16 14:56:02 +02002430 /* now check whether default value, if any, matches the type */
2431 for (u = 0; u < dflt_check->number; ++u) {
2432 value = NULL;
2433 rc = EXIT_SUCCESS;
2434 if (dflt_check->set.s[u]->nodetype == LYS_LEAF) {
2435 leaf = (struct lys_node_leaf *)dflt_check->set.s[u];
2436 target_name = leaf->name;
2437 rc = unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, value = leaf->dflt);
2438 } else { /* LYS_LEAFLIST */
2439 llist = (struct lys_node_leaflist *)dflt_check->set.s[u];
2440 target_name = llist->name;
2441 for (i = 0; i < llist->dflt_size; i++) {
2442 rc = unres_schema_add_str(module, unres, &llist->type, UNRES_TYPE_DFLT, value = llist->dflt[i]);
2443 if (rc == -1) {
Pavol Vicane92421d2016-03-08 10:12:33 +01002444 break;
2445 }
2446 }
2447 }
Pavol Vican38321d02016-08-16 14:56:02 +02002448 if (rc == -1) {
2449 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2450 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
2451 "The default value \"%s\" of the deviated node \"%s\"no longer matches its type.",
2452 target_name);
2453 return EXIT_FAILURE;
2454 }
Pavol Vicane92421d2016-03-08 10:12:33 +01002455 }
Pavol Vican38321d02016-08-16 14:56:02 +02002456
Pavol Vicane92421d2016-03-08 10:12:33 +01002457 return EXIT_SUCCESS;
Pavol Vican38321d02016-08-16 14:56:02 +02002458
Pavol Vican9b89dda2016-03-09 15:36:55 +01002459}
2460
2461int
2462yang_fill_include(struct lys_module *module, struct lys_submodule *submodule, char *value,
Pavol Vicane024ab72016-07-27 14:27:43 +02002463 struct lys_include *inc, struct unres_schema *unres)
Pavol Vican9b89dda2016-03-09 15:36:55 +01002464{
Pavol Vican9b89dda2016-03-09 15:36:55 +01002465 struct lys_module *trg;
Pavol Vican55870412016-03-10 12:36:21 +01002466 const char *str;
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002467 int rc;
Radek Krejci83e3f5b2016-06-24 14:55:25 +02002468 int ret = 0;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002469
Pavol Vican55870412016-03-10 12:36:21 +01002470 str = lydict_insert_zc(module->ctx, value);
Pavol Vican9b89dda2016-03-09 15:36:55 +01002471 trg = (submodule) ? (struct lys_module *)submodule : module;
Pavol Vicane024ab72016-07-27 14:27:43 +02002472 rc = lyp_check_include(module, submodule, str, inc, unres);
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002473 if (!rc) {
2474 /* success, copy the filled data into the final array */
Pavol Vicane024ab72016-07-27 14:27:43 +02002475 memcpy(&trg->inc[trg->inc_size], inc, sizeof *inc);
Radek Krejci4dcd3392016-06-22 10:28:40 +02002476 trg->inc_size++;
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002477 } else if (rc == -1) {
Radek Krejci83e3f5b2016-06-24 14:55:25 +02002478 ret = -1;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002479 }
Pavol Vican9b89dda2016-03-09 15:36:55 +01002480
Pavol Vican55870412016-03-10 12:36:21 +01002481 lydict_remove(module->ctx, str);
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002482 return ret;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002483}
Pavol Vicanf4717e62016-03-16 11:30:01 +01002484
2485int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002486yang_use_extension(struct lys_module *module, struct lys_node *data_node, void *actual, char *value)
Pavol Vicanf4717e62016-03-16 11:30:01 +01002487{
2488 char *prefix;
2489 char *identif;
2490 const char *ns = NULL;
2491 int i;
2492
Pavol Vicanf4717e62016-03-16 11:30:01 +01002493 /* check to the same pointer */
2494 if (data_node != actual) {
2495 return EXIT_SUCCESS;
2496 }
2497
Pavol Vicana302aa62016-03-17 10:45:35 +01002498 prefix = strdup(value);
2499 if (!prefix) {
2500 LOGMEM;
2501 goto error;
2502 }
2503 /* find prefix anf identificator*/
2504 identif = strchr(prefix, ':');
Pavol Vicanfbd02782016-08-29 11:14:45 +02002505 if (!identif) {
2506 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, prefix);
2507 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The extension must have prefix.");
2508 goto error;
2509 }
Pavol Vicana302aa62016-03-17 10:45:35 +01002510 *identif = '\0';
2511 identif++;
2512
Pavol Vicanf4717e62016-03-16 11:30:01 +01002513 for(i = 0; i < module->imp_size; ++i) {
2514 if (!strcmp(module->imp[i].prefix, prefix)) {
2515 ns = module->imp[i].module->ns;
2516 break;
2517 }
2518 }
Pavol Vican1ff1b7b2016-04-07 09:51:49 +02002519 if (!ns && !strcmp(module->prefix, prefix)) {
2520 ns = (module->type) ? ((struct lys_submodule *)module)->belongsto->ns : module->ns;
2521 }
Pavol Vicanf4717e62016-03-16 11:30:01 +01002522 if (ns && !strcmp(ns, LY_NSNACM)) {
2523 if (!strcmp(identif, "default-deny-write")) {
2524 data_node->nacm |= LYS_NACM_DENYW;
2525 } else if (!strcmp(identif, "default-deny-all")) {
2526 data_node->nacm |= LYS_NACM_DENYA;
2527 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002528 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, identif);
Pavol Vicana302aa62016-03-17 10:45:35 +01002529 goto error;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002530 }
2531 }
Pavol Vicana302aa62016-03-17 10:45:35 +01002532 free(prefix);
Pavol Vicanf4717e62016-03-16 11:30:01 +01002533 return EXIT_SUCCESS;
Pavol Vicana302aa62016-03-17 10:45:35 +01002534
2535error:
2536 free(prefix);
2537 return EXIT_FAILURE;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002538}
2539
2540void
2541nacm_inherit(struct lys_module *module)
2542{
Pavol Vican10ffba52016-04-04 12:21:22 +02002543 struct lys_node *next, *elem, *tmp_node, *tmp_child;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002544
2545 LY_TREE_DFS_BEGIN(module->data, next, elem) {
Pavol Vican10ffba52016-04-04 12:21:22 +02002546 tmp_node = NULL;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002547 if (elem->parent) {
2548 switch (elem->nodetype) {
Pavol Vican10ffba52016-04-04 12:21:22 +02002549 case LYS_GROUPING:
2550 /* extension nacm not inherited*/
2551 break;
2552 case LYS_CHOICE:
2553 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002554 case LYS_ANYDATA:
Pavol Vican10ffba52016-04-04 12:21:22 +02002555 case LYS_USES:
2556 if (elem->parent->nodetype != LYS_GROUPING) {
2557 elem->nacm |= elem->parent->nacm;
2558 }
2559 break;
2560 case LYS_CONTAINER:
2561 case LYS_LIST:
2562 case LYS_CASE:
2563 case LYS_NOTIF:
2564 case LYS_RPC:
2565 case LYS_INPUT:
2566 case LYS_OUTPUT:
2567 case LYS_AUGMENT:
2568 elem->nacm |= elem->parent->nacm;
2569 break;
2570 case LYS_LEAF:
2571 case LYS_LEAFLIST:
2572 tmp_node = elem;
2573 tmp_child = elem->child;
2574 elem->child = NULL;
2575 default:
2576 break;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002577 }
2578 }
2579 LY_TREE_DFS_END(module->data, next, elem);
Pavol Vican10ffba52016-04-04 12:21:22 +02002580 if (tmp_node) {
2581 tmp_node->child = tmp_child;
2582 }
Pavol Vicanf4717e62016-03-16 11:30:01 +01002583 }
2584}
Pavol Vican4fb66c92016-03-17 10:32:27 +01002585
Michal Vaskoe1e351e2016-08-25 12:13:39 +02002586int
Pavol Vican4fb66c92016-03-17 10:32:27 +01002587store_flags(struct lys_node *node, uint8_t flags, int config_inherit)
2588{
Michal Vaskoe10d14a2016-08-26 15:17:13 +02002589 struct lys_node *elem;
2590
Pavol Vican4fb66c92016-03-17 10:32:27 +01002591 node->flags |= flags;
Michal Vaskoe1e351e2016-08-25 12:13:39 +02002592 if (!(node->flags & LYS_CONFIG_MASK)) {
2593 if (config_inherit) {
2594 /* get config flag from parent */
2595 if (node->parent) {
2596 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
2597 } else {
2598 /* default config is true */
2599 node->flags |= LYS_CONFIG_W;
2600 }
Pavol Vican4fb66c92016-03-17 10:32:27 +01002601 }
Michal Vaskoe10d14a2016-08-26 15:17:13 +02002602 } else {
2603 /* do we even care about config flags? */
2604 for (elem = node; elem && !(elem->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); elem = elem->parent);
2605
2606 if (!elem && (node->flags & LYS_CONFIG_W) && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
2607 LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config");
2608 LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children.");
2609 return EXIT_FAILURE;
2610 }
Pavol Vican4fb66c92016-03-17 10:32:27 +01002611 }
Michal Vaskoe1e351e2016-08-25 12:13:39 +02002612
2613 return EXIT_SUCCESS;
Pavol Vican4fb66c92016-03-17 10:32:27 +01002614}
Pavol Vican8e7110b2016-03-22 17:00:26 +01002615
Pavol Vican1938d882016-04-10 13:36:31 +02002616static int
2617yang_parse(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, const char *data,
2618 unsigned int size, struct lys_array_size *size_arrays, int type_read)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002619{
2620 YY_BUFFER_STATE bp;
Pavol Vican802af1e2016-03-23 20:42:26 +01002621 yyscan_t scanner = NULL;
Pavol Vican1938d882016-04-10 13:36:31 +02002622 int ret = EXIT_SUCCESS;
2623
2624 yylex_init(&scanner);
2625 bp = yy_scan_buffer((char *)data, size, scanner);
2626 yy_switch_to_buffer(bp, scanner);
2627 if (yyparse(scanner, module, submodule, unres, size_arrays, type_read)) {
2628 ret = EXIT_FAILURE;
2629 }
2630 yy_delete_buffer(bp, scanner);
2631 yylex_destroy(scanner);
2632 return ret;
2633}
2634
2635int
2636yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres, const char *data, unsigned int size_data)
2637{
Pavol Vican8e7110b2016-03-22 17:00:26 +01002638 struct lys_array_size *size_arrays=NULL;
Pavol Vican974377b2016-03-23 00:38:53 +01002639 unsigned int size;
Pavol Vican1938d882016-04-10 13:36:31 +02002640 int ret;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002641
2642 size_arrays = calloc(1, sizeof *size_arrays);
2643 if (!size_arrays) {
2644 LOGMEM;
Pavol Vicanf7994fb2016-04-05 21:55:53 +02002645 return EXIT_FAILURE;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002646 }
Pavol Vican8e7110b2016-03-22 17:00:26 +01002647 size = (size_data) ? size_data : strlen(data) + 2;
Pavol Vican1938d882016-04-10 13:36:31 +02002648 ret = yang_parse(module, submodule, unres, data, size, size_arrays, LY_READ_ONLY_SIZE);
2649 if (!ret) {
2650 ret = yang_parse(module, submodule, unres, data, size, size_arrays, LY_READ_ALL);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002651 }
Pavol Vican8e7110b2016-03-22 17:00:26 +01002652 free(size_arrays->node);
2653 free(size_arrays);
Pavol Vican1938d882016-04-10 13:36:31 +02002654 return ret;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002655}
2656
2657struct lys_module *
Pavol Vican974377b2016-03-23 00:38:53 +01002658yang_read_module(struct ly_ctx *ctx, const char* data, unsigned int size, const char *revision, int implement)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002659{
2660
Pavol Vican10ffba52016-04-04 12:21:22 +02002661 struct lys_module *tmp_module, *module = NULL;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002662 struct unres_schema *unres = NULL;
2663
2664 unres = calloc(1, sizeof *unres);
2665 if (!unres) {
2666 LOGMEM;
2667 goto error;
2668 }
2669
2670 module = calloc(1, sizeof *module);
2671 if (!module) {
2672 LOGMEM;
Pavol Vicanf7994fb2016-04-05 21:55:53 +02002673 goto error;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002674 }
2675
2676 /* initiale module */
Michal Vaskofe7e5a72016-05-02 14:49:23 +02002677 module->ctx = ctx;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002678 module->type = 0;
2679 module->implemented = (implement ? 1 : 0);
2680
2681 if (yang_parse_mem(module, NULL, unres, data, size)) {
2682 goto error;
2683 }
2684
2685 if (module && unres->count && resolve_unres_schema(module, unres)) {
2686 goto error;
2687 }
2688
2689 if (revision) {
2690 /* check revision of the parsed model */
2691 if (!module->rev_size || strcmp(revision, module->rev[0].date)) {
2692 LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").",
2693 module->name, module->rev[0].date, revision);
2694 goto error;
2695 }
2696 }
2697
Pavol Vican10ffba52016-04-04 12:21:22 +02002698 tmp_module = module;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002699 if (lyp_ctx_add_module(&module)) {
Pavol Vican8e7110b2016-03-22 17:00:26 +01002700 goto error;
2701 }
2702
Pavol Vican10ffba52016-04-04 12:21:22 +02002703 if (module == tmp_module) {
2704 nacm_inherit(module);
2705 }
2706
Radek Krejci27fe55e2016-09-13 17:13:35 +02002707 if (module->deviation_size && !module->implemented) {
2708 LOGVRB("Module \"%s\" includes deviations, changing its conformance to \"implement\".", module->name);
2709 /* deviations always causes target to be made implemented,
2710 * but augents and leafrefs not, so we have to apply them now */
2711 if (lys_set_implemented(module)) {
Michal Vasko26055752016-05-03 11:36:31 +02002712 goto error;
2713 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002714 }
2715
Pavol Vican8e7110b2016-03-22 17:00:26 +01002716 unres_schema_free(NULL, &unres);
2717 LOGVRB("Module \"%s\" successfully parsed.", module->name);
2718 return module;
2719
2720error:
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002721 /* cleanup */
Pavol Vican8e7110b2016-03-22 17:00:26 +01002722 unres_schema_free(module, &unres);
2723 if (!module || !module->name) {
2724 free(module);
2725 LOGERR(ly_errno, "Module parsing failed.");
2726 return NULL;
2727 }
2728
2729 LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002730
2731 lys_sub_module_remove_devs_augs(module);
2732 lys_free(module, NULL, 1);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002733 return NULL;
2734}
2735
2736struct lys_submodule *
Pavol Vican974377b2016-03-23 00:38:53 +01002737yang_read_submodule(struct lys_module *module, const char *data, unsigned int size, struct unres_schema *unres)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002738{
2739 struct lys_submodule *submodule;
2740
2741 submodule = calloc(1, sizeof *submodule);
2742 if (!submodule) {
2743 LOGMEM;
2744 goto error;
2745 }
2746
2747 submodule->ctx = module->ctx;
2748 submodule->type = 1;
2749 submodule->belongsto = module;
2750
2751 if (yang_parse_mem(module, submodule, unres, data, size)) {
2752 goto error;
2753 }
2754
Pavol Vican8e7110b2016-03-22 17:00:26 +01002755 LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002756 return submodule;
2757
2758error:
2759 /* cleanup */
2760 unres_schema_free((struct lys_module *)submodule, &unres);
2761
2762 if (!submodule || !submodule->name) {
2763 free(submodule);
2764 LOGERR(ly_errno, "Submodule parsing failed.");
2765 return NULL;
2766 }
2767
2768 LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name);
2769
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002770 lys_sub_module_remove_devs_augs((struct lys_module *)submodule);
2771 lys_submodule_module_data_free(submodule);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002772 lys_submodule_free(submodule, NULL);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002773 return NULL;
2774}
Pavol Vican8760bb72016-04-07 09:44:01 +02002775
2776static int
2777count_substring(const char *str, char c)
2778{
2779 const char *tmp = str;
2780 int count = 0;
2781
2782 while ((tmp = strchr(tmp, c))) {
2783 tmp++;
2784 count++;
2785 }
2786 return count;
2787}
2788
2789static int
2790read_indent(const char *input, int indent, int size, int in_index, int *out_index, char *output)
2791{
2792 int k = 0, j;
2793
2794 while (in_index < size) {
2795 if (input[in_index] == ' ') {
2796 k++;
2797 } else if (input[in_index] == '\t') {
2798 /* RFC 6020 6.1.3 tab character is treated as 8 space characters */
2799 k += 8;
2800 } else {
2801 break;
2802 }
2803 ++in_index;
2804 if (k >= indent) {
2805 for (j = k - indent; j > 0; --j) {
2806 output[*out_index] = ' ';
2807 ++(*out_index);
2808 }
2809 break;
2810 }
2811 }
2812 return in_index;
2813}
2814
2815char *
Pavol Vican677b0132016-08-09 15:44:58 +02002816yang_read_string(const char *input, int size, int indent, int version)
Pavol Vican8760bb72016-04-07 09:44:01 +02002817{
2818 int space, count;
2819 int in_index, out_index;
2820 char *value;
2821 char *retval = NULL;
2822
2823 value = malloc(size + 1);
2824 if (!value) {
2825 LOGMEM;
2826 return NULL;
2827 }
2828 /* replace special character in escape sequence */
2829 in_index = out_index = 0;
2830 while (in_index < size) {
2831 if (input[in_index] == '\\') {
2832 if (input[in_index + 1] == 'n') {
2833 value[out_index] = '\n';
2834 ++in_index;
2835 } else if (input[in_index + 1] == 't') {
2836 value[out_index] = '\t';
2837 ++in_index;
2838 } else if (input[in_index + 1] == '\\') {
2839 value[out_index] = '\\';
2840 ++in_index;
2841 } else if ((in_index + 1) != size && input[in_index + 1] == '"') {
2842 value[out_index] = '"';
2843 ++in_index;
2844 } else {
Pavol Vican677b0132016-08-09 15:44:58 +02002845 if (version < 2) {
2846 value[out_index] = input[in_index];
2847 } else {
2848 /* YANG 1.1 backslash must not be followed by any other character */
2849 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, input);
2850 goto error;
2851 }
Pavol Vican8760bb72016-04-07 09:44:01 +02002852 }
2853 } else {
2854 value[out_index] = input[in_index];
2855 }
2856 ++in_index;
2857 ++out_index;
2858 }
2859 value[out_index] = '\0';
2860 size = out_index;
2861 count = count_substring(value, '\t');
2862
2863 /* extend size of string due to replacing character '\t' with 8 spaces */
2864 retval = malloc(size + 1 + 7 * count);
2865 if (!retval) {
2866 LOGMEM;
2867 goto error;
2868 }
2869 in_index = out_index = space = 0;
2870 while (in_index < size) {
2871 if (value[in_index] == '\n') {
2872 out_index -= space;
2873 space = 0;
2874 retval[out_index] = '\n';
2875 ++out_index;
2876 ++in_index;
2877 in_index = read_indent(value, indent, size, in_index, &out_index, retval);
2878 continue;
2879 } else {
2880 space = (value[in_index] == ' ' || value[in_index] == '\t') ? space + 1 : 0;
2881 retval[out_index] = value[in_index];
2882 ++out_index;
2883 }
2884 ++in_index;
2885 }
2886 retval[out_index] = '\0';
2887 if (out_index != size) {
2888 retval = ly_realloc(retval, out_index + 1);
2889 }
2890 free(value);
2891 return retval;
2892error:
2893 free(value);
2894 return NULL;
2895}