blob: 2eb0cac8a5a92f963bd90c1f1643a56e6f4ee087 [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 Vican7313fc02016-11-14 01:10:31 +010016#include <assert.h>
Pavol Vican021488a2016-01-25 23:56:12 +010017#include "parser_yang.h"
Pavol Vican8e7110b2016-03-22 17:00:26 +010018#include "parser_yang_lex.h"
Pavol Vican6eb14e82016-02-03 12:27:13 +010019#include "parser.h"
Pavol Vicanf37eeaa2016-02-09 20:54:06 +010020#include "xpath.h"
Pavol Vican021488a2016-01-25 23:56:12 +010021
Pavol Vican082afd02016-10-25 12:39:15 +020022static void yang_free_import(struct ly_ctx *ctx, struct lys_import *imp, uint8_t start, uint8_t size);
23static void yang_free_include(struct ly_ctx *ctx, struct lys_include *inc, uint8_t start, uint8_t size);
Pavol Vican7313fc02016-11-14 01:10:31 +010024static int yang_check_sub_module(struct lys_module *module, struct unres_schema *unres, struct lys_node *node);
Pavol Vican05810b62016-11-23 14:07:22 +010025static void free_yang_common(struct lys_module *module, struct lys_node *node);
Pavol Vican3ad50f82016-12-04 15:00:36 +010026static int yang_check_nodes(struct lys_module *module, struct lys_node *parent, struct lys_node *nodes,
27 int config_opt, struct unres_schema *unres);
Pavol Vican05810b62016-11-23 14:07:22 +010028void lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size);
Pavol Vican082afd02016-10-25 12:39:15 +020029
Michal Vaskofe7e5a72016-05-02 14:49:23 +020030static int
Pavol Vican0adf01d2016-03-22 12:29:33 +010031yang_check_string(struct lys_module *module, const char **target, char *what, char *where, char *value)
Pavol Vican2a064652016-02-02 22:54:34 +010032{
Pavol Vicanbf805472016-01-26 14:24:56 +010033 if (*target) {
Pavol Vican0adf01d2016-03-22 12:29:33 +010034 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, what, where);
Pavol Vicanbf805472016-01-26 14:24:56 +010035 free(value);
36 return 1;
37 } else {
Pavol Vican2a064652016-02-02 22:54:34 +010038 *target = lydict_insert_zc(module->ctx, value);
Pavol Vicanbf805472016-01-26 14:24:56 +010039 return 0;
40 }
41}
42
Michal Vaskofe7e5a72016-05-02 14:49:23 +020043int
Pavol Vican5f0316a2016-04-05 21:21:11 +020044yang_read_common(struct lys_module *module, char *value, enum yytokentype type)
Pavol Vican2a064652016-02-02 22:54:34 +010045{
Pavol Vican6eb14e82016-02-03 12:27:13 +010046 int ret = 0;
Pavol Vican021488a2016-01-25 23:56:12 +010047
48 switch (type) {
Pavol Vican2a064652016-02-02 22:54:34 +010049 case MODULE_KEYWORD:
50 module->name = lydict_insert_zc(module->ctx, value);
51 break;
52 case NAMESPACE_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +010053 ret = yang_check_string(module, &module->ns, "namespace", "module", value);
Pavol Vican2a064652016-02-02 22:54:34 +010054 break;
Pavol Vican1ca072c2016-02-03 13:03:56 +010055 case ORGANIZATION_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +010056 ret = yang_check_string(module, &module->org, "organization", "module", value);
Pavol Vican1ca072c2016-02-03 13:03:56 +010057 break;
58 case CONTACT_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +010059 ret = yang_check_string(module, &module->contact, "contact", "module", value);
Pavol Vican1ca072c2016-02-03 13:03:56 +010060 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +020061 default:
62 free(value);
63 LOGINT;
64 ret = EXIT_FAILURE;
65 break;
Pavol Vican2a064652016-02-02 22:54:34 +010066 }
67
Pavol Vican021488a2016-01-25 23:56:12 +010068 return ret;
Pavol Vicanbf805472016-01-26 14:24:56 +010069}
70
Michal Vaskofe7e5a72016-05-02 14:49:23 +020071int
Pavol Vicand0b64c12016-07-15 09:56:19 +020072yang_check_version(struct lys_module *module, struct lys_submodule *submodule, char *value, int repeat)
73{
74 int ret = EXIT_SUCCESS;
75
76 if (repeat) {
Michal Vasko3767fb22016-07-21 12:10:57 +020077 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "yang version", "module");
78 ret = EXIT_FAILURE;
Pavol Vicand0b64c12016-07-15 09:56:19 +020079 } else {
80 if (!strcmp(value, "1")) {
81 if (submodule) {
82 if (module->version > 1) {
83 LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
84 ret = EXIT_FAILURE;
85 }
86 } else {
87 module->version = 1;
88 }
89 } else if (!strcmp(value, "1.1")) {
90 if (submodule) {
91 if (module->version != 2) {
92 LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
93 ret = EXIT_FAILURE;
94 }
95 } else {
96 module->version = 2;
97 }
98 } else {
99 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "yang-version");
100 ret = EXIT_FAILURE;
Michal Vasko3767fb22016-07-21 12:10:57 +0200101 }
Pavol Vicand0b64c12016-07-15 09:56:19 +0200102 }
103 free(value);
104 return ret;
105}
106
107int
Pavol Vicane024ab72016-07-27 14:27:43 +0200108yang_read_prefix(struct lys_module *module, struct lys_import *imp, char *value)
Pavol Vican2a064652016-02-02 22:54:34 +0100109{
Pavol Vican6eb14e82016-02-03 12:27:13 +0100110 int ret = 0;
Pavol Vicanbf805472016-01-26 14:24:56 +0100111
Pavol Vican1cc4e192016-10-24 16:38:31 +0200112 if (!imp && lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) {
Pavol Vican6eb14e82016-02-03 12:27:13 +0100113 free(value);
114 return EXIT_FAILURE;
115 }
Pavol Vicane024ab72016-07-27 14:27:43 +0200116
117 if (imp) {
118 ret = yang_check_string(module, &imp->prefix, "prefix", "import", value);
119 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100120 ret = yang_check_string(module, &module->prefix, "prefix", "module", value);
Pavol Vican2a064652016-02-02 22:54:34 +0100121 }
Pavol Vican6eb14e82016-02-03 12:27:13 +0100122
Pavol Vicanbf805472016-01-26 14:24:56 +0100123 return ret;
124}
Pavol Vican6eb14e82016-02-03 12:27:13 +0100125
Pavol Vican1cc4e192016-10-24 16:38:31 +0200126static int
127yang_fill_import(struct lys_module *module, struct lys_import *imp_old, struct lys_import *imp_new, char *value)
Pavol Vican6eb14e82016-02-03 12:27:13 +0100128{
Pavol Vican0da132e2016-03-21 12:03:03 +0100129 const char *exp;
Radek Krejci4dcd3392016-06-22 10:28:40 +0200130 int rc;
Pavol Vican6eb14e82016-02-03 12:27:13 +0100131
Pavol Vican1cc4e192016-10-24 16:38:31 +0200132 if (!imp_old->prefix) {
Pavol Vicane024ab72016-07-27 14:27:43 +0200133 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", "import");
Pavol Vican1cc4e192016-10-24 16:38:31 +0200134 goto error;
135 } else {
136 if (lyp_check_identifier(imp_old->prefix, LY_IDENT_PREFIX, module, NULL)) {
137 goto error;
138 }
Pavol Vicane024ab72016-07-27 14:27:43 +0200139 }
Pavol Vican1cc4e192016-10-24 16:38:31 +0200140 memcpy(imp_new, imp_old, sizeof *imp_old);
Pavol Vican0da132e2016-03-21 12:03:03 +0100141 exp = lydict_insert_zc(module->ctx, value);
Pavol Vican1cc4e192016-10-24 16:38:31 +0200142 rc = lyp_check_import(module, exp, imp_new);
Pavol Vican0da132e2016-03-21 12:03:03 +0100143 lydict_remove(module->ctx, exp);
Radek Krejci4dcd3392016-06-22 10:28:40 +0200144 module->imp_size++;
Pavol Vican0da132e2016-03-21 12:03:03 +0100145 if (rc) {
Radek Krejci4dcd3392016-06-22 10:28:40 +0200146 return EXIT_FAILURE;
Pavol Vican6eb14e82016-02-03 12:27:13 +0100147 }
Pavol Vican6eb14e82016-02-03 12:27:13 +0100148
Pavol Vican6eb14e82016-02-03 12:27:13 +0100149 return EXIT_SUCCESS;
Pavol Vican1cc4e192016-10-24 16:38:31 +0200150
151error:
152 free(value);
153 lydict_remove(module->ctx, imp_old->dsc);
154 lydict_remove(module->ctx, imp_old->ref);
155 return EXIT_FAILURE;
Pavol Vican6eb14e82016-02-03 12:27:13 +0100156}
Pavol Vican1ca072c2016-02-03 13:03:56 +0100157
158int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100159yang_read_description(struct lys_module *module, void *node, char *value, char *where)
Pavol Vican1ca072c2016-02-03 13:03:56 +0100160{
161 int ret;
Pavol Vican9bcd7c62016-03-17 19:36:35 +0100162 char *dsc = "description";
Pavol Vican1ca072c2016-02-03 13:03:56 +0100163
164 if (!node) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100165 ret = yang_check_string(module, &module->dsc, dsc, "module", value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100166 } else {
Pavol Vican9bcd7c62016-03-17 19:36:35 +0100167 if (!strcmp("revision", where)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100168 ret = yang_check_string(module, &((struct lys_revision *)node)->dsc, dsc, where, value);
Pavol Vicane024ab72016-07-27 14:27:43 +0200169 } else if (!strcmp("import", where)){
170 ret = yang_check_string(module, &((struct lys_import *)node)->dsc, dsc, where, value);
171 } else if (!strcmp("include", where)){
172 ret = yang_check_string(module, &((struct lys_include *)node)->dsc, dsc, where, value);
Pavol Vican9bcd7c62016-03-17 19:36:35 +0100173 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100174 ret = yang_check_string(module, &((struct lys_node *)node)->dsc, dsc, where, value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100175 }
Pavol Vican1ca072c2016-02-03 13:03:56 +0100176 }
177 return ret;
178}
179
180int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100181yang_read_reference(struct lys_module *module, void *node, char *value, char *where)
Pavol Vican1ca072c2016-02-03 13:03:56 +0100182{
183 int ret;
Pavol Vicanf5fe9662016-03-17 20:00:16 +0100184 char *ref = "reference";
Pavol Vican1ca072c2016-02-03 13:03:56 +0100185
186 if (!node) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100187 ret = yang_check_string(module, &module->ref, "reference", "module", value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100188 } else {
Pavol Vicanf5fe9662016-03-17 20:00:16 +0100189 if (!strcmp("revision", where)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100190 ret = yang_check_string(module, &((struct lys_revision *)node)->ref, ref, where, value);
Pavol Vicane024ab72016-07-27 14:27:43 +0200191 } else if (!strcmp("import", where)){
192 ret = yang_check_string(module, &((struct lys_import *)node)->ref, ref, where, value);
193 } else if (!strcmp("include", where)){
194 ret = yang_check_string(module, &((struct lys_include *)node)->ref, ref, where, value);
Pavol Vicanf5fe9662016-03-17 20:00:16 +0100195 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100196 ret = yang_check_string(module, &((struct lys_node *)node)->ref, ref, where, value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100197 }
Pavol Vican1ca072c2016-02-03 13:03:56 +0100198 }
199 return ret;
200}
Pavol Vicanbedff692016-02-03 14:29:17 +0100201
Pavol Vican866d9912016-10-25 09:13:30 +0200202void
203yang_read_revision(struct lys_module *module, char *value, struct lys_revision *retval)
Pavol Vicanbedff692016-02-03 14:29:17 +0100204{
Pavol Vicanbedff692016-02-03 14:29:17 +0100205 /* first member of array is last revision */
Pavol Vican866d9912016-10-25 09:13:30 +0200206 if ((module->rev_size - 1) && strcmp(module->rev[0].date, value) < 0) {
Pavol Vicanbedff692016-02-03 14:29:17 +0100207 memcpy(retval->date, module->rev[0].date, LY_REV_SIZE);
208 memcpy(module->rev[0].date, value, LY_REV_SIZE);
209 retval->dsc = module->rev[0].dsc;
210 retval->ref = module->rev[0].ref;
211 retval = module->rev;
212 retval->dsc = NULL;
213 retval->ref = NULL;
214 } else {
215 memcpy(retval->date, value, LY_REV_SIZE);
216 }
Pavol Vicanbedff692016-02-03 14:29:17 +0100217 free(value);
Pavol Vicanbedff692016-02-03 14:29:17 +0100218}
Pavol Vican1eeb1992016-02-09 11:10:45 +0100219
220int
Pavol Vicana1827962016-02-29 15:39:42 +0100221yang_add_elem(struct lys_node_array **node, uint32_t *size)
Pavol Vican1eeb1992016-02-09 11:10:45 +0100222{
Pavol Vican45ccc592016-03-09 18:53:48 +0100223 if (!(*size % LY_ARRAY_SIZE)) {
Pavol Vican1eeb1992016-02-09 11:10:45 +0100224 if (!(*node = ly_realloc(*node, (*size + LY_ARRAY_SIZE) * sizeof **node))) {
225 LOGMEM;
226 return EXIT_FAILURE;
227 } else {
Pavol Vican45ccc592016-03-09 18:53:48 +0100228 memset(*node + *size, 0, LY_ARRAY_SIZE * sizeof **node);
Pavol Vican1eeb1992016-02-09 11:10:45 +0100229 }
230 }
231 (*size)++;
232 return EXIT_SUCCESS;
233}
Pavol Vicane1354e92016-02-09 14:02:09 +0100234
Pavol Vicane1354e92016-02-09 14:02:09 +0100235int
Pavol Vicandf9a95c2016-12-02 23:34:51 +0100236yang_fill_iffeature(struct lys_module *module, struct lys_iffeature *iffeature, void *parent,
237 char *value, struct unres_schema *unres, int parent_is_feature)
Pavol Vicane1354e92016-02-09 14:02:09 +0100238{
239 const char *exp;
240 int ret;
241
Michal Vasko97b32be2016-07-25 10:59:53 +0200242 if ((module->version != 2) && ((value[0] == '(') || strchr(value, ' '))) {
243 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
244 free(value);
245 return EXIT_FAILURE;
246 }
Pavol Vicane1354e92016-02-09 14:02:09 +0100247
Michal Vasko56d082c2016-10-25 14:00:42 +0200248 if (!(exp = transform_iffeat_schema2json(module, value))) {
Pavol Vicane1354e92016-02-09 14:02:09 +0100249 free(value);
250 return EXIT_FAILURE;
251 }
252 free(value);
253
Pavol Vicandf9a95c2016-12-02 23:34:51 +0100254 ret = resolve_iffeature_compile(iffeature, exp, (struct lys_node *)parent, parent_is_feature, unres);
Pavol Vicane1354e92016-02-09 14:02:09 +0100255 lydict_remove(module->ctx, exp);
Pavol Vicane1354e92016-02-09 14:02:09 +0100256
Pavol Vicandf9a95c2016-12-02 23:34:51 +0100257 return (ret) ? EXIT_FAILURE : EXIT_SUCCESS;
Pavol Vicane1354e92016-02-09 14:02:09 +0100258}
259
Pavol Vican4fb66c92016-03-17 10:32:27 +0100260int
Radek Krejci4372b4e2016-04-14 17:42:16 +0200261yang_check_flags(uint16_t *flags, uint16_t mask, char *what, char *where, uint16_t value, int shortint)
Pavol Vicane1354e92016-02-09 14:02:09 +0100262{
263 if (*flags & mask) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100264 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, what, where);
Pavol Vicane1354e92016-02-09 14:02:09 +0100265 return EXIT_FAILURE;
266 } else {
Radek Krejci4372b4e2016-04-14 17:42:16 +0200267 if (shortint) {
268 *((uint8_t *)flags) |= (uint8_t)value;
269 } else {
270 *flags |= value;
271 }
Pavol Vicane1354e92016-02-09 14:02:09 +0100272 return EXIT_SUCCESS;
273 }
274}
275
Pavol Vicanbbdef532016-02-09 14:52:12 +0100276int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100277yang_read_base(struct lys_module *module, struct lys_ident *ident, char *value, struct unres_schema *unres)
Pavol Vicanbbdef532016-02-09 14:52:12 +0100278{
279 const char *exp;
280
Pavol Vican0adf01d2016-03-22 12:29:33 +0100281 exp = transform_schema2json(module, value);
Pavol Vicanbbdef532016-02-09 14:52:12 +0100282 free(value);
283 if (!exp) {
284 return EXIT_FAILURE;
285 }
Pavol Vican4ffd0ab2016-08-27 16:35:04 +0200286
Pavol Vican0adf01d2016-03-22 12:29:33 +0100287 if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, exp) == -1) {
Pavol Vicanbbdef532016-02-09 14:52:12 +0100288 lydict_remove(module->ctx, exp);
289 return EXIT_FAILURE;
290 }
Pavol Vican44dde2c2016-02-10 11:18:14 +0100291
Pavol Vicanbbdef532016-02-09 14:52:12 +0100292 lydict_remove(module->ctx, exp);
293 return EXIT_SUCCESS;
294}
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100295
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100296int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100297yang_read_message(struct lys_module *module,struct lys_restr *save,char *value, char *what, int message)
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100298{
299 int ret;
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100300
Pavol Vicandde090a2016-08-30 15:12:14 +0200301 if (message == ERROR_APP_TAG_KEYWORD) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100302 ret = yang_check_string(module, &save->eapptag, "error_app_tag", what, value);
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100303 } else {
Pavol Vicandde090a2016-08-30 15:12:14 +0200304 ret = yang_check_string(module, &save->emsg, "error_message", what, value);
Pavol Vicanf37eeaa2016-02-09 20:54:06 +0100305 }
306 return ret;
307}
Pavol Vicanb5687112016-02-09 22:35:59 +0100308
309int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100310yang_read_presence(struct lys_module *module, struct lys_node_container *cont, char *value)
Pavol Vicanb5687112016-02-09 22:35:59 +0100311{
312 if (cont->presence) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100313 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, cont, "presence", "container");
Pavol Vicanb5687112016-02-09 22:35:59 +0100314 free(value);
315 return EXIT_FAILURE;
316 } else {
317 cont->presence = lydict_insert_zc(module->ctx, value);
318 return EXIT_SUCCESS;
319 }
320}
321
Pavol Vican235dbd42016-02-10 10:34:19 +0100322void *
Pavol Vican5f0316a2016-04-05 21:21:11 +0200323yang_read_when(struct lys_module *module, struct lys_node *node, enum yytokentype type, char *value)
Pavol Vican235dbd42016-02-10 10:34:19 +0100324{
325 struct lys_when *retval;
326
327 retval = calloc(1, sizeof *retval);
328 if (!retval) {
329 LOGMEM;
330 free(value);
331 return NULL;
332 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100333 retval->cond = transform_schema2json(module, value);
Michal Vasko508a50d2016-09-07 14:50:33 +0200334 if (!retval->cond) {
Pavol Vican235dbd42016-02-10 10:34:19 +0100335 goto error;
336 }
337 switch (type) {
338 case CONTAINER_KEYWORD:
339 if (((struct lys_node_container *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100340 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "container");
Pavol Vican235dbd42016-02-10 10:34:19 +0100341 goto error;
342 }
343 ((struct lys_node_container *)node)->when = retval;
344 break;
Pavol Vicandb7489e2016-08-23 17:23:39 +0200345 case ANYDATA_KEYWORD:
Pavol Vican1f06ba82016-02-10 17:39:50 +0100346 case ANYXML_KEYWORD:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200347 if (((struct lys_node_anydata *)node)->when) {
Pavol Vicandb7489e2016-08-23 17:23:39 +0200348 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", (type == ANYXML_KEYWORD) ? "anyxml" : "anydata");
Pavol Vican8c82fa82016-02-10 13:13:24 +0100349 goto error;
350 }
Radek Krejcibf2abff2016-08-23 15:51:52 +0200351 ((struct lys_node_anydata *)node)->when = retval;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100352 break;
Pavol Vican1f06ba82016-02-10 17:39:50 +0100353 case CHOICE_KEYWORD:
354 if (((struct lys_node_choice *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100355 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "choice");
Pavol Vican1f06ba82016-02-10 17:39:50 +0100356 goto error;
357 }
358 ((struct lys_node_choice *)node)->when = retval;
359 break;
Pavol Vicanbd098132016-02-11 10:56:49 +0100360 case CASE_KEYWORD:
361 if (((struct lys_node_case *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100362 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "case");
Pavol Vicanbd098132016-02-11 10:56:49 +0100363 goto error;
364 }
365 ((struct lys_node_case *)node)->when = retval;
366 break;
Pavol Vican096c6db2016-02-11 15:08:10 +0100367 case LEAF_KEYWORD:
368 if (((struct lys_node_leaf *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100369 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaf");
Pavol Vican096c6db2016-02-11 15:08:10 +0100370 goto error;
371 }
372 ((struct lys_node_leaf *)node)->when = retval;
373 break;
Pavol Vican339d4ad2016-02-12 12:49:22 +0100374 case LEAF_LIST_KEYWORD:
375 if (((struct lys_node_leaflist *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100376 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "leaflist");
Pavol Vican339d4ad2016-02-12 12:49:22 +0100377 goto error;
378 }
379 ((struct lys_node_leaflist *)node)->when = retval;
380 break;
Pavol Vican5de33492016-02-22 14:03:24 +0100381 case LIST_KEYWORD:
382 if (((struct lys_node_list *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100383 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "list");
Pavol Vican5de33492016-02-22 14:03:24 +0100384 goto error;
385 }
386 ((struct lys_node_list *)node)->when = retval;
387 break;
Pavol Vican14b18562016-03-01 16:04:29 +0100388 case USES_KEYWORD:
389 if (((struct lys_node_uses *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100390 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "uses");
Pavol Vican14b18562016-03-01 16:04:29 +0100391 goto error;
392 }
393 ((struct lys_node_uses *)node)->when = retval;
394 break;
Pavol Vican92fa0dc2016-03-02 14:20:39 +0100395 case AUGMENT_KEYWORD:
396 if (((struct lys_node_augment *)node)->when) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100397 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, node, "when", "augment");
Pavol Vican92fa0dc2016-03-02 14:20:39 +0100398 goto error;
399 }
400 ((struct lys_node_augment *)node)->when = retval;
401 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200402 default:
403 goto error;
404 break;
Pavol Vican235dbd42016-02-10 10:34:19 +0100405 }
406 free(value);
407 return retval;
408
409error:
410 free(value);
411 lys_when_free(module->ctx, retval);
412 return NULL;
413}
Pavol Vican8c82fa82016-02-10 13:13:24 +0100414
415void *
Pavol Vican05810b62016-11-23 14:07:22 +0100416yang_read_node(struct lys_module *module, struct lys_node *parent, struct lys_node **root,
417 char *value, int nodetype, int sizeof_struct)
Pavol Vican8c82fa82016-02-10 13:13:24 +0100418{
Pavol Vican05810b62016-11-23 14:07:22 +0100419 struct lys_node *node, **child;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100420
Pavol Vican7cadfe72016-02-11 12:33:34 +0100421 node = calloc(1, sizeof_struct);
422 if (!node) {
Pavol Vicand1dbfda2016-03-21 10:03:58 +0100423 free(value);
Pavol Vican8c82fa82016-02-10 13:13:24 +0100424 LOGMEM;
425 return NULL;
426 }
Pavol Vican531a9132016-03-03 10:10:09 +0100427 if (value) {
428 node->name = lydict_insert_zc(module->ctx, value);
429 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100430 node->module = module;
Pavol Vican7cadfe72016-02-11 12:33:34 +0100431 node->nodetype = nodetype;
Pavol Vican7cadfe72016-02-11 12:33:34 +0100432
433 /* insert the node into the schema tree */
Pavol Vican05810b62016-11-23 14:07:22 +0100434 child = (parent) ? &parent->child : root;
435 if (*child) {
436 (*child)->prev->next = node;
437 (*child)->prev = node;
438 } else {
439 *child = node;
440 node->prev = node;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100441 }
Pavol Vican7cadfe72016-02-11 12:33:34 +0100442 return node;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100443}
444
445int
Pavol Vican5f0316a2016-04-05 21:21:11 +0200446yang_read_default(struct lys_module *module, void *node, char *value, enum yytokentype type)
Pavol Vican096c6db2016-02-11 15:08:10 +0100447{
448 int ret;
449
450 switch (type) {
Pavol Vican339d4ad2016-02-12 12:49:22 +0100451 case LEAF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100452 ret = yang_check_string(module, &((struct lys_node_leaf *) node)->dflt, "default", "leaf", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100453 break;
Pavol Vican0df02b02016-03-01 10:28:50 +0100454 case TYPEDEF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100455 ret = yang_check_string(module, &((struct lys_tpdf *) node)->dflt, "default", "typedef", value);
Pavol Vican0df02b02016-03-01 10:28:50 +0100456 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200457 default:
458 free(value);
459 LOGINT;
460 ret = EXIT_FAILURE;
461 break;
Pavol Vican096c6db2016-02-11 15:08:10 +0100462 }
463 return ret;
464}
465
466int
Pavol Vican5f0316a2016-04-05 21:21:11 +0200467yang_read_units(struct lys_module *module, void *node, char *value, enum yytokentype type)
Pavol Vican096c6db2016-02-11 15:08:10 +0100468{
469 int ret;
470
471 switch (type) {
Pavol Vican339d4ad2016-02-12 12:49:22 +0100472 case LEAF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100473 ret = yang_check_string(module, &((struct lys_node_leaf *) node)->units, "units", "leaf", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100474 break;
475 case LEAF_LIST_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100476 ret = yang_check_string(module, &((struct lys_node_leaflist *) node)->units, "units", "leaflist", value);
Pavol Vican339d4ad2016-02-12 12:49:22 +0100477 break;
Pavol Vican0df02b02016-03-01 10:28:50 +0100478 case TYPEDEF_KEYWORD:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100479 ret = yang_check_string(module, &((struct lys_tpdf *) node)->units, "units", "typedef", value);
Pavol Vican0df02b02016-03-01 10:28:50 +0100480 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +0200481 default:
482 free(value);
483 LOGINT;
484 ret = EXIT_FAILURE;
485 break;
Pavol Vican8c82fa82016-02-10 13:13:24 +0100486 }
487 return ret;
488}
Pavol Vican5de33492016-02-22 14:03:24 +0100489
490int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100491yang_read_key(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres)
Pavol Vican5de33492016-02-22 14:03:24 +0100492{
493 char *exp, *value;
Radek Krejci5c08a992016-11-02 13:30:04 +0100494 struct lys_node *node;
Pavol Vican5de33492016-02-22 14:03:24 +0100495
496 exp = value = (char *) list->keys;
497 while ((value = strpbrk(value, " \t\n"))) {
498 list->keys_size++;
499 while (isspace(*value)) {
500 value++;
501 }
502 }
503 list->keys_size++;
Radek Krejci5c08a992016-11-02 13:30:04 +0100504
505 list->keys_str = lydict_insert_zc(module->ctx, exp);
Pavol Vican5de33492016-02-22 14:03:24 +0100506 list->keys = calloc(list->keys_size, sizeof *list->keys);
507 if (!list->keys) {
508 LOGMEM;
Radek Krejci5c08a992016-11-02 13:30:04 +0100509 return EXIT_FAILURE;
Pavol Vican5de33492016-02-22 14:03:24 +0100510 }
Radek Krejci5c08a992016-11-02 13:30:04 +0100511 for (node = list->parent; node && node->nodetype != LYS_GROUPING; node = lys_parent(node));
512 if (!node && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
513 return EXIT_FAILURE;
Pavol Vican5de33492016-02-22 14:03:24 +0100514 }
Pavol Vican5de33492016-02-22 14:03:24 +0100515 return EXIT_SUCCESS;
Pavol Vican5de33492016-02-22 14:03:24 +0100516}
517
518int
Pavol Vican0adf01d2016-03-22 12:29:33 +0100519yang_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 +0100520{
521 int i, j;
522 char *vaux;
Radek Krejcid09d1a52016-08-11 14:05:45 +0200523 struct unres_list_uniq *unique_info;
Pavol Vican85f12022016-03-05 16:30:35 +0100524
525 /* count the number of unique leafs in the value */
526 vaux = value;
527 while ((vaux = strpbrk(vaux, " \t\n"))) {
528 unique->expr_size++;
529 while (isspace(*vaux)) {
530 vaux++;
531 }
532 }
533 unique->expr_size++;
534 unique->expr = calloc(unique->expr_size, sizeof *unique->expr);
535 if (!unique->expr) {
536 LOGMEM;
537 goto error;
538 }
539
540 for (i = 0; i < unique->expr_size; i++) {
541 vaux = strpbrk(value, " \t\n");
542 if (!vaux) {
543 /* the last token, lydict_insert() will count its size on its own */
544 vaux = value;
545 }
546
547 /* store token into unique structure */
548 unique->expr[i] = lydict_insert(module->ctx, value, vaux - value);
549
550 /* check that the expression does not repeat */
551 for (j = 0; j < i; j++) {
552 if (ly_strequal(unique->expr[j], unique->expr[i], 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100553 LOGVAL(LYE_INARG, LY_VLOG_LYS, list, unique->expr[i], "unique");
554 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The identifier is not unique");
Pavol Vican85f12022016-03-05 16:30:35 +0100555 goto error;
556 }
557 }
558 /* try to resolve leaf */
559 if (unres) {
Radek Krejcid09d1a52016-08-11 14:05:45 +0200560 unique_info = malloc(sizeof *unique_info);
561 unique_info->list = (struct lys_node *)list;
562 unique_info->expr = unique->expr[i];
563 unique_info->trg_type = &unique->trg_type;
564 if (unres_schema_add_node(module, unres, unique_info, UNRES_LIST_UNIQ, NULL) == -1) {
Pavol Vican18b10212016-04-11 15:41:52 +0200565 goto error;
566 }
Pavol Vican85f12022016-03-05 16:30:35 +0100567 } else {
Radek Krejcid09d1a52016-08-11 14:05:45 +0200568 if (resolve_unique((struct lys_node *)list, unique->expr[i], &unique->trg_type)) {
Pavol Vican85f12022016-03-05 16:30:35 +0100569 goto error;
570 }
571 }
572
573 /* move to next token */
574 value = vaux;
575 while(isspace(*value)) {
576 value++;
577 }
578 }
579
580 return EXIT_SUCCESS;
581
582error:
583 return EXIT_FAILURE;
584}
585
586int
Pavol Vican5de33492016-02-22 14:03:24 +0100587yang_read_unique(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres)
588{
589 uint8_t k;
Pavol Vican0adf01d2016-03-22 12:29:33 +0100590 char *str;
Pavol Vican5de33492016-02-22 14:03:24 +0100591
592 for(k=0; k<list->unique_size; k++) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100593 str = (char *)list->unique[k].expr;
594 if (yang_fill_unique(module, list, &list->unique[k], str, unres)) {
Pavol Vican5de33492016-02-22 14:03:24 +0100595 goto error;
596 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100597 free(str);
Pavol Vican5de33492016-02-22 14:03:24 +0100598 }
599 return EXIT_SUCCESS;
600
601error:
Pavol Vican0adf01d2016-03-22 12:29:33 +0100602 free(str);
Pavol Vican5de33492016-02-22 14:03:24 +0100603 return EXIT_FAILURE;
Pavol Vican73e7c992016-02-24 12:18:05 +0100604}
605
Pavol Vican07f220f2016-09-02 13:04:37 +0200606int
Pavol Vican81344ac2016-09-02 14:23:06 +0200607yang_read_leafref_path(struct lys_module *module, struct yang_type *stype, char *value)
Pavol Vican1ff0e222016-02-26 12:27:01 +0100608{
Pavol Vican81344ac2016-09-02 14:23:06 +0200609 if (stype->base && (stype->base != LY_TYPE_LEAFREF)) {
610 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance");
611 goto error;
Pavol Vican1ff0e222016-02-26 12:27:01 +0100612 }
Pavol Vican81344ac2016-09-02 14:23:06 +0200613 if (stype->type->info.lref.path) {
614 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "path", "type");
615 goto error;
616 }
617 stype->type->info.lref.path = lydict_insert_zc(module->ctx, value);
618 stype->base = LY_TYPE_LEAFREF;
619 return EXIT_SUCCESS;
620
621error:
622 free(value);
623 return EXIT_FAILURE;
624}
625
626int
627yang_read_require_instance(struct yang_type *stype, int req)
628{
629 if (stype->base && (stype->base != LY_TYPE_LEAFREF)) {
630 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance");
631 return EXIT_FAILURE;
632 }
633 if (stype->type->info.lref.req) {
634 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "require-instance", "type");
635 return EXIT_FAILURE;
636 }
637 stype->type->info.lref.req = req;
638 stype->base = LY_TYPE_LEAFREF;
639 return EXIT_SUCCESS;
640}
641
642int
Pavol Vican7313fc02016-11-14 01:10:31 +0100643yang_check_type(struct lys_module *module, struct lys_node *parent, struct yang_type *typ, struct lys_type *type, int tpdftype, struct unres_schema *unres)
Pavol Vican73e7c992016-02-24 12:18:05 +0100644{
Pavol Vican81344ac2016-09-02 14:23:06 +0200645 int i, j, rc, ret = -1;
646 int8_t req;
Pavol Vican73e7c992016-02-24 12:18:05 +0100647 const char *name, *value;
Pavol Vican8bd72e42016-08-29 09:53:05 +0200648 LY_DATA_TYPE base = 0;
Radek Krejci3a5501d2016-07-18 22:03:34 +0200649 struct lys_node *siter;
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200650 struct lys_type *dertype;
651 struct lys_type_enum *enms_sc = NULL;
652 struct lys_type_bit *bits_sc = NULL;
653 struct lys_type_bit bit_tmp;
Pavol Vican73e7c992016-02-24 12:18:05 +0100654
Pavol Vican0adf01d2016-03-22 12:29:33 +0100655 value = transform_schema2json(module, typ->name);
Pavol Vican73e7c992016-02-24 12:18:05 +0100656 if (!value) {
657 goto error;
658 }
659
660 i = parse_identifier(value);
661 if (i < 1) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100662 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]);
Pavol Vican73e7c992016-02-24 12:18:05 +0100663 lydict_remove(module->ctx, value);
664 goto error;
665 }
Radek Krejci408ab2f2016-06-06 15:27:10 +0200666 /* module name */
Pavol Vican73e7c992016-02-24 12:18:05 +0100667 name = value;
668 if (value[i]) {
Pavol Vican7313fc02016-11-14 01:10:31 +0100669 type->module_name = lydict_insert(module->ctx, value, i);
Pavol Vican73e7c992016-02-24 12:18:05 +0100670 name += i;
671 if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100672 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name);
Pavol Vican73e7c992016-02-24 12:18:05 +0100673 lydict_remove(module->ctx, value);
674 goto error;
675 }
676 ++name;
677 }
678
Pavol Vican7313fc02016-11-14 01:10:31 +0100679 rc = resolve_superior_type(name, type->module_name, module, parent, &type->der);
Pavol Vican73e7c992016-02-24 12:18:05 +0100680 if (rc == -1) {
Pavol Vican7313fc02016-11-14 01:10:31 +0100681 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, type->module_name);
Radek Krejci408ab2f2016-06-06 15:27:10 +0200682 lydict_remove(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +0100683 goto error;
684
Michal Vasko01c6fd22016-05-20 11:43:05 +0200685 /* the type could not be resolved or it was resolved to an unresolved typedef or leafref */
Pavol Vican73e7c992016-02-24 12:18:05 +0100686 } else if (rc == EXIT_FAILURE) {
Michal Vasko01c6fd22016-05-20 11:43:05 +0200687 LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name);
Radek Krejci408ab2f2016-06-06 15:27:10 +0200688 lydict_remove(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +0100689 ret = EXIT_FAILURE;
690 goto error;
691 }
Radek Krejci408ab2f2016-06-06 15:27:10 +0200692 lydict_remove(module->ctx, value);
Radek Krejcic13db382016-08-16 10:52:42 +0200693
Pavol Vican7313fc02016-11-14 01:10:31 +0100694 if (type->base == LY_TYPE_ERR) {
Radek Krejcic13db382016-08-16 10:52:42 +0200695 /* resolved type in grouping, decrease the grouping's nacm number to indicate that one less
Radek Krejci9b6aad22016-09-20 15:55:51 +0200696 * unresolved item left inside the grouping, LY_TYPE_ERR used as a flag for types inside a grouping. */
Radek Krejcic13db382016-08-16 10:52:42 +0200697 for (siter = parent; siter && (siter->nodetype != LYS_GROUPING); siter = lys_parent(siter));
698 if (siter) {
699 if (!((struct lys_node_grp *)siter)->nacm) {
700 LOGINT;
701 goto error;
702 }
703 ((struct lys_node_grp *)siter)->nacm--;
704 } else {
705 LOGINT;
706 goto error;
707 }
708 }
Pavol Vicanfdab9f92016-09-07 15:23:27 +0200709
710 /* check status */
Pavol Vican7313fc02016-11-14 01:10:31 +0100711 if (lyp_check_status(type->parent->flags, type->parent->module, type->parent->name,
712 type->der->flags, type->der->module, type->der->name, parent)) {
Pavol Vicanfdab9f92016-09-07 15:23:27 +0200713 goto error;
714 }
715
Pavol Vican8bd72e42016-08-29 09:53:05 +0200716 base = typ->base;
Pavol Vican7313fc02016-11-14 01:10:31 +0100717 type->base = type->der->type.base;
Pavol Vican73e7c992016-02-24 12:18:05 +0100718 if (base == 0) {
Pavol Vican7313fc02016-11-14 01:10:31 +0100719 base = type->der->type.base;
Pavol Vican73e7c992016-02-24 12:18:05 +0100720 }
721 switch (base) {
722 case LY_TYPE_STRING:
Pavol Vican7313fc02016-11-14 01:10:31 +0100723 if (type->base == LY_TYPE_BINARY) {
724 if (type->info.str.pat_count) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100725 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Binary type could not include pattern statement.");
Pavol Vican73e7c992016-02-24 12:18:05 +0100726 goto error;
727 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100728 type->info.binary.length = type->info.str.length;
729 if (type->info.binary.length && lyp_check_length_range(type->info.binary.length->expr, type)) {
730 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, type->info.binary.length->expr, "length");
Pavol Vican73e7c992016-02-24 12:18:05 +0100731 goto error;
732 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100733 } else if (type->base == LY_TYPE_STRING) {
734 if (type->info.str.length && lyp_check_length_range(type->info.str.length->expr, type)) {
735 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, type->info.str.length->expr, "length");
Pavol Vican73e7c992016-02-24 12:18:05 +0100736 goto error;
737 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100738 } else {
Pavol Vican7313fc02016-11-14 01:10:31 +0100739 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name);
Pavol Vicanaff5c802016-02-24 15:56:45 +0100740 goto error;
Pavol Vican73e7c992016-02-24 12:18:05 +0100741 }
Pavol Vicanaff5c802016-02-24 15:56:45 +0100742 break;
743 case LY_TYPE_DEC64:
Pavol Vican7313fc02016-11-14 01:10:31 +0100744 if (type->base == LY_TYPE_DEC64) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100745 /* mandatory sub-statement(s) check */
Pavol Vican7313fc02016-11-14 01:10:31 +0100746 if (!type->info.dec64.dig && !type->der->type.der) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100747 /* decimal64 type directly derived from built-in type requires fraction-digits */
Pavol Vican0adf01d2016-03-22 12:29:33 +0100748 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type");
Pavol Vican07ea68d2016-02-25 12:01:37 +0100749 goto error;
750 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100751 if (type->info.dec64.dig && type->der->type.der) {
Pavol Vican07ea68d2016-02-25 12:01:37 +0100752 /* type is not directly derived from buit-in type and fraction-digits statement is prohibited */
Pavol Vican0adf01d2016-03-22 12:29:33 +0100753 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits");
Pavol Vican07ea68d2016-02-25 12:01:37 +0100754 goto error;
755 }
Radek Krejci4800f652016-09-08 14:02:52 +0200756
757 /* copy fraction-digits specification from parent type for easier internal use */
Pavol Vican7313fc02016-11-14 01:10:31 +0100758 if (type->der->type.der) {
759 type->info.dec64.dig = type->der->type.info.dec64.dig;
760 type->info.dec64.div = type->der->type.info.dec64.div;
Radek Krejci4800f652016-09-08 14:02:52 +0200761 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100762 if (type->info.dec64.range && lyp_check_length_range(type->info.dec64.range->expr, type)) {
763 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, type->info.dec64.range->expr, "range");
Pavol Vican3c8ee2b2016-09-29 13:18:13 +0200764 goto error;
765 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100766 } else if (type->base >= LY_TYPE_INT8 && type->base <=LY_TYPE_UINT64) {
767 if (type->info.dec64.dig) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100768 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Numerical type could not include fraction statement.");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100769 goto error;
770 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100771 type->info.num.range = type->info.dec64.range;
772 if (type->info.num.range && lyp_check_length_range(type->info.num.range->expr, type)) {
773 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, type->info.num.range->expr, "range");
Pavol Vicanaff5c802016-02-24 15:56:45 +0100774 goto error;
775 }
776 } else {
Pavol Vican7313fc02016-11-14 01:10:31 +0100777 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name);
Pavol Vicanaff5c802016-02-24 15:56:45 +0100778 goto error;
779 }
780 break;
Pavol Vican79a763d2016-02-25 15:41:27 +0100781 case LY_TYPE_ENUM:
Pavol Vican7313fc02016-11-14 01:10:31 +0100782 if (type->base != LY_TYPE_ENUM) {
783 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name);
Pavol Vican79a763d2016-02-25 15:41:27 +0100784 goto error;
785 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100786 dertype = &type->der->type;
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200787
788 if (!dertype->der) {
Pavol Vican7313fc02016-11-14 01:10:31 +0100789 if (!type->info.enums.count) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200790 /* type is derived directly from buit-in enumeartion type and enum statement is required */
791 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type");
792 goto error;
793 }
794 } else {
795 for (; !dertype->info.enums.count; dertype = &dertype->der->type);
Pavol Vican7313fc02016-11-14 01:10:31 +0100796 if (module->version < 2 && type->info.enums.count) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200797 /* type is not directly derived from built-in enumeration type and enum statement is prohibited
798 * in YANG 1.0, since YANG 1.1 enum statements can be used to restrict the base enumeration type */
799 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "enum");
800 goto error;
801 }
802
803 /* restricted enumeration type - the name MUST be used in the base type */
804 enms_sc = dertype->info.enums.enm;
Pavol Vican7313fc02016-11-14 01:10:31 +0100805 for(i = 0; i < type->info.enums.count; i++) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200806 for (j = 0; j < dertype->info.enums.count; j++) {
Pavol Vican7313fc02016-11-14 01:10:31 +0100807 if (ly_strequal(enms_sc[j].name, type->info.enums.enm[i].name, 1)) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200808 break;
809 }
810 }
811 if (j == dertype->info.enums.count) {
Pavol Vican7313fc02016-11-14 01:10:31 +0100812 LOGVAL(LYE_ENUM_INNAME, LY_VLOG_NONE, NULL, type->info.enums.enm[i].name);
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200813 goto error;
814 }
815
Pavol Vican7313fc02016-11-14 01:10:31 +0100816 if (type->info.enums.enm[i].flags & LYS_AUTOASSIGNED) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200817 /* automatically assign value from base type */
Pavol Vican7313fc02016-11-14 01:10:31 +0100818 type->info.enums.enm[i].value = enms_sc[j].value;
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200819 } else {
820 /* check that the assigned value corresponds to the original
821 * value of the enum in the base type */
Pavol Vican7313fc02016-11-14 01:10:31 +0100822 if (type->info.enums.enm[i].value != enms_sc[j].value) {
823 /* type->info.enums.enm[i].value - assigned value in restricted enum
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200824 * enms_sc[j].value - value assigned to the corresponding enum (detected above) in base type */
Pavol Vican7313fc02016-11-14 01:10:31 +0100825 LOGVAL(LYE_ENUM_INVAL, LY_VLOG_NONE, NULL, type->info.enums.enm[i].value,
826 type->info.enums.enm[i].name, enms_sc[j].value);
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200827 goto error;
828 }
829 }
830 }
Pavol Vican79a763d2016-02-25 15:41:27 +0100831 }
Pavol Vican1ff0e222016-02-26 12:27:01 +0100832 break;
Pavol Vican03a59442016-03-21 15:23:45 +0100833 case LY_TYPE_BITS:
Pavol Vican7313fc02016-11-14 01:10:31 +0100834 if (type->base != LY_TYPE_BITS) {
835 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name);
Pavol Vican03a59442016-03-21 15:23:45 +0100836 goto error;
837 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100838 dertype = &type->der->type;
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200839
840 if (!dertype->der) {
Pavol Vican7313fc02016-11-14 01:10:31 +0100841 if (!type->info.bits.count) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200842 /* type is derived directly from buit-in bits type and bit statement is required */
843 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type");
844 goto error;
845 }
846 } else {
847 for (; !dertype->info.enums.count; dertype = &dertype->der->type);
Pavol Vican7313fc02016-11-14 01:10:31 +0100848 if (module->version < 2 && type->info.bits.count) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200849 /* type is not directly derived from buit-in bits type and bit statement is prohibited,
850 * since YANG 1.1 the bit statements can be used to restrict the base bits type */
851 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "bit");
852 goto error;
853 }
854
855 bits_sc = dertype->info.bits.bit;
Pavol Vican7313fc02016-11-14 01:10:31 +0100856 for (i = 0; i < type->info.bits.count; i++) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200857 for (j = 0; j < dertype->info.bits.count; j++) {
Pavol Vican7313fc02016-11-14 01:10:31 +0100858 if (ly_strequal(bits_sc[j].name, type->info.bits.bit[i].name, 1)) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200859 break;
860 }
861 }
862 if (j == dertype->info.bits.count) {
Pavol Vican7313fc02016-11-14 01:10:31 +0100863 LOGVAL(LYE_BITS_INNAME, LY_VLOG_NONE, NULL, type->info.bits.bit[i].name);
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200864 goto error;
865 }
866
867 /* restricted bits type */
Pavol Vican7313fc02016-11-14 01:10:31 +0100868 if (type->info.bits.bit[i].flags & LYS_AUTOASSIGNED) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200869 /* automatically assign position from base type */
Pavol Vican7313fc02016-11-14 01:10:31 +0100870 type->info.bits.bit[i].pos = bits_sc[j].pos;
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200871 } else {
872 /* check that the assigned position corresponds to the original
873 * position of the bit in the base type */
Pavol Vican7313fc02016-11-14 01:10:31 +0100874 if (type->info.bits.bit[i].pos != bits_sc[j].pos) {
875 /* type->info.bits.bit[i].pos - assigned position in restricted bits
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200876 * bits_sc[j].pos - position assigned to the corresponding bit (detected above) in base type */
Pavol Vican7313fc02016-11-14 01:10:31 +0100877 LOGVAL(LYE_BITS_INVAL, LY_VLOG_NONE, NULL, type->info.bits.bit[i].pos,
878 type->info.bits.bit[i].name, bits_sc[j].pos);
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200879 goto error;
880 }
881 }
882 }
Pavol Vican03a59442016-03-21 15:23:45 +0100883 }
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200884
Pavol Vican7313fc02016-11-14 01:10:31 +0100885 for (i = type->info.bits.count - 1; i > 0; i--) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200886 j = i;
887
888 /* keep them ordered by position */
Pavol Vican7313fc02016-11-14 01:10:31 +0100889 while (j && type->info.bits.bit[j - 1].pos > type->info.bits.bit[j].pos) {
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200890 /* switch them */
Pavol Vican7313fc02016-11-14 01:10:31 +0100891 memcpy(&bit_tmp, &type->info.bits.bit[j], sizeof bit_tmp);
892 memcpy(&type->info.bits.bit[j], &type->info.bits.bit[j - 1], sizeof bit_tmp);
893 memcpy(&type->info.bits.bit[j - 1], &bit_tmp, sizeof bit_tmp);
Pavol Vican8ad2e0d2016-08-09 12:56:19 +0200894 j--;
895 }
Pavol Vican03a59442016-03-21 15:23:45 +0100896 }
897 break;
Pavol Vican1ff0e222016-02-26 12:27:01 +0100898 case LY_TYPE_LEAFREF:
Pavol Vican7313fc02016-11-14 01:10:31 +0100899 if (type->base == LY_TYPE_INST) {
900 if (type->info.lref.path) {
Pavol Vican81344ac2016-09-02 14:23:06 +0200901 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "path");
Pavol Vican1ff0e222016-02-26 12:27:01 +0100902 goto error;
903 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100904 if ((req = type->info.lref.req)) {
905 type->info.inst.req = req;
Pavol Vican81344ac2016-09-02 14:23:06 +0200906 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100907 } else if (type->base == LY_TYPE_LEAFREF) {
Pavol Vican92626d72016-09-21 09:36:09 +0200908 /* require-instance only YANG 1.1 */
Pavol Vican7313fc02016-11-14 01:10:31 +0100909 if (type->info.lref.req && (module->version < 2)) {
Pavol Vican92626d72016-09-21 09:36:09 +0200910 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "require-instance");
911 goto error;
912 }
Radek Krejci3a5501d2016-07-18 22:03:34 +0200913 /* flag resolving for later use */
914 if (!tpdftype) {
915 for (siter = parent; siter && siter->nodetype != LYS_GROUPING; siter = lys_parent(siter));
916 if (siter) {
917 /* just a flag - do not resolve */
918 tpdftype = 1;
919 }
920 }
921
Pavol Vican7313fc02016-11-14 01:10:31 +0100922 if (type->info.lref.path) {
923 if (type->der->type.der) {
Pavol Vican894ee0f2016-08-30 15:29:46 +0200924 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "path");
925 goto error;
926 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100927 value = type->info.lref.path;
Pavol Vican191613a2016-02-26 16:21:32 +0100928 /* store in the JSON format */
Pavol Vican7313fc02016-11-14 01:10:31 +0100929 type->info.lref.path = transform_schema2json(module, value);
Pavol Vican191613a2016-02-26 16:21:32 +0100930 lydict_remove(module->ctx, value);
Pavol Vican7313fc02016-11-14 01:10:31 +0100931 if (!type->info.lref.path) {
Pavol Vican191613a2016-02-26 16:21:32 +0100932 goto error;
933 }
Radek Krejci3a5501d2016-07-18 22:03:34 +0200934 /* try to resolve leafref path only when this is instantiated
935 * leaf, so it is not:
936 * - typedef's type,
937 * - in grouping definition,
938 * - just instantiated in a grouping definition,
939 * because in those cases the nodes referenced in path might not be present
940 * and it is not a bug. */
Pavol Vican7313fc02016-11-14 01:10:31 +0100941 if (!tpdftype && unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vican191613a2016-02-26 16:21:32 +0100942 goto error;
943 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100944 } else if (!type->der->type.der) {
Pavol Vican0adf01d2016-03-22 12:29:33 +0100945 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type");
Pavol Vican191613a2016-02-26 16:21:32 +0100946 goto error;
Michal Vasko01c6fd22016-05-20 11:43:05 +0200947 } else {
Radek Krejci3a5501d2016-07-18 22:03:34 +0200948 /* copy leafref definition into the derived type */
Pavol Vican7313fc02016-11-14 01:10:31 +0100949 type->info.lref.path = lydict_insert(module->ctx, type->der->type.info.lref.path, 0);
Radek Krejci3a5501d2016-07-18 22:03:34 +0200950 /* and resolve the path at the place we are (if not in grouping/typedef) */
Pavol Vican7313fc02016-11-14 01:10:31 +0100951 if (!tpdftype && unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent) == -1) {
Michal Vasko01c6fd22016-05-20 11:43:05 +0200952 goto error;
953 }
Radek Krejci742be352016-07-17 12:18:54 +0200954
Radek Krejci3a5501d2016-07-18 22:03:34 +0200955 /* add pointer to leafref target, only on leaves (not in typedefs) */
Pavol Vican7313fc02016-11-14 01:10:31 +0100956 if (type->info.lref.target && lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) {
Radek Krejci3a5501d2016-07-18 22:03:34 +0200957 goto error;
Radek Krejci742be352016-07-17 12:18:54 +0200958 }
Pavol Vican191613a2016-02-26 16:21:32 +0100959 }
Pavol Vican1ff0e222016-02-26 12:27:01 +0100960 } else {
Pavol Vican7313fc02016-11-14 01:10:31 +0100961 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name);
Pavol Vican1ff0e222016-02-26 12:27:01 +0100962 goto error;
963 }
964 break;
965 case LY_TYPE_IDENT:
Pavol Vican7313fc02016-11-14 01:10:31 +0100966 if (type->base != LY_TYPE_IDENT) {
967 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name);
Pavol Vican1ff0e222016-02-26 12:27:01 +0100968 goto error;
969 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100970 if (type->der->type.der) {
971 if (type->info.ident.ref) {
Pavol Vican07f220f2016-09-02 13:04:37 +0200972 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "base");
973 goto error;
974 }
975 } else {
Pavol Vican7313fc02016-11-14 01:10:31 +0100976 if (!type->info.ident.ref) {
Pavol Vican07f220f2016-09-02 13:04:37 +0200977 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type");
978 goto error;
979 }
980 }
Pavol Vican1ff0e222016-02-26 12:27:01 +0100981 break;
Pavol Vicana4f045a2016-02-29 15:01:20 +0100982 case LY_TYPE_UNION:
Pavol Vican7313fc02016-11-14 01:10:31 +0100983 if (type->base != LY_TYPE_UNION) {
984 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name);
Pavol Vicana4f045a2016-02-29 15:01:20 +0100985 goto error;
986 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100987 if (!type->info.uni.types) {
988 if (type->der->type.der) {
Pavol Vicana4f045a2016-02-29 15:01:20 +0100989 /* this is just a derived type with no additional type specified/required */
990 break;
991 }
Pavol Vican0adf01d2016-03-22 12:29:33 +0100992 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type");
Pavol Vicana4f045a2016-02-29 15:01:20 +0100993 goto error;
994 }
Pavol Vican7313fc02016-11-14 01:10:31 +0100995 for (i = 0; i < type->info.uni.count; i++) {
996 type->info.uni.types[i].parent = type->parent;
997 if (unres_schema_add_node(module, unres, &type->info.uni.types[i],
Michal Vasko5d631402016-07-21 13:15:15 +0200998 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Pavol Vicana4f045a2016-02-29 15:01:20 +0100999 goto error;
1000 }
Pavol Vican8e00e7a2016-09-20 11:07:48 +02001001 if (module->version < 2) {
Pavol Vican7313fc02016-11-14 01:10:31 +01001002 if (type->info.uni.types[i].base == LY_TYPE_EMPTY) {
Pavol Vican8e00e7a2016-09-20 11:07:48 +02001003 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", typ->name);
1004 goto error;
Pavol Vican7313fc02016-11-14 01:10:31 +01001005 } else if (type->info.uni.types[i].base == LY_TYPE_LEAFREF) {
Pavol Vican8e00e7a2016-09-20 11:07:48 +02001006 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", typ->name);
1007 goto error;
1008 }
Pavol Vicana4f045a2016-02-29 15:01:20 +01001009 }
1010 }
1011 break;
Pavol Vicana1827962016-02-29 15:39:42 +01001012
1013 default:
1014 if (base >= LY_TYPE_BINARY && base <= LY_TYPE_UINT64) {
Pavol Vican7313fc02016-11-14 01:10:31 +01001015 if (type->base != base) {
1016 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid restriction in type \"%s\".", type->parent->name);
Pavol Vicana1827962016-02-29 15:39:42 +01001017 goto error;
1018 }
1019 } else {
1020 LOGINT;
1021 goto error;
1022 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001023 }
1024 return EXIT_SUCCESS;
1025
1026error:
Pavol Vican7313fc02016-11-14 01:10:31 +01001027 if (type->module_name) {
1028 lydict_remove(module->ctx, type->module_name);
1029 type->module_name = NULL;
Pavol Vican73e7c992016-02-24 12:18:05 +01001030 }
Pavol Vican8bd72e42016-08-29 09:53:05 +02001031 if (base) {
Pavol Vican7313fc02016-11-14 01:10:31 +01001032 type->base = base;
Pavol Vican8bd72e42016-08-29 09:53:05 +02001033 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001034 return ret;
1035}
1036
1037void *
Pavol Vican5f0316a2016-04-05 21:21:11 +02001038yang_read_type(struct lys_module *module, void *parent, char *value, enum yytokentype type)
Pavol Vican73e7c992016-02-24 12:18:05 +01001039{
1040 struct yang_type *typ;
Pavol Vican4766aca2016-03-07 12:42:36 +01001041 struct type_deviation *dev;
1042 struct lys_tpdf *tmp_parent;
Pavol Vican73e7c992016-02-24 12:18:05 +01001043
Pavol Vicand01d8ae2016-03-01 10:45:59 +01001044 typ = calloc(1, sizeof *typ);
1045 if (!typ) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001046 LOGMEM;
1047 return NULL;
1048 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001049
1050 typ->flags = LY_YANG_STRUCTURE_FLAG;
1051 switch (type) {
1052 case LEAF_KEYWORD:
Pavol Vican85427f02016-09-26 15:21:05 +02001053 if (((struct lys_node_leaf *)parent)->type.der) {
1054 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, parent, "type", "leaf");
1055 goto error;
1056 }
Pavol Vican73e7c992016-02-24 12:18:05 +01001057 ((struct lys_node_leaf *)parent)->type.der = (struct lys_tpdf *)typ;
1058 ((struct lys_node_leaf *)parent)->type.parent = (struct lys_tpdf *)parent;
1059 typ->type = &((struct lys_node_leaf *)parent)->type;
1060 break;
Pavol Vicana55992a2016-03-01 13:37:17 +01001061 case LEAF_LIST_KEYWORD:
Pavol Vican85427f02016-09-26 15:21:05 +02001062 if (((struct lys_node_leaflist *)parent)->type.der) {
1063 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, parent, "type", "leaf-list");
1064 goto error;
1065 }
Pavol Vicana55992a2016-03-01 13:37:17 +01001066 ((struct lys_node_leaflist *)parent)->type.der = (struct lys_tpdf *)typ;
1067 ((struct lys_node_leaflist *)parent)->type.parent = (struct lys_tpdf *)parent;
1068 typ->type = &((struct lys_node_leaflist *)parent)->type;
1069 break;
Pavol Vicana4f045a2016-02-29 15:01:20 +01001070 case UNION_KEYWORD:
1071 ((struct lys_type *)parent)->der = (struct lys_tpdf *)typ;
1072 typ->type = (struct lys_type *)parent;
1073 break;
Pavol Vican0df02b02016-03-01 10:28:50 +01001074 case TYPEDEF_KEYWORD:
Pavol Vican85427f02016-09-26 15:21:05 +02001075 if (((struct lys_tpdf *)parent)->type.der) {
1076 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "typedef");
1077 goto error;
1078 }
Pavol Vican0df02b02016-03-01 10:28:50 +01001079 ((struct lys_tpdf *)parent)->type.der = (struct lys_tpdf *)typ;
1080 typ->type = &((struct lys_tpdf *)parent)->type;
Pavol Vican4766aca2016-03-07 12:42:36 +01001081 break;
1082 case REPLACE_KEYWORD:
1083 /* deviation replace type*/
1084 dev = (struct type_deviation *)parent;
1085 if (dev->deviate->type) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001086 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "type", "deviation");
Pavol Vican4766aca2016-03-07 12:42:36 +01001087 goto error;
1088 }
1089 /* check target node type */
1090 if (dev->target->nodetype == LYS_LEAF) {
1091 typ->type = &((struct lys_node_leaf *)dev->target)->type;
1092 } else if (dev->target->nodetype == LYS_LEAFLIST) {
1093 typ->type = &((struct lys_node_leaflist *)dev->target)->type;
1094 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001095 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "type");
1096 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"type\" property.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001097 goto error;
1098 }
1099
1100 /* remove type and initialize it */
1101 lys_type_free(module->ctx, typ->type);
1102 tmp_parent = typ->type->parent;
1103 memset(typ->type, 0, sizeof *typ->type);
1104 typ->type->parent = tmp_parent;
1105
1106 /* replace it with the value specified in deviation */
1107 /* HACK for unres */
1108 typ->type->der = (struct lys_tpdf *)typ;
1109 dev->deviate->type = typ->type;
1110 break;
Pavol Vican5f0316a2016-04-05 21:21:11 +02001111 default:
1112 goto error;
1113 break;
Pavol Vican73e7c992016-02-24 12:18:05 +01001114 }
Pavol Vican5f0316a2016-04-05 21:21:11 +02001115 typ->name = lydict_insert_zc(module->ctx, value);
Pavol Vican73e7c992016-02-24 12:18:05 +01001116 return typ;
Pavol Vican4766aca2016-03-07 12:42:36 +01001117
1118error:
Pavol Vican5f0316a2016-04-05 21:21:11 +02001119 free(value);
Pavol Vican4766aca2016-03-07 12:42:36 +01001120 free(typ);
1121 return NULL;
Pavol Vican73e7c992016-02-24 12:18:05 +01001122}
1123
Pavol Vicanbe9e3832016-04-28 02:21:37 +02001124void
1125yang_delete_type(struct lys_module *module, struct yang_type *stype)
1126{
1127 int i;
1128
Pavol Vican77a95e52016-08-15 09:49:26 +02001129 if (!stype) {
1130 return;
1131 }
Pavol Vicanbe9e3832016-04-28 02:21:37 +02001132 stype->type->base = stype->base;
1133 stype->type->der = NULL;
1134 lydict_remove(module->ctx, stype->name);
1135 if (stype->base == LY_TYPE_UNION) {
1136 for (i = 0; i < stype->type->info.uni.count; i++) {
1137 if (stype->type->info.uni.types[i].der) {
1138 yang_delete_type(module, (struct yang_type *)stype->type->info.uni.types[i].der);
1139 }
1140 }
1141 }
1142 free(stype);
1143}
1144
Pavol Vican73e7c992016-02-24 12:18:05 +01001145void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001146yang_read_length(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vican73e7c992016-02-24 12:18:05 +01001147{
1148 struct lys_restr **length;
1149
Pavol Vican6b072512016-04-04 10:50:21 +02001150 if (typ->base == 0 || typ->base == LY_TYPE_STRING) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001151 length = &typ->type->info.str.length;
Pavol Vican6b072512016-04-04 10:50:21 +02001152 typ->base = LY_TYPE_STRING;
1153 } else if (typ->base == LY_TYPE_BINARY) {
Pavol Vican73e7c992016-02-24 12:18:05 +01001154 length = &typ->type->info.binary.length;
1155 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001156 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected length statement.");
Pavol Vican73e7c992016-02-24 12:18:05 +01001157 goto error;
1158 }
1159
1160 if (*length) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001161 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "length", "type");
Pavol Vicanbe9e3832016-04-28 02:21:37 +02001162 goto error;
Pavol Vican73e7c992016-02-24 12:18:05 +01001163 }
1164 *length = calloc(1, sizeof **length);
1165 if (!*length) {
1166 LOGMEM;
1167 goto error;
1168 }
1169 (*length)->expr = lydict_insert_zc(module->ctx, value);
1170 return *length;
1171
1172error:
1173 free(value);
1174 return NULL;
1175
1176}
Pavol Vican1c203db2016-02-24 14:05:23 +01001177
Pavol Vican6eecf302016-08-10 11:09:05 +02001178int
1179yang_read_pattern(struct lys_module *module, struct lys_restr *pattern, char *value, char modifier)
Pavol Vican1c203db2016-02-24 14:05:23 +01001180{
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001181 char *buf;
1182 size_t len;
1183
Michal Vasko0aee5c12016-06-17 14:27:26 +02001184 if (lyp_check_pattern(value, NULL)) {
Pavol Vican1c203db2016-02-24 14:05:23 +01001185 free(value);
Pavol Vican6eecf302016-08-10 11:09:05 +02001186 return EXIT_FAILURE;
Pavol Vican1c203db2016-02-24 14:05:23 +01001187 }
Pavol Vican1c203db2016-02-24 14:05:23 +01001188
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001189 len = strlen(value);
1190 buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */
Pavol Vican6eecf302016-08-10 11:09:05 +02001191
1192 if (!buf) {
1193 LOGMEM;
1194 free(value);
1195 return EXIT_FAILURE;
1196 }
1197
1198 buf[0] = modifier;
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001199 strcpy(&buf[1], value);
1200 free(value);
1201
Pavol Vican6eecf302016-08-10 11:09:05 +02001202 pattern->expr = lydict_insert_zc(module->ctx, buf);
1203 return EXIT_SUCCESS;
Pavol Vican1c203db2016-02-24 14:05:23 +01001204}
Pavol Vicanaff5c802016-02-24 15:56:45 +01001205
1206void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001207yang_read_range(struct lys_module *module, struct yang_type *typ, char *value)
Pavol Vicanaff5c802016-02-24 15:56:45 +01001208{
Pavol Vican6b072512016-04-04 10:50:21 +02001209 if (typ->base != 0 && typ->base != LY_TYPE_DEC64) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001210 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected range statement.");
Pavol Vicanaff5c802016-02-24 15:56:45 +01001211 goto error;
1212 }
Pavol Vican6b072512016-04-04 10:50:21 +02001213 typ->base = LY_TYPE_DEC64;
Pavol Vicanaff5c802016-02-24 15:56:45 +01001214 if (typ->type->info.dec64.range) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001215 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "range", "type");
Pavol Vicanaff5c802016-02-24 15:56:45 +01001216 goto error;
1217 }
1218 typ->type->info.dec64.range = calloc(1, sizeof *typ->type->info.dec64.range);
1219 if (!typ->type->info.dec64.range) {
1220 LOGMEM;
1221 goto error;
1222 }
1223 typ->type->info.dec64.range->expr = lydict_insert_zc(module->ctx, value);
1224 return typ->type->info.dec64.range;
1225
1226error:
1227 free(value);
1228 return NULL;
1229}
Pavol Vican07ea68d2016-02-25 12:01:37 +01001230
1231int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001232yang_read_fraction(struct yang_type *typ, uint32_t value)
Pavol Vican07ea68d2016-02-25 12:01:37 +01001233{
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001234 unsigned int i;
1235
Pavol Vican6b072512016-04-04 10:50:21 +02001236 if (typ->base == 0 || typ->base == LY_TYPE_DEC64) {
1237 typ->base = LY_TYPE_DEC64;
Pavol Vican07ea68d2016-02-25 12:01:37 +01001238 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001239 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Unexpected fraction-digits statement.");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001240 goto error;
1241 }
1242 if (typ->type->info.dec64.dig) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001243 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "fraction-digits", "type");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001244 goto error;
1245 }
1246 /* range check */
1247 if (value < 1 || value > 18) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001248 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"%s\".", value, "fraction-digits");
Pavol Vican07ea68d2016-02-25 12:01:37 +01001249 goto error;
1250 }
1251 typ->type->info.dec64.dig = value;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001252 typ->type->info.dec64.div = 10;
1253 for (i = 1; i < value; i++) {
1254 typ->type->info.dec64.div *= 10;
1255 }
Pavol Vican07ea68d2016-02-25 12:01:37 +01001256 return EXIT_SUCCESS;
1257
1258error:
1259 return EXIT_FAILURE;
1260}
Pavol Vican79a763d2016-02-25 15:41:27 +01001261
Pavol Vican874715f2016-10-25 14:52:08 +02001262int
1263yang_read_enum(struct lys_module *module, struct yang_type *typ, struct lys_type_enum *enm, char *value)
Pavol Vican79a763d2016-02-25 15:41:27 +01001264{
Pavol Vican874715f2016-10-25 14:52:08 +02001265 int i, j;
Pavol Vican79a763d2016-02-25 15:41:27 +01001266
Pavol Vican5c8a9ad2016-11-22 11:50:54 +01001267 typ->base = LY_TYPE_ENUM;
Pavol Vicanc6662412016-08-30 08:06:28 +02001268 if (!value[0]) {
1269 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum name");
1270 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Enum name must not be empty.");
1271 free(value);
1272 goto error;
1273 }
1274
Pavol Vican79a763d2016-02-25 15:41:27 +01001275 enm->name = lydict_insert_zc(module->ctx, value);
1276
1277 /* the assigned name MUST NOT have any leading or trailing whitespace characters */
1278 if (isspace(enm->name[0]) || isspace(enm->name[strlen(enm->name) - 1])) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001279 LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, enm->name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001280 goto error;
1281 }
1282
Pavol Vican874715f2016-10-25 14:52:08 +02001283 j = typ->type->info.enums.count - 1;
Pavol Vican79a763d2016-02-25 15:41:27 +01001284 /* check the name uniqueness */
Pavol Vican874715f2016-10-25 14:52:08 +02001285 for (i = 0; i < j; i++) {
1286 if (!strcmp(typ->type->info.enums.enm[i].name, typ->type->info.enums.enm[j].name)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001287 LOGVAL(LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, typ->type->info.enums.enm[i].name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001288 goto error;
1289 }
1290 }
1291
Pavol Vican874715f2016-10-25 14:52:08 +02001292 return EXIT_SUCCESS;
Pavol Vican79a763d2016-02-25 15:41:27 +01001293
1294error:
Pavol Vican874715f2016-10-25 14:52:08 +02001295 return EXIT_FAILURE;
Pavol Vican79a763d2016-02-25 15:41:27 +01001296}
1297
1298int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001299yang_check_enum(struct yang_type *typ, struct lys_type_enum *enm, int64_t *value, int assign)
Pavol Vican79a763d2016-02-25 15:41:27 +01001300{
1301 int i, j;
1302
1303 if (!assign) {
1304 /* assign value automatically */
1305 if (*value > INT32_MAX) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001306 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value");
Pavol Vican79a763d2016-02-25 15:41:27 +01001307 goto error;
1308 }
1309 enm->value = *value;
1310 enm->flags |= LYS_AUTOASSIGNED;
1311 (*value)++;
Pavol Vican5de389c2016-08-30 08:55:15 +02001312 } else if (typ->type->info.enums.enm == enm) {
1313 /* change value, which is assigned automatically, if first enum has value. */
1314 *value = typ->type->info.enums.enm[0].value;
1315 (*value)++;
Pavol Vican79a763d2016-02-25 15:41:27 +01001316 }
1317
1318 /* check that the value is unique */
1319 j = typ->type->info.enums.count-1;
1320 for (i = 0; i < j; i++) {
1321 if (typ->type->info.enums.enm[i].value == typ->type->info.enums.enm[j].value) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001322 LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL,
Radek Krejcie663e012016-08-01 17:12:34 +02001323 typ->type->info.enums.enm[j].value, typ->type->info.enums.enm[j].name,
1324 typ->type->info.enums.enm[i].name);
Pavol Vican79a763d2016-02-25 15:41:27 +01001325 goto error;
1326 }
1327 }
1328
1329 return EXIT_SUCCESS;
1330
1331error:
1332 return EXIT_FAILURE;
1333}
Pavol Vican9887c682016-02-29 11:32:01 +01001334
Pavol Vican59e8dee2016-10-25 15:29:38 +02001335int
1336yang_read_bit(struct lys_module *module, struct yang_type *typ, struct lys_type_bit *bit, char *value)
Pavol Vican9887c682016-02-29 11:32:01 +01001337{
Pavol Vican59e8dee2016-10-25 15:29:38 +02001338 int i, j;
Pavol Vican9887c682016-02-29 11:32:01 +01001339
Pavol Vican59e8dee2016-10-25 15:29:38 +02001340 bit->name = lydict_insert_zc(module->ctx, value);
1341 if (lyp_check_identifier(bit->name, LY_IDENT_SIMPLE, NULL, NULL)) {
Pavol Vican9887c682016-02-29 11:32:01 +01001342 free(value);
1343 goto error;
1344 }
Pavol Vican9887c682016-02-29 11:32:01 +01001345
Pavol Vican59e8dee2016-10-25 15:29:38 +02001346 j = typ->type->info.bits.count - 1;
Pavol Vican9887c682016-02-29 11:32:01 +01001347 /* check the name uniqueness */
Pavol Vican59e8dee2016-10-25 15:29:38 +02001348 for (i = 0; i < j; i++) {
Pavol Vican9887c682016-02-29 11:32:01 +01001349 if (!strcmp(typ->type->info.bits.bit[i].name, bit->name)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001350 LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, bit->name);
Pavol Vican9887c682016-02-29 11:32:01 +01001351 goto error;
1352 }
1353 }
Pavol Vican59e8dee2016-10-25 15:29:38 +02001354 return EXIT_SUCCESS;
Pavol Vican9887c682016-02-29 11:32:01 +01001355
1356error:
Pavol Vican59e8dee2016-10-25 15:29:38 +02001357 return EXIT_FAILURE;
Pavol Vican9887c682016-02-29 11:32:01 +01001358}
1359
1360int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001361yang_check_bit(struct yang_type *typ, struct lys_type_bit *bit, int64_t *value, int assign)
Pavol Vican9887c682016-02-29 11:32:01 +01001362{
1363 int i,j;
Pavol Vican9887c682016-02-29 11:32:01 +01001364
1365 if (!assign) {
1366 /* assign value automatically */
1367 if (*value > UINT32_MAX) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001368 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position");
Pavol Vican9887c682016-02-29 11:32:01 +01001369 goto error;
1370 }
1371 bit->pos = (uint32_t)*value;
1372 bit->flags |= LYS_AUTOASSIGNED;
1373 (*value)++;
1374 }
1375
1376 j = typ->type->info.bits.count - 1;
1377 /* check that the value is unique */
1378 for (i = 0; i < j; i++) {
1379 if (typ->type->info.bits.bit[i].pos == bit->pos) {
Radek Krejcie663e012016-08-01 17:12:34 +02001380 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 +01001381 goto error;
1382 }
1383 }
1384
Pavol Vican9887c682016-02-29 11:32:01 +01001385 return EXIT_SUCCESS;
1386
1387error:
1388 return EXIT_FAILURE;
1389}
Pavol Vican0df02b02016-03-01 10:28:50 +01001390
Pavol Vican3ad50f82016-12-04 15:00:36 +01001391int
1392yang_read_augment(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *aug, char *value)
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001393{
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001394 aug->nodetype = LYS_AUGMENT;
Pavol Vican0adf01d2016-03-22 12:29:33 +01001395 aug->target_name = transform_schema2json(module, value);
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001396 free(value);
1397 if (!aug->target_name) {
Pavol Vican3ad50f82016-12-04 15:00:36 +01001398 return EXIT_FAILURE;
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001399 }
1400 aug->parent = parent;
1401 aug->module = module;
Pavol Vican3ad50f82016-12-04 15:00:36 +01001402 return EXIT_SUCCESS;
Pavol Vican92fa0dc2016-03-02 14:20:39 +01001403}
Pavol Vican220e5a12016-03-03 14:19:43 +01001404
1405void *
Pavol Vican0adf01d2016-03-22 12:29:33 +01001406yang_read_deviation(struct lys_module *module, char *value)
Pavol Vican220e5a12016-03-03 14:19:43 +01001407{
1408 struct lys_node *dev_target = NULL;
1409 struct lys_deviation *dev;
1410 struct type_deviation *deviation = NULL;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001411 int rc;
Pavol Vican220e5a12016-03-03 14:19:43 +01001412
Pavol Vican220e5a12016-03-03 14:19:43 +01001413 dev = &module->deviation[module->deviation_size];
Pavol Vican0adf01d2016-03-22 12:29:33 +01001414 dev->target_name = transform_schema2json(module, value);
Pavol Vican220e5a12016-03-03 14:19:43 +01001415 free(value);
1416 if (!dev->target_name) {
1417 goto error;
1418 }
1419
Pavol Vican974377b2016-03-23 00:38:53 +01001420 deviation = calloc(1, sizeof *deviation);
1421 if (!deviation) {
1422 LOGMEM;
1423 goto error;
1424 }
1425
Pavol Vican220e5a12016-03-03 14:19:43 +01001426 /* resolve target node */
Radek Krejcidf46e222016-11-08 11:57:37 +01001427 rc = resolve_augment_schema_nodeid(dev->target_name, NULL, module, 1, (const struct lys_node **)&dev_target);
Pavol Vican220e5a12016-03-03 14:19:43 +01001428 if (rc || !dev_target) {
Michal Vasko75c8daf2016-05-19 10:56:39 +02001429 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation");
Pavol Vican220e5a12016-03-03 14:19:43 +01001430 goto error;
1431 }
Radek Krejcic4283442016-04-22 09:19:27 +02001432 if (dev_target->module == lys_main_module(module)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001433 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, "deviation");
1434 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Deviating own module is not allowed.");
Pavol Vican220e5a12016-03-03 14:19:43 +01001435 goto error;
1436 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001437
Pavol Vican220e5a12016-03-03 14:19:43 +01001438 /*save pointer to the deviation and deviated target*/
1439 deviation->deviation = dev;
1440 deviation->target = dev_target;
1441
Pavol Vican38321d02016-08-16 14:56:02 +02001442 deviation->dflt_check = ly_set_new();
1443 if (!deviation->dflt_check) {
1444 LOGMEM;
1445 goto error;
1446 }
1447
Pavol Vican220e5a12016-03-03 14:19:43 +01001448 return deviation;
1449
1450error:
1451 free(deviation);
1452 lydict_remove(module->ctx, dev->target_name);
1453 return NULL;
1454}
Pavol Vican4c90c642016-03-03 15:06:47 +01001455
1456int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001457yang_read_deviate_unsupported(struct type_deviation *dev)
Pavol Vican4c90c642016-03-03 15:06:47 +01001458{
1459 int i;
1460
1461 if (dev->deviation->deviate_size) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001462 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
Pavol Vican4c90c642016-03-03 15:06:47 +01001463 return EXIT_FAILURE;
1464 }
1465 dev->deviation->deviate[dev->deviation->deviate_size].mod = LY_DEVIATE_NO;
1466
1467 /* you cannot remove a key leaf */
1468 if ((dev->target->nodetype == LYS_LEAF) && dev->target->parent && (dev->target->parent->nodetype == LYS_LIST)) {
1469 for (i = 0; i < ((struct lys_node_list *)dev->target->parent)->keys_size; ++i) {
1470 if (((struct lys_node_list *)dev->target->parent)->keys[i] == (struct lys_node_leaf *)dev->target) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001471 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "not-supported", "deviation");
1472 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot remove a list key.");
Pavol Vican4c90c642016-03-03 15:06:47 +01001473 return EXIT_FAILURE;
1474 }
1475 }
1476 }
Michal Vaskofe7e5a72016-05-02 14:49:23 +02001477
Pavol Vican4c90c642016-03-03 15:06:47 +01001478 /* unlink and store the original node */
Michal Vaskod921d682016-05-19 10:56:51 +02001479 lys_node_unlink(dev->target);
Pavol Vican4c90c642016-03-03 15:06:47 +01001480 dev->deviation->orig_node = dev->target;
1481
1482 dev->deviation->deviate_size = 1;
1483 return EXIT_SUCCESS;
1484}
Pavol Vican85f12022016-03-05 16:30:35 +01001485
1486int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001487yang_read_deviate(struct type_deviation *dev, LYS_DEVIATE_TYPE mod)
Pavol Vican85f12022016-03-05 16:30:35 +01001488{
1489 struct unres_schema tmp_unres;
1490
1491 dev->deviation->deviate[dev->deviation->deviate_size].mod = mod;
1492 dev->deviate = &dev->deviation->deviate[dev->deviation->deviate_size];
1493 dev->deviation->deviate_size++;
Pavol Vican33884462016-09-27 21:04:26 +02001494 dev->trg_must_size = NULL;
Pavol Vican85f12022016-03-05 16:30:35 +01001495 if (dev->deviation->deviate[0].mod == LY_DEVIATE_NO) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001496 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "not-supported");
1497 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
Pavol Vican85f12022016-03-05 16:30:35 +01001498 return EXIT_FAILURE;
1499 }
1500
1501 /* store a shallow copy of the original node */
1502 if (!dev->deviation->orig_node) {
1503 memset(&tmp_unres, 0, sizeof tmp_unres);
Michal Vaskoe022a562016-09-27 14:24:15 +02001504 dev->deviation->orig_node = lys_node_dup(dev->target->module, NULL, dev->target, 0, &tmp_unres, 1);
Pavol Vican85f12022016-03-05 16:30:35 +01001505 /* just to be safe */
1506 if (tmp_unres.count) {
1507 LOGINT;
1508 return EXIT_FAILURE;
1509 }
1510 }
1511
1512 return EXIT_SUCCESS;
1513}
1514
1515int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001516yang_read_deviate_units(struct ly_ctx *ctx, struct type_deviation *dev, char *value)
Pavol Vican85f12022016-03-05 16:30:35 +01001517{
1518 const char **stritem;
1519
1520 if (dev->deviate->units) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001521 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "units", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001522 free(value);
1523 goto error;
1524 }
1525
1526 /* check target node type */
1527 if (dev->target->nodetype == LYS_LEAFLIST) {
1528 stritem = &((struct lys_node_leaflist *)dev->target)->units;
1529 } else if (dev->target->nodetype == LYS_LEAF) {
1530 stritem = &((struct lys_node_leaf *)dev->target)->units;
1531 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001532 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1533 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"units\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001534 free(value);
1535 goto error;
1536 }
1537
1538 dev->deviate->units = lydict_insert_zc(ctx, value);
1539
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001540 if (dev->deviate->mod == LY_DEVIATE_DEL) {
1541 /* check values */
Michal Vaskob42b6972016-06-06 14:21:30 +02001542 if (!ly_strequal(*stritem, dev->deviate->units, 1)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001543 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->units, "units");
1544 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001545 goto error;
1546 }
1547 /* remove current units value of the target */
1548 lydict_remove(ctx, *stritem);
Pavol Vican4766aca2016-03-07 12:42:36 +01001549 } else {
1550 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1551 /* check that there is no current value */
1552 if (*stritem) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001553 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1554 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001555 goto error;
1556 }
1557 } else { /* replace */
1558 if (!*stritem) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001559 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "units");
1560 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001561 goto error;
1562 }
1563 }
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001564 /* remove current units value of the target ... */
1565 lydict_remove(ctx, *stritem);
1566
1567 /* ... and replace it with the value specified in deviation */
1568 *stritem = lydict_insert(ctx, dev->deviate->units, 0);
1569 }
1570
Pavol Vican85f12022016-03-05 16:30:35 +01001571 return EXIT_SUCCESS;
1572
1573error:
1574 return EXIT_FAILURE;
1575}
1576
1577int
Pavol Vican974377b2016-03-23 00:38:53 +01001578yang_read_deviate_must(struct type_deviation *dev, uint8_t c_must)
Pavol Vican85f12022016-03-05 16:30:35 +01001579{
Pavol Vican85f12022016-03-05 16:30:35 +01001580 /* check target node type */
1581 switch (dev->target->nodetype) {
1582 case LYS_LEAF:
1583 dev->trg_must = &((struct lys_node_leaf *)dev->target)->must;
1584 dev->trg_must_size = &((struct lys_node_leaf *)dev->target)->must_size;
1585 break;
1586 case LYS_CONTAINER:
1587 dev->trg_must = &((struct lys_node_container *)dev->target)->must;
1588 dev->trg_must_size = &((struct lys_node_container *)dev->target)->must_size;
1589 break;
1590 case LYS_LEAFLIST:
1591 dev->trg_must = &((struct lys_node_leaflist *)dev->target)->must;
1592 dev->trg_must_size = &((struct lys_node_leaflist *)dev->target)->must_size;
1593 break;
1594 case LYS_LIST:
1595 dev->trg_must = &((struct lys_node_list *)dev->target)->must;
1596 dev->trg_must_size = &((struct lys_node_list *)dev->target)->must_size;
1597 break;
1598 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001599 case LYS_ANYDATA:
1600 dev->trg_must = &((struct lys_node_anydata *)dev->target)->must;
1601 dev->trg_must_size = &((struct lys_node_anydata *)dev->target)->must_size;
Pavol Vican85f12022016-03-05 16:30:35 +01001602 break;
1603 default:
Pavol Vican0adf01d2016-03-22 12:29:33 +01001604 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must");
1605 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001606 goto error;
1607 }
1608
Michal Vasko508a50d2016-09-07 14:50:33 +02001609 /* flag will be checked again, clear it for now */
Michal Vaskoe9914d12016-10-07 14:32:37 +02001610 dev->target->flags &= ~LYS_VALID_DEP;
Michal Vasko508a50d2016-09-07 14:50:33 +02001611
Pavol Vican85f12022016-03-05 16:30:35 +01001612 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1613 /* reallocate the must array of the target */
1614 dev->deviate->must = ly_realloc(*dev->trg_must, (c_must + *dev->trg_must_size) * sizeof *dev->deviate->must);
1615 if (!dev->deviate->must) {
1616 LOGMEM;
1617 goto error;
1618 }
1619 *dev->trg_must = dev->deviate->must;
1620 dev->deviate->must = &((*dev->trg_must)[*dev->trg_must_size]);
1621 dev->deviate->must_size = c_must;
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001622 } else {
1623 /* LY_DEVIATE_DEL */
1624 dev->deviate->must = calloc(c_must, sizeof *dev->deviate->must);
1625 if (!dev->deviate->must) {
1626 LOGMEM;
1627 goto error;
1628 }
Pavol Vican85f12022016-03-05 16:30:35 +01001629 }
1630
1631 return EXIT_SUCCESS;
1632
1633error:
1634 return EXIT_FAILURE;
1635}
1636
1637int
Pavol Vican974377b2016-03-23 00:38:53 +01001638yang_read_deviate_unique(struct type_deviation *dev, uint8_t c_uniq)
Pavol Vican85f12022016-03-05 16:30:35 +01001639{
Pavol Vican85f12022016-03-05 16:30:35 +01001640 struct lys_node_list *list;
1641
1642 /* check target node type */
1643 if (dev->target->nodetype != LYS_LIST) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001644 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique");
1645 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"unique\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001646 goto error;
1647 }
1648
1649 list = (struct lys_node_list *)dev->target;
1650 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1651 /* reallocate the unique array of the target */
1652 dev->deviate->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *dev->deviate->unique);
1653 if (!dev->deviate->unique) {
1654 LOGMEM;
1655 goto error;
1656 }
1657 list->unique = dev->deviate->unique;
1658 dev->deviate->unique = &list->unique[list->unique_size];
1659 dev->deviate->unique_size = c_uniq;
1660 memset(dev->deviate->unique, 0, c_uniq * sizeof *dev->deviate->unique);
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001661 } else {
1662 /* LY_DEVIATE_DEL */
1663 dev->deviate->unique = calloc(c_uniq, sizeof *dev->deviate->unique);
1664 if (!dev->deviate->unique) {
1665 LOGMEM;
1666 goto error;
1667 }
Pavol Vican85f12022016-03-05 16:30:35 +01001668 }
1669
1670 return EXIT_SUCCESS;
1671
1672error:
1673 return EXIT_FAILURE;
1674}
1675
1676int
Pavol Vican38321d02016-08-16 14:56:02 +02001677yang_read_deviate_default(struct lys_module *module, struct type_deviation *dev, uint8_t c_dflt)
Pavol Vican85f12022016-03-05 16:30:35 +01001678{
Pavol Vican38321d02016-08-16 14:56:02 +02001679 int i;
1680 struct lys_node_leaflist *llist;
Pavol Vican85f12022016-03-05 16:30:35 +01001681
Pavol Vican38321d02016-08-16 14:56:02 +02001682 /* check target node type */
1683 if (module->version < 2 && dev->target->nodetype == LYS_LEAFLIST) {
1684 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1685 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
1686 goto error;
1687 } else if (c_dflt > 1 && dev->target->nodetype != LYS_LEAFLIST) { /* from YANG 1.1 */
1688 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1689 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow multiple \"default\" properties.");
1690 goto error;
1691 } else if (!(dev->target->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
1692 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1693 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001694 goto error;
1695 }
1696
Pavol Vican38321d02016-08-16 14:56:02 +02001697 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1698 /* check that there is no current value */
1699 if ((dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) ||
1700 (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt)) {
1701 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1702 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican85f12022016-03-05 16:30:35 +01001703 goto error;
1704 }
Pavol Vican4766aca2016-03-07 12:42:36 +01001705
Pavol Vican38321d02016-08-16 14:56:02 +02001706 /* check collision with mandatory/min-elements */
1707 if ((dev->target->flags & LYS_MAND_TRUE) ||
1708 (dev->target->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)dev->target)->min)) {
1709 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "deviation");
1710 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
1711 "Adding the \"default\" statement is forbidden on %s statement.",
1712 (dev->target->flags & LYS_MAND_TRUE) ? "nodes with the \"mandatory\"" : "leaflists with non-zero \"min-elements\"");
1713 goto error;
Pavol Vican85f12022016-03-05 16:30:35 +01001714 }
Pavol Vican38321d02016-08-16 14:56:02 +02001715 } else if (dev->deviate->mod == LY_DEVIATE_RPL) {
1716 /* check that there was a value before */
1717 if (((dev->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && !((struct lys_node_leaf *)dev->target)->dflt) ||
1718 (dev->target->nodetype == LYS_CHOICE && !((struct lys_node_choice *)dev->target)->dflt)) {
1719 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1720 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
1721 goto error;
Pavol Vicanc1f5a502016-03-06 16:51:26 +01001722 }
Pavol Vican38321d02016-08-16 14:56:02 +02001723 }
1724
1725 if (dev->target->nodetype == LYS_LEAFLIST) {
1726 /* reallocate default list in the target */
1727 llist = (struct lys_node_leaflist *)dev->target;
1728 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1729 /* reallocate (enlarge) the unique array of the target */
1730 llist->dflt = ly_realloc(llist->dflt, (c_dflt + llist->dflt_size) * sizeof *dev->deviate->dflt);
1731 } else if (dev->deviate->mod == LY_DEVIATE_RPL) {
1732 /* reallocate (replace) the unique array of the target */
1733 for (i = 0; i < llist->dflt_size; i++) {
1734 lydict_remove(llist->module->ctx, llist->dflt[i]);
1735 }
1736 llist->dflt = ly_realloc(llist->dflt, c_dflt * sizeof *dev->deviate->dflt);
1737 llist->dflt_size = 0;
1738 }
1739 }
1740
1741 dev->deviate->dflt = calloc(c_dflt, sizeof *dev->deviate->dflt);
1742 if (!dev->deviate->dflt) {
1743 LOGMEM;
Pavol Vican85f12022016-03-05 16:30:35 +01001744 goto error;
1745 }
1746
1747 return EXIT_SUCCESS;
1748
1749error:
1750 return EXIT_FAILURE;
1751}
1752
1753int
Pavol Vican38321d02016-08-16 14:56:02 +02001754yang_fill_deviate_default(struct ly_ctx *ctx, struct type_deviation *dev, char *exp)
1755{
1756 struct lys_node *node;
1757 struct lys_node_choice *choice;
1758 struct lys_node_leaf *leaf;
1759 struct lys_node_leaflist *llist;
1760 int rc, i;
1761 unsigned int u;
1762 const char *value;
1763
1764 value = lydict_insert_zc(ctx, exp);
1765 u = strlen(value);
1766 dev->deviate->dflt[dev->deviate->dflt_size++] = value;
1767
1768 if (dev->target->nodetype == LYS_CHOICE) {
1769 choice = (struct lys_node_choice *)dev->target;
1770 rc = resolve_choice_default_schema_nodeid(value, choice->child, (const struct lys_node **)&node);
1771 if (rc || !node) {
1772 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
1773 goto error;
1774 }
1775 if (dev->deviate->mod == LY_DEVIATE_DEL) {
1776 if (!choice->dflt || (choice->dflt != node)) {
1777 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
1778 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
1779 goto error;
1780 }
1781 } else { /* add or replace */
1782 choice->dflt = node;
1783 if (!choice->dflt) {
1784 /* default branch not found */
1785 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
1786 goto error;
1787 }
1788 }
1789 } else if (dev->target->nodetype == LYS_LEAF) {
1790 leaf = (struct lys_node_leaf *)dev->target;
1791 if (dev->deviate->mod == LY_DEVIATE_DEL) {
1792 if (!leaf->dflt || !ly_strequal(leaf->dflt, value, 1)) {
1793 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
1794 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
1795 goto error;
1796 }
1797 /* remove value */
1798 lydict_remove(ctx, leaf->dflt);
1799 leaf->dflt = NULL;
Radek Krejcibd117f02016-11-04 16:28:08 +01001800 leaf->flags &= ~LYS_DFLTJSON;
Pavol Vican38321d02016-08-16 14:56:02 +02001801 } else { /* add (already checked) and replace */
1802 /* remove value */
1803 lydict_remove(ctx, leaf->dflt);
Radek Krejcibd117f02016-11-04 16:28:08 +01001804 leaf->flags &= ~LYS_DFLTJSON;
Pavol Vican38321d02016-08-16 14:56:02 +02001805
1806 /* set new value */
1807 leaf->dflt = lydict_insert(ctx, value, u);
1808
1809 /* remember to check it later (it may not fit now, but the type can be deviated too) */
1810 ly_set_add(dev->dflt_check, dev->target, 0);
1811 }
1812 } else { /* LYS_LEAFLIST */
1813 llist = (struct lys_node_leaflist *)dev->target;
1814 if (dev->deviate->mod == LY_DEVIATE_DEL) {
1815 /* find and remove the value in target list */
1816 for (i = 0; i < llist->dflt_size; i++) {
1817 if (llist->dflt[i] && ly_strequal(llist->dflt[i], value, 1)) {
1818 /* match, remove the value */
1819 lydict_remove(llist->module->ctx, llist->dflt[i]);
1820 llist->dflt[i] = NULL;
1821 break;
1822 }
1823 }
1824 if (i == llist->dflt_size) {
1825 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
1826 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The default value to delete not found in the target node.");
1827 goto error;
1828 }
1829 } else {
1830 /* add or replace, anyway we place items into the deviate's list
1831 which propagates to the target */
1832 /* we just want to check that the value isn't already in the list */
1833 for (i = 0; i < llist->dflt_size; i++) {
1834 if (ly_strequal(llist->dflt[i], value, 1)) {
1835 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
1836 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
1837 goto error;
1838 }
1839 }
1840 /* store it in target node */
1841 llist->dflt[llist->dflt_size++] = lydict_insert(ctx, value, u);
1842
1843 /* remember to check it later (it may not fit now, but the type can be deviated too) */
1844 ly_set_add(dev->dflt_check, dev->target, 0);
Radek Krejcibd117f02016-11-04 16:28:08 +01001845 llist->flags &= ~LYS_DFLTJSON;
Pavol Vican38321d02016-08-16 14:56:02 +02001846 }
1847 }
1848
1849 return EXIT_SUCCESS;
1850error:
1851 return EXIT_FAILURE;
1852}
1853
1854
1855int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001856yang_read_deviate_config(struct type_deviation *dev, uint8_t value)
Pavol Vican85f12022016-03-05 16:30:35 +01001857{
1858 if (dev->deviate->flags & LYS_CONFIG_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001859 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "config", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001860 goto error;
1861 }
1862
1863 /* for we deviate from RFC 6020 and allow config property even it is/is not
1864 * specified in the target explicitly since config property inherits. So we expect
1865 * that config is specified in every node. But for delete, we check that the value
1866 * is the same as here in deviation
1867 */
1868 dev->deviate->flags |= value;
1869
1870 /* add and replace are the same in this case */
1871 /* remove current config value of the target ... */
1872 dev->target->flags &= ~LYS_CONFIG_MASK;
1873
1874 /* ... and replace it with the value specified in deviation */
1875 dev->target->flags |= dev->deviate->flags & LYS_CONFIG_MASK;
1876
1877 return EXIT_SUCCESS;
1878
1879error:
1880 return EXIT_FAILURE;
1881}
1882
1883int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001884yang_read_deviate_mandatory(struct type_deviation *dev, uint8_t value)
Pavol Vican85f12022016-03-05 16:30:35 +01001885{
Radek Krejcie00d2312016-08-12 15:27:49 +02001886 struct lys_node *parent;
1887
Pavol Vican85f12022016-03-05 16:30:35 +01001888 if (dev->deviate->flags & LYS_MAND_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001889 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "mandatory", "deviate");
Pavol Vican85f12022016-03-05 16:30:35 +01001890 goto error;
1891 }
1892
1893 /* check target node type */
Radek Krejcibf2abff2016-08-23 15:51:52 +02001894 if (!(dev->target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001895 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
1896 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"mandatory\" property.");
Pavol Vican85f12022016-03-05 16:30:35 +01001897 goto error;
1898 }
1899
1900 dev->deviate->flags |= value;
1901
1902 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1903 /* check that there is no current value */
1904 if (dev->target->flags & LYS_MAND_MASK) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001905 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
1906 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican85f12022016-03-05 16:30:35 +01001907 goto error;
Pavol Vican321a02e2016-04-05 16:11:59 +02001908 } else {
1909 if (dev->target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev->target)->dflt) {
1910 /* RFC 6020, 7.6.4 - default statement must not with mandatory true */
1911 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "leaf");
1912 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on leaf with \"default\".");
1913 goto error;
1914 } else if (dev->target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev->target)->dflt) {
1915 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "choice");
1916 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"mandatory\" statement is forbidden on choices with \"default\".");
1917 goto error;
1918 }
Pavol Vican85f12022016-03-05 16:30:35 +01001919 }
Pavol Vican4766aca2016-03-07 12:42:36 +01001920 } else { /* replace */
1921 if (!(dev->target->flags & LYS_MAND_MASK)) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001922 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "mandatory");
1923 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Pavol Vican4766aca2016-03-07 12:42:36 +01001924 goto error;
1925 }
Pavol Vican85f12022016-03-05 16:30:35 +01001926 }
1927
Pavol Vican85f12022016-03-05 16:30:35 +01001928 /* remove current mandatory value of the target ... */
1929 dev->target->flags &= ~LYS_MAND_MASK;
1930
1931 /* ... and replace it with the value specified in deviation */
1932 dev->target->flags |= dev->deviate->flags & LYS_MAND_MASK;
1933
Radek Krejcie00d2312016-08-12 15:27:49 +02001934 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
1935 for (parent = dev->target->parent;
1936 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION));
1937 parent = parent->parent) {
1938 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
1939 /* stop also on presence containers */
1940 break;
1941 }
1942 }
1943 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
1944 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
1945 if (lyp_check_mandatory_choice(parent)) {
1946 goto error;
1947 }
1948 }
1949
Pavol Vican85f12022016-03-05 16:30:35 +01001950 return EXIT_SUCCESS;
1951
1952error:
1953 return EXIT_FAILURE;
1954}
1955
1956int
Pavol Vican0adf01d2016-03-22 12:29:33 +01001957yang_read_deviate_minmax(struct type_deviation *dev, uint32_t value, int type)
Pavol Vican85f12022016-03-05 16:30:35 +01001958{
Pavol Vican09adcc32016-08-25 10:51:36 +02001959 uint32_t *ui32val, *min, *max;
Pavol Vican85f12022016-03-05 16:30:35 +01001960
1961 /* check target node type */
1962 if (dev->target->nodetype == LYS_LEAFLIST) {
Pavol Vican09adcc32016-08-25 10:51:36 +02001963 max = &((struct lys_node_leaflist *)dev->target)->max;
1964 min = &((struct lys_node_leaflist *)dev->target)->min;
Pavol Vican85f12022016-03-05 16:30:35 +01001965 } else if (dev->target->nodetype == LYS_LIST) {
Pavol Vican09adcc32016-08-25 10:51:36 +02001966 max = &((struct lys_node_list *)dev->target)->max;
1967 min = &((struct lys_node_list *)dev->target)->min;
Pavol Vican85f12022016-03-05 16:30:35 +01001968 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001969 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements");
1970 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 +01001971 goto error;
1972 }
1973
1974 if (type) {
1975 dev->deviate->max = value;
Pavol Vican4766aca2016-03-07 12:42:36 +01001976 dev->deviate->max_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02001977 ui32val = max;
Pavol Vican85f12022016-03-05 16:30:35 +01001978 } else {
1979 dev->deviate->min = value;
Pavol Vican4766aca2016-03-07 12:42:36 +01001980 dev->deviate->min_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02001981 ui32val = min;
Pavol Vican85f12022016-03-05 16:30:35 +01001982 }
1983
1984 if (dev->deviate->mod == LY_DEVIATE_ADD) {
1985 /* check that there is no current value */
1986 if (*ui32val) {
Pavol Vican0adf01d2016-03-22 12:29:33 +01001987 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, (type) ? "max-elements" : "min-elements");
1988 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Pavol Vican85f12022016-03-05 16:30:35 +01001989 goto error;
1990 }
Pavol Vican4766aca2016-03-07 12:42:36 +01001991 } else if (dev->deviate->mod == LY_DEVIATE_RPL) {
1992 /* unfortunately, there is no way to check reliably that there
1993 * was a value before, it could have been the default */
Pavol Vican85f12022016-03-05 16:30:35 +01001994 }
1995
1996 /* add (already checked) and replace */
1997 /* set new value specified in deviation */
1998 *ui32val = value;
1999
Pavol Vican09adcc32016-08-25 10:51:36 +02002000 /* check min-elements is smaller than max-elements */
2001 if (*max && *min > *max) {
2002 if (type) {
2003 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"max-elements\".", value);
2004 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\".");
2005 } else {
2006 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid value \"%d\" of \"min-elements\".", value);
2007 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
2008 }
2009 goto error;
2010 }
2011
Pavol Vican85f12022016-03-05 16:30:35 +01002012 return EXIT_SUCCESS;
2013
2014error:
2015 return EXIT_FAILURE;
2016}
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002017
2018int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002019yang_check_deviate_must(struct ly_ctx *ctx, struct type_deviation *dev)
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002020{
2021 int i;
2022
2023 /* find must to delete, we are ok with just matching conditions */
2024 for (i = 0; i < *dev->trg_must_size; i++) {
2025 if (ly_strequal(dev->deviate->must[dev->deviate->must_size - 1].expr, (*dev->trg_must)[i].expr, 1)) {
2026 /* we have a match, free the must structure ... */
2027 lys_restr_free(ctx, &((*dev->trg_must)[i]));
2028 /* ... and maintain the array */
2029 (*dev->trg_must_size)--;
2030 if (i != *dev->trg_must_size) {
2031 (*dev->trg_must)[i].expr = (*dev->trg_must)[*dev->trg_must_size].expr;
2032 (*dev->trg_must)[i].dsc = (*dev->trg_must)[*dev->trg_must_size].dsc;
2033 (*dev->trg_must)[i].ref = (*dev->trg_must)[*dev->trg_must_size].ref;
2034 (*dev->trg_must)[i].eapptag = (*dev->trg_must)[*dev->trg_must_size].eapptag;
2035 (*dev->trg_must)[i].emsg = (*dev->trg_must)[*dev->trg_must_size].emsg;
2036 }
2037 if (!(*dev->trg_must_size)) {
2038 free(*dev->trg_must);
2039 *dev->trg_must = NULL;
2040 } else {
2041 (*dev->trg_must)[*dev->trg_must_size].expr = NULL;
2042 (*dev->trg_must)[*dev->trg_must_size].dsc = NULL;
2043 (*dev->trg_must)[*dev->trg_must_size].ref = NULL;
2044 (*dev->trg_must)[*dev->trg_must_size].eapptag = NULL;
2045 (*dev->trg_must)[*dev->trg_must_size].emsg = NULL;
2046 }
2047
2048 i = -1; /* set match flag */
2049 break;
2050 }
2051 }
2052 if (i != -1) {
2053 /* no match found */
Pavol Vican0adf01d2016-03-22 12:29:33 +01002054 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->deviate->must[dev->deviate->must_size - 1].expr, "must");
2055 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value does not match any must from the target.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002056 return EXIT_FAILURE;
2057 }
2058
2059 return EXIT_SUCCESS;
2060}
2061
2062int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002063yang_check_deviate_unique(struct lys_module *module, struct type_deviation *dev, char *value)
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002064{
2065 struct lys_node_list *list;
2066 int i, j;
2067
2068 list = (struct lys_node_list *)dev->target;
Pavol Vican0adf01d2016-03-22 12:29:33 +01002069 if (yang_fill_unique(module, list, &dev->deviate->unique[dev->deviate->unique_size], value, NULL)) {
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002070 dev->deviate->unique_size++;
2071 goto error;
2072 }
2073
2074 /* find unique structures to delete */
2075 for (i = 0; i < list->unique_size; i++) {
2076 if (list->unique[i].expr_size != dev->deviate->unique[dev->deviate->unique_size].expr_size) {
2077 continue;
2078 }
2079
2080 for (j = 0; j < dev->deviate->unique[dev->deviate->unique_size].expr_size; j++) {
2081 if (!ly_strequal(list->unique[i].expr[j], dev->deviate->unique[dev->deviate->unique_size].expr[j], 1)) {
2082 break;
2083 }
2084 }
2085
2086 if (j == dev->deviate->unique[dev->deviate->unique_size].expr_size) {
2087 /* we have a match, free the unique structure ... */
2088 for (j = 0; j < list->unique[i].expr_size; j++) {
2089 lydict_remove(module->ctx, list->unique[i].expr[j]);
2090 }
2091 free(list->unique[i].expr);
2092 /* ... and maintain the array */
2093 list->unique_size--;
2094 if (i != list->unique_size) {
2095 list->unique[i].expr_size = list->unique[list->unique_size].expr_size;
2096 list->unique[i].expr = list->unique[list->unique_size].expr;
2097 }
2098
2099 if (!list->unique_size) {
2100 free(list->unique);
2101 list->unique = NULL;
2102 } else {
2103 list->unique[list->unique_size].expr_size = 0;
2104 list->unique[list->unique_size].expr = NULL;
2105 }
2106
2107 i = -1; /* set match flag */
2108 break;
2109 }
2110 }
2111 dev->deviate->unique_size++;
2112
2113 if (i != -1) {
2114 /* no match found */
Pavol Vican0adf01d2016-03-22 12:29:33 +01002115 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "unique");
2116 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Pavol Vicanc1f5a502016-03-06 16:51:26 +01002117 goto error;
2118 }
2119
2120 free(value);
2121 return EXIT_SUCCESS;
2122
2123error:
2124 free(value);
2125 return EXIT_FAILURE;
2126}
Pavol Vicane92421d2016-03-08 10:12:33 +01002127
2128int
Pavol Vican38321d02016-08-16 14:56:02 +02002129yang_check_deviation(struct lys_module *module, struct ly_set *dflt_check, struct unres_schema *unres)
Pavol Vicane92421d2016-03-08 10:12:33 +01002130{
2131 int i, rc;
Pavol Vican38321d02016-08-16 14:56:02 +02002132 unsigned int u;
2133 const char *value, *target_name;
2134 struct lys_node_leaflist *llist;
2135 struct lys_node_leaf *leaf;
Pavol Vicane92421d2016-03-08 10:12:33 +01002136
Pavol Vican38321d02016-08-16 14:56:02 +02002137 /* now check whether default value, if any, matches the type */
2138 for (u = 0; u < dflt_check->number; ++u) {
2139 value = NULL;
2140 rc = EXIT_SUCCESS;
2141 if (dflt_check->set.s[u]->nodetype == LYS_LEAF) {
2142 leaf = (struct lys_node_leaf *)dflt_check->set.s[u];
2143 target_name = leaf->name;
Radek Krejci51673202016-11-01 17:00:32 +01002144 value = leaf->dflt;
2145 rc = unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)(&leaf->dflt));
Pavol Vican38321d02016-08-16 14:56:02 +02002146 } else { /* LYS_LEAFLIST */
2147 llist = (struct lys_node_leaflist *)dflt_check->set.s[u];
2148 target_name = llist->name;
2149 for (i = 0; i < llist->dflt_size; i++) {
Radek Krejci51673202016-11-01 17:00:32 +01002150 rc = unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT,
2151 (struct lys_node *)(&llist->dflt[i]));
Pavol Vican38321d02016-08-16 14:56:02 +02002152 if (rc == -1) {
Radek Krejci51673202016-11-01 17:00:32 +01002153 value = llist->dflt[i];
Pavol Vicane92421d2016-03-08 10:12:33 +01002154 break;
2155 }
2156 }
2157 }
Pavol Vican38321d02016-08-16 14:56:02 +02002158 if (rc == -1) {
2159 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2160 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
2161 "The default value \"%s\" of the deviated node \"%s\"no longer matches its type.",
2162 target_name);
2163 return EXIT_FAILURE;
2164 }
Pavol Vicane92421d2016-03-08 10:12:33 +01002165 }
Pavol Vican38321d02016-08-16 14:56:02 +02002166
Pavol Vicane92421d2016-03-08 10:12:33 +01002167 return EXIT_SUCCESS;
Pavol Vican38321d02016-08-16 14:56:02 +02002168
Pavol Vican9b89dda2016-03-09 15:36:55 +01002169}
2170
Pavol Vicanec423c92016-10-24 21:33:43 +02002171static int
2172yang_fill_include(struct lys_module *trg, char *value, struct lys_include *inc,
2173 struct unres_schema *unres)
Pavol Vican9b89dda2016-03-09 15:36:55 +01002174{
Pavol Vicanec423c92016-10-24 21:33:43 +02002175 struct lys_submodule *submodule;
2176 struct lys_module *module;
Pavol Vican55870412016-03-10 12:36:21 +01002177 const char *str;
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002178 int rc;
Radek Krejci83e3f5b2016-06-24 14:55:25 +02002179 int ret = 0;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002180
Pavol Vicanec423c92016-10-24 21:33:43 +02002181 str = lydict_insert_zc(trg->ctx, value);
Pavol Vicanfda8c802016-12-03 02:00:42 +01002182 if (trg->type) {
Pavol Vicanec423c92016-10-24 21:33:43 +02002183 submodule = (struct lys_submodule *)trg;
2184 module = ((struct lys_submodule *)trg)->belongsto;
2185 } else {
2186 submodule = NULL;
2187 module = trg;
2188 }
Pavol Vicane024ab72016-07-27 14:27:43 +02002189 rc = lyp_check_include(module, submodule, str, inc, unres);
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002190 if (!rc) {
2191 /* success, copy the filled data into the final array */
Pavol Vicane024ab72016-07-27 14:27:43 +02002192 memcpy(&trg->inc[trg->inc_size], inc, sizeof *inc);
Radek Krejci4dcd3392016-06-22 10:28:40 +02002193 trg->inc_size++;
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002194 } else if (rc == -1) {
Radek Krejci83e3f5b2016-06-24 14:55:25 +02002195 ret = -1;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002196 }
Pavol Vican9b89dda2016-03-09 15:36:55 +01002197
Pavol Vicanec423c92016-10-24 21:33:43 +02002198 lydict_remove(trg->ctx, str);
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002199 return ret;
Pavol Vican9b89dda2016-03-09 15:36:55 +01002200}
Pavol Vicanf4717e62016-03-16 11:30:01 +01002201
2202int
Pavol Vican0adf01d2016-03-22 12:29:33 +01002203yang_use_extension(struct lys_module *module, struct lys_node *data_node, void *actual, char *value)
Pavol Vicanf4717e62016-03-16 11:30:01 +01002204{
2205 char *prefix;
2206 char *identif;
2207 const char *ns = NULL;
2208 int i;
2209
Pavol Vicanf4717e62016-03-16 11:30:01 +01002210 /* check to the same pointer */
2211 if (data_node != actual) {
2212 return EXIT_SUCCESS;
2213 }
2214
Pavol Vicana302aa62016-03-17 10:45:35 +01002215 prefix = strdup(value);
2216 if (!prefix) {
2217 LOGMEM;
2218 goto error;
2219 }
2220 /* find prefix anf identificator*/
2221 identif = strchr(prefix, ':');
Pavol Vicanfbd02782016-08-29 11:14:45 +02002222 if (!identif) {
2223 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, prefix);
2224 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The extension must have prefix.");
2225 goto error;
2226 }
Pavol Vicana302aa62016-03-17 10:45:35 +01002227 *identif = '\0';
2228 identif++;
2229
Pavol Vicanf4717e62016-03-16 11:30:01 +01002230 for(i = 0; i < module->imp_size; ++i) {
2231 if (!strcmp(module->imp[i].prefix, prefix)) {
2232 ns = module->imp[i].module->ns;
2233 break;
2234 }
2235 }
Pavol Vican1ff1b7b2016-04-07 09:51:49 +02002236 if (!ns && !strcmp(module->prefix, prefix)) {
2237 ns = (module->type) ? ((struct lys_submodule *)module)->belongsto->ns : module->ns;
2238 }
Pavol Vicanf4717e62016-03-16 11:30:01 +01002239 if (ns && !strcmp(ns, LY_NSNACM)) {
2240 if (!strcmp(identif, "default-deny-write")) {
2241 data_node->nacm |= LYS_NACM_DENYW;
2242 } else if (!strcmp(identif, "default-deny-all")) {
2243 data_node->nacm |= LYS_NACM_DENYA;
2244 } else {
Pavol Vican0adf01d2016-03-22 12:29:33 +01002245 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, identif);
Pavol Vicana302aa62016-03-17 10:45:35 +01002246 goto error;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002247 }
2248 }
Pavol Vicana302aa62016-03-17 10:45:35 +01002249 free(prefix);
Pavol Vicanf4717e62016-03-16 11:30:01 +01002250 return EXIT_SUCCESS;
Pavol Vicana302aa62016-03-17 10:45:35 +01002251
2252error:
2253 free(prefix);
2254 return EXIT_FAILURE;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002255}
2256
2257void
2258nacm_inherit(struct lys_module *module)
2259{
Pavol Vican10ffba52016-04-04 12:21:22 +02002260 struct lys_node *next, *elem, *tmp_node, *tmp_child;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002261
2262 LY_TREE_DFS_BEGIN(module->data, next, elem) {
Pavol Vican10ffba52016-04-04 12:21:22 +02002263 tmp_node = NULL;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002264 if (elem->parent) {
2265 switch (elem->nodetype) {
Pavol Vican10ffba52016-04-04 12:21:22 +02002266 case LYS_GROUPING:
2267 /* extension nacm not inherited*/
2268 break;
2269 case LYS_CHOICE:
2270 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002271 case LYS_ANYDATA:
Pavol Vican10ffba52016-04-04 12:21:22 +02002272 case LYS_USES:
2273 if (elem->parent->nodetype != LYS_GROUPING) {
2274 elem->nacm |= elem->parent->nacm;
2275 }
2276 break;
2277 case LYS_CONTAINER:
2278 case LYS_LIST:
2279 case LYS_CASE:
2280 case LYS_NOTIF:
2281 case LYS_RPC:
2282 case LYS_INPUT:
2283 case LYS_OUTPUT:
2284 case LYS_AUGMENT:
2285 elem->nacm |= elem->parent->nacm;
2286 break;
2287 case LYS_LEAF:
2288 case LYS_LEAFLIST:
2289 tmp_node = elem;
2290 tmp_child = elem->child;
2291 elem->child = NULL;
2292 default:
2293 break;
Pavol Vicanf4717e62016-03-16 11:30:01 +01002294 }
2295 }
2296 LY_TREE_DFS_END(module->data, next, elem);
Pavol Vican10ffba52016-04-04 12:21:22 +02002297 if (tmp_node) {
2298 tmp_node->child = tmp_child;
2299 }
Pavol Vicanf4717e62016-03-16 11:30:01 +01002300 }
2301}
Pavol Vican4fb66c92016-03-17 10:32:27 +01002302
Michal Vaskoe1e351e2016-08-25 12:13:39 +02002303int
Pavol Vican1dac40c2016-09-28 11:39:26 +02002304store_flags(struct lys_node *node, uint8_t flags, int config_opt)
Pavol Vican4fb66c92016-03-17 10:32:27 +01002305{
Michal Vaskoe10d14a2016-08-26 15:17:13 +02002306 struct lys_node *elem;
2307
Pavol Vican1dac40c2016-09-28 11:39:26 +02002308 node->flags |= (config_opt == CONFIG_IGNORE) ? flags & (~(LYS_CONFIG_MASK | LYS_CONFIG_SET)): flags;
2309 if (config_opt == CONFIG_INHERIT_ENABLE) {
2310 if (!(node->flags & LYS_CONFIG_MASK)) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +02002311 /* get config flag from parent */
Radek Krejcif71f48f2016-10-25 16:37:24 +02002312 if (node->parent) {
2313 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Michal Vaskoe1e351e2016-08-25 12:13:39 +02002314 } else {
2315 /* default config is true */
2316 node->flags |= LYS_CONFIG_W;
2317 }
Pavol Vican4fb66c92016-03-17 10:32:27 +01002318 } else {
Pavol Vican1dac40c2016-09-28 11:39:26 +02002319 /* do we even care about config flags? */
2320 for (elem = node; elem && !(elem->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); elem = elem->parent);
Michal Vaskoe10d14a2016-08-26 15:17:13 +02002321
Pavol Vican1dac40c2016-09-28 11:39:26 +02002322 if (!elem && (node->flags & LYS_CONFIG_W) && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
2323 LOGVAL(LYE_INARG, LY_VLOG_LYS, node, "true", "config");
2324 LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "State nodes cannot have configuration nodes as children.");
2325 return EXIT_FAILURE;
2326 }
Pavol Vican4fb66c92016-03-17 10:32:27 +01002327 }
2328 }
Michal Vaskoe1e351e2016-08-25 12:13:39 +02002329
2330 return EXIT_SUCCESS;
Pavol Vican4fb66c92016-03-17 10:32:27 +01002331}
Pavol Vican8e7110b2016-03-22 17:00:26 +01002332
Pavol Vicanec598812016-11-30 14:13:38 +01002333int
Pavol Vicanc54a8f72016-11-23 16:45:55 +01002334store_config_flag(struct lys_node *node, int config_opt)
2335{
Pavol Vicanec598812016-11-30 14:13:38 +01002336 int ret = config_opt;
2337
2338 switch (node->nodetype) {
2339 case LYS_CONTAINER:
2340 case LYS_LEAF:
2341 case LYS_LEAFLIST:
2342 case LYS_LIST:
2343 case LYS_CHOICE:
2344 case LYS_ANYDATA:
2345 case LYS_ANYXML:
2346 if (config_opt == CONFIG_IGNORE) {
2347 node->flags |= node->flags & (~(LYS_CONFIG_MASK | LYS_CONFIG_SET));
2348 } else if (config_opt == CONFIG_INHERIT_ENABLE) {
2349 if (!(node->flags & LYS_CONFIG_MASK)) {
2350 /* get config flag from parent */
2351 if (node->parent) {
2352 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
2353 } else {
2354 /* default config is true */
2355 node->flags |= LYS_CONFIG_W;
2356 }
Pavol Vicanc54a8f72016-11-23 16:45:55 +01002357 }
2358 }
Pavol Vicanec598812016-11-30 14:13:38 +01002359 break;
2360 case LYS_CASE:
2361 if (config_opt == CONFIG_INHERIT_ENABLE) {
2362 if (!(node->flags & LYS_CONFIG_MASK)) {
2363 /* get config flag from parent */
2364 if (node->parent) {
2365 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
2366 } else {
2367 /* default config is true */
2368 node->flags |= LYS_CONFIG_W;
2369 }
2370 }
2371 }
2372 break;
2373 case LYS_RPC:
2374 case LYS_ACTION:
2375 case LYS_NOTIF:
2376 ret = CONFIG_IGNORE;
2377 break;
2378 default:
2379 break;
Pavol Vicanc54a8f72016-11-23 16:45:55 +01002380 }
Pavol Vicanec598812016-11-30 14:13:38 +01002381
2382 return ret;
Pavol Vicanc54a8f72016-11-23 16:45:55 +01002383}
2384
Pavol Vican9d50a772016-10-14 22:23:36 +02002385int
2386yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres,
2387 const char *data, unsigned int size_data, struct lys_node **node)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002388{
Pavol Vican9d50a772016-10-14 22:23:36 +02002389 unsigned int size;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002390 YY_BUFFER_STATE bp;
Pavol Vican802af1e2016-03-23 20:42:26 +01002391 yyscan_t scanner = NULL;
Pavol Vican082afd02016-10-25 12:39:15 +02002392 int ret = EXIT_SUCCESS, remove_import = 1;
2393 struct lys_module *trg;
Pavol Vican1938d882016-04-10 13:36:31 +02002394
Pavol Vican9d50a772016-10-14 22:23:36 +02002395 size = (size_data) ? size_data : strlen(data) + 2;
Pavol Vican1938d882016-04-10 13:36:31 +02002396 yylex_init(&scanner);
2397 bp = yy_scan_buffer((char *)data, size, scanner);
2398 yy_switch_to_buffer(bp, scanner);
Pavol Vican082afd02016-10-25 12:39:15 +02002399 if (yyparse(scanner, NULL, module, submodule, unres, node, &remove_import)) {
2400 if (remove_import) {
2401 trg = (submodule) ? (struct lys_module *)submodule : module;
2402 yang_free_import(trg->ctx, trg->imp, 0, trg->imp_size);
2403 yang_free_include(trg->ctx, trg->inc, 0, trg->inc_size);
2404 trg->inc_size = 0;
2405 trg->imp_size = 0;
2406 }
Pavol Vican1938d882016-04-10 13:36:31 +02002407 ret = EXIT_FAILURE;
2408 }
2409 yy_delete_buffer(bp, scanner);
2410 yylex_destroy(scanner);
2411 return ret;
2412}
2413
Pavol Vican8e7110b2016-03-22 17:00:26 +01002414struct lys_module *
Pavol Vican974377b2016-03-23 00:38:53 +01002415yang_read_module(struct ly_ctx *ctx, const char* data, unsigned int size, const char *revision, int implement)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002416{
2417
Pavol Vican10ffba52016-04-04 12:21:22 +02002418 struct lys_module *tmp_module, *module = NULL;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002419 struct unres_schema *unres = NULL;
Pavol Vican9d50a772016-10-14 22:23:36 +02002420 struct lys_node *node = NULL;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002421
2422 unres = calloc(1, sizeof *unres);
2423 if (!unres) {
2424 LOGMEM;
2425 goto error;
2426 }
2427
2428 module = calloc(1, sizeof *module);
2429 if (!module) {
2430 LOGMEM;
Pavol Vicanf7994fb2016-04-05 21:55:53 +02002431 goto error;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002432 }
2433
2434 /* initiale module */
Michal Vaskofe7e5a72016-05-02 14:49:23 +02002435 module->ctx = ctx;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002436 module->type = 0;
2437 module->implemented = (implement ? 1 : 0);
2438
Pavol Vican9d50a772016-10-14 22:23:36 +02002439 if (yang_parse_mem(module, NULL, unres, data, size, &node)) {
Pavol Vican05810b62016-11-23 14:07:22 +01002440 free_yang_common(module, node);
Pavol Vican7313fc02016-11-14 01:10:31 +01002441 goto error;
2442 }
2443
2444 if (yang_check_sub_module(module, unres, node)) {
Pavol Vican8e7110b2016-03-22 17:00:26 +01002445 goto error;
2446 }
2447
2448 if (module && unres->count && resolve_unres_schema(module, unres)) {
2449 goto error;
2450 }
2451
2452 if (revision) {
2453 /* check revision of the parsed model */
2454 if (!module->rev_size || strcmp(revision, module->rev[0].date)) {
2455 LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").",
2456 module->name, module->rev[0].date, revision);
2457 goto error;
2458 }
2459 }
2460
Pavol Vican10ffba52016-04-04 12:21:22 +02002461 tmp_module = module;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002462 if (lyp_ctx_add_module(&module)) {
Pavol Vican8e7110b2016-03-22 17:00:26 +01002463 goto error;
2464 }
2465
Pavol Vican10ffba52016-04-04 12:21:22 +02002466 if (module == tmp_module) {
2467 nacm_inherit(module);
2468 }
2469
Radek Krejci27fe55e2016-09-13 17:13:35 +02002470 if (module->deviation_size && !module->implemented) {
2471 LOGVRB("Module \"%s\" includes deviations, changing its conformance to \"implement\".", module->name);
2472 /* deviations always causes target to be made implemented,
2473 * but augents and leafrefs not, so we have to apply them now */
2474 if (lys_set_implemented(module)) {
Michal Vasko26055752016-05-03 11:36:31 +02002475 goto error;
2476 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002477 }
2478
Pavol Vican8e7110b2016-03-22 17:00:26 +01002479 unres_schema_free(NULL, &unres);
2480 LOGVRB("Module \"%s\" successfully parsed.", module->name);
2481 return module;
2482
2483error:
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002484 /* cleanup */
Pavol Vican8e7110b2016-03-22 17:00:26 +01002485 unres_schema_free(module, &unres);
2486 if (!module || !module->name) {
2487 free(module);
Radek Krejci48cfa0f2016-11-08 19:18:34 +01002488 if (ly_vecode != LYVE_SUBMODULE) {
2489 LOGERR(ly_errno, "Module parsing failed.");
2490 }
Pavol Vican8e7110b2016-03-22 17:00:26 +01002491 return NULL;
2492 }
2493
2494 LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002495
2496 lys_sub_module_remove_devs_augs(module);
2497 lys_free(module, NULL, 1);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002498 return NULL;
2499}
2500
2501struct lys_submodule *
Pavol Vican974377b2016-03-23 00:38:53 +01002502yang_read_submodule(struct lys_module *module, const char *data, unsigned int size, struct unres_schema *unres)
Pavol Vican8e7110b2016-03-22 17:00:26 +01002503{
2504 struct lys_submodule *submodule;
Pavol Vican9d50a772016-10-14 22:23:36 +02002505 struct lys_node *node = NULL;
Pavol Vican8e7110b2016-03-22 17:00:26 +01002506
2507 submodule = calloc(1, sizeof *submodule);
2508 if (!submodule) {
2509 LOGMEM;
2510 goto error;
2511 }
2512
2513 submodule->ctx = module->ctx;
2514 submodule->type = 1;
2515 submodule->belongsto = module;
2516
Pavol Vican9d50a772016-10-14 22:23:36 +02002517 if (yang_parse_mem(module, submodule, unres, data, size, &node)) {
Pavol Vican05810b62016-11-23 14:07:22 +01002518 free_yang_common((struct lys_module *)submodule, node);
Pavol Vican7313fc02016-11-14 01:10:31 +01002519 goto error;
2520 }
2521
2522 if (yang_check_sub_module((struct lys_module *)submodule, unres, node)) {
Pavol Vican8e7110b2016-03-22 17:00:26 +01002523 goto error;
2524 }
2525
Pavol Vican8e7110b2016-03-22 17:00:26 +01002526 LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002527 return submodule;
2528
2529error:
2530 /* cleanup */
2531 unres_schema_free((struct lys_module *)submodule, &unres);
2532
2533 if (!submodule || !submodule->name) {
2534 free(submodule);
2535 LOGERR(ly_errno, "Submodule parsing failed.");
2536 return NULL;
2537 }
2538
2539 LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name);
2540
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002541 lys_sub_module_remove_devs_augs((struct lys_module *)submodule);
2542 lys_submodule_module_data_free(submodule);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002543 lys_submodule_free(submodule, NULL);
Pavol Vican8e7110b2016-03-22 17:00:26 +01002544 return NULL;
2545}
Pavol Vican8760bb72016-04-07 09:44:01 +02002546
2547static int
Pavol Vican8760bb72016-04-07 09:44:01 +02002548read_indent(const char *input, int indent, int size, int in_index, int *out_index, char *output)
2549{
2550 int k = 0, j;
2551
2552 while (in_index < size) {
2553 if (input[in_index] == ' ') {
2554 k++;
2555 } else if (input[in_index] == '\t') {
2556 /* RFC 6020 6.1.3 tab character is treated as 8 space characters */
2557 k += 8;
Pavol Vican0fbb57b2016-09-27 21:46:12 +02002558 } else if (input[in_index] == '\\' && input[in_index + 1] == 't') {
2559 /* RFC 6020 6.1.3 tab character is treated as 8 space characters */
2560 k += 8;
2561 ++in_index;
Pavol Vican8760bb72016-04-07 09:44:01 +02002562 } else {
2563 break;
2564 }
2565 ++in_index;
2566 if (k >= indent) {
2567 for (j = k - indent; j > 0; --j) {
2568 output[*out_index] = ' ';
2569 ++(*out_index);
2570 }
2571 break;
2572 }
2573 }
Pavol Vican0fbb57b2016-09-27 21:46:12 +02002574 return in_index - 1;
Pavol Vican8760bb72016-04-07 09:44:01 +02002575}
2576
2577char *
Pavol Vican3f598892016-09-28 15:41:07 +02002578yang_read_string(const char *input, char *output, int size, int offset, int indent, int version) {
2579 int i = 0, out_index = offset, space = 0;
Pavol Vican8760bb72016-04-07 09:44:01 +02002580
Pavol Vican0fbb57b2016-09-27 21:46:12 +02002581 while (i < size) {
2582 switch (input[i]) {
2583 case '\n':
2584 out_index -= space;
2585 output[out_index] = '\n';
2586 space = 0;
2587 i = read_indent(input, indent, size, i + 1, &out_index, output);
2588 break;
2589 case ' ':
2590 case '\t':
2591 output[out_index] = input[i];
2592 ++space;
2593 break;
2594 case '\\':
2595 if (input[i + 1] == 'n') {
2596 out_index -= space;
2597 output[out_index] = '\n';
2598 space = 0;
2599 i = read_indent(input, indent, size, i + 2, &out_index, output);
2600 } else if (input[i + 1] == 't') {
2601 output[out_index] = '\t';
2602 ++i;
2603 ++space;
2604 } else if (input[i + 1] == '\\') {
2605 output[out_index] = '\\';
2606 ++i;
2607 } else if ((i + 1) != size && input[i + 1] == '"') {
2608 output[out_index] = '"';
2609 ++i;
Pavol Vican8760bb72016-04-07 09:44:01 +02002610 } else {
Pavol Vican677b0132016-08-09 15:44:58 +02002611 if (version < 2) {
Pavol Vican0fbb57b2016-09-27 21:46:12 +02002612 output[out_index] = input[i];
Pavol Vican677b0132016-08-09 15:44:58 +02002613 } else {
2614 /* YANG 1.1 backslash must not be followed by any other character */
2615 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, input);
Pavol Vican3f598892016-09-28 15:41:07 +02002616 return NULL;
Pavol Vican677b0132016-08-09 15:44:58 +02002617 }
Pavol Vican8760bb72016-04-07 09:44:01 +02002618 }
Pavol Vican0fbb57b2016-09-27 21:46:12 +02002619 break;
2620 default:
2621 output[out_index] = input[i];
2622 space = 0;
2623 break;
Pavol Vican8760bb72016-04-07 09:44:01 +02002624 }
Pavol Vican0fbb57b2016-09-27 21:46:12 +02002625 ++i;
Pavol Vican8760bb72016-04-07 09:44:01 +02002626 ++out_index;
2627 }
Pavol Vican0fbb57b2016-09-27 21:46:12 +02002628 output[out_index] = '\0';
2629 if (size != out_index) {
Pavol Vican3f598892016-09-28 15:41:07 +02002630 output = realloc(output, out_index + 1);
Pavol Vican0fbb57b2016-09-27 21:46:12 +02002631 if (!output) {
2632 LOGMEM;
Pavol Vican3f598892016-09-28 15:41:07 +02002633 return NULL;
Pavol Vican8760bb72016-04-07 09:44:01 +02002634 }
Pavol Vican8760bb72016-04-07 09:44:01 +02002635 }
Pavol Vican3f598892016-09-28 15:41:07 +02002636 return output;
Pavol Vican8760bb72016-04-07 09:44:01 +02002637}
Pavol Vican1cc4e192016-10-24 16:38:31 +02002638
2639/* free function */
2640
Pavol Vican7313fc02016-11-14 01:10:31 +01002641static void yang_type_free(struct ly_ctx *ctx, struct lys_type *type)
2642{
2643 struct yang_type *stype = (struct yang_type *)type->der;
2644 int i;
2645
2646 if (!stype) {
2647 return ;
2648 }
2649 lydict_remove(ctx, stype->name);
2650 type->base = stype->base;
Pavol Vicanb60a1d12016-12-03 01:56:55 +01002651 if (type->base == LY_TYPE_IDENT && (!(stype->flags & LYS_NO_ERASE_IDENTITY))) {
Pavol Vican7313fc02016-11-14 01:10:31 +01002652 for (i = 0; i < type->info.ident.count; ++i) {
2653 free(type->info.ident.ref[i]);
2654 }
2655 }
2656 lys_type_free(ctx, type);
Pavol Vican36aff862016-11-26 17:07:05 +01002657 type->base = LY_TYPE_DER;
Pavol Vican7313fc02016-11-14 01:10:31 +01002658 free(stype);
2659}
2660
2661static void
2662yang_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf, uint8_t start, uint8_t size)
2663{
2664 uint8_t i;
2665
2666 assert(ctx);
2667 if (!tpdf) {
2668 return;
2669 }
2670
2671 for (i = start; i < size; ++i) {
Pavol Vicancee10802016-11-22 15:48:35 +01002672 lydict_remove(ctx, tpdf[i].name);
2673 lydict_remove(ctx, tpdf[i].dsc);
2674 lydict_remove(ctx, tpdf[i].ref);
Pavol Vican7313fc02016-11-14 01:10:31 +01002675
Pavol Vicancee10802016-11-22 15:48:35 +01002676 yang_type_free(ctx, &tpdf[i].type);
Pavol Vican7313fc02016-11-14 01:10:31 +01002677
Pavol Vicancee10802016-11-22 15:48:35 +01002678 lydict_remove(ctx, tpdf[i].units);
2679 lydict_remove(ctx, tpdf[i].dflt);
Pavol Vican7313fc02016-11-14 01:10:31 +01002680 }
2681}
2682
Pavol Vican1cc4e192016-10-24 16:38:31 +02002683static void
2684yang_free_import(struct ly_ctx *ctx, struct lys_import *imp, uint8_t start, uint8_t size)
2685{
2686 uint8_t i;
2687
2688 for (i = start; i < size; ++i){
2689 free((char *)imp[i].module);
2690 lydict_remove(ctx, imp[i].prefix);
2691 lydict_remove(ctx, imp[i].dsc);
2692 lydict_remove(ctx, imp[i].ref);
2693 }
2694}
2695
Pavol Vicanec423c92016-10-24 21:33:43 +02002696static void
2697yang_free_include(struct ly_ctx *ctx, struct lys_include *inc, uint8_t start, uint8_t size)
2698{
2699 uint8_t i;
2700
2701 for (i = start; i < size; ++i){
2702 free((char *)inc[i].submodule);
2703 lydict_remove(ctx, inc[i].dsc);
2704 lydict_remove(ctx, inc[i].ref);
2705 }
2706}
2707
Pavol Vican36e27272016-11-22 15:47:28 +01002708static void
2709yang_free_ident_base(struct lys_ident *ident, uint32_t start, uint32_t size)
2710{
2711 uint32_t i;
2712 uint8_t j;
2713
2714 /* free base name */
2715 for (i = start; i < size; ++i) {
2716 for (j = 0; j < ident[i].base_size; ++j) {
2717 free(ident[i].base[j]);
2718 }
2719 }
2720}
2721
Pavol Vican05810b62016-11-23 14:07:22 +01002722static void
2723yang_free_grouping(struct ly_ctx *ctx, struct lys_node_grp * grp)
2724{
2725 yang_tpdf_free(ctx, grp->tpdf, 0, grp->tpdf_size);
2726 free(grp->tpdf);
2727}
2728
2729static void
Pavol Vicanc54a8f72016-11-23 16:45:55 +01002730yang_free_container(struct ly_ctx *ctx, struct lys_node_container * cont)
2731{
2732 uint8_t i;
2733
2734 yang_tpdf_free(ctx, cont->tpdf, 0, cont->tpdf_size);
2735 free(cont->tpdf);
2736 lydict_remove(ctx, cont->presence);
2737
Pavol Vicanfda8c802016-12-03 02:00:42 +01002738 for (i = 0; i < cont->must_size; ++i) {
Pavol Vicanc54a8f72016-11-23 16:45:55 +01002739 lys_restr_free(ctx, &cont->must[i]);
2740 }
2741 free(cont->must);
2742
2743 lys_when_free(ctx, cont->when);
2744}
2745
2746static void
Pavol Vicana69aff22016-11-24 18:23:50 +01002747yang_free_leaf(struct ly_ctx *ctx, struct lys_node_leaf *leaf)
2748{
2749 uint8_t i;
2750
2751 for (i = 0; i < leaf->must_size; i++) {
2752 lys_restr_free(ctx, &leaf->must[i]);
2753 }
2754 free(leaf->must);
2755
2756 lys_when_free(ctx, leaf->when);
2757
2758 yang_type_free(ctx, &leaf->type);
2759 lydict_remove(ctx, leaf->units);
2760 lydict_remove(ctx, leaf->dflt);
2761}
2762
2763static void
Pavol Vican36aff862016-11-26 17:07:05 +01002764yang_free_leaflist(struct ly_ctx *ctx, struct lys_node_leaflist *leaflist)
2765{
2766 uint8_t i;
2767
2768 for (i = 0; i < leaflist->must_size; i++) {
2769 lys_restr_free(ctx, &leaflist->must[i]);
2770 }
2771 free(leaflist->must);
2772
2773 for (i = 0; i < leaflist->dflt_size; i++) {
2774 lydict_remove(ctx, leaflist->dflt[i]);
2775 }
2776 free(leaflist->dflt);
2777
2778 lys_when_free(ctx, leaflist->when);
2779
2780 yang_type_free(ctx, &leaflist->type);
2781 lydict_remove(ctx, leaflist->units);
2782}
2783
2784static void
Pavol Vicand8136a42016-11-27 13:28:04 +01002785yang_free_list(struct ly_ctx *ctx, struct lys_node_list *list)
2786{
2787 uint8_t i;
2788
2789 yang_tpdf_free(ctx, list->tpdf, 0, list->tpdf_size);
2790 free(list->tpdf);
2791
2792 for (i = 0; i < list->must_size; ++i) {
2793 lys_restr_free(ctx, &list->must[i]);
2794 }
2795 free(list->must);
2796
2797 lys_when_free(ctx, list->when);
2798
2799 for (i = 0; i < list->unique_size; ++i) {
2800 free(list->unique[i].expr);
2801 }
2802 free(list->unique);
2803
2804 free(list->keys);
2805}
2806
2807static void
Pavol Vican36ace102016-11-28 11:46:59 +01002808yang_free_choice(struct ly_ctx *ctx, struct lys_node_choice *choice)
2809{
2810 free(choice->dflt);
2811 lys_when_free(ctx, choice->when);
2812}
2813
2814static void
Pavol Vicanbfa1a582016-11-28 15:35:59 +01002815yang_free_anydata(struct ly_ctx *ctx, struct lys_node_anydata *anydata)
2816{
2817 uint8_t i;
2818
2819 for (i = 0; i < anydata->must_size; ++i) {
2820 lys_restr_free(ctx, &anydata->must[i]);
2821 }
2822 free(anydata->must);
2823
2824 lys_when_free(ctx, anydata->when);
2825}
2826
2827static void
Pavol Vican78729392016-11-28 17:18:22 +01002828yang_free_inout(struct ly_ctx *ctx, struct lys_node_inout *inout)
2829{
2830 uint8_t i;
2831
2832 yang_tpdf_free(ctx, inout->tpdf, 0, inout->tpdf_size);
2833 free(inout->tpdf);
2834
2835 for (i = 0; i < inout->must_size; ++i) {
2836 lys_restr_free(ctx, &inout->must[i]);
2837 }
2838 free(inout->must);
2839}
2840
2841static void
Pavol Vican29bf8802016-11-28 20:44:57 +01002842yang_free_notif(struct ly_ctx *ctx, struct lys_node_notif *notif)
2843{
2844 uint8_t i;
2845
2846 yang_tpdf_free(ctx, notif->tpdf, 0, notif->tpdf_size);
2847 free(notif->tpdf);
2848
2849 for (i = 0; i < notif->must_size; ++i) {
2850 lys_restr_free(ctx, &notif->must[i]);
2851 }
2852 free(notif->must);
2853}
2854
2855static void
Pavol Vican3b5e82a2016-11-29 21:41:56 +01002856yang_free_uses(struct ly_ctx *ctx, struct lys_node_uses *uses)
2857{
2858 int i, j;
2859
2860 for (i = 0; i < uses->refine_size; i++) {
2861 lydict_remove(ctx, uses->refine[i].target_name);
2862 lydict_remove(ctx, uses->refine[i].dsc);
2863 lydict_remove(ctx, uses->refine[i].ref);
2864
2865 for (j = 0; j < uses->refine[i].must_size; j++) {
2866 lys_restr_free(ctx, &uses->refine[i].must[j]);
2867 }
2868 free(uses->refine[i].must);
2869
2870 for (j = 0; j < uses->refine[i].dflt_size; j++) {
2871 lydict_remove(ctx, uses->refine[i].dflt[j]);
2872 }
2873 free(uses->refine[i].dflt);
2874
2875 if (uses->refine[i].target_type & LYS_CONTAINER) {
2876 lydict_remove(ctx, uses->refine[i].mod.presence);
2877 }
2878 }
2879 free(uses->refine);
2880
2881 lys_when_free(ctx, uses->when);
2882}
2883
Pavol Vican3b5e82a2016-11-29 21:41:56 +01002884static void
Pavol Vican05810b62016-11-23 14:07:22 +01002885yang_free_nodes(struct ly_ctx *ctx, struct lys_node *node)
2886{
2887 struct lys_node *tmp, *child, *sibling;
2888
2889 if (!node) {
2890 return;
2891 }
2892 tmp = node;
Pavol Vican05810b62016-11-23 14:07:22 +01002893
2894 while (tmp) {
Pavol Vicane87ff8d2016-11-24 18:25:01 +01002895 child = tmp->child;
Pavol Vican05810b62016-11-23 14:07:22 +01002896 sibling = tmp->next;
2897 /* common part */
2898 lydict_remove(ctx, tmp->name);
2899 if (!(tmp->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Pavol Vicane87ff8d2016-11-24 18:25:01 +01002900 lys_iffeature_free(tmp->iffeature, tmp->iffeature_size);
2901 lydict_remove(ctx, tmp->dsc);
2902 lydict_remove(ctx, tmp->ref);
Pavol Vican05810b62016-11-23 14:07:22 +01002903 }
2904
2905 switch (tmp->nodetype) {
2906 case LYS_GROUPING:
Pavol Vicanebc9ef82016-11-28 16:46:49 +01002907 case LYS_RPC:
2908 case LYS_ACTION:
Pavol Vican05810b62016-11-23 14:07:22 +01002909 yang_free_grouping(ctx, (struct lys_node_grp *)tmp);
2910 break;
Pavol Vicanc54a8f72016-11-23 16:45:55 +01002911 case LYS_CONTAINER:
2912 yang_free_container(ctx, (struct lys_node_container *)tmp);
2913 break;
Pavol Vicana69aff22016-11-24 18:23:50 +01002914 case LYS_LEAF:
2915 yang_free_leaf(ctx, (struct lys_node_leaf *)tmp);
2916 break;
Pavol Vican36aff862016-11-26 17:07:05 +01002917 case LYS_LEAFLIST:
2918 yang_free_leaflist(ctx, (struct lys_node_leaflist *)tmp);
2919 break;
Pavol Vicand8136a42016-11-27 13:28:04 +01002920 case LYS_LIST:
2921 yang_free_list(ctx, (struct lys_node_list *)tmp);
2922 break;
Pavol Vican36ace102016-11-28 11:46:59 +01002923 case LYS_CHOICE:
2924 yang_free_choice(ctx, (struct lys_node_choice *)tmp);
2925 break;
Pavol Vicana420bac2016-11-28 14:51:54 +01002926 case LYS_CASE:
2927 lys_when_free(ctx, ((struct lys_node_case *)tmp)->when);
2928 break;
Pavol Vicanbfa1a582016-11-28 15:35:59 +01002929 case LYS_ANYXML:
2930 case LYS_ANYDATA:
2931 yang_free_anydata(ctx, (struct lys_node_anydata *)tmp);
2932 break;
Pavol Vican78729392016-11-28 17:18:22 +01002933 case LYS_INPUT:
2934 case LYS_OUTPUT:
2935 yang_free_inout(ctx, (struct lys_node_inout *)tmp);
2936 break;
Pavol Vican29bf8802016-11-28 20:44:57 +01002937 case LYS_NOTIF:
2938 yang_free_notif(ctx, (struct lys_node_notif *)tmp);
2939 break;
Pavol Vican3b5e82a2016-11-29 21:41:56 +01002940 case LYS_USES:
2941 yang_free_uses(ctx, (struct lys_node_uses *)tmp);
2942 break;
Pavol Vican05810b62016-11-23 14:07:22 +01002943 default:
2944 break;
2945 }
2946
2947 yang_free_nodes(ctx, child);
2948 free(tmp);
2949 tmp = sibling;
2950 }
2951}
2952
Pavol Vican3ad50f82016-12-04 15:00:36 +01002953static void
2954yang_free_augment(struct ly_ctx *ctx, struct lys_node_augment *aug)
2955{
2956 lydict_remove(ctx, aug->target_name);
2957 lydict_remove(ctx, aug->dsc);
2958 lydict_remove(ctx, aug->ref);
2959
2960 lys_iffeature_free(aug->iffeature, aug->iffeature_size);
2961 lys_when_free(ctx, aug->when);
2962 yang_free_nodes(ctx, aug->child);
2963}
2964
Pavol Vican7313fc02016-11-14 01:10:31 +01002965/* free common item from module and submodule */
2966static void
Pavol Vican05810b62016-11-23 14:07:22 +01002967free_yang_common(struct lys_module *module, struct lys_node *node)
Pavol Vican7313fc02016-11-14 01:10:31 +01002968{
2969 yang_tpdf_free(module->ctx, module->tpdf, 0, module->tpdf_size);
2970 module->tpdf_size = 0;
Pavol Vican36e27272016-11-22 15:47:28 +01002971 yang_free_ident_base(module->ident, 0, module->ident_size);
Pavol Vican05810b62016-11-23 14:07:22 +01002972 yang_free_nodes(module->ctx, node);
Pavol Vican7313fc02016-11-14 01:10:31 +01002973}
2974
Pavol Vican1cc4e192016-10-24 16:38:31 +02002975/* check function*/
2976
2977int
Pavol Vicanec423c92016-10-24 21:33:43 +02002978yang_check_imports(struct lys_module *module, struct unres_schema *unres)
Pavol Vican1cc4e192016-10-24 16:38:31 +02002979{
2980 struct lys_import *imp;
Pavol Vicanec423c92016-10-24 21:33:43 +02002981 struct lys_include *inc;
2982 uint8_t imp_size, inc_size, j = 0, i = 0;
Pavol Vican1cc4e192016-10-24 16:38:31 +02002983 size_t size;
2984 char *s;
2985
2986 imp = module->imp;
2987 imp_size = module->imp_size;
Pavol Vicanec423c92016-10-24 21:33:43 +02002988 inc = module->inc;
2989 inc_size = module->inc_size;
Pavol Vican1cc4e192016-10-24 16:38:31 +02002990
2991 if (imp_size) {
2992 size = (imp_size * sizeof *module->imp) + sizeof(void*);
2993 module->imp_size = 0;
2994 module->imp = calloc(1, size);
2995 if (!module->imp) {
2996 LOGMEM;
2997 goto error;
2998 }
2999 /* set stop block for possible realloc */
3000 module->imp[imp_size].module = (void*)0x1;
Pavol Vicanec423c92016-10-24 21:33:43 +02003001 }
Pavol Vican1cc4e192016-10-24 16:38:31 +02003002
Pavol Vicanec423c92016-10-24 21:33:43 +02003003 if (inc_size) {
3004 size = (inc_size * sizeof *module->inc) + sizeof(void*);
3005 module->inc_size = 0;
3006 module->inc = calloc(1, size);
3007 if (!module->inc) {
3008 LOGMEM;
3009 goto error;
3010 }
3011 /* set stop block for possible realloc */
3012 module->inc[inc_size].submodule = (void*)0x1;
Pavol Vican1cc4e192016-10-24 16:38:31 +02003013 }
3014
3015 for (i = 0; i < imp_size; ++i) {
3016 s = (char *) imp[i].module;
3017 imp[i].module = NULL;
3018 if (yang_fill_import(module, &imp[i], &module->imp[module->imp_size], s)) {
3019 ++i;
3020 goto error;
3021 }
3022 }
Pavol Vicanec423c92016-10-24 21:33:43 +02003023 for (j = 0; j < inc_size; ++j) {
Pavol Vicanfda8c802016-12-03 02:00:42 +01003024 s = (char *) inc[j].submodule;
3025 inc[j].submodule = NULL;
3026 if (yang_fill_include(module, s, &inc[j], unres)) {
3027 ++j;
Pavol Vicanec423c92016-10-24 21:33:43 +02003028 goto error;
3029 }
3030 }
3031 free(inc);
Pavol Vican1cc4e192016-10-24 16:38:31 +02003032 free(imp);
3033
3034 return EXIT_SUCCESS;
3035
3036error:
3037 yang_free_import(module->ctx, imp, i, imp_size);
Pavol Vicanec423c92016-10-24 21:33:43 +02003038 yang_free_include(module->ctx, inc, j, inc_size);
Pavol Vican1cc4e192016-10-24 16:38:31 +02003039 free(imp);
Pavol Vicanec423c92016-10-24 21:33:43 +02003040 free(inc);
Pavol Vican1cc4e192016-10-24 16:38:31 +02003041 return EXIT_FAILURE;
3042}
Pavol Vican7313fc02016-11-14 01:10:31 +01003043
Pavol Vican5c8a9ad2016-11-22 11:50:54 +01003044static int
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003045yang_check_iffeatures(struct lys_module *module, void *ptr, void *parent, enum yytokentype type, struct unres_schema *unres)
Pavol Vican5c8a9ad2016-11-22 11:50:54 +01003046{
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003047 struct lys_iffeature *iffeature;
3048 uint8_t *ptr_size, size, i;
3049 char *s;
3050 int parent_is_feature = 0;
Pavol Vican5c8a9ad2016-11-22 11:50:54 +01003051
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003052 switch (type) {
3053 case FEATURE_KEYWORD:
3054 iffeature = ((struct lys_feature *)parent)->iffeature;
3055 size = ((struct lys_feature *)parent)->iffeature_size;
3056 ptr_size = &((struct lys_feature *)parent)->iffeature_size;
3057 parent_is_feature = 1;
3058 break;
3059 case IDENTITY_KEYWORD:
3060 iffeature = ((struct lys_ident *)parent)->iffeature;
3061 size = ((struct lys_ident *)parent)->iffeature_size;
3062 ptr_size = &((struct lys_ident *)parent)->iffeature_size;
3063 break;
3064 case ENUM_KEYWORD:
3065 iffeature = ((struct lys_type_enum *)ptr)->iffeature;
3066 size = ((struct lys_type_enum *)ptr)->iffeature_size;
3067 ptr_size = &((struct lys_type_enum *)ptr)->iffeature_size;
3068 break;
3069 case BIT_KEYWORD:
3070 iffeature = ((struct lys_type_bit *)ptr)->iffeature;
3071 size = ((struct lys_type_bit *)ptr)->iffeature_size;
3072 ptr_size = &((struct lys_type_bit *)ptr)->iffeature_size;
3073 break;
3074 case REFINE_KEYWORD:
3075 iffeature = ((struct lys_refine *)ptr)->iffeature;
3076 size = ((struct lys_refine *)ptr)->iffeature_size;
3077 ptr_size = &((struct lys_refine *)ptr)->iffeature_size;
3078 break;
3079 default:
3080 iffeature = ((struct lys_node *)parent)->iffeature;
3081 size = ((struct lys_node *)parent)->iffeature_size;
3082 ptr_size = &((struct lys_node *)parent)->iffeature_size;
3083 break;
Pavol Vican5c8a9ad2016-11-22 11:50:54 +01003084 }
3085
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003086 *ptr_size = 0;
Pavol Vican5c8a9ad2016-11-22 11:50:54 +01003087 for (i = 0; i < size; ++i) {
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003088 s = (char *)iffeature[i].features;
3089 iffeature[i].features = NULL;
3090 if (yang_fill_iffeature(module, &iffeature[i], parent, s, unres, parent_is_feature)) {
3091 *ptr_size = size;
3092 return EXIT_FAILURE;
Pavol Vican5c8a9ad2016-11-22 11:50:54 +01003093 }
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003094 (*ptr_size)++;
Pavol Vican5c8a9ad2016-11-22 11:50:54 +01003095 }
3096
3097 return EXIT_SUCCESS;
Pavol Vican5c8a9ad2016-11-22 11:50:54 +01003098}
3099
Pavol Vicanb60a1d12016-12-03 01:56:55 +01003100static int
3101yang_check_identityref(struct lys_module *module, struct lys_type *type, struct unres_schema *unres)
3102{
3103 uint size, i;
3104 int rc;
3105 struct lys_ident **ref;
3106 const char *value;
3107 char *expr;
3108
3109 ref = type->info.ident.ref;
3110 size = type->info.ident.count;
3111 type->info.ident.count = 0;
3112 type->info.ident.ref = NULL;
3113 ((struct yang_type *)type->der)->flags |= LYS_NO_ERASE_IDENTITY;
3114
3115 for (i = 0; i < size; ++i) {
3116 expr = (char *)ref[i];
3117 /* store in the JSON format */
3118 value = transform_schema2json(module, expr);
3119 free(expr);
3120
3121 if (!value) {
3122 goto error;
3123 }
3124 rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value);
3125 lydict_remove(module->ctx, value);
3126
3127 if (rc == -1) {
3128 goto error;
3129 }
3130 }
3131 free(ref);
3132
3133 return EXIT_SUCCESS;
3134error:
3135 for (i = i+1; i < size; ++i) {
3136 free(ref[i]);
3137 }
3138 free(ref);
3139 return EXIT_FAILURE;
3140}
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003141
Pavol Vican7313fc02016-11-14 01:10:31 +01003142int
3143yang_check_typedef(struct lys_module *module, struct lys_node *parent, struct unres_schema *unres)
3144{
3145 struct lys_tpdf *tpdf;
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003146 uint8_t j, i, tpdf_size, *ptr_tpdf_size;
3147 struct yang_type *stype;
Pavol Vican7313fc02016-11-14 01:10:31 +01003148
3149 if (!parent) {
3150 tpdf = module->tpdf;
3151 ptr_tpdf_size = &module->tpdf_size;
Pavol Vican05810b62016-11-23 14:07:22 +01003152 } else {
3153 switch (parent->nodetype) {
3154 case LYS_GROUPING:
3155 tpdf = ((struct lys_node_grp *)parent)->tpdf;
3156 ptr_tpdf_size = &((struct lys_node_grp *)parent)->tpdf_size;
3157 break;
Pavol Vicanc54a8f72016-11-23 16:45:55 +01003158 case LYS_CONTAINER:
3159 tpdf = ((struct lys_node_container *)parent)->tpdf;
3160 ptr_tpdf_size = &((struct lys_node_container *)parent)->tpdf_size;
3161 break;
Pavol Vicand8136a42016-11-27 13:28:04 +01003162 case LYS_LIST:
3163 tpdf = ((struct lys_node_list *)parent)->tpdf;
3164 ptr_tpdf_size = &((struct lys_node_list *)parent)->tpdf_size;
3165 break;
Pavol Vicanebc9ef82016-11-28 16:46:49 +01003166 case LYS_RPC:
3167 case LYS_ACTION:
3168 tpdf = ((struct lys_node_rpc_action *)parent)->tpdf;
3169 ptr_tpdf_size = &((struct lys_node_rpc_action *)parent)->tpdf_size;
3170 break;
Pavol Vican78729392016-11-28 17:18:22 +01003171 case LYS_INPUT:
3172 case LYS_OUTPUT:
3173 tpdf = ((struct lys_node_inout *)parent)->tpdf;
3174 ptr_tpdf_size = &((struct lys_node_inout *)parent)->tpdf_size;
3175 break;
Pavol Vican29bf8802016-11-28 20:44:57 +01003176 case LYS_NOTIF:
3177 tpdf = ((struct lys_node_notif *)parent)->tpdf;
3178 ptr_tpdf_size = &((struct lys_node_notif *)parent)->tpdf_size;
3179 break;
Pavol Vican05810b62016-11-23 14:07:22 +01003180 default:
3181 LOGINT;
3182 return EXIT_FAILURE;
3183 }
Pavol Vican7313fc02016-11-14 01:10:31 +01003184 }
3185
3186 tpdf_size = *ptr_tpdf_size;
3187 *ptr_tpdf_size = 0;
3188
3189 for (i = 0; i < tpdf_size; ++i) {
Pavol Vicanfda8c802016-12-03 02:00:42 +01003190 if (lyp_check_identifier(tpdf[i].name, LY_IDENT_TYPE, module, parent)) {
3191 goto error;
3192 }
Pavol Vican7313fc02016-11-14 01:10:31 +01003193 tpdf[i].type.parent = &tpdf[i];
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003194
3195 stype = (struct yang_type *)tpdf[i].type.der;
3196 if (stype->base == LY_TYPE_ENUM) {
3197 for (j = 0; j < tpdf[i].type.info.enums.count; ++j) {
3198 if (yang_check_iffeatures(module, &tpdf[i].type.info.enums.enm[j], &tpdf[i], ENUM_KEYWORD, unres)) {
3199 goto error;
3200 }
3201 }
3202 } else if (stype->base == LY_TYPE_BITS) {
3203 for (j = 0; j < tpdf[i].type.info.bits.count; ++j) {
3204 if (yang_check_iffeatures(module, &tpdf[i].type.info.bits.bit[j], &tpdf[i], BIT_KEYWORD, unres)) {
3205 goto error;
3206 }
3207 }
Pavol Vicanb60a1d12016-12-03 01:56:55 +01003208 } else if (stype->base == LY_TYPE_IDENT) {
3209 if (yang_check_identityref(module, &tpdf[i].type, unres)) {
3210 goto error;
3211 }
Pavol Vican5c8a9ad2016-11-22 11:50:54 +01003212 }
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003213
Pavol Vican7313fc02016-11-14 01:10:31 +01003214 if (unres_schema_add_node(module, unres, &tpdf[i].type, UNRES_TYPE_DER_TPDF, parent) == -1) {
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003215 goto error;
Pavol Vican7313fc02016-11-14 01:10:31 +01003216 }
3217
3218 /* check default value*/
Pavol Vicanfda8c802016-12-03 02:00:42 +01003219 if (unres_schema_add_node(module, unres, &tpdf[i].type, UNRES_TYPE_DFLT, (struct lys_node *)(&tpdf[i].dflt)) == -1) {
Pavol Vican7313fc02016-11-14 01:10:31 +01003220 ++i;
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003221 goto error;
Pavol Vican7313fc02016-11-14 01:10:31 +01003222 }
3223 (*ptr_tpdf_size)++;
3224 }
Pavol Vican7313fc02016-11-14 01:10:31 +01003225
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003226 return EXIT_SUCCESS;
3227
3228error:
3229 yang_tpdf_free(module->ctx, tpdf, i, tpdf_size);
3230 return EXIT_FAILURE;
Pavol Vican7313fc02016-11-14 01:10:31 +01003231}
3232
3233static int
Pavol Vican36e27272016-11-22 15:47:28 +01003234yang_check_identities(struct lys_module *module, struct unres_schema *unres)
3235{
3236 uint32_t i, size, base_size;
3237 uint8_t j;
3238
3239 size = module->ident_size;
3240 module->ident_size = 0;
3241 for (i = 0; i < size; ++i) {
3242 base_size = module->ident[i].base_size;
3243 module->ident[i].base_size = 0;
3244 for (j = 0; j < base_size; ++j) {
3245 if (yang_read_base(module, &module->ident[i], (char *)module->ident[i].base[j], unres)) {
3246 ++j;
3247 module->ident_size = size;
3248 goto error;
3249 }
3250 }
3251 module->ident_size++;
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003252 if (yang_check_iffeatures(module, NULL, &module->ident[i], IDENTITY_KEYWORD, unres)) {
3253 goto error;
3254 }
Pavol Vican36e27272016-11-22 15:47:28 +01003255 }
3256
3257 return EXIT_SUCCESS;
3258
3259error:
3260 for (; j< module->ident[i].base_size; ++j) {
3261 free(module->ident[i].base[j]);
3262 }
3263 yang_free_ident_base(module->ident, i + 1, size);
3264 return EXIT_FAILURE;
3265}
3266
3267static int
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003268yang_check_container(struct lys_module *module, struct lys_node_container *cont, struct unres_schema *unres)
Pavol Vican05810b62016-11-23 14:07:22 +01003269{
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003270 if (yang_check_typedef(module, (struct lys_node *)cont, unres)) {
Pavol Vican05810b62016-11-23 14:07:22 +01003271 goto error;
3272 }
3273
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003274 if (yang_check_iffeatures(module, NULL, cont, CONTAINER_KEYWORD, unres)) {
Pavol Vicanc54a8f72016-11-23 16:45:55 +01003275 goto error;
3276 }
3277
Pavol Vicanc54a8f72016-11-23 16:45:55 +01003278 /* check XPath dependencies */
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003279 if ((cont->when || cont->must_size) && (unres_schema_add_node(module, unres, cont, UNRES_XPATH, NULL) == -1)) {
Pavol Vicanc54a8f72016-11-23 16:45:55 +01003280 goto error;
3281 }
3282
3283 return EXIT_SUCCESS;
3284error:
3285 return EXIT_FAILURE;
3286}
3287
3288static int
Pavol Vicana69aff22016-11-24 18:23:50 +01003289yang_check_leaf(struct lys_module *module, struct lys_node_leaf *leaf, struct unres_schema *unres)
3290{
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003291 int i;
3292 struct yang_type *stype;
Pavol Vicana69aff22016-11-24 18:23:50 +01003293
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003294 stype = (struct yang_type *)leaf->type.der;
3295 if (stype->base == LY_TYPE_ENUM) {
3296 for (i = 0; i < leaf->type.info.enums.count; ++i) {
3297 if (yang_check_iffeatures(module, &leaf->type.info.enums.enm[i], leaf, ENUM_KEYWORD, unres)) {
3298 yang_type_free(module->ctx, &leaf->type);
3299 goto error;
3300 }
3301 }
3302 } else if (stype->base == LY_TYPE_BITS) {
3303 for (i = 0; i < leaf->type.info.bits.count; ++i) {
3304 if (yang_check_iffeatures(module, &leaf->type.info.bits.bit[i], leaf, BIT_KEYWORD, unres)) {
3305 yang_type_free(module->ctx, &leaf->type);
3306 goto error;
3307 }
3308 }
Pavol Vicanb60a1d12016-12-03 01:56:55 +01003309 } else if (stype->base == LY_TYPE_IDENT) {
3310 if (yang_check_identityref(module, &leaf->type, unres)) {
3311 goto error;
3312 }
Pavol Vicana69aff22016-11-24 18:23:50 +01003313 }
3314
Pavol Vicanfda8c802016-12-03 02:00:42 +01003315 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DER, (struct lys_node *)leaf) == -1) {
Pavol Vicana69aff22016-11-24 18:23:50 +01003316 yang_type_free(module->ctx, &leaf->type);
3317 goto error;
3318 }
3319
Pavol Vicanfda8c802016-12-03 02:00:42 +01003320 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)&leaf->dflt) == -1) {
Pavol Vicana69aff22016-11-24 18:23:50 +01003321 goto error;
3322 }
3323
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003324 if (yang_check_iffeatures(module, NULL, leaf, LEAF_KEYWORD, unres)) {
3325 goto error;
Pavol Vicana69aff22016-11-24 18:23:50 +01003326 }
3327
3328 /* check XPath dependencies */
3329 if ((leaf->when || leaf->must_size) && (unres_schema_add_node(module, unres, leaf, UNRES_XPATH, NULL) == -1)) {
3330 goto error;
3331 }
3332
3333 return EXIT_SUCCESS;
3334error:
3335 return EXIT_FAILURE;
3336}
3337
3338static int
Pavol Vican36aff862016-11-26 17:07:05 +01003339yang_check_leaflist(struct lys_module *module, struct lys_node_leaflist *leaflist, struct unres_schema *unres)
3340{
Pavol Vicanfda8c802016-12-03 02:00:42 +01003341 int i, j;
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003342 struct yang_type *stype;
Pavol Vican36aff862016-11-26 17:07:05 +01003343
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003344 stype = (struct yang_type *)leaflist->type.der;
3345 if (stype->base == LY_TYPE_ENUM) {
3346 for (i = 0; i < leaflist->type.info.enums.count; ++i) {
3347 if (yang_check_iffeatures(module, &leaflist->type.info.enums.enm[i], leaflist, ENUM_KEYWORD, unres)) {
3348 yang_type_free(module->ctx, &leaflist->type);
3349 goto error;
3350 }
3351 }
3352 } else if (stype->base == LY_TYPE_BITS) {
3353 for (i = 0; i < leaflist->type.info.bits.count; ++i) {
3354 if (yang_check_iffeatures(module, &leaflist->type.info.bits.bit[i], leaflist, BIT_KEYWORD, unres)) {
3355 yang_type_free(module->ctx, &leaflist->type);
3356 goto error;
3357 }
3358 }
Pavol Vicanb60a1d12016-12-03 01:56:55 +01003359 } else if (stype->base == LY_TYPE_IDENT) {
3360 if (yang_check_identityref(module, &leaflist->type, unres)) {
3361 goto error;
3362 }
Pavol Vican36aff862016-11-26 17:07:05 +01003363 }
3364
Pavol Vicanfda8c802016-12-03 02:00:42 +01003365 if (unres_schema_add_node(module, unres, &leaflist->type, UNRES_TYPE_DER, (struct lys_node *)leaflist) == -1) {
Pavol Vican36aff862016-11-26 17:07:05 +01003366 yang_type_free(module->ctx, &leaflist->type);
3367 goto error;
3368 }
3369
Pavol Vican36aff862016-11-26 17:07:05 +01003370 for (i = 0; i < leaflist->dflt_size; ++i) {
Pavol Vicanfda8c802016-12-03 02:00:42 +01003371 /* check for duplicity in case of configuration data,
3372 * in case of status data duplicities are allowed */
3373 if (leaflist->flags & LYS_CONFIG_W) {
3374 for (j = i +1; j < leaflist->dflt_size; ++j) {
3375 if (ly_strequal(leaflist->dflt[i], leaflist->dflt[j], 1)) {
3376 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, leaflist->dflt[i], "default");
3377 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", leaflist->dflt[i]);
3378 goto error;
3379 }
3380 }
3381 }
3382 /* check default value (if not defined, there still could be some restrictions
3383 * that need to be checked against a default value from a derived type) */
Pavol Vican36aff862016-11-26 17:07:05 +01003384 if (unres_schema_add_node(module, unres, &leaflist->type, UNRES_TYPE_DFLT, (struct lys_node *)(&leaflist->dflt[i])) == -1) {
3385 goto error;
3386 }
3387 }
3388
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003389 if (yang_check_iffeatures(module, NULL, leaflist, LEAF_LIST_KEYWORD, unres)) {
3390 goto error;
Pavol Vican36aff862016-11-26 17:07:05 +01003391 }
3392
3393 /* check XPath dependencies */
3394 if ((leaflist->when || leaflist->must_size) && (unres_schema_add_node(module, unres, leaflist, UNRES_XPATH, NULL) == -1)) {
3395 goto error;
3396 }
3397
3398 return EXIT_SUCCESS;
3399error:
3400 return EXIT_FAILURE;
3401}
3402
3403static int
Pavol Vicand8136a42016-11-27 13:28:04 +01003404yang_check_list(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres)
3405{
Pavol Vicanfda8c802016-12-03 02:00:42 +01003406 struct lys_node *node;
3407
Pavol Vicand8136a42016-11-27 13:28:04 +01003408 if (yang_check_typedef(module, (struct lys_node *)list, unres)) {
3409 goto error;
3410 }
3411
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003412 if (yang_check_iffeatures(module, NULL, list, LIST_KEYWORD, unres)) {
3413 goto error;
Pavol Vicand8136a42016-11-27 13:28:04 +01003414 }
3415
Pavol Vicanfda8c802016-12-03 02:00:42 +01003416 if (list->flags & LYS_CONFIG_R) {
3417 /* RFC 6020, 7.7.5 - ignore ordering when the list represents state data
3418 * ignore oredering MASK - 0x7F
3419 */
3420 list->flags &= 0x7F;
3421 }
3422 /* check - if list is configuration, key statement is mandatory
3423 * (but only if we are not in a grouping or augment, then the check is deferred) */
3424 for (node = (struct lys_node *)list; node && !(node->nodetype & (LYS_GROUPING | LYS_AUGMENT)); node = node->parent);
3425 if (!node && (list->flags & LYS_CONFIG_W) && !list->keys) {
3426 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, list, "key", "list");
3427 goto error;
3428 }
3429
Pavol Vicand8136a42016-11-27 13:28:04 +01003430 if (list->keys && yang_read_key(module, list, unres)) {
3431 goto error;
3432 }
3433
3434 if (yang_read_unique(module, list, unres)) {
3435 goto error;
3436 }
3437
3438 /* check XPath dependencies */
3439 if ((list->when || list->must_size) && (unres_schema_add_node(module, unres, list, UNRES_XPATH, NULL) == -1)) {
3440 goto error;
3441 }
3442
3443 return EXIT_SUCCESS;
3444error:
3445 return EXIT_FAILURE;
3446}
3447
3448static int
Pavol Vican36ace102016-11-28 11:46:59 +01003449yang_check_choice(struct lys_module *module, struct lys_node_choice *choice, struct unres_schema *unres)
3450{
3451 char *value;
Pavol Vican36ace102016-11-28 11:46:59 +01003452
3453 if (choice->dflt) {
3454 value = (char *)choice->dflt;
3455 choice->dflt = NULL;
3456 if (unres_schema_add_str(module, unres, choice, UNRES_CHOICE_DFLT, value) == -1) {
3457 free(value);
3458 goto error;
3459 }
3460 free(value);
3461 }
3462
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003463 if (yang_check_iffeatures(module, NULL, choice, CHOICE_KEYWORD, unres)) {
3464 goto error;
Pavol Vican36ace102016-11-28 11:46:59 +01003465 }
3466
3467 /* check XPath dependencies */
3468 if ((choice->when) && (unres_schema_add_node(module, unres, choice, UNRES_XPATH, NULL) == -1)) {
3469 goto error;
3470 }
3471
3472 return EXIT_SUCCESS;
3473error:
3474 return EXIT_FAILURE;
3475}
3476
3477static int
Pavol Vicanebc9ef82016-11-28 16:46:49 +01003478yang_check_rpc_action(struct lys_module *module, struct lys_node_rpc_action *rpc, struct unres_schema *unres)
3479{
Pavol Vicanebc9ef82016-11-28 16:46:49 +01003480 struct lys_node *node;
3481
3482 if (rpc->nodetype == LYS_ACTION) {
3483 for (node = rpc->parent; node; node = lys_parent(node)) {
3484 if ((node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
3485 || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys_size)) {
3486 LOGVAL(LYE_INPAR, LY_VLOG_NONE, NULL, strnodetype(node->nodetype), "action");
3487 goto error;
3488 }
3489 }
3490 }
3491 if (yang_check_typedef(module, (struct lys_node *)rpc, unres)) {
3492 goto error;
3493 }
3494
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003495 if (yang_check_iffeatures(module, NULL, rpc, RPC_KEYWORD, unres)) {
3496 goto error;
Pavol Vicanebc9ef82016-11-28 16:46:49 +01003497 }
3498
3499 return EXIT_SUCCESS;
3500error:
3501 return EXIT_FAILURE;
3502}
3503
3504static int
Pavol Vican29bf8802016-11-28 20:44:57 +01003505yang_check_notif(struct lys_module *module, struct lys_node_notif *notif, struct unres_schema *unres)
3506{
Pavol Vican29bf8802016-11-28 20:44:57 +01003507 if (yang_check_typedef(module, (struct lys_node *)notif, unres)) {
3508 goto error;
3509 }
3510
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003511 if (yang_check_iffeatures(module, NULL, notif, NOTIFICATION_KEYWORD, unres)) {
3512 goto error;
Pavol Vican29bf8802016-11-28 20:44:57 +01003513 }
3514
3515 /* check XPath dependencies */
3516 if ((notif->must_size) && (unres_schema_add_node(module, unres, notif, UNRES_XPATH, NULL) == -1)) {
3517 goto error;
3518 }
3519
3520 return EXIT_SUCCESS;
3521error:
3522 return EXIT_FAILURE;
3523}
3524
3525static int
Pavol Vican3ad50f82016-12-04 15:00:36 +01003526yang_check_augment(struct lys_module *module, struct lys_node_augment *augment, int config_opt, struct unres_schema *unres)
Pavol Vican3b5e82a2016-11-29 21:41:56 +01003527{
Pavol Vican3ad50f82016-12-04 15:00:36 +01003528 struct lys_node *child;
Pavol Vican3b5e82a2016-11-29 21:41:56 +01003529
Pavol Vican3ad50f82016-12-04 15:00:36 +01003530 child = augment->child;
3531 augment->child = NULL;
3532
3533 if (yang_check_nodes(module, (struct lys_node *)augment, child, config_opt, unres)) {
3534 goto error;
3535 }
3536
3537 if (yang_check_iffeatures(module, NULL, augment, AUGMENT_KEYWORD, unres)) {
3538 goto error;
3539 }
3540
3541 /* check XPath dependencies */
3542 if (augment->when && (unres_schema_add_node(module, unres, augment, UNRES_XPATH, NULL) == -1)) {
3543 goto error;
3544 }
3545
3546 return EXIT_SUCCESS;
3547error:
3548 return EXIT_FAILURE;
3549}
3550
3551static int
3552yang_check_uses(struct lys_module *module, struct lys_node_uses *uses, int config_opt, struct unres_schema *unres)
3553{
3554 uint i, size;
3555
3556 size = uses->augment_size;
3557 uses->augment_size = 0;
3558
3559 if (yang_check_iffeatures(module, NULL, uses, USES_KEYWORD, unres)) {
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003560 goto error;
Pavol Vican3b5e82a2016-11-29 21:41:56 +01003561 }
3562
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003563 for (i = 0; i < uses->refine_size; ++i) {
3564 if (yang_check_iffeatures(module, &uses->refine[i], uses, REFINE_KEYWORD, unres)) {
3565 goto error;
Pavol Vican3b5e82a2016-11-29 21:41:56 +01003566 }
3567 }
3568
Pavol Vican3ad50f82016-12-04 15:00:36 +01003569 for (i = 0; i < size; ++i) {
3570 uses->augment_size++;
3571 if (yang_check_augment(module, &uses->augment[i], config_opt, unres)) {
3572 goto error;
3573 }
3574 }
3575
Pavol Vican3b5e82a2016-11-29 21:41:56 +01003576 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) {
3577 goto error;
3578 }
3579
3580 /* check XPath dependencies */
3581 if (uses->when && (unres_schema_add_node(module, unres, uses, UNRES_XPATH, NULL) == -1)) {
3582 goto error;
3583 }
3584
3585 return EXIT_SUCCESS;
3586error:
Pavol Vican3ad50f82016-12-04 15:00:36 +01003587 for (i = uses->augment_size; i < size; ++i) {
3588 yang_free_augment(module->ctx, &uses->augment[i]);
3589 }
Pavol Vican3b5e82a2016-11-29 21:41:56 +01003590 return EXIT_FAILURE;
3591}
3592
3593static int
Pavol Vican3ad50f82016-12-04 15:00:36 +01003594yang_check_nodes(struct lys_module *module, struct lys_node *parent, struct lys_node *nodes,
3595 int config_opt, struct unres_schema *unres)
Pavol Vican05810b62016-11-23 14:07:22 +01003596{
Pavol Vican3ad50f82016-12-04 15:00:36 +01003597 struct lys_node *node = nodes, *sibling, *child;
Pavol Vican05810b62016-11-23 14:07:22 +01003598
3599 while (node) {
3600 sibling = node->next;
3601 child = node->child;
3602 node->next = NULL;
3603 node->child = NULL;
3604 node->prev = node;
3605
Pavol Vican24ba7f62016-11-28 12:15:20 +01003606 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, node)) {
3607 lys_node_unlink(node);
3608 node->next = sibling;
Pavol Vican05810b62016-11-23 14:07:22 +01003609 sibling = node;
Pavol Vican05810b62016-11-23 14:07:22 +01003610 goto error;
3611 }
Pavol Vicanec598812016-11-30 14:13:38 +01003612 config_opt = store_config_flag(node, config_opt);
Pavol Vicanfda8c802016-12-03 02:00:42 +01003613
Pavol Vican3ad50f82016-12-04 15:00:36 +01003614 if (yang_check_nodes(module, node, child, config_opt, unres)) {
Pavol Vicanfda8c802016-12-03 02:00:42 +01003615 child = NULL;
3616 goto error;
3617 }
3618 child = NULL;
3619
Pavol Vican05810b62016-11-23 14:07:22 +01003620 switch (node->nodetype) {
3621 case LYS_GROUPING:
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003622 if (yang_check_typedef(module, node, unres)) {
3623 goto error;
3624 }
3625 if (yang_check_iffeatures(module, NULL, node, GROUPING_KEYWORD, unres)) {
Pavol Vican05810b62016-11-23 14:07:22 +01003626 goto error;
3627 }
3628 break;
Pavol Vicanc54a8f72016-11-23 16:45:55 +01003629 case LYS_CONTAINER:
3630 if (yang_check_container(module, (struct lys_node_container *)node, unres)) {
3631 goto error;
3632 }
3633 break;
Pavol Vicana69aff22016-11-24 18:23:50 +01003634 case LYS_LEAF:
3635 if (yang_check_leaf(module, (struct lys_node_leaf *)node, unres)) {
3636 goto error;
3637 }
3638 break;
Pavol Vican36aff862016-11-26 17:07:05 +01003639 case LYS_LEAFLIST:
3640 if (yang_check_leaflist(module, (struct lys_node_leaflist *)node, unres)) {
3641 goto error;
3642 }
3643 break;
Pavol Vicand8136a42016-11-27 13:28:04 +01003644 case LYS_LIST:
3645 if (yang_check_list(module, (struct lys_node_list *)node, unres)) {
3646 goto error;
3647 }
3648 break;
Pavol Vican36ace102016-11-28 11:46:59 +01003649 case LYS_CHOICE:
3650 if (yang_check_choice(module, (struct lys_node_choice *)node, unres)) {
3651 goto error;
3652 }
3653 break;
Pavol Vicana420bac2016-11-28 14:51:54 +01003654 case LYS_CASE:
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003655 if (yang_check_iffeatures(module, NULL, node, CASE_KEYWORD, unres)) {
Pavol Vicana420bac2016-11-28 14:51:54 +01003656 goto error;
3657 }
3658 break;
Pavol Vicanbfa1a582016-11-28 15:35:59 +01003659 case LYS_ANYDATA:
3660 case LYS_ANYXML:
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003661 if (yang_check_iffeatures(module, NULL, node, CHOICE_KEYWORD, unres)) {
3662 goto error;
3663 }
3664 /* check XPath dependencies */
3665 if ((((struct lys_node_anydata *)node)->when) && (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1)) {
Pavol Vicanbfa1a582016-11-28 15:35:59 +01003666 goto error;
3667 }
3668 break;
Pavol Vicanebc9ef82016-11-28 16:46:49 +01003669 case LYS_RPC:
3670 case LYS_ACTION:
3671 if (yang_check_rpc_action(module, (struct lys_node_rpc_action *)node, unres)){
3672 goto error;
3673 }
3674 break;
Pavol Vican78729392016-11-28 17:18:22 +01003675 case LYS_INPUT:
3676 case LYS_OUTPUT:
3677 /* check XPath dependencies */
3678 if (((struct lys_node_inout *)node)->must_size &&
3679 (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1)) {
3680 goto error;
3681 }
3682 if (yang_check_typedef(module, node, unres)) {
3683 goto error;
3684 }
3685 break;
Pavol Vican29bf8802016-11-28 20:44:57 +01003686 case LYS_NOTIF:
3687 if (yang_check_notif(module, (struct lys_node_notif *)node, unres)) {
3688 goto error;
3689 }
3690 break;
Pavol Vican3b5e82a2016-11-29 21:41:56 +01003691 case LYS_USES:
Pavol Vican3ad50f82016-12-04 15:00:36 +01003692 if (yang_check_uses(module, (struct lys_node_uses *)node, config_opt, unres)) {
Pavol Vican3b5e82a2016-11-29 21:41:56 +01003693 goto error;
3694 }
3695 break;
Pavol Vican05810b62016-11-23 14:07:22 +01003696 default:
3697 LOGINT;
3698 sibling = node;
3699 child = NULL;
3700 goto error;
3701 }
Pavol Vican05810b62016-11-23 14:07:22 +01003702 node = sibling;
3703 }
3704
3705 return EXIT_SUCCESS;
3706error:
3707 yang_free_nodes(module->ctx, sibling);
3708 yang_free_nodes(module->ctx, child);
3709 return EXIT_FAILURE;
3710}
3711
3712static int
Pavol Vican7313fc02016-11-14 01:10:31 +01003713yang_check_sub_module(struct lys_module *module, struct unres_schema *unres, struct lys_node *node)
3714{
Pavol Vican3ad50f82016-12-04 15:00:36 +01003715 uint i, erase_identities = 1, erase_nodes = 1, aug_size;
3716
3717 aug_size = module->augment_size;
3718 module->augment_size = 0;
Pavol Vican7a7916f2016-11-21 23:38:30 +01003719
Pavol Vican7313fc02016-11-14 01:10:31 +01003720 if (yang_check_typedef(module, NULL, unres)) {
3721 goto error;
3722 }
3723
Pavol Vican7a7916f2016-11-21 23:38:30 +01003724 /* check features */
3725 for (i = 0; i < module->features_size; ++i) {
Pavol Vicandf9a95c2016-12-02 23:34:51 +01003726 if (yang_check_iffeatures(module, NULL, &module->features[i], FEATURE_KEYWORD, unres)) {
3727 goto error;
3728 }
3729 /* check for circular dependencies */
3730 if (module->features[i].iffeature_size && (unres_schema_add_node(module, unres, &module->features[i], UNRES_FEATURE, NULL) == -1)) {
3731 goto error;
Pavol Vican7a7916f2016-11-21 23:38:30 +01003732 }
3733 }
Pavol Vican36e27272016-11-22 15:47:28 +01003734 erase_identities = 0;
3735 if (yang_check_identities(module, unres)) {
3736 goto error;
3737 }
Pavol Vican05810b62016-11-23 14:07:22 +01003738 erase_nodes = 0;
Pavol Vican3ad50f82016-12-04 15:00:36 +01003739 if (yang_check_nodes(module, NULL, node, CONFIG_INHERIT_ENABLE, unres)) {
Pavol Vican05810b62016-11-23 14:07:22 +01003740 goto error;
3741 }
Pavol Vican7a7916f2016-11-21 23:38:30 +01003742
Pavol Vican3ad50f82016-12-04 15:00:36 +01003743 /* check augments */
3744 for (i = 0; i < aug_size; ++i) {
3745 module->augment_size++;
3746 if (yang_check_augment(module, &module->augment[i], CONFIG_INHERIT_ENABLE, unres)) {
3747 goto error;
3748 }
3749 if (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1) {
3750 goto error;
3751 }
3752 }
3753
Pavol Vican7313fc02016-11-14 01:10:31 +01003754 return EXIT_SUCCESS;
3755error:
Pavol Vican36e27272016-11-22 15:47:28 +01003756 if (erase_identities) {
3757 yang_free_ident_base(module->ident, 0, module->ident_size);
3758 }
Pavol Vican05810b62016-11-23 14:07:22 +01003759 if (erase_nodes) {
3760 yang_free_nodes(module->ctx, node);
3761 }
Pavol Vican3ad50f82016-12-04 15:00:36 +01003762 for (i = module->augment_size; i < aug_size; ++i) {
3763 yang_free_augment(module->ctx, &module->augment[i]);
3764 }
3765
Pavol Vican7313fc02016-11-14 01:10:31 +01003766 return EXIT_FAILURE;
3767}