blob: e8b889208ad1b463db4c87997fab34968cac8ef9 [file] [log] [blame]
Radek Krejcida04f4a2015-05-21 12:54:09 +02001/**
2 * @file yin.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief YIN parser for libyang
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +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
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcida04f4a2015-05-21 12:54:09 +020013 */
14
Radek Krejci812b10a2015-05-28 16:48:25 +020015#include <assert.h>
Radek Krejci25d782a2015-05-22 15:03:23 +020016#include <ctype.h>
Radek Krejci8b4f23c2015-06-02 16:09:25 +020017#include <errno.h>
18#include <limits.h>
Radek Krejci25d782a2015-05-22 15:03:23 +020019#include <stdint.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020020#include <stdlib.h>
21#include <string.h>
Radek Krejci25d782a2015-05-22 15:03:23 +020022#include <stddef.h>
Michal Vasko69068852015-07-13 14:34:31 +020023#include <sys/types.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020024
Radek Krejci998a0b82015-08-17 13:14:36 +020025#include "libyang.h"
26#include "common.h"
27#include "context.h"
Radek Krejci41912fe2015-10-22 10:22:12 +020028#include "dict_private.h"
Michal Vaskofcdac172015-10-07 09:35:05 +020029#include "xpath.h"
Radek Krejci998a0b82015-08-17 13:14:36 +020030#include "parser.h"
Radek Krejci998a0b82015-08-17 13:14:36 +020031#include "resolve.h"
32#include "tree_internal.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020033#include "xml_internal.h"
Radek Krejciefdd0ce2015-05-26 16:48:29 +020034
Radek Krejciadb57612016-02-16 13:34:34 +010035#define GETVAL(value, node, arg) \
36 value = lyxml_get_attr(node, arg, NULL); \
37 if (!value) { \
Radek Krejci48464ed2016-03-17 15:44:09 +010038 LOGVAL(LYE_MISSARG, LY_VLOG_NONE, NULL, arg, node->name); \
Radek Krejciadb57612016-02-16 13:34:34 +010039 goto error; \
Michal Vasko2d710f32016-02-05 12:29:21 +010040 }
Radek Krejcice7fb782015-05-29 16:52:34 +020041
Michal Vaskoe022a562016-09-27 14:24:15 +020042#define OPT_IDENT 0x01
43#define OPT_CFG_PARSE 0x02
44#define OPT_CFG_INHERIT 0x04
45#define OPT_CFG_IGNORE 0x08
46#define OPT_MODULE 0x10
Radek Krejci07d0fb92017-01-13 14:11:05 +010047static int read_yin_common(struct lys_module *, struct lys_node *, void *, LYEXT_PAR, struct lyxml_elem *, int,
48 struct unres_schema *);
Radek Krejcib388c152015-06-04 17:03:03 +020049
Radek Krejcib8048692015-08-05 13:36:34 +020050static struct lys_node *read_yin_choice(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoe022a562016-09-27 14:24:15 +020051 int valid_config, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020052static struct lys_node *read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoe022a562016-09-27 14:24:15 +020053 int valid_config, struct unres_schema *unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +020054static struct lys_node *read_yin_anydata(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoe022a562016-09-27 14:24:15 +020055 LYS_NODE type, int valid_config, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020056static struct lys_node *read_yin_container(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoe022a562016-09-27 14:24:15 +020057 int valid_config, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020058static struct lys_node *read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoe022a562016-09-27 14:24:15 +020059 int valid_config, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020060static struct lys_node *read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoe022a562016-09-27 14:24:15 +020061 int valid_config, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020062static struct lys_node *read_yin_list(struct lys_module *module,struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoe022a562016-09-27 14:24:15 +020063 int valid_config, struct unres_schema *unres);
Radek Krejcia9544502015-08-14 08:24:29 +020064static struct lys_node *read_yin_uses(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Radek Krejci3440cc52016-06-23 17:03:59 +020065 struct unres_schema *unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +020066static struct lys_node *read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoe022a562016-09-27 14:24:15 +020067 int valid_config, struct unres_schema *unres);
Michal Vaskoca7cbc42016-07-01 11:36:53 +020068static struct lys_node *read_yin_rpc_action(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoe022a562016-09-27 14:24:15 +020069 struct unres_schema *unres);
Michal Vaskob4c608c2016-09-15 09:36:20 +020070static struct lys_node *read_yin_notif(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoe022a562016-09-27 14:24:15 +020071 struct unres_schema *unres);
Radek Krejci5323b492017-01-16 15:40:11 +010072static struct lys_when *read_yin_when(struct lys_module *module, struct lyxml_elem *yin, struct unres_schema *unres);
Radek Krejci74705112015-06-05 10:25:44 +020073
Radek Krejci07d0fb92017-01-13 14:11:05 +010074/*
75 * yin - the provided XML subtree is unlinked
Radek Krejci2b999ac2017-01-18 16:22:12 +010076 * ext - pointer to the storage in the parent structure to be able to update its location after realloc
Radek Krejci07d0fb92017-01-13 14:11:05 +010077 */
78int
79lyp_yin_fill_ext(void *parent, LYEXT_PAR parent_type, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci2b999ac2017-01-18 16:22:12 +010080 struct lys_module *module, struct lyxml_elem *yin, struct lys_ext_instance ***ext,
81 uint8_t ext_index, struct unres_schema *unres)
Radek Krejci07d0fb92017-01-13 14:11:05 +010082{
83 struct unres_ext *info;
84
85 info = malloc(sizeof *info);
86 lyxml_unlink(module->ctx, yin);
87 info->data.yin = yin;
88 info->datatype = LYS_IN_YIN;
89 info->parent = parent;
Radek Krejcia7db9702017-01-20 12:55:14 +010090 info->mod = module;
Radek Krejci07d0fb92017-01-13 14:11:05 +010091 info->parent_type = parent_type;
92 info->substmt = substmt;
93 info->substmt_index = substmt_index;
Radek Krejci2b999ac2017-01-18 16:22:12 +010094 info->ext_index = ext_index;
Radek Krejci07d0fb92017-01-13 14:11:05 +010095
96 if (unres_schema_add_node(module, unres, ext, UNRES_EXT, (struct lys_node *)info) == -1) {
97 return EXIT_FAILURE;
98 }
99
100 return EXIT_SUCCESS;
101}
102
Michal Vasko0d343d12015-08-24 14:57:36 +0200103/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200104static const char *
105read_yin_subnode(struct ly_ctx *ctx, struct lyxml_elem *node, const char *name)
Radek Krejcice7fb782015-05-29 16:52:34 +0200106{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200107 int len;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200108
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200109 /* there should be <text> child */
110 if (!node->child || !node->child->name || strcmp(node->child->name, name)) {
Radek Krejci218436d2016-02-10 12:54:06 +0100111 LOGERR(LY_EVALID, "Expected \"%s\" element in \"%s\" element.", name, node->name);
Radek Krejci48464ed2016-03-17 15:44:09 +0100112 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, name, node->name);
Radek Krejci218436d2016-02-10 12:54:06 +0100113 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200114 } else if (node->child->content) {
115 len = strlen(node->child->content);
116 return lydict_insert(ctx, node->child->content, len);
Radek Krejci218436d2016-02-10 12:54:06 +0100117 } else {
118 return lydict_insert(ctx, "", 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200119 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200120}
121
Radek Krejcie534c132016-11-23 13:32:31 +0100122static int
Radek Krejci07d0fb92017-01-13 14:11:05 +0100123read_yin_subnode_ext(struct lys_module *mod, void *elem, LYEXT_PAR elem_type,
Radek Krejciac00b2a2017-01-17 14:05:00 +0100124 struct lyxml_elem *yin, LYEXT_SUBSTMT type, uint8_t i, struct unres_schema *unres)
Radek Krejcie534c132016-11-23 13:32:31 +0100125{
Radek Krejci07d0fb92017-01-13 14:11:05 +0100126 void *reallocated;
127 struct lyxml_elem *next, *child;
128 int r;
129 struct lys_ext_instance ***ext;
130 uint8_t *ext_size;
Radek Krejcie534c132016-11-23 13:32:31 +0100131
Radek Krejci07d0fb92017-01-13 14:11:05 +0100132 switch (elem_type) {
133 case LYEXT_PAR_MODULE:
134 ext_size = &((struct lys_module *)elem)->ext_size;
135 ext = &((struct lys_module *)elem)->ext;
136 break;
137 case LYEXT_PAR_NODE:
138 ext_size = &((struct lys_node *)elem)->ext_size;
139 ext = &((struct lys_node *)elem)->ext;
140 break;
141 case LYEXT_PAR_IDENT:
142 ext_size = &((struct lys_ident *)elem)->ext_size;
143 ext = &((struct lys_ident *)elem)->ext;
144 break;
145 case LYEXT_PAR_TYPE_BIT:
146 ext_size = &((struct lys_type_bit *)elem)->ext_size;
147 ext = &((struct lys_type_bit *)elem)->ext;
148 break;
149 case LYEXT_PAR_TYPE_ENUM:
150 ext_size = &((struct lys_type_enum *)elem)->ext_size;
151 ext = &((struct lys_type_enum *)elem)->ext;
152 break;
153 case LYEXT_PAR_TPDF:
154 ext_size = &((struct lys_tpdf *)elem)->ext_size;
155 ext = &((struct lys_tpdf *)elem)->ext;
156 break;
157 case LYEXT_PAR_EXT:
158 ext_size = &((struct lys_ext *)elem)->ext_size;
159 ext = &((struct lys_ext *)elem)->ext;
160 break;
161 case LYEXT_PAR_FEATURE:
162 ext_size = &((struct lys_feature *)elem)->ext_size;
163 ext = &((struct lys_feature *)elem)->ext;
164 break;
165 case LYEXT_PAR_REFINE:
166 ext_size = &((struct lys_refine *)elem)->ext_size;
167 ext = &((struct lys_refine *)elem)->ext;
168 break;
Radek Krejcifccd1442017-01-16 10:26:57 +0100169 case LYEXT_PAR_MUST:
170 case LYEXT_PAR_PATTERN:
171 case LYEXT_PAR_LENGTH:
172 case LYEXT_PAR_RANGE:
173 ext_size = &((struct lys_restr *)elem)->ext_size;
174 ext = &((struct lys_restr *)elem)->ext;
175 break;
Radek Krejci5323b492017-01-16 15:40:11 +0100176 case LYEXT_PAR_WHEN:
177 ext_size = &((struct lys_when *)elem)->ext_size;
178 ext = &((struct lys_when *)elem)->ext;
179 break;
Radek Krejci07d0fb92017-01-13 14:11:05 +0100180 default:
Radek Krejcifccd1442017-01-16 10:26:57 +0100181 LOGERR(LY_EINT, "parent type %d", elem_type);
Radek Krejcie534c132016-11-23 13:32:31 +0100182 return EXIT_FAILURE;
183 }
184
Radek Krejcifccd1442017-01-16 10:26:57 +0100185 if (type == LYEXT_SUBSTMT_SELF) {
186 /* parse for the statement self, not for the substatement */
187 child = yin;
188 next = NULL;
189 goto parseext;
190 }
191
Radek Krejci07d0fb92017-01-13 14:11:05 +0100192 LY_TREE_FOR_SAFE(yin->child, next, child) {
193 if (!strcmp(child->ns->value, LY_NSYIN)) {
194 /* skip the regular YIN nodes */
195 continue;
196 }
197
198 /* parse it as extension */
Radek Krejcifccd1442017-01-16 10:26:57 +0100199parseext:
Radek Krejci07d0fb92017-01-13 14:11:05 +0100200
201 /* first, allocate a space for the extension instance in the parent elem */
202 reallocated = realloc(*ext, (1 + (*ext_size)) * sizeof **ext);
203 if (!reallocated) {
204 LOGMEM;
205 return EXIT_FAILURE;
206 }
207 (*ext) = reallocated;
208
209 /* init memory */
210 (*ext)[(*ext_size)] = NULL;
211
212 /* parse YIN data */
Radek Krejci2b999ac2017-01-18 16:22:12 +0100213 r = lyp_yin_fill_ext(elem, elem_type, type, i, mod, child, &(*ext), (*ext_size), unres);
Radek Krejci07d0fb92017-01-13 14:11:05 +0100214 (*ext_size)++;
215 if (r) {
216 return EXIT_FAILURE;
217 }
218
Radek Krejcifccd1442017-01-16 10:26:57 +0100219 /* done - do not free the child, it is unlinked in lyp_yin_fill_ext */
Radek Krejci07d0fb92017-01-13 14:11:05 +0100220 }
221
Radek Krejcie534c132016-11-23 13:32:31 +0100222 return EXIT_SUCCESS;
223}
224
Michal Vasko0d343d12015-08-24 14:57:36 +0200225/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200226static int
Radek Krejci9de2c042016-10-19 16:53:06 +0200227fill_yin_iffeature(struct lys_node *parent, int parent_is_feature, struct lyxml_elem *yin, struct lys_iffeature *iffeat,
228 struct unres_schema *unres)
Michal Vasko1d337e12016-02-15 12:32:04 +0100229{
Radek Krejci5323b492017-01-16 15:40:11 +0100230 int r, c_ext = 0;
Michal Vasko1d337e12016-02-15 12:32:04 +0100231 const char *value;
Radek Krejci5323b492017-01-16 15:40:11 +0100232 struct lyxml_elem *node, *next;
Michal Vasko1d337e12016-02-15 12:32:04 +0100233
234 GETVAL(value, yin, "name");
Michal Vasko97b32be2016-07-25 10:59:53 +0200235
236 if ((lys_node_module(parent)->version != 2) && ((value[0] == '(') || strchr(value, ' '))) {
237 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +0200238error:
Michal Vasko1d337e12016-02-15 12:32:04 +0100239 return EXIT_FAILURE;
240 }
241
Michal Vasko56d082c2016-10-25 14:00:42 +0200242 if (!(value = transform_iffeat_schema2json(parent->module, value))) {
Michal Vasko1d337e12016-02-15 12:32:04 +0100243 return EXIT_FAILURE;
244 }
245
Radek Krejci9de2c042016-10-19 16:53:06 +0200246 r = resolve_iffeature_compile(iffeat, value, parent, parent_is_feature, unres);
Michal Vasko1d337e12016-02-15 12:32:04 +0100247 lydict_remove(parent->module->ctx, value);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200248 if (r) {
249 return EXIT_FAILURE;
Michal Vasko1d337e12016-02-15 12:32:04 +0100250 }
251
Radek Krejci5323b492017-01-16 15:40:11 +0100252 LY_TREE_FOR_SAFE(yin->child, next, node) {
253 if (!node->ns) {
254 /* garbage */
255 lyxml_free(parent->module->ctx, node);
256 } else if (strcmp(node->ns->value, LY_NSYIN)) {
257 /* extension */
258 c_ext++;
259 } else {
260 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name, "if-feature");
261 return EXIT_FAILURE;
262 }
263 }
264 if (c_ext) {
265 iffeat->ext = calloc(c_ext, sizeof *iffeat->ext);
266 if (!iffeat->ext) {
267 LOGMEM;
268 return EXIT_FAILURE;
269 }
270 LY_TREE_FOR_SAFE(yin->child, next, node) {
271 /* extensions */
272 r = lyp_yin_fill_ext(iffeat, LYEXT_PAR_IDENT, 0, 0, parent->module, node,
Radek Krejci2b999ac2017-01-18 16:22:12 +0100273 &iffeat->ext, iffeat->ext_size, unres);
Radek Krejci5323b492017-01-16 15:40:11 +0100274 iffeat->ext_size++;
275 if (r) {
276 return EXIT_FAILURE;
277 }
278 }
279 }
280
Radek Krejci9ff0a922016-07-14 13:08:05 +0200281 return EXIT_SUCCESS;
Michal Vasko1d337e12016-02-15 12:32:04 +0100282}
283
284/* logs directly */
285static int
Michal Vaskof02e3742015-08-05 16:27:02 +0200286fill_yin_identity(struct lys_module *module, struct lyxml_elem *yin, struct lys_ident *ident, struct unres_schema *unres)
Radek Krejci04581c62015-05-22 21:24:00 +0200287{
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200288 struct lyxml_elem *node, *next;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200289 const char *value;
Radek Krejci018f1f52016-08-03 16:01:20 +0200290 int rc;
Radek Krejcie534c132016-11-23 13:32:31 +0100291 int c_ftrs = 0, c_base = 0, c_ext = 0;
Radek Krejci04581c62015-05-22 21:24:00 +0200292
Michal Vasko4cfcd252015-08-03 14:31:10 +0200293 GETVAL(value, yin, "name");
Michal Vaskoc94283a2015-10-29 09:07:20 +0100294 ident->name = value;
Michal Vasko4cfcd252015-08-03 14:31:10 +0200295
Radek Krejci07d0fb92017-01-13 14:11:05 +0100296 if (read_yin_common(module, NULL, ident, LYEXT_PAR_IDENT, yin, OPT_IDENT | OPT_MODULE, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200297 return EXIT_FAILURE;
298 }
Radek Krejci04581c62015-05-22 21:24:00 +0200299
Pavol Vicand6cda452016-07-13 15:08:29 +0200300 if (dup_identities_check(ident->name, module)) {
301 return EXIT_FAILURE;
302 }
303
Radek Krejcie534c132016-11-23 13:32:31 +0100304 LY_TREE_FOR(yin->child, node) {
305 if (strcmp(node->ns->value, LY_NSYIN)) {
306 /* extension */
307 c_ext++;
308 } else if (!strcmp(node->name, "base")) {
Radek Krejci018f1f52016-08-03 16:01:20 +0200309 if (c_base && (module->version < 2)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100310 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "base", "identity");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200311 return EXIT_FAILURE;
312 }
Radek Krejci018f1f52016-08-03 16:01:20 +0200313 c_base++;
Radek Krejciad73b6f2016-02-09 15:42:55 +0100314
Radek Krejci018f1f52016-08-03 16:01:20 +0200315 } else if ((module->version >= 2) && !strcmp(node->name, "if-feature")) {
316 c_ftrs++;
317
318 } else {
319 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name, "identity");
320 return EXIT_FAILURE;
321 }
322 }
323
324 if (c_base) {
325 ident->base_size = 0;
326 ident->base = calloc(c_base, sizeof *ident->base);
327 if (!ident->base) {
328 LOGMEM;
329 return EXIT_FAILURE;
330 }
331 }
Radek Krejci018f1f52016-08-03 16:01:20 +0200332 if (c_ftrs) {
333 ident->iffeature = calloc(c_ftrs, sizeof *ident->iffeature);
334 if (!ident->iffeature) {
335 LOGMEM;
336 return EXIT_FAILURE;
337 }
338 }
Radek Krejcie534c132016-11-23 13:32:31 +0100339 if (c_ext) {
340 ident->ext = calloc(c_ext, sizeof *ident->ext);
341 if (!ident->ext) {
342 LOGMEM;
343 return EXIT_FAILURE;
344 }
345 }
Radek Krejci018f1f52016-08-03 16:01:20 +0200346
Radek Krejcie534c132016-11-23 13:32:31 +0100347 LY_TREE_FOR_SAFE(yin->child, next, node) {
348 if (strcmp(node->ns->value, LY_NSYIN)) {
349 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +0100350 rc = lyp_yin_fill_ext(ident, LYEXT_PAR_IDENT, 0, 0, module, node, &ident->ext, ident->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +0100351 ident->ext_size++;
352 if (rc) {
353 goto error;
354 }
355 } else if (!strcmp(node->name, "base")) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200356 GETVAL(value, node, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +0100357 value = transform_schema2json(module, value);
Michal Vaskoc94283a2015-10-29 09:07:20 +0100358 if (!value) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +0200359 return EXIT_FAILURE;
360 }
Michal Vaskoc94283a2015-10-29 09:07:20 +0100361
Radek Krejci48464ed2016-03-17 15:44:09 +0100362 if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, value) == -1) {
Michal Vaskoc94283a2015-10-29 09:07:20 +0100363 lydict_remove(module->ctx, value);
364 return EXIT_FAILURE;
365 }
366 lydict_remove(module->ctx, value);
Radek Krejci018f1f52016-08-03 16:01:20 +0200367 } else if (!strcmp(node->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +0200368 rc = fill_yin_iffeature((struct lys_node *)ident, 0, node, &ident->iffeature[ident->iffeature_size], unres);
Radek Krejci018f1f52016-08-03 16:01:20 +0200369 ident->iffeature_size++;
370 if (rc) {
371 return EXIT_FAILURE;
372 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200373 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200374 }
Radek Krejci04581c62015-05-22 21:24:00 +0200375
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200376 return EXIT_SUCCESS;
Michal Vasko2d851a92015-10-20 16:16:36 +0200377
378error:
379 return EXIT_FAILURE;
Radek Krejci04581c62015-05-22 21:24:00 +0200380}
381
Michal Vasko0d343d12015-08-24 14:57:36 +0200382/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200383static int
Radek Krejcifccd1442017-01-16 10:26:57 +0100384read_restr_substmt(struct lys_module *module, LYEXT_PAR restr_type, struct lys_restr *restr, struct lyxml_elem *yin,
385 struct unres_schema *unres)
Radek Krejci41726f92015-06-19 13:11:05 +0200386{
Radek Krejcifccd1442017-01-16 10:26:57 +0100387 struct lyxml_elem *child, *next;
Radek Krejci461d1622015-06-30 14:06:28 +0200388 const char *value;
Radek Krejci41726f92015-06-19 13:11:05 +0200389
Radek Krejcifccd1442017-01-16 10:26:57 +0100390 LY_TREE_FOR_SAFE(yin->child, next, child) {
391 if (!child->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200392 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200393 continue;
Radek Krejcifccd1442017-01-16 10:26:57 +0100394 } else if (strcmp(child->ns->value, LY_NSYIN)) {
395 /* extension */
Radek Krejciac00b2a2017-01-17 14:05:00 +0100396 if (read_yin_subnode_ext(module, restr, restr_type, child, LYEXT_SUBSTMT_SELF, 0, unres)) {
Radek Krejcifccd1442017-01-16 10:26:57 +0100397 return EXIT_FAILURE;
398 }
399 } else if (!strcmp(child->name, "description")) {
Radek Krejci41726f92015-06-19 13:11:05 +0200400 if (restr->dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100401 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200402 return EXIT_FAILURE;
403 }
Radek Krejciac00b2a2017-01-17 14:05:00 +0100404 if (read_yin_subnode_ext(module, restr, restr_type, child, LYEXT_SUBSTMT_DESCRIPTION, 0, unres)) {
Radek Krejcifccd1442017-01-16 10:26:57 +0100405 return EXIT_FAILURE;
406 }
407 restr->dsc = read_yin_subnode(module->ctx, child, "text");
Radek Krejci41726f92015-06-19 13:11:05 +0200408 if (!restr->dsc) {
409 return EXIT_FAILURE;
410 }
411 } else if (!strcmp(child->name, "reference")) {
412 if (restr->ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100413 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200414 return EXIT_FAILURE;
415 }
Radek Krejciac00b2a2017-01-17 14:05:00 +0100416 if (read_yin_subnode_ext(module, restr, restr_type, child, LYEXT_SUBSTMT_REFERENCE, 0, unres)) {
Radek Krejcifccd1442017-01-16 10:26:57 +0100417 return EXIT_FAILURE;
418 }
419 restr->ref = read_yin_subnode(module->ctx, child, "text");
Radek Krejci41726f92015-06-19 13:11:05 +0200420 if (!restr->ref) {
421 return EXIT_FAILURE;
422 }
423 } else if (!strcmp(child->name, "error-app-tag")) {
424 if (restr->eapptag) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100425 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200426 return EXIT_FAILURE;
427 }
Radek Krejciac00b2a2017-01-17 14:05:00 +0100428 if (read_yin_subnode_ext(module, restr, restr_type, child, LYEXT_SUBSTMT_ERRTAG, 0, unres)) {
Radek Krejcifccd1442017-01-16 10:26:57 +0100429 return EXIT_FAILURE;
430 }
Michal Vasko54e426f2015-07-07 15:38:02 +0200431 GETVAL(value, child, "value");
Radek Krejcifccd1442017-01-16 10:26:57 +0100432 restr->eapptag = lydict_insert(module->ctx, value, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200433 } else if (!strcmp(child->name, "error-message")) {
434 if (restr->emsg) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100435 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200436 return EXIT_FAILURE;
437 }
Radek Krejciac00b2a2017-01-17 14:05:00 +0100438 if (read_yin_subnode_ext(module, restr, restr_type, child, LYEXT_SUBSTMT_ERRMSG, 0, unres)) {
Radek Krejcifccd1442017-01-16 10:26:57 +0100439 return EXIT_FAILURE;
440 }
441 restr->emsg = read_yin_subnode(module->ctx, child, "value");
Radek Krejci41726f92015-06-19 13:11:05 +0200442 if (!restr->emsg) {
443 return EXIT_FAILURE;
444 }
445 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100446 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200447 return EXIT_FAILURE;
448 }
Radek Krejci41726f92015-06-19 13:11:05 +0200449 }
450
451 return EXIT_SUCCESS;
Michal Vaskoc8ef47f2015-06-29 14:56:19 +0200452
453error:
454 return EXIT_FAILURE;
Radek Krejci41726f92015-06-19 13:11:05 +0200455}
456
Michal Vasko88c29542015-11-27 14:57:53 +0100457/* logs directly, returns EXIT_SUCCESS, EXIT_FAILURE, -1 */
458int
Radek Krejcib8048692015-08-05 13:36:34 +0200459fill_yin_type(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_type *type,
Radek Krejci3a5501d2016-07-18 22:03:34 +0200460 int tpdftype, struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200461{
Michal Vasko0aee5c12016-06-17 14:27:26 +0200462 const char *value, *name;
Radek Krejci3a5501d2016-07-18 22:03:34 +0200463 struct lys_node *siter;
Radek Krejcie534c132016-11-23 13:32:31 +0100464 struct lyxml_elem *next, *next2, *node, *child, exts;
Radek Krejcifccd1442017-01-16 10:26:57 +0100465 struct lys_restr **restrs, *restr;
Radek Krejci1c5890d2016-08-02 13:15:42 +0200466 struct lys_type_bit bit, *bits_sc = NULL;
467 struct lys_type_enum *enms_sc = NULL; /* shortcut */
Radek Krejcie663e012016-08-01 17:12:34 +0200468 struct lys_type *dertype;
Radek Krejcie534c132016-11-23 13:32:31 +0100469 int i, j, rc, val_set, c_ftrs, c_ext = 0;
Radek Krejcidc008d72016-02-17 13:12:14 +0100470 int ret = -1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200471 int64_t v, v_;
Radek Krejci994b6f62015-06-18 16:47:27 +0200472 int64_t p, p_;
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200473 size_t len;
474 char *buf, modifier;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200475
Radek Krejcie534c132016-11-23 13:32:31 +0100476 /* init */
477 memset(&exts, 0, sizeof exts);
478
Radek Krejci8de7b0f2015-07-02 11:43:42 +0200479 GETVAL(value, yin, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +0100480 value = transform_schema2json(module, value);
Michal Vasko1dca6882015-10-22 14:29:42 +0200481 if (!value) {
482 goto error;
Michal Vaskoa5835e92015-10-20 15:07:39 +0200483 }
Michal Vaskob362b4c2015-10-20 15:15:46 +0200484
485 i = parse_identifier(value);
486 if (i < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100487 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]);
Michal Vasko88c29542015-11-27 14:57:53 +0100488 lydict_remove(module->ctx, value);
Michal Vaskob362b4c2015-10-20 15:15:46 +0200489 goto error;
490 }
491 /* module name */
Radek Krejci225376f2016-02-16 17:36:22 +0100492 name = value;
Michal Vaskob362b4c2015-10-20 15:15:46 +0200493 if (value[i]) {
494 type->module_name = lydict_insert(module->ctx, value, i);
Radek Krejci225376f2016-02-16 17:36:22 +0100495 name += i;
496 if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100497 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name);
Michal Vasko88c29542015-11-27 14:57:53 +0100498 lydict_remove(module->ctx, value);
Michal Vaskob362b4c2015-10-20 15:15:46 +0200499 goto error;
500 }
Michal Vasko5b61c6d2016-06-06 15:15:30 +0200501 /* name is in dictionary, but moved */
Radek Krejci225376f2016-02-16 17:36:22 +0100502 ++name;
Michal Vaskob362b4c2015-10-20 15:15:46 +0200503 }
Michal Vaskoa5835e92015-10-20 15:07:39 +0200504
Radek Krejci225376f2016-02-16 17:36:22 +0100505 rc = resolve_superior_type(name, type->module_name, module, parent, &type->der);
Michal Vaskof7eee892015-08-24 15:03:11 +0200506 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100507 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, type->module_name);
Michal Vasko5b61c6d2016-06-06 15:15:30 +0200508 lydict_remove(module->ctx, value);
Michal Vaskof7eee892015-08-24 15:03:11 +0200509 goto error;
Michal Vasko88c29542015-11-27 14:57:53 +0100510
511 /* the type could not be resolved or it was resolved to an unresolved typedef */
Michal Vaskof7eee892015-08-24 15:03:11 +0200512 } else if (rc == EXIT_FAILURE) {
Michal Vasko01c6fd22016-05-20 11:43:05 +0200513 LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name);
Michal Vasko5b61c6d2016-06-06 15:15:30 +0200514 lydict_remove(module->ctx, value);
Radek Krejcidc008d72016-02-17 13:12:14 +0100515 ret = EXIT_FAILURE;
516 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200517 }
Michal Vasko5b61c6d2016-06-06 15:15:30 +0200518 lydict_remove(module->ctx, value);
Radek Krejcic13db382016-08-16 10:52:42 +0200519
Radek Krejci9b6aad22016-09-20 15:55:51 +0200520 if (type->base == LY_TYPE_ERR) {
Radek Krejcic13db382016-08-16 10:52:42 +0200521 /* resolved type in grouping, decrease the grouping's nacm number to indicate that one less
Radek Krejci9b6aad22016-09-20 15:55:51 +0200522 * 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 +0200523 for (siter = parent; siter && (siter->nodetype != LYS_GROUPING); siter = lys_parent(siter));
524 if (siter) {
Radek Krejci6ff885d2017-01-03 14:06:22 +0100525#if __BYTE_ORDER == __LITTLE_ENDIAN
526 if (!((uint8_t*)&((struct lys_node_grp *)siter)->flags)[1]) {
Radek Krejcic13db382016-08-16 10:52:42 +0200527 LOGINT;
528 goto error;
529 }
Radek Krejci6ff885d2017-01-03 14:06:22 +0100530 ((uint8_t*)&((struct lys_node_grp *)siter)->flags)[1]--;
531#else
532 if (!((uint8_t*)&((struct lys_node_grp *)siter)->flags)[0]) {
533 LOGINT;
534 goto error;
535 }
536 ((uint8_t*)&((struct lys_node_grp *)siter)->flags)[0]--;
537#endif
Radek Krejcic13db382016-08-16 10:52:42 +0200538 } else {
539 LOGINT;
540 goto error;
541 }
542 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200543 type->base = type->der->type.base;
Radek Krejci25d782a2015-05-22 15:03:23 +0200544
Radek Krejcicf509982015-12-15 09:22:44 +0100545 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +0100546 if (lyp_check_status(type->parent->flags, type->parent->module, type->parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +0100547 type->der->flags, type->der->module, type->der->name, parent)) {
Radek Krejcicf509982015-12-15 09:22:44 +0100548 return -1;
549 }
550
Radek Krejcie534c132016-11-23 13:32:31 +0100551 /* parse extension instances */
552 LY_TREE_FOR_SAFE(yin->child, next, node) {
553 if (!node->ns) {
554 /* garbage */
555 lyxml_free(module->ctx, node);
556 continue;
557 } else if (!strcmp(node->ns->value, LY_NSYIN)) {
558 /* YANG (YIN) statements - process later */
559 continue;
560 }
561
562 lyxml_unlink_elem(module->ctx, node, 2);
563 lyxml_add_child(module->ctx, &exts, node);
564 c_ext++;
565 }
566 if (c_ext) {
567 type->ext = calloc(c_ext, sizeof *type->ext);
568 if (!type->ext) {
569 LOGMEM;
570 goto error;
571 }
572 LY_TREE_FOR_SAFE(exts.child, next, node) {
Radek Krejci2b999ac2017-01-18 16:22:12 +0100573 rc = lyp_yin_fill_ext(type, LYEXT_PAR_TYPE, 0, 0, module, node, &type->ext, type->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +0100574 type->ext_size++;
575 if (rc) {
576 goto error;
577 }
578 }
579 }
580
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200581 switch (type->base) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200582 case LY_TYPE_BITS:
Radek Krejci994b6f62015-06-18 16:47:27 +0200583 /* RFC 6020 9.7.4 - bit */
584
585 /* get bit specifications, at least one must be present */
586 LY_TREE_FOR_SAFE(yin->child, next, node) {
587 if (!strcmp(node->name, "bit")) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200588 type->info.bits.count++;
Radek Krejci41726f92015-06-19 13:11:05 +0200589 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100590 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci69794c62016-08-02 11:01:21 +0200591 type->info.bits.count = 0;
Radek Krejci41726f92015-06-19 13:11:05 +0200592 goto error;
Radek Krejci994b6f62015-06-18 16:47:27 +0200593 }
594 }
Radek Krejcie663e012016-08-01 17:12:34 +0200595 dertype = &type->der->type;
596 if (!dertype->der) {
597 if (!type->info.bits.count) {
598 /* type is derived directly from buit-in bits type and bit statement is required */
599 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type");
600 goto error;
601 }
602 } else {
603 for (; !dertype->info.enums.count; dertype = &dertype->der->type);
604 if (module->version < 2 && type->info.bits.count) {
605 /* type is not directly derived from buit-in bits type and bit statement is prohibited,
606 * since YANG 1.1 the bit statements can be used to restrict the base bits type */
607 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "bit");
Radek Krejci69794c62016-08-02 11:01:21 +0200608 type->info.bits.count = 0;
Radek Krejcie663e012016-08-01 17:12:34 +0200609 goto error;
610 }
Radek Krejciac781922015-07-09 15:35:14 +0200611 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200612
613 type->info.bits.bit = calloc(type->info.bits.count, sizeof *type->info.bits.bit);
Michal Vasko253035f2015-12-17 16:58:13 +0100614 if (!type->info.bits.bit) {
615 LOGMEM;
616 goto error;
617 }
Radek Krejci73adb602015-07-02 18:07:40 +0200618 p = 0;
619 i = -1;
620 LY_TREE_FOR(yin->child, next) {
621 i++;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200622 c_ftrs = 0;
Radek Krejci73adb602015-07-02 18:07:40 +0200623
624 GETVAL(value, next, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +0100625 if (lyp_check_identifier(value, LY_IDENT_SIMPLE, NULL, NULL)) {
Michal Vasko2d26a022015-12-07 09:27:21 +0100626 goto error;
627 }
628
Radek Krejci994b6f62015-06-18 16:47:27 +0200629 type->info.bits.bit[i].name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci07d0fb92017-01-13 14:11:05 +0100630 if (read_yin_common(module, NULL, &type->info.bits.bit[i], LYEXT_PAR_TYPE_BIT, next, 0, unres)) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200631 type->info.bits.count = i + 1;
632 goto error;
633 }
634
Radek Krejcie663e012016-08-01 17:12:34 +0200635 if (!dertype->der) { /* directly derived type from bits built-in type */
636 /* check the name uniqueness */
637 for (j = 0; j < i; j++) {
638 if (!strcmp(type->info.bits.bit[j].name, type->info.bits.bit[i].name)) {
639 LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, type->info.bits.bit[i].name);
640 type->info.bits.count = i + 1;
641 goto error;
642 }
643 }
644 } else {
645 /* restricted bits type - the name MUST be used in the base type */
646 bits_sc = dertype->info.bits.bit;
647 for (j = 0; j < dertype->info.bits.count; j++) {
648 if (ly_strequal(bits_sc[j].name, value, 1)) {
649 break;
650 }
651 }
652 if (j == dertype->info.bits.count) {
653 LOGVAL(LYE_BITS_INNAME, LY_VLOG_NONE, NULL, value);
Radek Krejci994b6f62015-06-18 16:47:27 +0200654 type->info.bits.count = i + 1;
655 goto error;
656 }
657 }
658
Radek Krejcie663e012016-08-01 17:12:34 +0200659
Radek Krejci0d70c372015-07-02 16:23:10 +0200660 p_ = -1;
Radek Krejci73adb602015-07-02 18:07:40 +0200661 LY_TREE_FOR(next->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200662 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
663 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200664 continue;
Radek Krejci994b6f62015-06-18 16:47:27 +0200665 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200666
Radek Krejci0d70c372015-07-02 16:23:10 +0200667 if (!strcmp(node->name, "position")) {
Radek Krejcie663e012016-08-01 17:12:34 +0200668 if (p_ != -1) {
669 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, next->name);
670 type->info.bits.count = i + 1;
671 goto error;
672 }
673
Radek Krejci0d70c372015-07-02 16:23:10 +0200674 GETVAL(value, node, "value");
Radek Krejci7511f402015-07-10 09:56:30 +0200675 p_ = strtoll(value, NULL, 10);
Radek Krejci0d70c372015-07-02 16:23:10 +0200676
677 /* range check */
Radek Krejcib8ca1082015-07-10 11:24:11 +0200678 if (p_ < 0 || p_ > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100679 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "bit/position");
Radek Krejci0d70c372015-07-02 16:23:10 +0200680 type->info.bits.count = i + 1;
681 goto error;
682 }
683 type->info.bits.bit[i].pos = (uint32_t)p_;
684
Radek Krejcie663e012016-08-01 17:12:34 +0200685 if (!dertype->der) { /* directly derived type from bits built-in type */
686 /* keep the highest enum value for automatic increment */
687 if (type->info.bits.bit[i].pos >= p) {
688 p = type->info.bits.bit[i].pos;
689 p++;
690 } else {
691 /* check that the value is unique */
692 for (j = 0; j < i; j++) {
693 if (type->info.bits.bit[j].pos == type->info.bits.bit[i].pos) {
694 LOGVAL(LYE_BITS_DUPVAL, LY_VLOG_NONE, NULL,
695 type->info.bits.bit[i].pos, type->info.bits.bit[i].name,
696 type->info.bits.bit[j].name);
697 type->info.bits.count = i + 1;
698 goto error;
699 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200700 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200701 }
702 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200703
704 } else if ((module->version >= 2) && !strcmp(node->name, "if-feature")) {
705 c_ftrs++;
Radek Krejci0d70c372015-07-02 16:23:10 +0200706 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100707 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200708 goto error;
Radek Krejci994b6f62015-06-18 16:47:27 +0200709 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200710 }
Radek Krejcie663e012016-08-01 17:12:34 +0200711
712 if (!dertype->der) { /* directly derived type from bits built-in type */
713 if (p_ == -1) {
714 /* assign value automatically */
715 if (p > UINT32_MAX) {
716 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position");
717 type->info.bits.count = i + 1;
718 goto error;
719 }
720 type->info.bits.bit[i].pos = (uint32_t)p;
721 type->info.bits.bit[i].flags |= LYS_AUTOASSIGNED;
722 p++;
Radek Krejci994b6f62015-06-18 16:47:27 +0200723 }
Radek Krejcie663e012016-08-01 17:12:34 +0200724 } else { /* restricted bits type */
725 if (p_ == -1) {
726 /* automatically assign position from base type */
727 type->info.bits.bit[i].pos = bits_sc[j].pos;
728 type->info.bits.bit[i].flags |= LYS_AUTOASSIGNED;
729 } else {
730 /* check that the assigned position corresponds to the original
731 * position of the bit in the base type */
732 if (p_ != bits_sc[j].pos) {
733 /* p_ - assigned position in restricted bits
734 * bits_sc[j].pos - position assigned to the corresponding bit (detected above) in base type */
735 LOGVAL(LYE_BITS_INVAL, LY_VLOG_NONE, NULL, type->info.bits.bit[i].pos,
Radek Krejci541a45d2016-08-02 13:12:07 +0200736 type->info.bits.bit[i].name, bits_sc[j].pos);
Radek Krejcie663e012016-08-01 17:12:34 +0200737 type->info.bits.count = i + 1;
738 goto error;
739 }
740 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200741 }
Radek Krejci9a1b95a2015-07-09 15:32:21 +0200742
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200743 /* if-features */
744 if (c_ftrs) {
745 bits_sc = &type->info.bits.bit[i];
746 bits_sc->iffeature = calloc(c_ftrs, sizeof *bits_sc->iffeature);
747 if (!bits_sc->iffeature) {
748 LOGMEM;
Radek Krejci994b6f62015-06-18 16:47:27 +0200749 type->info.bits.count = i + 1;
750 goto error;
751 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200752
753 LY_TREE_FOR(next->child, node) {
754 if (!strcmp(node->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +0200755 rc = fill_yin_iffeature((struct lys_node *)type->parent, 0, node,
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200756 &bits_sc->iffeature[bits_sc->iffeature_size], unres);
757 bits_sc->iffeature_size++;
758 if (rc) {
759 type->info.bits.count = i + 1;
760 goto error;
761 }
762 }
763 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200764 }
Radek Krejci9a1b95a2015-07-09 15:32:21 +0200765
766 /* keep them ordered by position */
767 j = i;
768 while (j && type->info.bits.bit[j - 1].pos > type->info.bits.bit[j].pos) {
769 /* switch them */
770 memcpy(&bit, &type->info.bits.bit[j], sizeof bit);
771 memcpy(&type->info.bits.bit[j], &type->info.bits.bit[j - 1], sizeof bit);
772 memcpy(&type->info.bits.bit[j - 1], &bit, sizeof bit);
773 j--;
774 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200775 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200776 break;
Radek Krejci25d782a2015-05-22 15:03:23 +0200777
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200778 case LY_TYPE_DEC64:
Radek Krejcif9401c32015-06-26 16:47:36 +0200779 /* RFC 6020 9.2.4 - range and 9.3.4 - fraction-digits */
Radek Krejci73adb602015-07-02 18:07:40 +0200780 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200781
Radek Krejcif9401c32015-06-26 16:47:36 +0200782 if (!strcmp(node->name, "range")) {
783 if (type->info.dec64.range) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100784 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200785 goto error;
786 }
787
788 GETVAL(value, node, "value");
Radek Krejcif9401c32015-06-26 16:47:36 +0200789 type->info.dec64.range = calloc(1, sizeof *type->info.dec64.range);
Michal Vasko253035f2015-12-17 16:58:13 +0100790 if (!type->info.dec64.range) {
791 LOGMEM;
792 goto error;
793 }
Radek Krejcif9401c32015-06-26 16:47:36 +0200794 type->info.dec64.range->expr = lydict_insert(module->ctx, value, 0);
795
796 /* get possible substatements */
Radek Krejcifccd1442017-01-16 10:26:57 +0100797 if (read_restr_substmt(module, LYEXT_PAR_RANGE, type->info.dec64.range, node, unres)) {
Radek Krejcif9401c32015-06-26 16:47:36 +0200798 goto error;
799 }
800 } else if (!strcmp(node->name, "fraction-digits")) {
801 if (type->info.dec64.dig) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100802 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200803 goto error;
804 }
805 GETVAL(value, node, "value");
806 v = strtol(value, NULL, 10);
807
808 /* range check */
809 if (v < 1 || v > 18) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100810 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200811 goto error;
812 }
813 type->info.dec64.dig = (uint8_t)v;
Radek Krejci8c3b4b62016-06-17 14:32:12 +0200814 type->info.dec64.div = 10;
815 for (i = 1; i < v; i++) {
816 type->info.dec64.div *= 10;
817 }
Radek Krejcif9401c32015-06-26 16:47:36 +0200818 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100819 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200820 goto error;
821 }
Radek Krejcif9401c32015-06-26 16:47:36 +0200822 }
823
824 /* mandatory sub-statement(s) check */
825 if (!type->info.dec64.dig && !type->der->type.der) {
826 /* decimal64 type directly derived from built-in type requires fraction-digits */
Radek Krejci48464ed2016-03-17 15:44:09 +0100827 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type");
Radek Krejcif9401c32015-06-26 16:47:36 +0200828 goto error;
829 }
Radek Krejci7511f402015-07-10 09:56:30 +0200830 if (type->info.dec64.dig && type->der->type.der) {
831 /* type is not directly derived from buit-in type and fraction-digits statement is prohibited */
Radek Krejci48464ed2016-03-17 15:44:09 +0100832 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits");
Radek Krejci7511f402015-07-10 09:56:30 +0200833 goto error;
834 }
Radek Krejci4800f652016-09-08 14:02:52 +0200835
836 /* copy fraction-digits specification from parent type for easier internal use */
837 if (type->der->type.der) {
838 type->info.dec64.dig = type->der->type.info.dec64.dig;
839 type->info.dec64.div = type->der->type.info.dec64.div;
840 }
841
Pavol Vican3c8ee2b2016-09-29 13:18:13 +0200842 if (type->info.dec64.range && lyp_check_length_range(type->info.dec64.range->expr, type)) {
843 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "range");
844 goto error;
845 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200846 break;
Radek Krejci25d782a2015-05-22 15:03:23 +0200847
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200848 case LY_TYPE_ENUM:
Radek Krejci994b6f62015-06-18 16:47:27 +0200849 /* RFC 6020 9.6 - enum */
Radek Krejci25d782a2015-05-22 15:03:23 +0200850
Radek Krejci994b6f62015-06-18 16:47:27 +0200851 /* get enum specifications, at least one must be present */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200852 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200853
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200854 if (!strcmp(node->name, "enum")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200855 type->info.enums.count++;
Radek Krejci5fbc9162015-06-19 14:11:11 +0200856 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100857 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci69794c62016-08-02 11:01:21 +0200858 type->info.enums.count = 0;
Radek Krejci5fbc9162015-06-19 14:11:11 +0200859 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200860 }
861 }
Radek Krejcie663e012016-08-01 17:12:34 +0200862 dertype = &type->der->type;
863 if (!dertype->der) {
864 if (!type->info.enums.count) {
865 /* type is derived directly from buit-in enumeartion type and enum statement is required */
866 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type");
867 goto error;
868 }
869 } else {
870 for (; !dertype->info.enums.count; dertype = &dertype->der->type);
871 if (module->version < 2 && type->info.enums.count) {
872 /* type is not directly derived from built-in enumeration type and enum statement is prohibited
873 * in YANG 1.0, since YANG 1.1 enum statements can be used to restrict the base enumeration type */
874 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "enum");
Radek Krejci69794c62016-08-02 11:01:21 +0200875 type->info.enums.count = 0;
Radek Krejcie663e012016-08-01 17:12:34 +0200876 goto error;
877 }
Radek Krejcib54bcb12015-07-10 13:16:40 +0200878 }
Radek Krejci25d782a2015-05-22 15:03:23 +0200879
Radek Krejci1574a8d2015-08-03 14:16:52 +0200880 type->info.enums.enm = calloc(type->info.enums.count, sizeof *type->info.enums.enm);
Michal Vasko253035f2015-12-17 16:58:13 +0100881 if (!type->info.enums.enm) {
882 LOGMEM;
883 goto error;
884 }
Radek Krejcifc8d8322016-06-24 11:23:23 +0200885
Radek Krejcie663e012016-08-01 17:12:34 +0200886 v = 0;
Radek Krejci73adb602015-07-02 18:07:40 +0200887 i = -1;
888 LY_TREE_FOR(yin->child, next) {
889 i++;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200890 c_ftrs = 0;
Radek Krejci73adb602015-07-02 18:07:40 +0200891
892 GETVAL(value, next, "name");
Michal Vasko0fb58e92015-12-07 09:27:44 +0100893 if (!value[0]) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100894 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum name");
895 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Enum name must not be empty.");
Michal Vasko0fb58e92015-12-07 09:27:44 +0100896 goto error;
897 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200898 type->info.enums.enm[i].name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci07d0fb92017-01-13 14:11:05 +0100899 if (read_yin_common(module, NULL, &type->info.enums.enm[i], LYEXT_PAR_TYPE_ENUM, next, 0, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200900 type->info.enums.count = i + 1;
901 goto error;
902 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200903
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200904 /* the assigned name MUST NOT have any leading or trailing whitespace characters */
Radek Krejci1574a8d2015-08-03 14:16:52 +0200905 value = type->info.enums.enm[i].name;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200906 if (isspace(value[0]) || isspace(value[strlen(value) - 1])) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100907 LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200908 type->info.enums.count = i + 1;
909 goto error;
910 }
Radek Krejci25d782a2015-05-22 15:03:23 +0200911
Radek Krejcie663e012016-08-01 17:12:34 +0200912 if (!dertype->der) { /* directly derived type from enumeration built-in type */
913 /* check the name uniqueness */
914 for (j = 0; j < i; j++) {
915 if (ly_strequal(type->info.enums.enm[j].name, value, 1)) {
916 LOGVAL(LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, value);
917 type->info.enums.count = i + 1;
918 goto error;
919 }
920 }
921 } else {
922 /* restricted enumeration type - the name MUST be used in the base type */
923 enms_sc = dertype->info.enums.enm;
924 for (j = 0; j < dertype->info.enums.count; j++) {
925 if (ly_strequal(enms_sc[j].name, value, 1)) {
926 break;
927 }
928 }
929 if (j == dertype->info.enums.count) {
930 LOGVAL(LYE_ENUM_INNAME, LY_VLOG_NONE, NULL, value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200931 type->info.enums.count = i + 1;
932 goto error;
933 }
934 }
Radek Krejci04581c62015-05-22 21:24:00 +0200935
Radek Krejcie663e012016-08-01 17:12:34 +0200936 val_set = 0;
Radek Krejci73adb602015-07-02 18:07:40 +0200937 LY_TREE_FOR(next->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200938 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
939 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200940 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200941 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200942
Radek Krejci0d70c372015-07-02 16:23:10 +0200943 if (!strcmp(node->name, "value")) {
Radek Krejcie663e012016-08-01 17:12:34 +0200944 if (val_set) {
945 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, next->name);
946 type->info.enums.count = i + 1;
947 goto error;
948 }
949
Radek Krejci0d70c372015-07-02 16:23:10 +0200950 GETVAL(value, node, "value");
Radek Krejci7511f402015-07-10 09:56:30 +0200951 v_ = strtoll(value, NULL, 10);
Radek Krejci0d70c372015-07-02 16:23:10 +0200952
953 /* range check */
954 if (v_ < INT32_MIN || v_ > INT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100955 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum/value");
Radek Krejci0d70c372015-07-02 16:23:10 +0200956 type->info.enums.count = i + 1;
957 goto error;
958 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200959 type->info.enums.enm[i].value = v_;
Radek Krejci0d70c372015-07-02 16:23:10 +0200960
Radek Krejcie663e012016-08-01 17:12:34 +0200961 if (!dertype->der) { /* directly derived type from enumeration built-in type */
Pavol Vican5de389c2016-08-30 08:55:15 +0200962 if (!i) {
963 /* change value, which is assigned automatically, if first enum has value. */
Radek Krejcie663e012016-08-01 17:12:34 +0200964 v = type->info.enums.enm[i].value;
965 v++;
966 } else {
Pavol Vican5de389c2016-08-30 08:55:15 +0200967 /* keep the highest enum value for automatic increment */
968 if (type->info.enums.enm[i].value >= v) {
969 v = type->info.enums.enm[i].value;
970 v++;
971 } else {
972 /* check that the value is unique */
973 for (j = 0; j < i; j++) {
974 if (type->info.enums.enm[j].value == type->info.enums.enm[i].value) {
975 LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL,
976 type->info.enums.enm[i].value, type->info.enums.enm[i].name,
977 type->info.enums.enm[j].name);
978 type->info.enums.count = i + 1;
979 goto error;
980 }
Radek Krejcie663e012016-08-01 17:12:34 +0200981 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200982 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200983 }
984 }
Radek Krejcifc8d8322016-06-24 11:23:23 +0200985 val_set = 1;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200986
987 } else if ((module->version >= 2) && !strcmp(node->name, "if-feature")) {
988 c_ftrs++;
989
Radek Krejci0d70c372015-07-02 16:23:10 +0200990 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100991 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200992 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200993 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200994 }
Radek Krejcie663e012016-08-01 17:12:34 +0200995
996 if (!dertype->der) { /* directly derived type from enumeration */
997 if (!val_set) {
998 /* assign value automatically */
999 if (v > INT32_MAX) {
1000 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value");
1001 type->info.enums.count = i + 1;
1002 goto error;
1003 }
1004 type->info.enums.enm[i].value = v;
1005 type->info.enums.enm[i].flags |= LYS_AUTOASSIGNED;
1006 v++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001007 }
Radek Krejcie663e012016-08-01 17:12:34 +02001008 } else { /* restricted enum type */
1009 if (!val_set) {
1010 /* automatically assign value from base type */
1011 type->info.enums.enm[i].value = enms_sc[j].value;
1012 type->info.enums.enm[i].flags |= LYS_AUTOASSIGNED;
1013 } else {
1014 /* check that the assigned value corresponds to the original
1015 * value of the enum in the base type */
1016 if (v_ != enms_sc[j].value) {
1017 /* v_ - assigned value in restricted enum
1018 * enms_sc[j].value - value assigned to the corresponding enum (detected above) in base type */
1019 LOGVAL(LYE_ENUM_INVAL, LY_VLOG_NONE, NULL,
Radek Krejci541a45d2016-08-02 13:12:07 +02001020 type->info.enums.enm[i].value, type->info.enums.enm[i].name, enms_sc[j].value);
Radek Krejcie663e012016-08-01 17:12:34 +02001021 type->info.enums.count = i + 1;
1022 goto error;
1023 }
1024 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001025 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001026
1027 /* if-features */
1028 if (c_ftrs) {
1029 enms_sc = &type->info.enums.enm[i];
1030 enms_sc->iffeature = calloc(c_ftrs, sizeof *enms_sc->iffeature);
1031 if (!enms_sc->iffeature) {
1032 LOGMEM;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001033 type->info.enums.count = i + 1;
1034 goto error;
1035 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001036
1037 LY_TREE_FOR(next->child, node) {
1038 if (!strcmp(node->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02001039 rc = fill_yin_iffeature((struct lys_node *)type->parent, 0, node,
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001040 &enms_sc->iffeature[enms_sc->iffeature_size], unres);
1041 enms_sc->iffeature_size++;
1042 if (rc) {
1043 type->info.enums.count = i + 1;
1044 goto error;
1045 }
1046 }
1047 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001048 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001049
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001050 }
1051 break;
1052
1053 case LY_TYPE_IDENT:
Radek Krejci994b6f62015-06-18 16:47:27 +02001054 /* RFC 6020 9.10 - base */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001055
Michal Vaskof2d43962016-09-02 11:10:16 +02001056 /* get base specification, at least one must be present */
Radek Krejci0d70c372015-07-02 16:23:10 +02001057 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001058
Michal Vaskoe29c6622015-11-27 15:02:31 +01001059 if (strcmp(node->name, "base")) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001060 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +02001061 goto error;
1062 }
Michal Vaskof2d43962016-09-02 11:10:16 +02001063
1064 GETVAL(value, yin->child, "name");
1065 /* store in the JSON format */
1066 value = transform_schema2json(module, value);
1067 if (!value) {
1068 goto error;
1069 }
1070 rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value);
1071 lydict_remove(module->ctx, value);
1072
1073 if (rc == -1) {
1074 goto error;
1075 }
Radek Krejci0d70c372015-07-02 16:23:10 +02001076 }
1077
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001078 if (!yin->child) {
Radek Krejci65c889c2015-06-22 10:17:22 +02001079 if (type->der->type.der) {
Michal Vaskof2d43962016-09-02 11:10:16 +02001080 /* this is just a derived type with no base required */
Radek Krejci65c889c2015-06-22 10:17:22 +02001081 break;
1082 }
Radek Krejci48464ed2016-03-17 15:44:09 +01001083 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001084 goto error;
Pavol Vicanf0046f42016-09-07 15:11:09 +02001085 } else {
1086 if (type->der->type.der) {
1087 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "base");
1088 goto error;
1089 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001090 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001091 if (yin->child->next) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001092 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, yin->child->next->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001093 goto error;
1094 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001095 break;
1096
1097 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +02001098 /* RFC 6020 9.13.2 - require-instance */
Radek Krejci73adb602015-07-02 18:07:40 +02001099 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001100
Radek Krejciaf351422015-06-19 14:49:38 +02001101 if (!strcmp(node->name, "require-instance")) {
1102 if (type->info.inst.req) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001103 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejciaf351422015-06-19 14:49:38 +02001104 goto error;
1105 }
1106 GETVAL(value, node, "value");
Michal Vasko25fa5ac2015-07-17 10:53:38 +02001107 if (!strcmp(value, "true")) {
Radek Krejciaf351422015-06-19 14:49:38 +02001108 type->info.inst.req = 1;
Michal Vasko25fa5ac2015-07-17 10:53:38 +02001109 } else if (!strcmp(value, "false")) {
Radek Krejciaf351422015-06-19 14:49:38 +02001110 type->info.inst.req = -1;
1111 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001112 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
Radek Krejciaf351422015-06-19 14:49:38 +02001113 goto error;
1114 }
1115 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001116 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejciaf351422015-06-19 14:49:38 +02001117 goto error;
1118 }
Radek Krejciaf351422015-06-19 14:49:38 +02001119 }
Michal Vasko8548cf92015-07-20 15:17:53 +02001120
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001121 break;
1122
Radek Krejcif2860132015-06-20 12:37:20 +02001123 case LY_TYPE_BINARY:
1124 /* RFC 6020 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001125 case LY_TYPE_INT8:
1126 case LY_TYPE_INT16:
1127 case LY_TYPE_INT32:
1128 case LY_TYPE_INT64:
1129 case LY_TYPE_UINT8:
1130 case LY_TYPE_UINT16:
1131 case LY_TYPE_UINT32:
1132 case LY_TYPE_UINT64:
Radek Krejcif2860132015-06-20 12:37:20 +02001133 /* RFC 6020 9.2.4 - range */
1134
1135 /* length and range are actually the same restriction, so process
1136 * them by this common code, we just need to differ the name and
1137 * structure where the information will be stored
1138 */
1139 if (type->base == LY_TYPE_BINARY) {
Radek Krejcifccd1442017-01-16 10:26:57 +01001140 restrs = &type->info.binary.length;
Radek Krejcif2860132015-06-20 12:37:20 +02001141 name = "length";
1142 } else {
Radek Krejcifccd1442017-01-16 10:26:57 +01001143 restrs = &type->info.num.range;
Radek Krejcif2860132015-06-20 12:37:20 +02001144 name = "range";
1145 }
1146
Radek Krejci73adb602015-07-02 18:07:40 +02001147 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001148
Radek Krejcif2860132015-06-20 12:37:20 +02001149 if (!strcmp(node->name, name)) {
Radek Krejcifccd1442017-01-16 10:26:57 +01001150 if (*restrs) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001151 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcif2860132015-06-20 12:37:20 +02001152 goto error;
1153 }
1154
1155 GETVAL(value, node, "value");
Radek Krejci6dc53a22015-08-17 13:27:59 +02001156 if (lyp_check_length_range(value, type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001157 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, name);
Radek Krejcif2860132015-06-20 12:37:20 +02001158 goto error;
1159 }
Radek Krejcifccd1442017-01-16 10:26:57 +01001160 *restrs = calloc(1, sizeof **restrs);
1161 if (!(*restrs)) {
Michal Vasko253035f2015-12-17 16:58:13 +01001162 LOGMEM;
1163 goto error;
1164 }
Radek Krejcifccd1442017-01-16 10:26:57 +01001165 (*restrs)->expr = lydict_insert(module->ctx, value, 0);
Radek Krejcif2860132015-06-20 12:37:20 +02001166
1167 /* get possible substatements */
Radek Krejcifccd1442017-01-16 10:26:57 +01001168 if (read_restr_substmt(module, type->base == LY_TYPE_BINARY ? LYEXT_PAR_LENGTH : LYEXT_PAR_RANGE,
1169 *restrs, node, unres)) {
Radek Krejcif2860132015-06-20 12:37:20 +02001170 goto error;
1171 }
1172 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001173 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejcif2860132015-06-20 12:37:20 +02001174 goto error;
1175 }
Radek Krejcif2860132015-06-20 12:37:20 +02001176 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001177 break;
1178
1179 case LY_TYPE_LEAFREF:
Radek Krejci3a5501d2016-07-18 22:03:34 +02001180 /* flag resolving for later use */
1181 if (!tpdftype) {
1182 for (siter = parent; siter && siter->nodetype != LYS_GROUPING; siter = lys_parent(siter));
1183 if (siter) {
1184 /* just a flag - do not resolve */
1185 tpdftype = 1;
1186 }
1187 }
1188
Radek Krejcidc4c1412015-06-19 15:39:54 +02001189 /* RFC 6020 9.9.2 - path */
Radek Krejci73adb602015-07-02 18:07:40 +02001190 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001191
Michal Vasko88c29542015-11-27 14:57:53 +01001192 if (!strcmp(node->name, "path") && !type->der->type.der) {
Radek Krejcidc4c1412015-06-19 15:39:54 +02001193 if (type->info.lref.path) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001194 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcidc4c1412015-06-19 15:39:54 +02001195 goto error;
1196 }
1197
1198 GETVAL(value, node, "value");
Michal Vasko1dca6882015-10-22 14:29:42 +02001199 /* store in the JSON format */
Radek Krejci48464ed2016-03-17 15:44:09 +01001200 type->info.lref.path = transform_schema2json(module, value);
Michal Vasko1dca6882015-10-22 14:29:42 +02001201 if (!type->info.lref.path) {
1202 goto error;
1203 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001204
1205 /* try to resolve leafref path only when this is instantiated
1206 * leaf, so it is not:
1207 * - typedef's type,
1208 * - in grouping definition,
1209 * - just instantiated in a grouping definition,
1210 * because in those cases the nodes referenced in path might not be present
1211 * and it is not a bug. */
1212 if (!tpdftype && unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001213 goto error;
1214 }
Radek Krejcid6353ed2016-09-15 13:30:45 +02001215 } else if (module->version >= 2 && !strcmp(node->name, "require-instance")) {
Michal Vasko08ae53e2016-09-02 12:40:04 +02001216 if (type->info.lref.req) {
1217 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
1218 goto error;
1219 }
1220 GETVAL(value, node, "value");
1221 if (!strcmp(value, "true")) {
1222 type->info.lref.req = 1;
1223 } else if (!strcmp(value, "false")) {
1224 type->info.lref.req = -1;
1225 } else {
1226 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
1227 goto error;
1228 }
Radek Krejcidc4c1412015-06-19 15:39:54 +02001229 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001230 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejcidc4c1412015-06-19 15:39:54 +02001231 goto error;
1232 }
Radek Krejci73adb602015-07-02 18:07:40 +02001233 }
1234
Radek Krejci742be352016-07-17 12:18:54 +02001235 if (!type->info.lref.path) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001236 if (!type->der->type.der) {
1237 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type");
1238 goto error;
1239 } else {
1240 /* copy leafref definition into the derived type */
1241 type->info.lref.path = lydict_insert(module->ctx, type->der->type.info.lref.path, 0);
1242 /* and resolve the path at the place we are (if not in grouping/typedef) */
1243 if (!tpdftype && unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent) == -1) {
1244 goto error;
1245 }
1246
1247 /* add pointer to leafref target, only on leaves (not in typedefs) */
1248 if (type->info.lref.target && lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) {
1249 goto error;
1250 }
1251 }
Radek Krejci742be352016-07-17 12:18:54 +02001252 }
1253
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001254 break;
1255
1256 case LY_TYPE_STRING:
Radek Krejci3733a802015-06-19 13:43:21 +02001257 /* RFC 6020 9.4.4 - length */
1258 /* RFC 6020 9.4.6 - pattern */
Radek Krejci73adb602015-07-02 18:07:40 +02001259 i = 0;
Radek Krejci3733a802015-06-19 13:43:21 +02001260 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001261
Radek Krejci3733a802015-06-19 13:43:21 +02001262 if (!strcmp(node->name, "length")) {
1263 if (type->info.str.length) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001264 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci3733a802015-06-19 13:43:21 +02001265 goto error;
1266 }
1267
1268 GETVAL(value, node, "value");
Radek Krejci6dc53a22015-08-17 13:27:59 +02001269 if (lyp_check_length_range(value, type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001270 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "length");
Radek Krejci3733a802015-06-19 13:43:21 +02001271 goto error;
1272 }
1273 type->info.str.length = calloc(1, sizeof *type->info.str.length);
Michal Vasko253035f2015-12-17 16:58:13 +01001274 if (!type->info.str.length) {
1275 LOGMEM;
1276 goto error;
1277 }
Radek Krejci3733a802015-06-19 13:43:21 +02001278 type->info.str.length->expr = lydict_insert(module->ctx, value, 0);
1279
Radek Krejci5fbc9162015-06-19 14:11:11 +02001280 /* get possible sub-statements */
Radek Krejcifccd1442017-01-16 10:26:57 +01001281 if (read_restr_substmt(module, LYEXT_PAR_LENGTH, type->info.str.length, node, unres)) {
Radek Krejci3733a802015-06-19 13:43:21 +02001282 goto error;
1283 }
Michal Vasko345da0a2015-12-02 10:35:55 +01001284 lyxml_free(module->ctx, node);
Radek Krejci3733a802015-06-19 13:43:21 +02001285 } else if (!strcmp(node->name, "pattern")) {
Radek Krejci73adb602015-07-02 18:07:40 +02001286 i++;
Radek Krejci3733a802015-06-19 13:43:21 +02001287 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001288 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci3733a802015-06-19 13:43:21 +02001289 goto error;
1290 }
1291 }
Radek Krejci5fbc9162015-06-19 14:11:11 +02001292 /* store patterns in array */
Radek Krejci73adb602015-07-02 18:07:40 +02001293 if (i) {
1294 type->info.str.patterns = calloc(i, sizeof *type->info.str.patterns);
Michal Vasko253035f2015-12-17 16:58:13 +01001295 if (!type->info.str.patterns) {
1296 LOGMEM;
1297 goto error;
1298 }
Radek Krejci73adb602015-07-02 18:07:40 +02001299 LY_TREE_FOR(yin->child, node) {
Michal Vasko5b64da22015-11-23 15:22:30 +01001300 GETVAL(value, node, "value");
Michal Vasko0aee5c12016-06-17 14:27:26 +02001301 if (lyp_check_pattern(value, NULL)) {
Michal Vasko69068852015-07-13 14:34:31 +02001302 free(type->info.str.patterns);
Radek Krejci9f1e8532016-06-16 11:18:21 +02001303 type->info.str.patterns = NULL;
Michal Vasko69068852015-07-13 14:34:31 +02001304 goto error;
1305 }
Radek Krejcifccd1442017-01-16 10:26:57 +01001306 restr = &type->info.str.patterns[type->info.str.pat_count]; /* shortcut */
Michal Vasko69068852015-07-13 14:34:31 +02001307
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001308 modifier = 0x06; /* ACK */
1309 name = NULL;
Radek Krejcifccd1442017-01-16 10:26:57 +01001310 if (module->version >= 2) {
1311 LY_TREE_FOR_SAFE(node->child, next2, child) {
1312 if (child->ns && !strcmp(child->ns->value, LY_NSYIN) && !strcmp(child->name, "modifier")) {
1313 if (name) {
1314 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "modifier", node->name);
1315 goto error;
1316 }
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001317
Radek Krejcifccd1442017-01-16 10:26:57 +01001318 GETVAL(name, child, "value");
1319 if (!strcmp(name, "invert-match")) {
1320 modifier = 0x15; /* NACK */
1321 } else {
1322 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, name, "modifier");
1323 goto error;
1324 }
1325 /* get extensions of the modifier */
1326 if (read_yin_subnode_ext(module, restr, LYEXT_PAR_PATTERN, child,
Radek Krejciac00b2a2017-01-17 14:05:00 +01001327 LYEXT_SUBSTMT_MODIFIER, 0, unres)) {
Radek Krejcifccd1442017-01-16 10:26:57 +01001328 return EXIT_FAILURE;
1329 }
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001330
Radek Krejcifccd1442017-01-16 10:26:57 +01001331 lyxml_free(module->ctx, child);
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001332 }
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001333 }
1334 }
1335
1336 len = strlen(value);
1337 buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */
1338 buf[0] = modifier;
1339 strcpy(&buf[1], value);
1340
Radek Krejcifccd1442017-01-16 10:26:57 +01001341 restr->expr = lydict_insert_zc(module->ctx, buf);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001342
1343 /* get possible sub-statements */
Radek Krejcifccd1442017-01-16 10:26:57 +01001344 if (read_restr_substmt(module, LYEXT_PAR_PATTERN, restr, node, unres)) {
Michal Vasko69068852015-07-13 14:34:31 +02001345 free(type->info.str.patterns);
Pavol Vican9fc5d642016-08-30 15:23:19 +02001346 type->info.str.patterns = NULL;
Radek Krejci5fbc9162015-06-19 14:11:11 +02001347 goto error;
1348 }
Radek Krejci73adb602015-07-02 18:07:40 +02001349 type->info.str.pat_count++;
Radek Krejci5fbc9162015-06-19 14:11:11 +02001350 }
1351 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001352 break;
1353
1354 case LY_TYPE_UNION:
Radek Krejcie4c366b2015-07-02 10:11:31 +02001355 /* RFC 6020 7.4 - type */
1356 /* count number of types in union */
1357 i = 0;
Radek Krejci0d70c372015-07-02 16:23:10 +02001358 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001359
Radek Krejcie4c366b2015-07-02 10:11:31 +02001360 if (!strcmp(node->name, "type")) {
Radek Krejci038d5d92016-09-12 15:07:15 +02001361 if (type->der->type.der) {
1362 /* type can be a substatement only in "union" type, not in derived types */
1363 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "derived type");
1364 goto error;
1365 }
Radek Krejcie4c366b2015-07-02 10:11:31 +02001366 i++;
1367 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001368 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001369 goto error;
1370 }
1371 }
1372
Radek Krejci038d5d92016-09-12 15:07:15 +02001373 if (!i && !type->der->type.der) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001374 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type");
Radek Krejcie4c366b2015-07-02 10:11:31 +02001375 goto error;
1376 }
1377
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001378 /* inherit instid presence information */
1379 if ((type->der->type.base == LY_TYPE_UNION) && type->der->type.info.uni.has_ptr_type) {
1380 type->info.uni.has_ptr_type = 1;
1381 }
1382
Radek Krejcie4c366b2015-07-02 10:11:31 +02001383 /* allocate array for union's types ... */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001384 type->info.uni.types = calloc(i, sizeof *type->info.uni.types);
Michal Vasko253035f2015-12-17 16:58:13 +01001385 if (!type->info.uni.types) {
1386 LOGMEM;
1387 goto error;
1388 }
Radek Krejcie4c366b2015-07-02 10:11:31 +02001389 /* ... and fill the structures */
Radek Krejci73adb602015-07-02 18:07:40 +02001390 LY_TREE_FOR(yin->child, node) {
Radek Krejcicf509982015-12-15 09:22:44 +01001391 type->info.uni.types[type->info.uni.count].parent = type->parent;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001392 rc = fill_yin_type(module, parent, node, &type->info.uni.types[type->info.uni.count], tpdftype, unres);
Michal Vasko88c29542015-11-27 14:57:53 +01001393 if (!rc) {
1394 type->info.uni.count++;
1395
Radek Krejci7de36cf2016-09-12 16:18:50 +02001396 if (module->version < 2) {
1397 /* union's type cannot be empty or leafref */
1398 if (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_EMPTY) {
1399 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", node->name);
1400 rc = -1;
1401 } else if (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_LEAFREF) {
1402 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", node->name);
1403 rc = -1;
1404 }
Michal Vasko88c29542015-11-27 14:57:53 +01001405 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001406
1407 if ((type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_INST)
1408 || (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_LEAFREF)) {
1409 type->info.uni.has_ptr_type = 1;
1410 }
Michal Vasko88c29542015-11-27 14:57:53 +01001411 }
1412 if (rc) {
1413 /* even if we got EXIT_FAILURE, throw it all away, too much trouble doing something else */
1414 for (i = 0; i < type->info.uni.count; ++i) {
1415 lys_type_free(module->ctx, &type->info.uni.types[i]);
1416 }
1417 free(type->info.uni.types);
1418 type->info.uni.types = NULL;
1419 type->info.uni.count = 0;
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001420 type->info.uni.has_ptr_type = 0;
Michal Vaskoeac08182016-07-21 12:16:32 +02001421 type->der = NULL;
1422 type->base = LY_TYPE_DER;
Michal Vasko88c29542015-11-27 14:57:53 +01001423
1424 if (rc == EXIT_FAILURE) {
Radek Krejcidc008d72016-02-17 13:12:14 +01001425 ret = EXIT_FAILURE;
Michal Vasko88c29542015-11-27 14:57:53 +01001426 }
Radek Krejcie4c366b2015-07-02 10:11:31 +02001427 goto error;
1428 }
Michal Vasko88c29542015-11-27 14:57:53 +01001429 }
1430 break;
Radek Krejcie4c366b2015-07-02 10:11:31 +02001431
Michal Vasko88c29542015-11-27 14:57:53 +01001432 case LY_TYPE_BOOL:
1433 case LY_TYPE_EMPTY:
1434 /* no sub-statement allowed */
Radek Krejcie534c132016-11-23 13:32:31 +01001435 if (yin->child) {
1436 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, yin->child->name);
1437 goto error;
Radek Krejcie4c366b2015-07-02 10:11:31 +02001438 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001439 break;
1440
1441 default:
Michal Vasko88c29542015-11-27 14:57:53 +01001442 LOGINT;
1443 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001444 }
1445
1446 return EXIT_SUCCESS;
Radek Krejci25d782a2015-05-22 15:03:23 +02001447
1448error:
Radek Krejcidc008d72016-02-17 13:12:14 +01001449 if (type->module_name) {
1450 lydict_remove(module->ctx, type->module_name);
1451 type->module_name = NULL;
1452 }
Radek Krejcie534c132016-11-23 13:32:31 +01001453 lyxml_free_withsiblings(module->ctx, exts.child);
1454
Radek Krejcidc008d72016-02-17 13:12:14 +01001455 return ret;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001456}
1457
Michal Vasko0d343d12015-08-24 14:57:36 +02001458/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001459static int
Radek Krejci07d0fb92017-01-13 14:11:05 +01001460fill_yin_typedef(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_tpdf *tpdf,
1461 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001462{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001463 const char *value;
Michal Vasko88c29542015-11-27 14:57:53 +01001464 struct lyxml_elem *node, *next;
Radek Krejcie534c132016-11-23 13:32:31 +01001465 int rc, has_type = 0, c_ext = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001466
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001467 GETVAL(value, yin, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +01001468 if (lyp_check_identifier(value, LY_IDENT_TYPE, module, parent)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001469 goto error;
1470 }
1471 tpdf->name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejcida04f4a2015-05-21 12:54:09 +02001472
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001473 /* generic part - status, description, reference */
Radek Krejci07d0fb92017-01-13 14:11:05 +01001474 if (read_yin_common(module, NULL, tpdf, LYEXT_PAR_TPDF, yin, OPT_MODULE, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001475 goto error;
1476 }
Radek Krejcieac35532015-05-31 19:09:15 +02001477
Michal Vasko88c29542015-11-27 14:57:53 +01001478 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejcie534c132016-11-23 13:32:31 +01001479 if (strcmp(node->ns->value, LY_NSYIN)) {
1480 /* extension */
1481 c_ext++;
Radek Krejci0d70c372015-07-02 16:23:10 +02001482 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01001483 } else if (!strcmp(node->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +01001484 if (has_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001485 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001486 goto error;
1487 }
Michal Vasko88c29542015-11-27 14:57:53 +01001488 /* HACK for unres */
1489 tpdf->type.der = (struct lys_tpdf *)node;
Radek Krejcicf509982015-12-15 09:22:44 +01001490 tpdf->type.parent = tpdf;
Michal Vasko5d631402016-07-21 13:15:15 +02001491 if (unres_schema_add_node(module, unres, &tpdf->type, UNRES_TYPE_DER_TPDF, parent) == -1) {
Radek Krejci73adb602015-07-02 18:07:40 +02001492 goto error;
1493 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001494 has_type = 1;
Radek Krejcie534c132016-11-23 13:32:31 +01001495
Radek Krejci07d0fb92017-01-13 14:11:05 +01001496 /* skip lyxml_free() at the end of the loop, node was freed or at least unlinked in unres processing */
Radek Krejcie534c132016-11-23 13:32:31 +01001497 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001498 } else if (!strcmp(node->name, "default")) {
1499 if (tpdf->dflt) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001500 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001501 goto error;
1502 }
1503 GETVAL(value, node, "value");
1504 tpdf->dflt = lydict_insert(module->ctx, value, strlen(value));
1505 } else if (!strcmp(node->name, "units")) {
1506 if (tpdf->units) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001507 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001508 goto error;
1509 }
1510 GETVAL(value, node, "name");
1511 tpdf->units = lydict_insert(module->ctx, value, strlen(value));
1512 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001513 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001514 goto error;
1515 }
Radek Krejcie534c132016-11-23 13:32:31 +01001516
1517 lyxml_free(module->ctx, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001518 }
Radek Krejci25d782a2015-05-22 15:03:23 +02001519
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001520 /* check mandatory value */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001521 if (!has_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001522 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001523 goto error;
1524 }
Radek Krejcieac35532015-05-31 19:09:15 +02001525
Michal Vasko478c4652016-07-21 12:55:01 +02001526 /* check default value (if not defined, there still could be some restrictions
1527 * that need to be checked against a default value from a derived type) */
Radek Krejci51673202016-11-01 17:00:32 +01001528 if (unres_schema_add_node(module, unres, &tpdf->type, UNRES_TYPE_DFLT, (struct lys_node *)(&tpdf->dflt)) == -1) {
Michal Vasko478c4652016-07-21 12:55:01 +02001529 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001530 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001531
Radek Krejcie534c132016-11-23 13:32:31 +01001532 /* finish extensions parsing */
1533 if (c_ext) {
1534 tpdf->ext = calloc(c_ext, sizeof *tpdf->ext);
1535 if (!tpdf->ext) {
1536 LOGMEM;
1537 goto error;
1538 }
1539 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01001540 rc = lyp_yin_fill_ext(tpdf, LYEXT_PAR_TYPE, 0, 0, module, node, &tpdf->ext, tpdf->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01001541 tpdf->ext_size++;
1542 if (rc) {
1543 goto error;
1544 }
1545 }
1546 }
1547
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001548 return EXIT_SUCCESS;
Radek Krejcieac35532015-05-31 19:09:15 +02001549
1550error:
1551
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001552 return EXIT_FAILURE;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001553}
1554
Radek Krejcia1a6b762016-11-14 09:53:38 +09001555static int
Radek Krejcie534c132016-11-23 13:32:31 +01001556fill_yin_extension(struct lys_module *module, struct lyxml_elem *yin, struct lys_ext *ext, struct unres_schema *unres)
Radek Krejcia1a6b762016-11-14 09:53:38 +09001557{
1558 const char *value;
Radek Krejcie534c132016-11-23 13:32:31 +01001559 struct lyxml_elem *child, *node, *next, *next2;
1560 int c_ext = 0, rc;
Radek Krejcia1a6b762016-11-14 09:53:38 +09001561
1562 GETVAL(value, yin, "name");
1563
1564 if (lyp_check_identifier(value, LY_IDENT_EXTENSION, module, NULL)) {
1565 goto error;
1566 }
1567 ext->name = lydict_insert(module->ctx, value, strlen(value));
1568
Radek Krejci07d0fb92017-01-13 14:11:05 +01001569 if (read_yin_common(module, NULL, ext, LYEXT_PAR_EXT, yin, OPT_MODULE, unres)) {
Radek Krejcia1a6b762016-11-14 09:53:38 +09001570 goto error;
1571 }
1572
Radek Krejcie534c132016-11-23 13:32:31 +01001573 LY_TREE_FOR_SAFE(yin->child, next, node) {
1574 if (strcmp(node->ns->value, LY_NSYIN)) {
1575 /* possible extension instance */
1576 c_ext++;
1577 } else if (!strcmp(node->name, "argument")) {
1578 /* argument */
1579 GETVAL(value, node, "name");
1580 ext->argument = lydict_insert(module->ctx, value, strlen(value));
Radek Krejcia1a6b762016-11-14 09:53:38 +09001581
Radek Krejcia1a6b762016-11-14 09:53:38 +09001582 /* yin-element */
Radek Krejcie534c132016-11-23 13:32:31 +01001583 LY_TREE_FOR_SAFE(node->child, next2, child) {
1584 if (child->ns == node->ns && !strcmp(child->name, "yin-element")) {
1585 GETVAL(value, child, "value");
1586 if (ly_strequal(value, "true", 0)) {
1587 ext->flags |= LYS_YINELEM;
1588 }
1589 } else if (child->ns) {
1590 /* unexpected YANG statement */
1591 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->name);
1592 goto error;
1593 } /* else garbage, but save resource needed for unlinking */
Radek Krejcia1a6b762016-11-14 09:53:38 +09001594 }
1595
Radek Krejcie534c132016-11-23 13:32:31 +01001596 lyxml_free(module->ctx, node);
1597 } else {
1598 /* unexpected YANG statement */
1599 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, node->name, node->name);
1600 goto error;
1601 }
1602 }
1603
1604 if (c_ext) {
1605 ext->ext = calloc(c_ext, sizeof *ext->ext);
1606 if (!ext->ext) {
1607 LOGMEM;
1608 goto error;
1609 }
1610 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01001611 rc = lyp_yin_fill_ext(ext, LYEXT_PAR_EXT, 0, 0, module, node, &ext->ext, ext->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01001612 ext->ext_size++;
1613 if (rc) {
1614 goto error;
Radek Krejcia1a6b762016-11-14 09:53:38 +09001615 }
1616 }
1617 }
1618
Radek Krejci0a498f82017-01-04 16:24:15 +01001619 /* search for plugin */
1620 ext->plugin = ext_get_plugin(ext->name, ext->module->name, ext->module->rev ? ext->module->rev[0].date : NULL);
Radek Krejcia1a6b762016-11-14 09:53:38 +09001621
1622 return EXIT_SUCCESS;
1623
1624error:
1625 return EXIT_FAILURE;
1626}
1627
Michal Vasko0d343d12015-08-24 14:57:36 +02001628/* logs directly */
Radek Krejci3cf9e222015-06-18 11:37:50 +02001629static int
Michal Vaskof02e3742015-08-05 16:27:02 +02001630fill_yin_feature(struct lys_module *module, struct lyxml_elem *yin, struct lys_feature *f, struct unres_schema *unres)
Radek Krejci3cf9e222015-06-18 11:37:50 +02001631{
1632 const char *value;
1633 struct lyxml_elem *child, *next;
Radek Krejcie534c132016-11-23 13:32:31 +01001634 int c_ftrs = 0, c_ext = 0, ret;
Radek Krejci5323b492017-01-16 15:40:11 +01001635 void *reallocated;
Radek Krejci3cf9e222015-06-18 11:37:50 +02001636
Radek Krejcib05774c2015-06-18 13:52:59 +02001637 GETVAL(value, yin, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +01001638 if (lyp_check_identifier(value, LY_IDENT_FEATURE, module, NULL)) {
Radek Krejcib05774c2015-06-18 13:52:59 +02001639 goto error;
1640 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001641 f->name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci6a113852015-07-03 16:04:20 +02001642 f->module = module;
Radek Krejcib05774c2015-06-18 13:52:59 +02001643
Radek Krejci07d0fb92017-01-13 14:11:05 +01001644 if (read_yin_common(module, NULL, f, LYEXT_PAR_FEATURE, yin, 0, unres)) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02001645 goto error;
1646 }
1647
Radek Krejcie534c132016-11-23 13:32:31 +01001648 LY_TREE_FOR(yin->child, child) {
1649 if (strcmp(child->ns->value, LY_NSYIN)) {
1650 /* extension */
1651 c_ext++;
1652 } else if (!strcmp(child->name, "if-feature")) {
1653 c_ftrs++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02001654 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001655 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001656 goto error;
1657 }
1658 }
1659
Radek Krejcie534c132016-11-23 13:32:31 +01001660 if (c_ftrs) {
1661 f->iffeature = calloc(c_ftrs, sizeof *f->iffeature);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001662 if (!f->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01001663 LOGMEM;
1664 goto error;
1665 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02001666 }
Radek Krejcie534c132016-11-23 13:32:31 +01001667 if (c_ext) {
Radek Krejci5323b492017-01-16 15:40:11 +01001668 /* some extensions may be already present from the substatements */
1669 reallocated = realloc(f->ext, (c_ext + f->ext_size) * sizeof *f->ext);
1670 if (!reallocated) {
Radek Krejcie534c132016-11-23 13:32:31 +01001671 LOGMEM;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001672 goto error;
1673 }
Radek Krejci5323b492017-01-16 15:40:11 +01001674 f->ext = reallocated;
1675
1676 /* init memory */
1677 memset(&f->ext[f->ext_size], 0, c_ext * sizeof *f->ext);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001678 }
1679
Radek Krejcie534c132016-11-23 13:32:31 +01001680 LY_TREE_FOR_SAFE(yin->child, next, child) {
1681 if (strcmp(child->ns->value, LY_NSYIN)) {
1682 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01001683 ret = lyp_yin_fill_ext(f, LYEXT_PAR_FEATURE, 0, 0, module, child, &f->ext, f->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01001684 f->ext_size++;
1685 if (ret) {
1686 goto error;
1687 }
1688 } else { /* if-feature */
1689 ret = fill_yin_iffeature((struct lys_node *)f, 1, child, &f->iffeature[f->iffeature_size], unres);
1690 f->iffeature_size++;
1691 if (ret) {
1692 goto error;
1693 }
1694 }
1695 }
1696
Radek Krejcic79c6b12016-07-26 15:11:49 +02001697 /* check for circular dependencies */
1698 if (f->iffeature_size) {
1699 if (unres_schema_add_node(module, unres, f, UNRES_FEATURE, NULL) == -1) {
1700 goto error;
1701 }
1702 }
1703
Radek Krejci3cf9e222015-06-18 11:37:50 +02001704 return EXIT_SUCCESS;
1705
1706error:
1707
1708 return EXIT_FAILURE;
1709}
1710
Michal Vasko0d343d12015-08-24 14:57:36 +02001711/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001712static int
Radek Krejcifccd1442017-01-16 10:26:57 +01001713fill_yin_must(struct lys_module *module, struct lyxml_elem *yin, struct lys_restr *must, struct unres_schema *unres)
Radek Krejci800af702015-06-02 13:46:01 +02001714{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001715 const char *value;
Radek Krejci800af702015-06-02 13:46:01 +02001716
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001717 GETVAL(value, yin, "condition");
Radek Krejci48464ed2016-03-17 15:44:09 +01001718 must->expr = transform_schema2json(module, value);
Michal Vaskof9893382015-10-09 14:03:04 +02001719 if (!must->expr) {
1720 goto error;
1721 }
Radek Krejci800af702015-06-02 13:46:01 +02001722
Radek Krejcifccd1442017-01-16 10:26:57 +01001723 return read_restr_substmt(module, LYEXT_PAR_MUST, must, yin, unres);
Radek Krejci800af702015-06-02 13:46:01 +02001724
Michal Vasko77dc5652016-02-15 12:32:42 +01001725error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001726 return EXIT_FAILURE;
Radek Krejci800af702015-06-02 13:46:01 +02001727}
1728
Radek Krejci581ce772015-11-10 17:22:40 +01001729static int
Michal Vasko88c29542015-11-27 14:57:53 +01001730fill_yin_unique(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_unique *unique,
1731 struct unres_schema *unres)
Radek Krejci581ce772015-11-10 17:22:40 +01001732{
1733 int i, j;
1734 const char *value, *vaux;
Radek Krejcid09d1a52016-08-11 14:05:45 +02001735 struct unres_list_uniq *unique_info;
Radek Krejci581ce772015-11-10 17:22:40 +01001736
1737 /* get unique value (list of leafs supposed to be unique */
1738 GETVAL(value, yin, "tag");
1739
1740 /* count the number of unique leafs in the value */
1741 vaux = value;
1742 while ((vaux = strpbrk(vaux, " \t\n"))) {
Michal Vasko98645db2016-03-07 14:38:49 +01001743 unique->expr_size++;
Radek Krejci581ce772015-11-10 17:22:40 +01001744 while (isspace(*vaux)) {
1745 vaux++;
1746 }
1747 }
1748 unique->expr_size++;
1749 unique->expr = calloc(unique->expr_size, sizeof *unique->expr);
Michal Vasko253035f2015-12-17 16:58:13 +01001750 if (!unique->expr) {
1751 LOGMEM;
1752 goto error;
1753 }
Radek Krejci581ce772015-11-10 17:22:40 +01001754
1755 for (i = 0; i < unique->expr_size; i++) {
1756 vaux = strpbrk(value, " \t\n");
1757 if (!vaux) {
1758 /* the last token, lydict_insert() will count its size on its own */
1759 vaux = value;
1760 }
1761
1762 /* store token into unique structure */
1763 unique->expr[i] = lydict_insert(module->ctx, value, vaux - value);
1764
1765 /* check that the expression does not repeat */
1766 for (j = 0; j < i; j++) {
Radek Krejci749190d2016-02-18 16:26:25 +01001767 if (ly_strequal(unique->expr[j], unique->expr[i], 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001768 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, unique->expr[i], "unique");
1769 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The identifier is not unique");
Radek Krejci581ce772015-11-10 17:22:40 +01001770 goto error;
1771 }
1772 }
1773
1774 /* try to resolve leaf */
1775 if (unres) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02001776 unique_info = malloc(sizeof *unique_info);
1777 unique_info->list = parent;
1778 unique_info->expr = unique->expr[i];
1779 unique_info->trg_type = &unique->trg_type;
1780 if (unres_schema_add_node(module, unres, unique_info, UNRES_LIST_UNIQ, NULL) == -1){
Pavol Vican18b10212016-04-11 15:41:52 +02001781 goto error;
1782 }
Radek Krejci581ce772015-11-10 17:22:40 +01001783 } else {
Radek Krejcid09d1a52016-08-11 14:05:45 +02001784 if (resolve_unique(parent, unique->expr[i], &unique->trg_type)) {
Radek Krejci581ce772015-11-10 17:22:40 +01001785 goto error;
1786 }
1787 }
1788
1789 /* move to next token */
1790 value = vaux;
1791 while(isspace(*value)) {
1792 value++;
1793 }
1794 }
1795
1796 return EXIT_SUCCESS;
1797
1798error:
1799 return EXIT_FAILURE;
1800}
1801
Michal Vasko0d343d12015-08-24 14:57:36 +02001802/* logs directly
1803 *
Radek Krejcieb00f512015-07-01 16:44:58 +02001804 * type: 0 - min, 1 - max
1805 */
1806static int
Radek Krejcia52656e2015-08-05 13:41:50 +02001807deviate_minmax(struct lys_node *target, struct lyxml_elem *node, struct lys_deviate *d, int type)
Radek Krejcieb00f512015-07-01 16:44:58 +02001808{
1809 const char *value;
1810 char *endptr;
1811 unsigned long val;
Pavol Vican09adcc32016-08-25 10:51:36 +02001812 uint32_t *ui32val, *min, *max;
Radek Krejcieb00f512015-07-01 16:44:58 +02001813
Michal Vaskof7e57d52016-03-07 11:31:09 +01001814 /* del min/max is forbidden */
1815 if (d->mod == LY_DEVIATE_DEL) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001816 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, (type ? "max-elements" : "min-elements"), "deviate delete");
Michal Vaskof7e57d52016-03-07 11:31:09 +01001817 goto error;
1818 }
1819
Radek Krejcieb00f512015-07-01 16:44:58 +02001820 /* check target node type */
Radek Krejci76512572015-08-04 09:47:08 +02001821 if (target->nodetype == LYS_LEAFLIST) {
Pavol Vican09adcc32016-08-25 10:51:36 +02001822 max = &((struct lys_node_leaflist *)target)->max;
1823 min = &((struct lys_node_leaflist *)target)->min;
Radek Krejci76512572015-08-04 09:47:08 +02001824 } else if (target->nodetype == LYS_LIST) {
Pavol Vican09adcc32016-08-25 10:51:36 +02001825 max = &((struct lys_node_list *)target)->max;
1826 min = &((struct lys_node_list *)target)->min;
Radek Krejcieb00f512015-07-01 16:44:58 +02001827 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001828 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
1829 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", node->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001830 goto error;
1831 }
1832
1833 GETVAL(value, node, "value");
1834 while (isspace(value[0])) {
1835 value++;
1836 }
1837
Radek Krejci0d7b2472016-02-12 11:11:03 +01001838 if (type && !strcmp(value, "unbounded")) {
1839 d->max = val = 0;
Radek Krejcia31ca7a2016-03-07 15:05:20 +01001840 d->max_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02001841 ui32val = max;
Radek Krejcieb00f512015-07-01 16:44:58 +02001842 } else {
Radek Krejci0d7b2472016-02-12 11:11:03 +01001843 /* convert it to uint32_t */
1844 errno = 0;
1845 endptr = NULL;
1846 val = strtoul(value, &endptr, 10);
1847 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001848 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01001849 goto error;
1850 }
1851 if (type) {
1852 d->max = (uint32_t)val;
Radek Krejcia31ca7a2016-03-07 15:05:20 +01001853 d->max_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02001854 ui32val = max;
Radek Krejci0d7b2472016-02-12 11:11:03 +01001855 } else {
1856 d->min = (uint32_t)val;
Radek Krejcia31ca7a2016-03-07 15:05:20 +01001857 d->min_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02001858 ui32val = min;
Radek Krejci0d7b2472016-02-12 11:11:03 +01001859 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001860 }
1861
1862 if (d->mod == LY_DEVIATE_ADD) {
1863 /* check that there is no current value */
1864 if (*ui32val) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001865 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
1866 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001867 goto error;
1868 }
Michal Vasko21be1b32016-03-07 12:31:34 +01001869 } else if (d->mod == LY_DEVIATE_RPL) {
1870 /* unfortunately, there is no way to check reliably that there
1871 * was a value before, it could have been the default */
Radek Krejcieb00f512015-07-01 16:44:58 +02001872 }
1873
Michal Vaskof7e57d52016-03-07 11:31:09 +01001874 /* add (already checked) and replace */
1875 /* set new value specified in deviation */
1876 *ui32val = (uint32_t)val;
Radek Krejcieb00f512015-07-01 16:44:58 +02001877
Pavol Vican09adcc32016-08-25 10:51:36 +02001878 /* check min-elements is smaller than max-elements */
1879 if (*max && *min > *max) {
1880 if (type) {
Radek Krejci73f96012016-10-03 13:53:53 +02001881 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "max-elements");
Pavol Vican09adcc32016-08-25 10:51:36 +02001882 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\".");
1883 } else {
Radek Krejci73f96012016-10-03 13:53:53 +02001884 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "min-elements");
Pavol Vican09adcc32016-08-25 10:51:36 +02001885 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
1886 }
1887 goto error;
1888 }
1889
Radek Krejcieb00f512015-07-01 16:44:58 +02001890 return EXIT_SUCCESS;
1891
1892error:
1893
1894 return EXIT_FAILURE;
1895}
1896
Michal Vasko0d343d12015-08-24 14:57:36 +02001897/* logs directly */
Radek Krejcieb00f512015-07-01 16:44:58 +02001898static int
Michal Vasko88c29542015-11-27 14:57:53 +01001899fill_yin_deviation(struct lys_module *module, struct lyxml_elem *yin, struct lys_deviation *dev,
1900 struct unres_schema *unres)
Radek Krejcieb00f512015-07-01 16:44:58 +02001901{
1902 const char *value, **stritem;
Radek Krejcie534c132016-11-23 13:32:31 +01001903 struct lyxml_elem *next, *next2, *child, *develem;
1904 int c_dev = 0, c_must, c_uniq, c_dflt, c_ext = 0;
Radek Krejci0d7b2472016-02-12 11:11:03 +01001905 int f_min = 0, f_max = 0; /* flags */
Michal Vaskob40b4512016-02-11 11:35:37 +01001906 int i, j, rc;
Radek Krejcid5a5c282016-08-15 15:38:08 +02001907 unsigned int u;
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001908 struct ly_ctx *ctx;
Radek Krejcia52656e2015-08-05 13:41:50 +02001909 struct lys_deviate *d = NULL;
Radek Krejcie00d2312016-08-12 15:27:49 +02001910 struct lys_node *node = NULL, *parent, *dev_target = NULL;
Radek Krejcib8048692015-08-05 13:36:34 +02001911 struct lys_node_choice *choice = NULL;
Radek Krejcid5a5c282016-08-15 15:38:08 +02001912 struct lys_node_leaf *leaf = NULL;
1913 struct ly_set *dflt_check = ly_set_new();
Radek Krejcib8048692015-08-05 13:36:34 +02001914 struct lys_node_list *list = NULL;
Radek Krejcid5a5c282016-08-15 15:38:08 +02001915 struct lys_node_leaflist *llist = NULL;
Radek Krejci1574a8d2015-08-03 14:16:52 +02001916 struct lys_type *t = NULL;
Radek Krejcid5a5c282016-08-15 15:38:08 +02001917 uint8_t *trg_must_size = NULL;
Radek Krejci1574a8d2015-08-03 14:16:52 +02001918 struct lys_restr **trg_must = NULL;
Michal Vaskoff006c12016-02-17 11:15:19 +01001919 struct unres_schema tmp_unres;
Radek Krejci27fe55e2016-09-13 17:13:35 +02001920 struct lys_module *mod;
Radek Krejcieb00f512015-07-01 16:44:58 +02001921
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001922 ctx = module->ctx;
1923
Radek Krejcieb00f512015-07-01 16:44:58 +02001924 GETVAL(value, yin, "target-node");
Radek Krejci48464ed2016-03-17 15:44:09 +01001925 dev->target_name = transform_schema2json(module, value);
Michal Vaskoa8b25952015-10-20 15:30:25 +02001926 if (!dev->target_name) {
1927 goto error;
1928 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001929
1930 /* resolve target node */
Radek Krejcidf46e222016-11-08 11:57:37 +01001931 rc = resolve_augment_schema_nodeid(dev->target_name, NULL, module, 1, (const struct lys_node **)&dev_target);
Michal Vasko60f4b452016-02-12 11:02:55 +01001932 if (rc || !dev_target) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001933 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001934 goto error;
1935 }
Radek Krejcic4283442016-04-22 09:19:27 +02001936 if (dev_target->module == lys_main_module(module)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001937 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, yin->name);
1938 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Deviating own module is not allowed.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001939 goto error;
1940 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001941
1942 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejcie534c132016-11-23 13:32:31 +01001943 if (!child->ns ) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001944 /* garbage */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001945 lyxml_free(ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001946 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01001947 } else if (strcmp(child->ns->value, LY_NSYIN)) {
1948 /* extension */
1949 c_ext++;
1950 continue;
1951 } else if (!strcmp(child->name, "description")) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001952 if (dev->dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001953 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001954 goto error;
1955 }
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001956 dev->dsc = read_yin_subnode(ctx, child, "text");
Radek Krejcieb00f512015-07-01 16:44:58 +02001957 if (!dev->dsc) {
1958 goto error;
1959 }
1960 } else if (!strcmp(child->name, "reference")) {
1961 if (dev->ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001962 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001963 goto error;
1964 }
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001965 dev->ref = read_yin_subnode(ctx, child, "text");
Radek Krejcieb00f512015-07-01 16:44:58 +02001966 if (!dev->ref) {
1967 goto error;
1968 }
1969 } else if (!strcmp(child->name, "deviate")) {
1970 c_dev++;
1971
Michal Vasko345da0a2015-12-02 10:35:55 +01001972 /* skip lyxml_free() at the end of the loop, node will be
Radek Krejcieb00f512015-07-01 16:44:58 +02001973 * further processed later
1974 */
1975 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02001976
Radek Krejcieb00f512015-07-01 16:44:58 +02001977 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001978 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001979 goto error;
1980 }
1981
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001982 lyxml_free(ctx, child);
Radek Krejcieb00f512015-07-01 16:44:58 +02001983 }
1984
1985 if (c_dev) {
1986 dev->deviate = calloc(c_dev, sizeof *dev->deviate);
Michal Vasko253035f2015-12-17 16:58:13 +01001987 if (!dev->deviate) {
1988 LOGMEM;
1989 goto error;
1990 }
Pavol Vican09adcc32016-08-25 10:51:36 +02001991 } else {
1992 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "deviate", "deviation");
1993 goto error;
Radek Krejcieb00f512015-07-01 16:44:58 +02001994 }
Radek Krejcie534c132016-11-23 13:32:31 +01001995 if (c_ext) {
1996 dev->ext = calloc(c_ext, sizeof *dev->ext);
1997 if (!dev->ext) {
1998 LOGMEM;
1999 goto error;
2000 }
2001 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002002
Radek Krejcie534c132016-11-23 13:32:31 +01002003
2004 LY_TREE_FOR_SAFE(yin->child, next, develem) {
2005 if (strcmp(develem->ns->value, LY_NSYIN)) {
2006 /* deviation's extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01002007 rc = lyp_yin_fill_ext(dev, LYEXT_PAR_DEVIATION, 0, 0, module, develem, &dev->ext, dev->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01002008 dev->ext_size++;
2009 if (rc) {
2010 goto error;
2011 }
2012 continue;
2013 }
2014
2015 /* deviate */
Radek Krejcieb00f512015-07-01 16:44:58 +02002016 /* init */
2017 f_min = 0;
Radek Krejci0d7b2472016-02-12 11:11:03 +01002018 f_max = 0;
Radek Krejcieb00f512015-07-01 16:44:58 +02002019 c_must = 0;
2020 c_uniq = 0;
Radek Krejcid5a5c282016-08-15 15:38:08 +02002021 c_dflt = 0;
Radek Krejcie534c132016-11-23 13:32:31 +01002022 c_ext = 0;
Radek Krejcieb00f512015-07-01 16:44:58 +02002023
2024 /* get deviation type */
2025 GETVAL(value, develem, "value");
2026 if (!strcmp(value, "not-supported")) {
2027 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_NO;
2028 /* no property expected in this case */
2029 if (develem->child) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002030 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, develem->child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002031 goto error;
2032 }
2033
Radek Krejci5b917642015-07-02 09:03:13 +02002034 /* and neither any other deviate statement is expected,
2035 * not-supported deviation must be the only deviation of the target
2036 */
2037 if (dev->deviate_size || develem->next) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002038 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
2039 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
Radek Krejci5b917642015-07-02 09:03:13 +02002040 goto error;
2041 }
2042
Michal Vaskoad1f7b72016-02-17 11:13:58 +01002043 /* you cannot remove a key leaf */
Michal Vasko43c94772016-05-03 11:47:44 +02002044 if ((dev_target->nodetype == LYS_LEAF) && lys_parent(dev_target) && (lys_parent(dev_target)->nodetype == LYS_LIST)) {
2045 for (i = 0; i < ((struct lys_node_list *)lys_parent(dev_target))->keys_size; ++i) {
2046 if (((struct lys_node_list *)lys_parent(dev_target))->keys[i] == (struct lys_node_leaf *)dev_target) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002047 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
2048 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot remove a list key.");
Michal Vaskoad1f7b72016-02-17 11:13:58 +01002049 goto error;
2050 }
2051 }
2052 }
Radek Krejci5b917642015-07-02 09:03:13 +02002053
Michal Vaskoff006c12016-02-17 11:15:19 +01002054 /* unlink and store the original node */
2055 lys_node_unlink(dev_target);
2056 dev->orig_node = dev_target;
Radek Krejcieb00f512015-07-01 16:44:58 +02002057
Radek Krejci5b917642015-07-02 09:03:13 +02002058 dev->deviate_size = 1;
Pavol Vican85991ec2016-08-16 14:58:12 +02002059 ly_set_free(dflt_check);
Radek Krejci5b917642015-07-02 09:03:13 +02002060 return EXIT_SUCCESS;
Radek Krejcieb00f512015-07-01 16:44:58 +02002061 } else if (!strcmp(value, "add")) {
2062 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_ADD;
2063 } else if (!strcmp(value, "replace")) {
2064 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_RPL;
2065 } else if (!strcmp(value, "delete")) {
2066 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_DEL;
2067 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002068 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002069 goto error;
2070 }
2071 d = &dev->deviate[dev->deviate_size];
Michal Vasko0f7d7ee2016-03-08 09:20:25 +01002072 dev->deviate_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02002073
Michal Vaskoff006c12016-02-17 11:15:19 +01002074 /* store a shallow copy of the original node */
2075 if (!dev->orig_node) {
2076 memset(&tmp_unres, 0, sizeof tmp_unres);
Radek Krejci6ff885d2017-01-03 14:06:22 +01002077 dev->orig_node = lys_node_dup(dev_target->module, NULL, dev_target, &tmp_unres, 1);
Michal Vaskoff006c12016-02-17 11:15:19 +01002078 /* just to be safe */
2079 if (tmp_unres.count) {
2080 LOGINT;
2081 goto error;
2082 }
2083 }
2084
Radek Krejcieb00f512015-07-01 16:44:58 +02002085 /* process deviation properties */
Radek Krejcie534c132016-11-23 13:32:31 +01002086 LY_TREE_FOR_SAFE(develem->child, next2, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002087 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
2088 /* garbage */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002089 lyxml_free(ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02002090 continue;
2091 }
2092
Radek Krejcieb00f512015-07-01 16:44:58 +02002093 if (!strcmp(child->name, "config")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002094 if (d->flags & LYS_CONFIG_MASK) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002095 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002096 goto error;
2097 }
2098
2099 /* for we deviate from RFC 6020 and allow config property even it is/is not
2100 * specified in the target explicitly since config property inherits. So we expect
2101 * that config is specified in every node. But for delete, we check that the value
2102 * is the same as here in deviation
2103 */
2104 GETVAL(value, child, "value");
2105 if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002106 d->flags |= LYS_CONFIG_R;
Radek Krejcieb00f512015-07-01 16:44:58 +02002107 } else if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002108 d->flags |= LYS_CONFIG_W;
Radek Krejcieb00f512015-07-01 16:44:58 +02002109 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002110 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002111 goto error;
2112 }
2113
2114 if (d->mod == LY_DEVIATE_DEL) {
Michal Vaskof7e57d52016-03-07 11:31:09 +01002115 /* del config is forbidden */
Radek Krejci48464ed2016-03-17 15:44:09 +01002116 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "config", "deviate delete");
Michal Vaskof7e57d52016-03-07 11:31:09 +01002117 goto error;
Radek Krejcieb00f512015-07-01 16:44:58 +02002118 } else { /* add and replace are the same in this case */
2119 /* remove current config value of the target ... */
Michal Vasko60f4b452016-02-12 11:02:55 +01002120 dev_target->flags &= ~LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02002121
2122 /* ... and replace it with the value specified in deviation */
Michal Vasko60f4b452016-02-12 11:02:55 +01002123 dev_target->flags |= d->flags & LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02002124 }
2125 } else if (!strcmp(child->name, "default")) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02002126 c_dflt++;
2127
2128 /* check target node type */
2129 if (module->version < 2 && dev_target->nodetype == LYS_LEAFLIST) {
2130 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
2131 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
2132 goto error;
2133 } else if (c_dflt > 1 && dev_target->nodetype != LYS_LEAFLIST) { /* from YANG 1.1 */
2134 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
2135 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow multiple \"default\" properties.");
2136 goto error;
2137 } else if (c_dflt == 1 && (!(dev_target->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE)))) {
2138 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
2139 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002140 goto error;
2141 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002142
Radek Krejcid5a5c282016-08-15 15:38:08 +02002143 /* skip lyxml_free() at the end of the loop, this node will be processed later */
2144 continue;
Radek Krejcieb00f512015-07-01 16:44:58 +02002145
Radek Krejcieb00f512015-07-01 16:44:58 +02002146 } else if (!strcmp(child->name, "mandatory")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002147 if (d->flags & LYS_MAND_MASK) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002148 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002149 goto error;
2150 }
2151
2152 /* check target node type */
Radek Krejcibf2abff2016-08-23 15:51:52 +02002153 if (!(dev_target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002154 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
2155 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002156 goto error;
2157 }
2158
2159 GETVAL(value, child, "value");
2160 if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002161 d->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02002162 } else if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002163 d->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02002164 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002165 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002166 goto error;
2167 }
2168
2169 if (d->mod == LY_DEVIATE_ADD) {
2170 /* check that there is no current value */
Michal Vasko60f4b452016-02-12 11:02:55 +01002171 if (dev_target->flags & LYS_MAND_MASK) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002172 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
2173 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002174 goto error;
2175 }
Radek Krejcie00d2312016-08-12 15:27:49 +02002176
Radek Krejci841ec082016-04-05 13:05:17 +02002177 /* check collision with default-stmt */
Radek Krejcie00d2312016-08-12 15:27:49 +02002178 if (d->flags & LYS_MAND_TRUE) {
2179 if (dev_target->nodetype == LYS_CHOICE) {
2180 if (((struct lys_node_choice *)(dev_target))->dflt) {
2181 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
2182 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
2183 "Adding the \"mandatory\" statement is forbidden on choice with the \"default\" statement.");
2184 goto error;
2185 }
2186 } else if (dev_target->nodetype == LYS_LEAF) {
2187 if (((struct lys_node_leaf *)(dev_target))->dflt) {
2188 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
2189 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
2190 "Adding the \"mandatory\" statement is forbidden on leaf with the \"default\" statement.");
2191 goto error;
2192 }
2193 }
Radek Krejci841ec082016-04-05 13:05:17 +02002194 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002195
Michal Vasko21be1b32016-03-07 12:31:34 +01002196 dev_target->flags |= d->flags & LYS_MAND_MASK;
2197 } else if (d->mod == LY_DEVIATE_RPL) {
2198 /* check that there was a value before */
2199 if (!(dev_target->flags & LYS_MAND_MASK)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002200 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
2201 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002202 goto error;
2203 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002204
Michal Vasko21be1b32016-03-07 12:31:34 +01002205 dev_target->flags &= ~LYS_MAND_MASK;
Michal Vasko60f4b452016-02-12 11:02:55 +01002206 dev_target->flags |= d->flags & LYS_MAND_MASK;
Michal Vasko21be1b32016-03-07 12:31:34 +01002207 } else if (d->mod == LY_DEVIATE_DEL) {
Michal Vaskof7e57d52016-03-07 11:31:09 +01002208 /* del mandatory is forbidden */
Radek Krejci48464ed2016-03-17 15:44:09 +01002209 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "deviate delete");
Michal Vaskof7e57d52016-03-07 11:31:09 +01002210 goto error;
Radek Krejcieb00f512015-07-01 16:44:58 +02002211 }
Radek Krejcie00d2312016-08-12 15:27:49 +02002212
2213 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
2214 for (parent = dev_target->parent;
2215 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION));
2216 parent = parent->parent) {
2217 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
2218 /* stop also on presence containers */
2219 break;
2220 }
2221 }
2222 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
2223 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
2224 if (lyp_check_mandatory_choice(parent)) {
2225 goto error;
2226 }
2227 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002228 } else if (!strcmp(child->name, "min-elements")) {
2229 if (f_min) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002230 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002231 goto error;
2232 }
2233 f_min = 1;
2234
Michal Vasko60f4b452016-02-12 11:02:55 +01002235 if (deviate_minmax(dev_target, child, d, 0)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002236 goto error;
2237 }
2238 } else if (!strcmp(child->name, "max-elements")) {
Radek Krejci0d7b2472016-02-12 11:11:03 +01002239 if (f_max) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002240 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002241 goto error;
2242 }
Radek Krejci0d7b2472016-02-12 11:11:03 +01002243 f_max = 1;
Radek Krejcieb00f512015-07-01 16:44:58 +02002244
Michal Vasko60f4b452016-02-12 11:02:55 +01002245 if (deviate_minmax(dev_target, child, d, 1)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002246 goto error;
2247 }
2248 } else if (!strcmp(child->name, "must")) {
2249 c_must++;
Michal Vasko345da0a2015-12-02 10:35:55 +01002250 /* skip lyxml_free() at the end of the loop, this node will be processed later */
Radek Krejcieb00f512015-07-01 16:44:58 +02002251 continue;
2252 } else if (!strcmp(child->name, "type")) {
2253 if (d->type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002254 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002255 goto error;
2256 }
2257
Michal Vaskof7e57d52016-03-07 11:31:09 +01002258 /* add, del type is forbidden */
2259 if (d->mod == LY_DEVIATE_ADD) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002260 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "deviate add");
Michal Vaskof7e57d52016-03-07 11:31:09 +01002261 goto error;
2262 } else if (d->mod == LY_DEVIATE_DEL) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002263 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "deviate delete");
Michal Vaskof7e57d52016-03-07 11:31:09 +01002264 goto error;
2265 }
2266
Radek Krejcieb00f512015-07-01 16:44:58 +02002267 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01002268 if (dev_target->nodetype == LYS_LEAF) {
2269 t = &((struct lys_node_leaf *)dev_target)->type;
Pavol Vican2e322822016-09-07 15:48:13 +02002270 if (((struct lys_node_leaf *)dev_target)->dflt) {
2271 ly_set_add(dflt_check, dev_target, 0);
2272 }
Michal Vasko60f4b452016-02-12 11:02:55 +01002273 } else if (dev_target->nodetype == LYS_LEAFLIST) {
2274 t = &((struct lys_node_leaflist *)dev_target)->type;
Pavol Vican2e322822016-09-07 15:48:13 +02002275 if (((struct lys_node_leaflist *)dev_target)->dflt) {
2276 ly_set_add(dflt_check, dev_target, 0);
2277 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002278 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002279 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
2280 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002281 goto error;
2282 }
2283
Radek Krejcieb00f512015-07-01 16:44:58 +02002284 /* replace */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002285 lys_type_free(ctx, t);
Michal Vasko88c29542015-11-27 14:57:53 +01002286 /* HACK for unres */
2287 t->der = (struct lys_tpdf *)child;
Michal Vasko3767fb22016-07-21 12:10:57 +02002288 if (unres_schema_add_node(module, unres, t, UNRES_TYPE_DER, dev_target) == -1) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002289 goto error;
2290 }
2291 d->type = t;
2292 } else if (!strcmp(child->name, "unique")) {
2293 c_uniq++;
Michal Vasko345da0a2015-12-02 10:35:55 +01002294 /* skip lyxml_free() at the end of the loop, this node will be processed later */
Radek Krejcieb00f512015-07-01 16:44:58 +02002295 continue;
2296 } else if (!strcmp(child->name, "units")) {
2297 if (d->units) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002298 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002299 goto error;
2300 }
2301
2302 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01002303 if (dev_target->nodetype == LYS_LEAFLIST) {
2304 stritem = &((struct lys_node_leaflist *)dev_target)->units;
2305 } else if (dev_target->nodetype == LYS_LEAF) {
2306 stritem = &((struct lys_node_leaf *)dev_target)->units;
Radek Krejcieb00f512015-07-01 16:44:58 +02002307 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002308 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
2309 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002310 goto error;
2311 }
2312
2313 /* get units value */
2314 GETVAL(value, child, "name");
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002315 d->units = lydict_insert(ctx, value, 0);
Radek Krejcieb00f512015-07-01 16:44:58 +02002316
2317 /* apply to target */
2318 if (d->mod == LY_DEVIATE_ADD) {
2319 /* check that there is no current value */
2320 if (*stritem) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002321 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
2322 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002323 goto error;
2324 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002325
Michal Vasko21be1b32016-03-07 12:31:34 +01002326 *stritem = lydict_insert(ctx, value, 0);
2327 } else if (d->mod == LY_DEVIATE_RPL) {
2328 /* check that there was a value before */
2329 if (!*stritem) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002330 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
2331 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Michal Vasko21be1b32016-03-07 12:31:34 +01002332 goto error;
2333 }
2334
2335 lydict_remove(ctx, *stritem);
2336 *stritem = lydict_insert(ctx, value, 0);
2337 } else if (d->mod == LY_DEVIATE_DEL) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002338 /* check values */
Michal Vaskob42b6972016-06-06 14:21:30 +02002339 if (!ly_strequal(*stritem, d->units, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002340 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
2341 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002342 goto error;
2343 }
2344 /* remove current units value of the target */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002345 lydict_remove(ctx, *stritem);
Radek Krejcieb00f512015-07-01 16:44:58 +02002346 }
2347 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002348 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002349 goto error;
2350 }
2351
Michal Vasko88c29542015-11-27 14:57:53 +01002352 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejcieb00f512015-07-01 16:44:58 +02002353 }
2354
2355 if (c_must) {
2356 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01002357 switch (dev_target->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002358 case LYS_LEAF:
Michal Vasko60f4b452016-02-12 11:02:55 +01002359 trg_must = &((struct lys_node_leaf *)dev_target)->must;
2360 trg_must_size = &((struct lys_node_leaf *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02002361 break;
Radek Krejci76512572015-08-04 09:47:08 +02002362 case LYS_CONTAINER:
Michal Vasko60f4b452016-02-12 11:02:55 +01002363 trg_must = &((struct lys_node_container *)dev_target)->must;
2364 trg_must_size = &((struct lys_node_container *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02002365 break;
Radek Krejci76512572015-08-04 09:47:08 +02002366 case LYS_LEAFLIST:
Michal Vasko60f4b452016-02-12 11:02:55 +01002367 trg_must = &((struct lys_node_leaflist *)dev_target)->must;
2368 trg_must_size = &((struct lys_node_leaflist *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02002369 break;
Radek Krejci76512572015-08-04 09:47:08 +02002370 case LYS_LIST:
Michal Vasko60f4b452016-02-12 11:02:55 +01002371 trg_must = &((struct lys_node_list *)dev_target)->must;
2372 trg_must_size = &((struct lys_node_list *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02002373 break;
Radek Krejci76512572015-08-04 09:47:08 +02002374 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002375 case LYS_ANYDATA:
2376 trg_must = &((struct lys_node_anydata *)dev_target)->must;
2377 trg_must_size = &((struct lys_node_anydata *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02002378 break;
2379 default:
Radek Krejcid5a5c282016-08-15 15:38:08 +02002380 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must");
2381 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002382 goto error;
2383 }
2384
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002385 dev_target->flags &= ~LYS_XPATH_DEP;
Michal Vasko508a50d2016-09-07 14:50:33 +02002386
Radek Krejcieb00f512015-07-01 16:44:58 +02002387 if (d->mod == LY_DEVIATE_RPL) {
Michal Vaskof7e57d52016-03-07 11:31:09 +01002388 /* replace must is forbidden */
Radek Krejci48464ed2016-03-17 15:44:09 +01002389 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "must", "deviate replace");
Michal Vaskof7e57d52016-03-07 11:31:09 +01002390 goto error;
Radek Krejcieb00f512015-07-01 16:44:58 +02002391 } else if (d->mod == LY_DEVIATE_ADD) {
2392 /* reallocate the must array of the target */
Michal Vasko253035f2015-12-17 16:58:13 +01002393 d->must = ly_realloc(*trg_must, (c_must + *trg_must_size) * sizeof *d->must);
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002394 if (!d->must) {
2395 LOGMEM;
2396 goto error;
2397 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002398 *trg_must = d->must;
Michal Vasko979ad5b2015-10-23 10:12:55 +02002399 d->must = &((*trg_must)[*trg_must_size]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002400 d->must_size = c_must;
2401 } else { /* LY_DEVIATE_DEL */
2402 d->must = calloc(c_must, sizeof *d->must);
2403 }
Michal Vasko253035f2015-12-17 16:58:13 +01002404 if (!d->must) {
2405 LOGMEM;
2406 goto error;
2407 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002408 }
2409 if (c_uniq) {
Michal Vaskof7e57d52016-03-07 11:31:09 +01002410 /* replace unique is forbidden */
2411 if (d->mod == LY_DEVIATE_RPL) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002412 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "unique", "deviate replace");
Michal Vaskof7e57d52016-03-07 11:31:09 +01002413 goto error;
2414 }
2415
Radek Krejcieb00f512015-07-01 16:44:58 +02002416 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01002417 if (dev_target->nodetype != LYS_LIST) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02002418 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique");
2419 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"unique\" property.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002420 goto error;
2421 }
2422
Michal Vasko60f4b452016-02-12 11:02:55 +01002423 list = (struct lys_node_list *)dev_target;
Michal Vaskof7e57d52016-03-07 11:31:09 +01002424 if (d->mod == LY_DEVIATE_ADD) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002425 /* reallocate the unique array of the target */
Michal Vasko253035f2015-12-17 16:58:13 +01002426 d->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *d->unique);
Radek Krejcieb00f512015-07-01 16:44:58 +02002427 list->unique = d->unique;
2428 d->unique = &list->unique[list->unique_size];
2429 d->unique_size = c_uniq;
2430 } else { /* LY_DEVIATE_DEL */
2431 d->unique = calloc(c_uniq, sizeof *d->unique);
2432 }
Michal Vasko253035f2015-12-17 16:58:13 +01002433 if (!d->unique) {
2434 LOGMEM;
2435 goto error;
2436 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002437 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02002438 if (c_dflt) {
2439 if (d->mod == LY_DEVIATE_ADD) {
2440 /* check that there is no current value */
2441 if ((dev_target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev_target)->dflt) ||
2442 (dev_target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev_target)->dflt)) {
2443 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
2444 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
2445 goto error;
2446 }
2447
2448 /* check collision with mandatory/min-elements */
2449 if ((dev_target->flags & LYS_MAND_TRUE) ||
2450 (dev_target->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)dev_target)->min)) {
2451 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
2452 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
2453 "Adding the \"default\" statement is forbidden on %s statement.",
2454 (dev_target->flags & LYS_MAND_TRUE) ? "nodes with the \"mandatory\"" : "leaflists with non-zero \"min-elements\"");
2455 goto error;
2456 }
2457 } else if (d->mod == LY_DEVIATE_RPL) {
2458 /* check that there was a value before */
2459 if (((dev_target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && !((struct lys_node_leaf *)dev_target)->dflt) ||
2460 (dev_target->nodetype == LYS_CHOICE && !((struct lys_node_choice *)dev_target)->dflt)) {
2461 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
2462 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
2463 goto error;
2464 }
2465 }
2466
2467 if (dev_target->nodetype == LYS_LEAFLIST) {
2468 /* reallocate default list in the target */
2469 llist = (struct lys_node_leaflist *)dev_target;
2470 if (d->mod == LY_DEVIATE_ADD) {
2471 /* reallocate (enlarge) the unique array of the target */
2472 llist->dflt = ly_realloc(llist->dflt, (c_dflt + llist->dflt_size) * sizeof *d->dflt);
2473 } else if (d->mod == LY_DEVIATE_RPL) {
2474 /* reallocate (replace) the unique array of the target */
2475 for (i = 0; i < llist->dflt_size; i++) {
2476 lydict_remove(llist->module->ctx, llist->dflt[i]);
2477 }
2478 llist->dflt = ly_realloc(llist->dflt, c_dflt * sizeof *d->dflt);
2479 llist->dflt_size = 0;
2480 }
2481 }
2482 d->dflt = calloc(c_dflt, sizeof *d->dflt);
2483 if (!d->dflt) {
2484 LOGMEM;
2485 goto error;
2486 }
2487 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002488
2489 /* process deviation properties with 0..n cardinality */
Radek Krejci73adb602015-07-02 18:07:40 +02002490 LY_TREE_FOR(develem->child, child) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002491 if (!strcmp(child->name, "must")) {
2492 if (d->mod == LY_DEVIATE_DEL) {
Radek Krejcifccd1442017-01-16 10:26:57 +01002493 if (fill_yin_must(module, child, &d->must[d->must_size], unres)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002494 goto error;
2495 }
2496
2497 /* find must to delete, we are ok with just matching conditions */
2498 for (i = 0; i < *trg_must_size; i++) {
Radek Krejci749190d2016-02-18 16:26:25 +01002499 if (ly_strequal(d->must[d->must_size].expr, (*trg_must)[i].expr, 1)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002500 /* we have a match, free the must structure ... */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002501 lys_restr_free(ctx, &((*trg_must)[i]));
Radek Krejcieb00f512015-07-01 16:44:58 +02002502 /* ... and maintain the array */
2503 (*trg_must_size)--;
2504 if (i != *trg_must_size) {
2505 (*trg_must)[i].expr = (*trg_must)[*trg_must_size].expr;
2506 (*trg_must)[i].dsc = (*trg_must)[*trg_must_size].dsc;
2507 (*trg_must)[i].ref = (*trg_must)[*trg_must_size].ref;
2508 (*trg_must)[i].eapptag = (*trg_must)[*trg_must_size].eapptag;
2509 (*trg_must)[i].emsg = (*trg_must)[*trg_must_size].emsg;
2510 }
2511 if (!(*trg_must_size)) {
2512 free(*trg_must);
2513 *trg_must = NULL;
2514 } else {
2515 (*trg_must)[*trg_must_size].expr = NULL;
2516 (*trg_must)[*trg_must_size].dsc = NULL;
2517 (*trg_must)[*trg_must_size].ref = NULL;
2518 (*trg_must)[*trg_must_size].eapptag = NULL;
2519 (*trg_must)[*trg_must_size].emsg = NULL;
2520 }
2521
2522 i = -1; /* set match flag */
2523 break;
2524 }
2525 }
2526 d->must_size++;
2527 if (i != -1) {
2528 /* no match found */
Radek Krejci48464ed2016-03-17 15:44:09 +01002529 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL,
Radek Krejciadb57612016-02-16 13:34:34 +01002530 d->must[d->must_size - 1].expr, child->name);
Radek Krejci48464ed2016-03-17 15:44:09 +01002531 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value does not match any must from the target.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002532 goto error;
2533 }
2534 } else { /* replace or add */
Michal Vaskof92a7282016-02-11 12:35:57 +01002535 memset(&((*trg_must)[*trg_must_size]), 0, sizeof **trg_must);
Radek Krejcifccd1442017-01-16 10:26:57 +01002536 if (fill_yin_must(module, child, &((*trg_must)[*trg_must_size]), unres)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002537 goto error;
2538 }
2539 (*trg_must_size)++;
2540 }
Michal Vasko508a50d2016-09-07 14:50:33 +02002541
2542 /* check XPath dependencies again */
Michal Vaskoe8734262016-09-29 14:12:06 +02002543 if (*trg_must_size && unres_schema_add_node(module, unres, dev_target, UNRES_XPATH, NULL)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02002544 goto error;
2545 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002546 } else if (!strcmp(child->name, "unique")) {
2547 if (d->mod == LY_DEVIATE_DEL) {
Michal Vaskoa0a10ab2016-03-07 14:41:23 +01002548 memset(&d->unique[d->unique_size], 0, sizeof *d->unique);
Michal Vasko60f4b452016-02-12 11:02:55 +01002549 if (fill_yin_unique(module, dev_target, child, &d->unique[d->unique_size], NULL)) {
Radek Krejci581ce772015-11-10 17:22:40 +01002550 d->unique_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02002551 goto error;
2552 }
2553
2554 /* find unique structures to delete */
2555 for (i = 0; i < list->unique_size; i++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002556 if (list->unique[i].expr_size != d->unique[d->unique_size].expr_size) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002557 continue;
2558 }
2559
Radek Krejci581ce772015-11-10 17:22:40 +01002560 for (j = 0; j < d->unique[d->unique_size].expr_size; j++) {
Radek Krejci749190d2016-02-18 16:26:25 +01002561 if (!ly_strequal(list->unique[i].expr[j], d->unique[d->unique_size].expr[j], 1)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002562 break;
2563 }
2564 }
2565
Radek Krejci581ce772015-11-10 17:22:40 +01002566 if (j == d->unique[d->unique_size].expr_size) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002567 /* we have a match, free the unique structure ... */
Radek Krejci581ce772015-11-10 17:22:40 +01002568 for (j = 0; j < list->unique[i].expr_size; j++) {
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002569 lydict_remove(ctx, list->unique[i].expr[j]);
Radek Krejci581ce772015-11-10 17:22:40 +01002570 }
2571 free(list->unique[i].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02002572 /* ... and maintain the array */
2573 list->unique_size--;
2574 if (i != list->unique_size) {
Radek Krejci581ce772015-11-10 17:22:40 +01002575 list->unique[i].expr_size = list->unique[list->unique_size].expr_size;
2576 list->unique[i].expr = list->unique[list->unique_size].expr;
Radek Krejcieb00f512015-07-01 16:44:58 +02002577 }
2578
2579 if (!list->unique_size) {
2580 free(list->unique);
2581 list->unique = NULL;
2582 } else {
Radek Krejci581ce772015-11-10 17:22:40 +01002583 list->unique[list->unique_size].expr_size = 0;
2584 list->unique[list->unique_size].expr = NULL;
Radek Krejcieb00f512015-07-01 16:44:58 +02002585 }
2586
2587 i = -1; /* set match flag */
2588 break;
2589 }
2590 }
2591
2592 d->unique_size++;
2593 if (i != -1) {
2594 /* no match found */
Radek Krejci48464ed2016-03-17 15:44:09 +01002595 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, lyxml_get_attr(child, "tag", NULL), child->name);
2596 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002597 goto error;
2598 }
2599 } else { /* replace or add */
Michal Vaskoa0a10ab2016-03-07 14:41:23 +01002600 memset(&list->unique[list->unique_size], 0, sizeof *list->unique);
Michal Vasko60f4b452016-02-12 11:02:55 +01002601 i = fill_yin_unique(module, dev_target, child, &list->unique[list->unique_size], NULL);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002602 list->unique_size++;
2603 if (i) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002604 goto error;
2605 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002606 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02002607 } else if (!strcmp(child->name, "default")) {
2608 GETVAL(value, child, "value");
2609 u = strlen(value);
2610 d->dflt[d->dflt_size++] = lydict_insert(module->ctx, value, u);
2611
2612 if (dev_target->nodetype == LYS_CHOICE) {
2613 choice = (struct lys_node_choice *)dev_target;
2614 rc = resolve_choice_default_schema_nodeid(value, choice->child, (const struct lys_node **)&node);
2615 if (rc || !node) {
2616 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2617 goto error;
2618 }
2619 if (d->mod == LY_DEVIATE_DEL) {
2620 if (!choice->dflt || (choice->dflt != node)) {
2621 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2622 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
2623 goto error;
2624 }
2625 } else { /* add or replace */
2626 choice->dflt = node;
2627 if (!choice->dflt) {
2628 /* default branch not found */
2629 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2630 goto error;
2631 }
2632 }
2633 } else if (dev_target->nodetype == LYS_LEAF) {
2634 leaf = (struct lys_node_leaf *)dev_target;
2635 if (d->mod == LY_DEVIATE_DEL) {
2636 if (!leaf->dflt || !ly_strequal(leaf->dflt, value, 1)) {
2637 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2638 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
2639 goto error;
2640 }
2641 /* remove value */
2642 lydict_remove(ctx, leaf->dflt);
2643 leaf->dflt = NULL;
Radek Krejcibd117f02016-11-04 16:28:08 +01002644 leaf->flags &= ~LYS_DFLTJSON;
Radek Krejcid5a5c282016-08-15 15:38:08 +02002645 } else { /* add (already checked) and replace */
2646 /* remove value */
2647 lydict_remove(ctx, leaf->dflt);
Radek Krejcibd117f02016-11-04 16:28:08 +01002648 leaf->flags &= ~LYS_DFLTJSON;
Radek Krejcid5a5c282016-08-15 15:38:08 +02002649
2650 /* set new value */
2651 leaf->dflt = lydict_insert(ctx, value, u);
2652
Radek Krejcibd117f02016-11-04 16:28:08 +01002653 /* remember to check it later (it may not fit now, because the type can be deviated too) */
Radek Krejcid5a5c282016-08-15 15:38:08 +02002654 ly_set_add(dflt_check, dev_target, 0);
2655 }
2656 } else { /* LYS_LEAFLIST */
2657 llist = (struct lys_node_leaflist *)dev_target;
2658 if (d->mod == LY_DEVIATE_DEL) {
2659 /* find and remove the value in target list */
2660 for (i = 0; i < llist->dflt_size; i++) {
2661 if (llist->dflt[i] && ly_strequal(llist->dflt[i], value, 1)) {
2662 /* match, remove the value */
2663 lydict_remove(llist->module->ctx, llist->dflt[i]);
2664 llist->dflt[i] = NULL;
2665 break;
2666 }
2667 }
2668 if (i == llist->dflt_size) {
2669 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2670 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The default value to delete not found in the target node.");
2671 goto error;
2672 }
2673 } else {
2674 /* add or replace, anyway we place items into the deviate's list
2675 which propagates to the target */
2676 /* we just want to check that the value isn't already in the list */
2677 for (i = 0; i < llist->dflt_size; i++) {
2678 if (ly_strequal(llist->dflt[i], value, 1)) {
2679 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2680 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
2681 goto error;
2682 }
2683 }
2684 /* store it in target node */
2685 llist->dflt[llist->dflt_size++] = lydict_insert(module->ctx, value, u);
2686
2687 /* remember to check it later (it may not fit now, but the type can be deviated too) */
2688 ly_set_add(dflt_check, dev_target, 0);
Radek Krejcibd117f02016-11-04 16:28:08 +01002689 leaf->flags &= ~LYS_DFLTJSON;
Radek Krejcid5a5c282016-08-15 15:38:08 +02002690 }
2691 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002692 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002693 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02002694
2695 if (c_dflt && dev_target->nodetype == LYS_LEAFLIST && d->mod == LY_DEVIATE_DEL) {
2696 /* consolidate the final list in the target after removing items from it */
2697 llist = (struct lys_node_leaflist *)dev_target;
2698 for (i = j = 0; j < llist->dflt_size; j++) {
2699 llist->dflt[i] = llist->dflt[j];
2700 if (llist->dflt[i]) {
2701 i++;
2702 }
2703 }
2704 llist->dflt_size = i + 1;
2705 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002706 }
2707
Michal Vasko43a1feb2016-03-07 12:03:02 +01002708 /* now check whether default value, if any, matches the type */
Pavol Vican85991ec2016-08-16 14:58:12 +02002709 for (u = 0; u < dflt_check->number; ++u) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02002710 value = NULL;
2711 rc = EXIT_SUCCESS;
2712 if (dflt_check->set.s[u]->nodetype == LYS_LEAF) {
2713 leaf = (struct lys_node_leaf *)dflt_check->set.s[u];
Radek Krejci51673202016-11-01 17:00:32 +01002714 value = leaf->dflt;
2715 rc = unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)(&leaf->dflt));
Radek Krejcid5a5c282016-08-15 15:38:08 +02002716 } else { /* LYS_LEAFLIST */
2717 llist = (struct lys_node_leaflist *)dflt_check->set.s[u];
2718 for (j = 0; j < llist->dflt_size; j++) {
Radek Krejci51673202016-11-01 17:00:32 +01002719 rc = unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT,
2720 (struct lys_node *)(&llist->dflt[j]));
Radek Krejcid5a5c282016-08-15 15:38:08 +02002721 if (rc == -1) {
Radek Krejci51673202016-11-01 17:00:32 +01002722 value = llist->dflt[j];
Radek Krejcid5a5c282016-08-15 15:38:08 +02002723 break;
2724 }
Michal Vasko43a1feb2016-03-07 12:03:02 +01002725 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02002726
2727 }
2728 if (rc == -1) {
2729 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2730 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
2731 "The default value \"%s\" of the deviated node \"%s\"no longer matches its type.",
2732 dev->target_name);
2733 goto error;
Michal Vasko43a1feb2016-03-07 12:03:02 +01002734 }
2735 }
Michal Vasko43a1feb2016-03-07 12:03:02 +01002736
Radek Krejci27fe55e2016-09-13 17:13:35 +02002737 /* mark all the affected modules as deviated and implemented */
2738 for(parent = dev_target; parent; parent = lys_parent(parent)) {
2739 mod = lys_node_module(parent);
2740 if (module != mod) {
2741 mod->deviated = 1;
2742 lys_set_implemented(mod);
2743 }
2744 }
2745
Radek Krejcid5a5c282016-08-15 15:38:08 +02002746 ly_set_free(dflt_check);
Radek Krejcieb00f512015-07-01 16:44:58 +02002747 return EXIT_SUCCESS;
2748
2749error:
Radek Krejcid5a5c282016-08-15 15:38:08 +02002750 ly_set_free(dflt_check);
Radek Krejcieb00f512015-07-01 16:44:58 +02002751 return EXIT_FAILURE;
2752}
2753
Michal Vasko0d343d12015-08-24 14:57:36 +02002754/* logs directly */
Radek Krejcieb00f512015-07-01 16:44:58 +02002755static int
Radek Krejcib8048692015-08-05 13:36:34 +02002756fill_yin_augment(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_node_augment *aug,
Michal Vaskof02e3742015-08-05 16:27:02 +02002757 struct unres_schema *unres)
Radek Krejci106efc02015-06-10 14:36:27 +02002758{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002759 const char *value;
Radek Krejcie534c132016-11-23 13:32:31 +01002760 struct lyxml_elem *sub, *next;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002761 struct lys_node *node;
Radek Krejcie534c132016-11-23 13:32:31 +01002762 int ret, c_ftrs = 0, c_ext = 0;
Radek Krejci106efc02015-06-10 14:36:27 +02002763
Michal Vasko591e0b22015-08-13 13:53:43 +02002764 aug->nodetype = LYS_AUGMENT;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002765 GETVAL(value, yin, "target-node");
Radek Krejci48464ed2016-03-17 15:44:09 +01002766 aug->target_name = transform_schema2json(module, value);
Michal Vasko488c19e2015-10-20 15:21:00 +02002767 if (!aug->target_name) {
2768 goto error;
2769 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002770 aug->parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02002771
Radek Krejci07d0fb92017-01-13 14:11:05 +01002772 if (read_yin_common(module, NULL, aug, LYEXT_PAR_NODE, yin, OPT_MODULE, unres)) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02002773 goto error;
2774 }
2775
Radek Krejcie534c132016-11-23 13:32:31 +01002776 LY_TREE_FOR_SAFE(yin->child, next, sub) {
2777 if (strcmp(sub->ns->value, LY_NSYIN)) {
2778 /* extension */
2779 c_ext++;
Radek Krejci0d70c372015-07-02 16:23:10 +02002780 continue;
2781 }
2782
Radek Krejcie534c132016-11-23 13:32:31 +01002783 if (!strcmp(sub->name, "if-feature")) {
2784 c_ftrs++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002785 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01002786 } else if (!strcmp(sub->name, "when")) {
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002787 if (aug->when) {
Radek Krejcie534c132016-11-23 13:32:31 +01002788 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002789 goto error;
2790 }
2791
Radek Krejci5323b492017-01-16 15:40:11 +01002792 aug->when = read_yin_when(module, sub, unres);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002793 if (!aug->when) {
Radek Krejcie534c132016-11-23 13:32:31 +01002794 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002795 goto error;
2796 }
Radek Krejcie534c132016-11-23 13:32:31 +01002797 lyxml_free(module->ctx, sub);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002798 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02002799
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002800 /* check allowed data sub-statements */
Radek Krejcie534c132016-11-23 13:32:31 +01002801 } else if (!strcmp(sub->name, "container")) {
2802 node = read_yin_container(module, (struct lys_node *)aug, sub, 1, unres);
2803 } else if (!strcmp(sub->name, "leaf-list")) {
2804 node = read_yin_leaflist(module, (struct lys_node *)aug, sub, 1, unres);
2805 } else if (!strcmp(sub->name, "leaf")) {
2806 node = read_yin_leaf(module, (struct lys_node *)aug, sub, 1, unres);
2807 } else if (!strcmp(sub->name, "list")) {
2808 node = read_yin_list(module, (struct lys_node *)aug, sub, 1, unres);
2809 } else if (!strcmp(sub->name, "uses")) {
2810 node = read_yin_uses(module, (struct lys_node *)aug, sub, unres);
2811 } else if (!strcmp(sub->name, "choice")) {
2812 node = read_yin_choice(module, (struct lys_node *)aug, sub, 1, unres);
2813 } else if (!strcmp(sub->name, "case")) {
2814 node = read_yin_case(module, (struct lys_node *)aug, sub, 1, unres);
2815 } else if (!strcmp(sub->name, "anyxml")) {
2816 node = read_yin_anydata(module, (struct lys_node *)aug, sub, LYS_ANYXML, 1, unres);
2817 } else if (!strcmp(sub->name, "anydata")) {
2818 node = read_yin_anydata(module, (struct lys_node *)aug, sub, LYS_ANYDATA, 1, unres);
2819 } else if (!strcmp(sub->name, "action")) {
2820 node = read_yin_rpc_action(module, (struct lys_node *)aug, sub, unres);
2821 } else if (!strcmp(sub->name, "notification")) {
2822 node = read_yin_notif(module, (struct lys_node *)aug, sub, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002823 } else {
Radek Krejcie534c132016-11-23 13:32:31 +01002824 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002825 goto error;
2826 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002827
Radek Krejci1d82ef62015-08-07 14:44:40 +02002828 if (!node) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002829 goto error;
2830 }
2831
Radek Krejci1d82ef62015-08-07 14:44:40 +02002832 node = NULL;
Radek Krejcie534c132016-11-23 13:32:31 +01002833 lyxml_free(module->ctx, sub);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002834 }
2835
Radek Krejcie534c132016-11-23 13:32:31 +01002836 if (c_ftrs) {
2837 aug->iffeature = calloc(c_ftrs, sizeof *aug->iffeature);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002838 if (!aug->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01002839 LOGMEM;
2840 goto error;
2841 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002842 }
Radek Krejcie534c132016-11-23 13:32:31 +01002843 if (c_ext) {
2844 aug->ext = calloc(c_ext, sizeof *aug->ext);
2845 if (!aug->ext) {
2846 LOGMEM;
2847 goto error;
2848 }
2849 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002850
Radek Krejcie534c132016-11-23 13:32:31 +01002851 LY_TREE_FOR_SAFE(yin->child, next, sub) {
2852 if (strcmp(sub->ns->value, LY_NSYIN)) {
2853 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01002854 ret = lyp_yin_fill_ext(aug, LYEXT_PAR_NODE, 0, 0, module, sub, &aug->ext, aug->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01002855 aug->ext_size++;
2856 if (ret) {
2857 goto error;
2858 }
2859 } else if (!strcmp(sub->name, "if-feature")) {
2860 ret = fill_yin_iffeature((struct lys_node *)aug, 0, sub, &aug->iffeature[aug->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002861 aug->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01002862 if (ret) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002863 goto error;
2864 }
Radek Krejcie534c132016-11-23 13:32:31 +01002865 lyxml_free(module->ctx, sub);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002866 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002867 }
2868
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002869 /* aug->child points to the parsed nodes, they must now be
Michal Vasko49291b32015-08-06 09:49:41 +02002870 * connected to the tree and adjusted (if possible right now).
Radek Krejci27fe55e2016-09-13 17:13:35 +02002871 * However, if this is augment in a uses (parent is NULL), it gets resolved
Michal Vasko49291b32015-08-06 09:49:41 +02002872 * when the uses does and cannot be resolved now for sure
2873 * (the grouping was not yet copied into uses).
2874 */
Radek Krejci27fe55e2016-09-13 17:13:35 +02002875 if (!parent) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002876 if (unres_schema_add_node(module, unres, aug, UNRES_AUGMENT, NULL) == -1) {
Michal Vasko4adc10f2015-08-11 15:26:17 +02002877 goto error;
2878 }
Michal Vasko49291b32015-08-06 09:49:41 +02002879 }
Radek Krejci106efc02015-06-10 14:36:27 +02002880
Michal Vasko508a50d2016-09-07 14:50:33 +02002881 /* check XPath dependencies */
2882 if (aug->when && (unres_schema_add_node(module, unres, (struct lys_node *)aug, UNRES_XPATH, NULL) == -1)) {
2883 goto error;
2884 }
2885
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002886 return EXIT_SUCCESS;
Radek Krejci106efc02015-06-10 14:36:27 +02002887
2888error:
2889
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002890 return EXIT_FAILURE;
Radek Krejci106efc02015-06-10 14:36:27 +02002891}
2892
Michal Vasko0d343d12015-08-24 14:57:36 +02002893/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002894static int
Radek Krejci363bd4a2016-07-29 14:30:20 +02002895fill_yin_refine(struct lys_node *uses, struct lyxml_elem *yin, struct lys_refine *rfn, struct unres_schema *unres)
Radek Krejci3bde87f2015-06-05 16:51:58 +02002896{
Radek Krejci363bd4a2016-07-29 14:30:20 +02002897 struct lys_module *module;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002898 struct lyxml_elem *sub, *next;
2899 const char *value;
2900 char *endptr;
2901 int f_mand = 0, f_min = 0, f_max = 0;
Radek Krejcie534c132016-11-23 13:32:31 +01002902 int c_must = 0, c_ftrs = 0, c_dflt = 0, c_ext = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002903 int r;
2904 unsigned long int val;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002905
Radek Krejci363bd4a2016-07-29 14:30:20 +02002906 assert(uses);
2907 module = uses->module; /* shorthand */
2908
Radek Krejci07d0fb92017-01-13 14:11:05 +01002909 if (read_yin_common(module, NULL, rfn, LYEXT_PAR_REFINE, yin, OPT_CFG_PARSE | OPT_MODULE, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002910 goto error;
2911 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002912
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002913 GETVAL(value, yin, "target-node");
Radek Krejci48464ed2016-03-17 15:44:09 +01002914 rfn->target_name = transform_schema2json(module, value);
Michal Vaskoa8b25952015-10-20 15:30:25 +02002915 if (!rfn->target_name) {
2916 goto error;
2917 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002918
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002919 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01002920 if (strcmp(sub->ns->value, LY_NSYIN)) {
2921 /* extension */
2922 c_ext++;
Radek Krejci0d70c372015-07-02 16:23:10 +02002923 continue;
Radek Krejci0d70c372015-07-02 16:23:10 +02002924
Radek Krejcie534c132016-11-23 13:32:31 +01002925 } else if (!strcmp(sub->name, "default")) {
Radek Krejci200bf712016-08-16 17:11:04 +02002926 /* leaf, leaf-list or choice */
Radek Krejci3bde87f2015-06-05 16:51:58 +02002927
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002928 /* check possibility of statements combination */
2929 if (rfn->target_type) {
Radek Krejci200bf712016-08-16 17:11:04 +02002930 if (c_dflt) {
2931 /* multiple defaults are allowed only in leaf-list */
Pavol Vican35aa9ea2016-08-17 10:27:43 +02002932 if (module->version < 2) {
2933 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
2934 goto error;
2935 }
Radek Krejci200bf712016-08-16 17:11:04 +02002936 rfn->target_type &= LYS_LEAFLIST;
2937 } else {
Pavol Vican35aa9ea2016-08-17 10:27:43 +02002938 if (module->version < 2) {
2939 rfn->target_type &= (LYS_LEAF | LYS_CHOICE);
2940 } else {
2941 /* YANG 1.1 */
2942 rfn->target_type &= (LYS_LEAFLIST | LYS_LEAF | LYS_CHOICE);
2943 }
Radek Krejci200bf712016-08-16 17:11:04 +02002944 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002945 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002946 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
2947 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002948 goto error;
2949 }
2950 } else {
Pavol Vican35aa9ea2016-08-17 10:27:43 +02002951 if (module->version < 2) {
2952 rfn->target_type = LYS_LEAF | LYS_CHOICE;
2953 } else {
2954 /* YANG 1.1 */
2955 rfn->target_type = LYS_LEAFLIST | LYS_LEAF | LYS_CHOICE;
2956 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002957 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002958
Radek Krejci200bf712016-08-16 17:11:04 +02002959 c_dflt++;
Pavol Vican35aa9ea2016-08-17 10:27:43 +02002960 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002961 } else if (!strcmp(sub->name, "mandatory")) {
2962 /* leaf, choice or anyxml */
2963 if (f_mand) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002964 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002965 goto error;
2966 }
2967 /* just checking the flags in leaf is not sufficient, we would allow
2968 * multiple mandatory statements with the "false" value
2969 */
2970 f_mand = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002971
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002972 /* check possibility of statements combination */
2973 if (rfn->target_type) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002974 rfn->target_type &= (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002975 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002976 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
2977 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002978 goto error;
2979 }
2980 } else {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002981 rfn->target_type = LYS_LEAF | LYS_CHOICE | LYS_ANYDATA;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002982 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002983
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002984 GETVAL(value, sub, "value");
2985 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002986 rfn->flags |= LYS_MAND_TRUE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002987 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002988 rfn->flags |= LYS_MAND_FALSE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002989 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002990 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002991 goto error;
2992 }
2993 } else if (!strcmp(sub->name, "min-elements")) {
2994 /* list or leaf-list */
2995 if (f_min) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002996 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002997 goto error;
2998 }
2999 f_min = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02003000
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003001 /* check possibility of statements combination */
3002 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02003003 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003004 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003005 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
3006 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003007 goto error;
3008 }
3009 } else {
Radek Krejci76512572015-08-04 09:47:08 +02003010 rfn->target_type = LYS_LIST | LYS_LEAFLIST;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003011 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003012
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003013 GETVAL(value, sub, "value");
3014 while (isspace(value[0])) {
3015 value++;
3016 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003017
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003018 /* convert it to uint32_t */
3019 errno = 0;
3020 endptr = NULL;
3021 val = strtoul(value, &endptr, 10);
3022 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003023 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003024 goto error;
3025 }
3026 rfn->mod.list.min = (uint32_t) val;
Radek Krejci0f04a6c2016-04-14 16:16:36 +02003027 rfn->flags |= LYS_RFN_MINSET;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003028 } else if (!strcmp(sub->name, "max-elements")) {
3029 /* list or leaf-list */
3030 if (f_max) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003031 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003032 goto error;
3033 }
3034 f_max = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02003035
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003036 /* check possibility of statements combination */
3037 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02003038 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003039 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003040 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
3041 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003042 goto error;
3043 }
3044 } else {
Radek Krejci76512572015-08-04 09:47:08 +02003045 rfn->target_type = LYS_LIST | LYS_LEAFLIST;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003046 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003047
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003048 GETVAL(value, sub, "value");
3049 while (isspace(value[0])) {
3050 value++;
3051 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003052
Radek Krejci0d7b2472016-02-12 11:11:03 +01003053 if (!strcmp(value, "unbounded")) {
3054 rfn->mod.list.max = 0;
3055 } else {
3056 /* convert it to uint32_t */
3057 errno = 0;
3058 endptr = NULL;
3059 val = strtoul(value, &endptr, 10);
3060 if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003061 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01003062 goto error;
3063 }
3064 rfn->mod.list.max = (uint32_t) val;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003065 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02003066 rfn->flags |= LYS_RFN_MAXSET;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003067 } else if (!strcmp(sub->name, "presence")) {
3068 /* container */
3069 if (rfn->mod.presence) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003070 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003071 goto error;
3072 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003073
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003074 /* check possibility of statements combination */
3075 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02003076 rfn->target_type &= LYS_CONTAINER;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003077 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003078 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
3079 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003080 goto error;
3081 }
3082 } else {
Radek Krejci76512572015-08-04 09:47:08 +02003083 rfn->target_type = LYS_CONTAINER;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003084 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003085
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003086 GETVAL(value, sub, "value");
3087 rfn->mod.presence = lydict_insert(module->ctx, value, strlen(value));
3088 } else if (!strcmp(sub->name, "must")) {
Radek Krejci363bd4a2016-07-29 14:30:20 +02003089 /* leafm leaf-list, list, container or anyxml */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003090 /* check possibility of statements combination */
3091 if (rfn->target_type) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003092 rfn->target_type &= (LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003093 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003094 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
3095 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003096 goto error;
3097 }
3098 } else {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003099 rfn->target_type = LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003100 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003101
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003102 c_must++;
Radek Krejci41882de2015-07-02 16:34:58 +02003103 continue;
Radek Krejci3bde87f2015-06-05 16:51:58 +02003104
Radek Krejci363bd4a2016-07-29 14:30:20 +02003105 } else if ((module->version >= 2) && !strcmp(sub->name, "if-feature")) {
3106 /* leaf, leaf-list, list, container or anyxml */
3107 /* check possibility of statements combination */
3108 if (rfn->target_type) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003109 rfn->target_type &= (LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA);
Radek Krejci363bd4a2016-07-29 14:30:20 +02003110 if (!rfn->target_type) {
3111 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
3112 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
3113 goto error;
3114 }
3115 } else {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003116 rfn->target_type = LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA;
Radek Krejci363bd4a2016-07-29 14:30:20 +02003117 }
3118
3119 c_ftrs++;
3120 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003121 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003122 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003123 goto error;
3124 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003125
Michal Vasko345da0a2015-12-02 10:35:55 +01003126 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003127 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003128
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003129 /* process nodes with cardinality of 0..n */
3130 if (c_must) {
3131 rfn->must = calloc(c_must, sizeof *rfn->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003132 if (!rfn->must) {
3133 LOGMEM;
3134 goto error;
3135 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003136 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02003137 if (c_ftrs) {
Radek Krejci947e0342016-08-15 09:42:56 +02003138 rfn->iffeature = calloc(c_ftrs, sizeof *rfn->iffeature);
Radek Krejci363bd4a2016-07-29 14:30:20 +02003139 if (!rfn->iffeature) {
3140 LOGMEM;
Radek Krejci73adb602015-07-02 18:07:40 +02003141 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003142 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003143 }
Radek Krejci200bf712016-08-16 17:11:04 +02003144 if (c_dflt) {
Pavol Vican35aa9ea2016-08-17 10:27:43 +02003145 rfn->dflt = calloc(c_dflt, sizeof *rfn->dflt);
Radek Krejci200bf712016-08-16 17:11:04 +02003146 if (!rfn->dflt) {
3147 LOGMEM;
3148 goto error;
3149 }
3150 }
Radek Krejcie534c132016-11-23 13:32:31 +01003151 if (c_ext) {
3152 rfn->ext = calloc(c_ext, sizeof *rfn->ext);
3153 if (!rfn->ext) {
3154 LOGMEM;
3155 goto error;
3156 }
3157 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003158
Radek Krejcie534c132016-11-23 13:32:31 +01003159 LY_TREE_FOR_SAFE(yin->child, next, sub) {
3160 if (strcmp(sub->ns->value, LY_NSYIN)) {
3161 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01003162 r = lyp_yin_fill_ext(rfn, LYEXT_PAR_REFINE, 0, 0, module, sub, &rfn->ext, rfn->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01003163 rfn->ext_size++;
3164 if (r) {
3165 goto error;
3166 }
3167 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02003168 r = fill_yin_iffeature(uses, 0, sub, &rfn->iffeature[rfn->iffeature_size], unres);
Radek Krejci363bd4a2016-07-29 14:30:20 +02003169 rfn->iffeature_size++;
3170 if (r) {
3171 goto error;
3172 }
Radek Krejci200bf712016-08-16 17:11:04 +02003173 } else if (!strcmp(sub->name, "must")) {
Radek Krejcifccd1442017-01-16 10:26:57 +01003174 r = fill_yin_must(module, sub, &rfn->must[rfn->must_size], unres);
Radek Krejci363bd4a2016-07-29 14:30:20 +02003175 rfn->must_size++;
3176 if (r) {
3177 goto error;
3178 }
Radek Krejci200bf712016-08-16 17:11:04 +02003179 } else { /* default */
3180 GETVAL(value, sub, "value");
3181
3182 /* check for duplicity */
3183 for (r = 0; r < rfn->dflt_size; r++) {
3184 if (ly_strequal(rfn->dflt[r], value, 1)) {
3185 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
3186 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
3187 goto error;
3188 }
3189 }
3190 rfn->dflt[rfn->dflt_size++] = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci363bd4a2016-07-29 14:30:20 +02003191 }
3192 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02003193
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003194 return EXIT_SUCCESS;
Radek Krejci3bde87f2015-06-05 16:51:58 +02003195
3196error:
3197
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003198 return EXIT_FAILURE;
Radek Krejci3bde87f2015-06-05 16:51:58 +02003199}
3200
Michal Vasko0d343d12015-08-24 14:57:36 +02003201/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003202static int
Radek Krejcie534c132016-11-23 13:32:31 +01003203fill_yin_import(struct lys_module *module, struct lyxml_elem *yin, struct lys_import *imp, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02003204{
Radek Krejcie534c132016-11-23 13:32:31 +01003205 struct lyxml_elem *child, *next, exts;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003206 const char *value;
Radek Krejcie534c132016-11-23 13:32:31 +01003207 int r, c_ext = 0;
Radek Krejciefaeba32015-05-27 14:30:57 +02003208
Radek Krejcie534c132016-11-23 13:32:31 +01003209 /* init */
3210 memset(&exts, 0, sizeof exts);
3211
3212 LY_TREE_FOR_SAFE(yin->child, next, child) {
3213 if (!child->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003214 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +02003215 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01003216 } else if (strcmp(child->ns->value, LY_NSYIN)) {
3217 /* extension */
3218 c_ext++;
3219 lyxml_unlink_elem(module->ctx, child, 2);
3220 lyxml_add_child(module->ctx, &exts, child);
3221 } else if (!strcmp(child->name, "prefix")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003222 GETVAL(value, child, "value");
Radek Krejci48464ed2016-03-17 15:44:09 +01003223 if (lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003224 goto error;
3225 }
3226 imp->prefix = lydict_insert(module->ctx, value, strlen(value));
3227 } else if (!strcmp(child->name, "revision-date")) {
3228 if (imp->rev[0]) {
Radek Krejcid52195b2016-06-22 11:18:49 +02003229 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
3230 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003231 }
3232 GETVAL(value, child, "date");
Radek Krejci48464ed2016-03-17 15:44:09 +01003233 if (lyp_check_date(value)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003234 goto error;
3235 }
3236 memcpy(imp->rev, value, LY_REV_SIZE - 1);
Radek Krejci1a31efe2016-07-29 11:04:16 +02003237 } else if ((module->version >= 2) && !strcmp(child->name, "description")) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02003238 if (imp->dsc) {
3239 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
3240 goto error;
3241 }
3242 imp->dsc = read_yin_subnode(module->ctx, child, "text");
3243 if (!imp->dsc) {
3244 goto error;
3245 }
Radek Krejci1a31efe2016-07-29 11:04:16 +02003246 } else if ((module->version >= 2) && !strcmp(child->name, "reference")) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02003247 if (imp->ref) {
3248 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
3249 goto error;
3250 }
3251 imp->ref = read_yin_subnode(module->ctx, child, "text");
3252 if (!imp->ref) {
3253 goto error;
3254 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003255 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003256 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003257 goto error;
3258 }
3259 }
Radek Krejciefaeba32015-05-27 14:30:57 +02003260
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003261 /* check mandatory information */
3262 if (!imp->prefix) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003263 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003264 goto error;
3265 }
Radek Krejcice7fb782015-05-29 16:52:34 +02003266
Radek Krejcie534c132016-11-23 13:32:31 +01003267 /* process extensions */
3268 if (c_ext) {
3269 imp->ext = calloc(c_ext, sizeof *imp->ext);
3270 if (!imp->ext) {
3271 LOGMEM;
3272 goto error;
3273 }
3274 LY_TREE_FOR_SAFE(exts.child, next, child) {
3275 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01003276 r = lyp_yin_fill_ext(imp, LYEXT_PAR_IMPORT, 0, 0, module, child, &imp->ext, imp->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01003277 imp->ext_size++;
3278 if (r) {
3279 goto error;
3280 }
3281 }
3282 }
Michal Vasko1b882eb2015-10-22 11:43:14 +02003283
Radek Krejcie534c132016-11-23 13:32:31 +01003284 GETVAL(value, yin, "module");
Pavol Vicane994fda2016-03-22 10:47:58 +01003285 return lyp_check_import(module, value, imp);
Radek Krejcice7fb782015-05-29 16:52:34 +02003286
3287error:
3288
Radek Krejcie534c132016-11-23 13:32:31 +01003289 while (exts.child) {
3290 lyxml_free(module->ctx, exts.child);
3291 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003292 return EXIT_FAILURE;
Radek Krejciefaeba32015-05-27 14:30:57 +02003293}
3294
Radek Krejci5b2c8a82016-06-17 15:36:03 +02003295/* logs directly
3296 * returns:
3297 * 0 - inc successfully filled
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01003298 * -1 - error
Radek Krejci5b2c8a82016-06-17 15:36:03 +02003299 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003300static int
Michal Vasko5ff78822016-02-12 09:33:31 +01003301fill_yin_include(struct lys_module *module, struct lys_submodule *submodule, struct lyxml_elem *yin,
3302 struct lys_include *inc, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02003303{
Radek Krejcie534c132016-11-23 13:32:31 +01003304 struct lyxml_elem *child, *next, exts;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003305 const char *value;
Radek Krejcie534c132016-11-23 13:32:31 +01003306 int r, c_ext = 0;
Radek Krejciefaeba32015-05-27 14:30:57 +02003307
Radek Krejcie534c132016-11-23 13:32:31 +01003308 /* init */
3309 memset(&exts, 0, sizeof exts);
3310
3311 LY_TREE_FOR_SAFE(yin->child, next, child) {
3312 if (!child->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003313 /* garbage */
3314 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01003315 } else if (strcmp(child->ns->value, LY_NSYIN)) {
3316 /* extension */
3317 c_ext++;
3318 lyxml_unlink_elem(module->ctx, child, 2);
3319 lyxml_add_child(module->ctx, &exts, child);
3320 } else if (!strcmp(child->name, "revision-date")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003321 if (inc->rev[0]) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003322 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "revision-date", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003323 goto error;
3324 }
3325 GETVAL(value, child, "date");
Radek Krejci48464ed2016-03-17 15:44:09 +01003326 if (lyp_check_date(value)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003327 goto error;
3328 }
3329 memcpy(inc->rev, value, LY_REV_SIZE - 1);
Radek Krejci1a31efe2016-07-29 11:04:16 +02003330 } else if ((module->version >= 2) && !strcmp(child->name, "description")) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02003331 if (inc->dsc) {
3332 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
3333 goto error;
3334 }
3335 inc->dsc = read_yin_subnode(module->ctx, child, "text");
3336 if (!inc->dsc) {
3337 goto error;
3338 }
Radek Krejci1a31efe2016-07-29 11:04:16 +02003339 } else if ((module->version >= 2) && !strcmp(child->name, "reference")) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02003340 if (inc->ref) {
3341 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
3342 goto error;
3343 }
3344 inc->ref = read_yin_subnode(module->ctx, child, "text");
3345 if (!inc->ref) {
3346 goto error;
3347 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003348 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003349 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003350 goto error;
3351 }
3352 }
Radek Krejciefaeba32015-05-27 14:30:57 +02003353
Radek Krejcie534c132016-11-23 13:32:31 +01003354 /* process extensions */
3355 if (c_ext) {
3356 inc->ext = calloc(c_ext, sizeof *inc->ext);
3357 if (!inc->ext) {
3358 LOGMEM;
3359 goto error;
3360 }
3361 LY_TREE_FOR_SAFE(exts.child, next, child) {
3362 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01003363 r = lyp_yin_fill_ext(inc, LYEXT_PAR_INCLUDE, 0, 0, module, child, &inc->ext, inc->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01003364 inc->ext_size++;
3365 if (r) {
3366 goto error;
3367 }
3368 }
3369 }
Michal Vasko1b882eb2015-10-22 11:43:14 +02003370
Radek Krejcie534c132016-11-23 13:32:31 +01003371 GETVAL(value, yin, "module");
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01003372 return lyp_check_include(submodule ? (struct lys_module *)submodule : module, value, inc, unres);
Radek Krejcice7fb782015-05-29 16:52:34 +02003373
3374error:
3375
Radek Krejci83e3f5b2016-06-24 14:55:25 +02003376 return -1;
Radek Krejciefaeba32015-05-27 14:30:57 +02003377}
3378
Michal Vasko0d343d12015-08-24 14:57:36 +02003379/* logs directly
3380 *
Radek Krejcida04f4a2015-05-21 12:54:09 +02003381 * Covers:
Radek Krejci25d782a2015-05-22 15:03:23 +02003382 * description, reference, status, optionaly config
Radek Krejcib388c152015-06-04 17:03:03 +02003383 *
Radek Krejcida04f4a2015-05-21 12:54:09 +02003384 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003385static int
Radek Krejci07d0fb92017-01-13 14:11:05 +01003386read_yin_common(struct lys_module *module, struct lys_node *parent, void *stmt, LYEXT_PAR stmt_type,
3387 struct lyxml_elem *xmlnode, int opt, struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003388{
Radek Krejci07d0fb92017-01-13 14:11:05 +01003389 struct lys_node *node = stmt;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003390 const char *value;
3391 struct lyxml_elem *sub, *next;
3392 struct ly_ctx *const ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003393
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003394 if (opt & OPT_MODULE) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003395 node->module = module;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003396 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003397
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003398 if (opt & OPT_IDENT) {
3399 GETVAL(value, xmlnode, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +01003400 if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003401 goto error;
3402 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003403 node->name = lydict_insert(ctx, value, strlen(value));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003404 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003405
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003406 /* process local parameters */
3407 LY_TREE_FOR_SAFE(xmlnode->child, next, sub) {
Radek Krejci6764bb32015-07-03 15:16:04 +02003408 if (!sub->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003409 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003410 lyxml_free(ctx, sub);
Radek Krejci6764bb32015-07-03 15:16:04 +02003411 continue;
3412 }
3413 if (strcmp(sub->ns->value, LY_NSYIN)) {
Radek Krejci6ff885d2017-01-03 14:06:22 +01003414 /* possibly an extension, keep the node for later processing, so skipping lyxml_free() */
Radek Krejci0d70c372015-07-02 16:23:10 +02003415 continue;
3416 }
3417
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003418 if (!strcmp(sub->name, "description")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003419 if (node->dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003420 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003421 goto error;
3422 }
Radek Krejci07d0fb92017-01-13 14:11:05 +01003423
Radek Krejciac00b2a2017-01-17 14:05:00 +01003424 if (read_yin_subnode_ext(module, stmt, stmt_type, sub, LYEXT_SUBSTMT_DESCRIPTION, 0, unres)) {
Radek Krejci07d0fb92017-01-13 14:11:05 +01003425 goto error;
3426 }
3427
Radek Krejci1d82ef62015-08-07 14:44:40 +02003428 node->dsc = read_yin_subnode(ctx, sub, "text");
3429 if (!node->dsc) {
Radek Krejci73adb602015-07-02 18:07:40 +02003430 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003431 }
3432 } else if (!strcmp(sub->name, "reference")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003433 if (node->ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003434 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003435 goto error;
3436 }
Radek Krejci07d0fb92017-01-13 14:11:05 +01003437
Radek Krejciac00b2a2017-01-17 14:05:00 +01003438 if (read_yin_subnode_ext(module, stmt, stmt_type, sub, LYEXT_SUBSTMT_REFERENCE, 0, unres)) {
Radek Krejci07d0fb92017-01-13 14:11:05 +01003439 goto error;
3440 }
3441
Radek Krejci1d82ef62015-08-07 14:44:40 +02003442 node->ref = read_yin_subnode(ctx, sub, "text");
3443 if (!node->ref) {
Radek Krejci73adb602015-07-02 18:07:40 +02003444 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003445 }
3446 } else if (!strcmp(sub->name, "status")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003447 if (node->flags & LYS_STATUS_MASK) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003448 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003449 goto error;
3450 }
3451 GETVAL(value, sub, "value");
3452 if (!strcmp(value, "current")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003453 node->flags |= LYS_STATUS_CURR;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003454 } else if (!strcmp(value, "deprecated")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003455 node->flags |= LYS_STATUS_DEPRC;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003456 } else if (!strcmp(value, "obsolete")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003457 node->flags |= LYS_STATUS_OBSLT;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003458 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003459 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci73adb602015-07-02 18:07:40 +02003460 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003461 }
Radek Krejci07d0fb92017-01-13 14:11:05 +01003462
Radek Krejciac00b2a2017-01-17 14:05:00 +01003463 if (read_yin_subnode_ext(module, stmt, stmt_type, sub, LYEXT_SUBSTMT_STATUS, 0, unres)) {
Radek Krejci07d0fb92017-01-13 14:11:05 +01003464 goto error;
3465 }
Michal Vaskoe022a562016-09-27 14:24:15 +02003466 } else if ((opt & (OPT_CFG_PARSE | OPT_CFG_IGNORE)) && !strcmp(sub->name, "config")) {
3467 if (opt & OPT_CFG_PARSE) {
3468 if (node->flags & LYS_CONFIG_MASK) {
3469 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
3470 goto error;
3471 }
3472 GETVAL(value, sub, "value");
3473 if (!strcmp(value, "false")) {
3474 node->flags |= LYS_CONFIG_R;
3475 } else if (!strcmp(value, "true")) {
3476 node->flags |= LYS_CONFIG_W;
3477 } else {
3478 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
3479 goto error;
3480 }
3481 node->flags |= LYS_CONFIG_SET;
Radek Krejci07d0fb92017-01-13 14:11:05 +01003482
Radek Krejciac00b2a2017-01-17 14:05:00 +01003483 if (read_yin_subnode_ext(module, stmt, stmt_type, sub, LYEXT_SUBSTMT_CONFIG, 0, unres)) {
Radek Krejci07d0fb92017-01-13 14:11:05 +01003484 goto error;
3485 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003486 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003487 } else {
Michal Vasko345da0a2015-12-02 10:35:55 +01003488 /* skip the lyxml_free */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003489 continue;
3490 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003491 lyxml_free(ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003492 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003493
Michal Vaskoe022a562016-09-27 14:24:15 +02003494 if ((opt & OPT_CFG_INHERIT) && !(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003495 /* get config flag from parent */
Radek Krejcif71f48f2016-10-25 16:37:24 +02003496 if (parent) {
3497 node->flags |= parent->flags & LYS_CONFIG_MASK;
3498 } else if (!parent) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003499 /* default config is true */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003500 node->flags |= LYS_CONFIG_W;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003501 }
3502 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003503
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003504 return EXIT_SUCCESS;
Radek Krejcieac35532015-05-31 19:09:15 +02003505
3506error:
3507
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003508 return EXIT_FAILURE;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003509}
3510
Michal Vasko0d343d12015-08-24 14:57:36 +02003511/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003512static struct lys_when *
Radek Krejci5323b492017-01-16 15:40:11 +01003513read_yin_when(struct lys_module *module, struct lyxml_elem *yin, struct unres_schema *unres)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003514{
Radek Krejci76512572015-08-04 09:47:08 +02003515 struct lys_when *retval = NULL;
Radek Krejci5323b492017-01-16 15:40:11 +01003516 struct lyxml_elem *child, *next;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003517 const char *value;
3518
3519 retval = calloc(1, sizeof *retval);
Michal Vasko253035f2015-12-17 16:58:13 +01003520 if (!retval) {
3521 LOGMEM;
3522 return NULL;
3523 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003524
3525 GETVAL(value, yin, "condition");
Radek Krejci48464ed2016-03-17 15:44:09 +01003526 retval->cond = transform_schema2json(module, value);
Michal Vaskof9893382015-10-09 14:03:04 +02003527 if (!retval->cond) {
3528 goto error;
3529 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003530
Radek Krejci5323b492017-01-16 15:40:11 +01003531 LY_TREE_FOR_SAFE(yin->child, next, child) {
3532 if (!child->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003533 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +02003534 continue;
Radek Krejci5323b492017-01-16 15:40:11 +01003535 } else if (strcmp(child->ns->value, LY_NSYIN)) {
3536 /* extensions */
Radek Krejciac00b2a2017-01-17 14:05:00 +01003537 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_WHEN, child, LYEXT_SUBSTMT_SELF, 0, unres)) {
Radek Krejci5323b492017-01-16 15:40:11 +01003538 goto error;
3539 }
3540 } else if (!strcmp(child->name, "description")) {
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003541 if (retval->dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003542 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003543 goto error;
3544 }
Radek Krejci5323b492017-01-16 15:40:11 +01003545
Radek Krejciac00b2a2017-01-17 14:05:00 +01003546 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_WHEN, child, LYEXT_SUBSTMT_DESCRIPTION, 0, unres)) {
Radek Krejci5323b492017-01-16 15:40:11 +01003547 goto error;
3548 }
3549
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003550 retval->dsc = read_yin_subnode(module->ctx, child, "text");
3551 if (!retval->dsc) {
3552 goto error;
3553 }
3554 } else if (!strcmp(child->name, "reference")) {
3555 if (retval->ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003556 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003557 goto error;
3558 }
Radek Krejci5323b492017-01-16 15:40:11 +01003559
Radek Krejciac00b2a2017-01-17 14:05:00 +01003560 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_WHEN, child, LYEXT_SUBSTMT_REFERENCE, 0, unres)) {
Radek Krejci5323b492017-01-16 15:40:11 +01003561 goto error;
3562 }
3563
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003564 retval->ref = read_yin_subnode(module->ctx, child, "text");
3565 if (!retval->ref) {
3566 goto error;
3567 }
3568 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003569 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003570 goto error;
3571 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003572 }
3573
3574 return retval;
3575
3576error:
3577
Michal Vasko0308dd62015-10-07 09:14:40 +02003578 lys_when_free(module->ctx, retval);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003579 return NULL;
3580}
3581
Michal Vasko0d343d12015-08-24 14:57:36 +02003582/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003583static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02003584read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02003585 struct unres_schema *unres)
Radek Krejcib4cf2022015-06-03 14:40:05 +02003586{
Michal Vasko29fc0182015-08-24 15:02:39 +02003587 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02003588 struct lys_node_case *cs;
3589 struct lys_node *retval, *node = NULL;
Radek Krejcie534c132016-11-23 13:32:31 +01003590 int c_ftrs = 0, c_ext = 0, ret;
Radek Krejcib4cf2022015-06-03 14:40:05 +02003591
Radek Krejcie867c852015-08-27 09:52:34 +02003592 /* init */
3593 memset(&root, 0, sizeof root);
3594
Radek Krejci1d82ef62015-08-07 14:44:40 +02003595 cs = calloc(1, sizeof *cs);
Michal Vasko253035f2015-12-17 16:58:13 +01003596 if (!cs) {
3597 LOGMEM;
3598 return NULL;
3599 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003600 cs->nodetype = LYS_CASE;
3601 cs->prev = (struct lys_node *)cs;
3602 retval = (struct lys_node *)cs;
Radek Krejcib4cf2022015-06-03 14:40:05 +02003603
Radek Krejci07d0fb92017-01-13 14:11:05 +01003604 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
3605 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_INHERIT : 0), unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003606 goto error;
3607 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02003608
Radek Krejcia9544502015-08-14 08:24:29 +02003609 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
3610
Michal Vasko3a0043f2015-08-12 12:11:30 +02003611 /* insert the node into the schema tree */
Radek Krejcic4283442016-04-22 09:19:27 +02003612 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003613 goto error;
3614 }
3615
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003616 /* process choice's specific children */
3617 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01003618 if (strcmp(sub->ns->value, LY_NSYIN)) {
3619 /* extension */
3620 c_ext++;
3621 } else if (!strcmp(sub->name, "container") ||
Michal Vasko29fc0182015-08-24 15:02:39 +02003622 !strcmp(sub->name, "leaf-list") ||
3623 !strcmp(sub->name, "leaf") ||
3624 !strcmp(sub->name, "list") ||
3625 !strcmp(sub->name, "uses") ||
3626 !strcmp(sub->name, "choice") ||
3627 !strcmp(sub->name, "anyxml")) {
3628
Michal Vaskof3930de2015-10-22 12:03:59 +02003629 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vasko29fc0182015-08-24 15:02:39 +02003630 lyxml_add_child(module->ctx, &root, sub);
Radek Krejci3cf9e222015-06-18 11:37:50 +02003631 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko29fc0182015-08-24 15:02:39 +02003632 c_ftrs++;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003633 } else if (!strcmp(sub->name, "when")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003634 if (cs->when) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003635 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003636 goto error;
3637 }
3638
Radek Krejci5323b492017-01-16 15:40:11 +01003639 cs->when = read_yin_when(module, sub, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02003640 if (!cs->when) {
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003641 goto error;
3642 }
Michal Vasko29fc0182015-08-24 15:02:39 +02003643
Michal Vasko345da0a2015-12-02 10:35:55 +01003644 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003645 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003646 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003647 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003648 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003649 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02003650
Radek Krejci3cf9e222015-06-18 11:37:50 +02003651 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003652 cs->iffeature = calloc(c_ftrs, sizeof *cs->iffeature);
3653 if (!cs->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01003654 LOGMEM;
3655 goto error;
3656 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003657 }
Radek Krejcie534c132016-11-23 13:32:31 +01003658 if (c_ext) {
3659 cs->ext = calloc(c_ext, sizeof *cs->ext);
3660 if (!cs->ext) {
3661 LOGMEM;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003662 goto error;
3663 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003664 }
Radek Krejcie534c132016-11-23 13:32:31 +01003665 LY_TREE_FOR_SAFE(yin->child, next, sub) {
3666 if (strcmp(sub->ns->value, LY_NSYIN)) {
3667 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01003668 ret = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01003669 retval->ext_size++;
3670 if (ret) {
3671 goto error;
3672 }
3673 } else {
3674 /* if-feature */
3675 ret = fill_yin_iffeature(retval, 0, sub, &cs->iffeature[cs->iffeature_size], unres);
3676 cs->iffeature_size++;
3677 if (ret) {
3678 goto error;
3679 }
3680 }
3681 }
Radek Krejcib388c152015-06-04 17:03:03 +02003682
Michal Vasko29fc0182015-08-24 15:02:39 +02003683 /* last part - process data nodes */
3684 LY_TREE_FOR_SAFE(root.child, next, sub) {
3685 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003686 node = read_yin_container(module, retval, sub, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003687 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003688 node = read_yin_leaflist(module, retval, sub, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003689 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003690 node = read_yin_leaf(module, retval, sub, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003691 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003692 node = read_yin_list(module, retval, sub, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003693 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003694 node = read_yin_choice(module, retval, sub, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003695 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02003696 node = read_yin_uses(module, retval, sub, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003697 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003698 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02003699 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003700 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003701 }
3702 if (!node) {
3703 goto error;
3704 }
3705
Michal Vasko345da0a2015-12-02 10:35:55 +01003706 lyxml_free(module->ctx, sub);
Michal Vasko29fc0182015-08-24 15:02:39 +02003707 }
3708
Michal Vasko508a50d2016-09-07 14:50:33 +02003709 /* check XPath dependencies */
3710 if (cs->when && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
3711 goto error;
3712 }
3713
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003714 return retval;
Radek Krejcib4cf2022015-06-03 14:40:05 +02003715
3716error:
3717
Michal Vasko29fc0182015-08-24 15:02:39 +02003718 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003719 lyxml_free(module->ctx, root.child);
Michal Vasko29fc0182015-08-24 15:02:39 +02003720 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003721 lys_node_free(retval, NULL, 0);
Radek Krejcib4cf2022015-06-03 14:40:05 +02003722
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003723 return NULL;
Radek Krejcib4cf2022015-06-03 14:40:05 +02003724}
3725
Michal Vasko0d343d12015-08-24 14:57:36 +02003726/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003727static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02003728read_yin_choice(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
3729 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003730{
Radek Krejci629cdef2016-06-06 15:06:36 +02003731 struct lyxml_elem *sub, *next, *dflt = NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003732 struct ly_ctx *const ctx = module->ctx;
Radek Krejci1d82ef62015-08-07 14:44:40 +02003733 struct lys_node *retval, *node = NULL;
Radek Krejcib8048692015-08-05 13:36:34 +02003734 struct lys_node_choice *choice;
Radek Krejci629cdef2016-06-06 15:06:36 +02003735 const char *value;
Radek Krejcie534c132016-11-23 13:32:31 +01003736 int f_mand = 0, c_ftrs = 0, c_ext = 0, ret;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003737
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003738 choice = calloc(1, sizeof *choice);
Michal Vasko253035f2015-12-17 16:58:13 +01003739 if (!choice) {
3740 LOGMEM;
3741 return NULL;
3742 }
Radek Krejci76512572015-08-04 09:47:08 +02003743 choice->nodetype = LYS_CHOICE;
3744 choice->prev = (struct lys_node *)choice;
3745 retval = (struct lys_node *)choice;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003746
Radek Krejci07d0fb92017-01-13 14:11:05 +01003747 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
3748 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003749 goto error;
3750 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003751
Radek Krejcia9544502015-08-14 08:24:29 +02003752 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
3753
Michal Vasko3a0043f2015-08-12 12:11:30 +02003754 /* insert the node into the schema tree */
Radek Krejcic4283442016-04-22 09:19:27 +02003755 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003756 goto error;
3757 }
3758
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003759 /* process choice's specific children */
3760 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01003761 if (strcmp(sub->ns->value, LY_NSYIN)) {
3762 /* extension */
3763 c_ext++;
3764 /* keep it for later processing, skip lyxml_free() */
Radek Krejci0d70c372015-07-02 16:23:10 +02003765 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01003766 } else if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003767 if (!(node = read_yin_container(module, retval, sub, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003768 goto error;
3769 }
3770 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003771 if (!(node = read_yin_leaflist(module, retval, sub, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003772 goto error;
3773 }
3774 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003775 if (!(node = read_yin_leaf(module, retval, sub, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003776 goto error;
3777 }
3778 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003779 if (!(node = read_yin_list(module, retval, sub, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003780 goto error;
3781 }
3782 } else if (!strcmp(sub->name, "case")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003783 if (!(node = read_yin_case(module, retval, sub, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003784 goto error;
3785 }
3786 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003787 if (!(node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres))) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003788 goto error;
3789 }
3790 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003791 if (!(node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003792 goto error;
3793 }
3794 } else if (!strcmp(sub->name, "default")) {
Radek Krejci629cdef2016-06-06 15:06:36 +02003795 if (dflt) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003796 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003797 goto error;
3798 }
Radek Krejci629cdef2016-06-06 15:06:36 +02003799 dflt = sub;
3800 lyxml_unlink_elem(ctx, dflt, 0);
3801
Radek Krejcif9a312c2016-06-06 15:14:30 +02003802 continue;
3803 /* skip lyxml_free() at the end of the loop, the sub node is processed later as dflt */
3804
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003805 } else if (!strcmp(sub->name, "mandatory")) {
3806 if (f_mand) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003807 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003808 goto error;
3809 }
3810 /* just checking the flags in leaf is not sufficient, we would allow
3811 * multiple mandatory statements with the "false" value
3812 */
3813 f_mand = 1;
Radek Krejcib4cf2022015-06-03 14:40:05 +02003814
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003815 GETVAL(value, sub, "value");
3816 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003817 choice->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003818 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003819 choice->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003820 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003821 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003822 goto error;
3823 } /* else false is the default value, so we can ignore it */
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003824 } else if (!strcmp(sub->name, "when")) {
3825 if (choice->when) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003826 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003827 goto error;
3828 }
3829
Radek Krejci5323b492017-01-16 15:40:11 +01003830 choice->when = read_yin_when(module, sub, unres);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003831 if (!choice->when) {
3832 goto error;
3833 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003834 } else if (!strcmp(sub->name, "if-feature")) {
3835 c_ftrs++;
3836
Michal Vasko345da0a2015-12-02 10:35:55 +01003837 /* skip lyxml_free() at the end of the loop, the sub node is processed later */
Radek Krejci3cf9e222015-06-18 11:37:50 +02003838 continue;
Radek Krejci2f792db2016-09-12 10:52:33 +02003839 } else if (module->version >= 2 && !strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003840 if (!(node = read_yin_choice(module, retval, sub, valid_config, unres))) {
Radek Krejci2f792db2016-09-12 10:52:33 +02003841 goto error;
3842 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003843 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003844 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003845 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003846 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02003847
Radek Krejci1d82ef62015-08-07 14:44:40 +02003848 node = NULL;
Michal Vasko345da0a2015-12-02 10:35:55 +01003849 lyxml_free(ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003850 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02003851
Radek Krejci3cf9e222015-06-18 11:37:50 +02003852 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003853 choice->iffeature = calloc(c_ftrs, sizeof *choice->iffeature);
3854 if (!choice->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01003855 LOGMEM;
3856 goto error;
3857 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003858 }
Radek Krejcie534c132016-11-23 13:32:31 +01003859 if (c_ext) {
3860 choice->ext = calloc(c_ext, sizeof *choice->ext);
3861 if (!choice->ext) {
3862 LOGMEM;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003863 goto error;
3864 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003865 }
3866
Radek Krejcie534c132016-11-23 13:32:31 +01003867 LY_TREE_FOR_SAFE(yin->child, next, sub) {
3868 if (strcmp(sub->ns->value, LY_NSYIN)) {
3869 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01003870 ret = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01003871 retval->ext_size++;
3872 if (ret) {
3873 goto error;
3874 }
3875 } else {
3876 ret = fill_yin_iffeature(retval, 0, sub, &choice->iffeature[choice->iffeature_size], unres);
3877 choice->iffeature_size++;
3878 if (ret) {
3879 goto error;
3880 }
3881 }
3882 }
3883
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003884 /* check - default is prohibited in combination with mandatory */
Radek Krejci629cdef2016-06-06 15:06:36 +02003885 if (dflt && (choice->flags & LYS_MAND_TRUE)) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003886 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, retval, "default", "choice");
Michal Vasko51e5c582017-01-19 14:16:39 +01003887 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "The \"default\" statement is forbidden on choices with \"mandatory\".");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003888 goto error;
3889 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02003890
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003891 /* link default with the case */
Radek Krejci629cdef2016-06-06 15:06:36 +02003892 if (dflt) {
3893 GETVAL(value, dflt, "value");
3894 if (unres_schema_add_str(module, unres, choice, UNRES_CHOICE_DFLT, value) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003895 goto error;
3896 }
Radek Krejci629cdef2016-06-06 15:06:36 +02003897 lyxml_free(ctx, dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003898 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003899
Michal Vasko508a50d2016-09-07 14:50:33 +02003900 /* check XPath dependencies */
3901 if (choice->when && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
3902 goto error;
3903 }
3904
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003905 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003906
3907error:
3908
Radek Krejci629cdef2016-06-06 15:06:36 +02003909 lyxml_free(ctx, dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003910 lys_node_free(retval, NULL, 0);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003911
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003912 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003913}
3914
Michal Vasko0d343d12015-08-24 14:57:36 +02003915/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003916static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02003917read_yin_anydata(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, LYS_NODE type,
3918 int valid_config, struct unres_schema *unres)
Radek Krejci863c2852015-06-03 15:47:11 +02003919{
Radek Krejci76512572015-08-04 09:47:08 +02003920 struct lys_node *retval;
Radek Krejcibf2abff2016-08-23 15:51:52 +02003921 struct lys_node_anydata *anyxml;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003922 struct lyxml_elem *sub, *next;
3923 const char *value;
3924 int r;
3925 int f_mand = 0;
Radek Krejcie534c132016-11-23 13:32:31 +01003926 int c_must = 0, c_ftrs = 0, c_ext = 0;
Radek Krejci3a5d9cf2017-01-18 14:06:25 +01003927 void *reallocated;
Radek Krejci863c2852015-06-03 15:47:11 +02003928
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003929 anyxml = calloc(1, sizeof *anyxml);
Michal Vasko253035f2015-12-17 16:58:13 +01003930 if (!anyxml) {
3931 LOGMEM;
3932 return NULL;
3933 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02003934 anyxml->nodetype = type;
Radek Krejci76512572015-08-04 09:47:08 +02003935 anyxml->prev = (struct lys_node *)anyxml;
3936 retval = (struct lys_node *)anyxml;
Radek Krejci863c2852015-06-03 15:47:11 +02003937
Radek Krejci07d0fb92017-01-13 14:11:05 +01003938 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
3939 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003940 goto error;
3941 }
Radek Krejci863c2852015-06-03 15:47:11 +02003942
Radek Krejcia9544502015-08-14 08:24:29 +02003943 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02003944
Radek Krejcic189a952016-07-11 15:27:07 +02003945 /* insert the node into the schema tree */
Radek Krejcic4283442016-04-22 09:19:27 +02003946 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003947 goto error;
3948 }
3949
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003950 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01003951 if (strcmp(sub->ns->value, LY_NSYIN)) {
3952 /* extension */
3953 c_ext++;
3954 } else if (!strcmp(sub->name, "mandatory")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003955 if (f_mand) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003956 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003957 goto error;
3958 }
3959 /* just checking the flags in leaf is not sufficient, we would allow
3960 * multiple mandatory statements with the "false" value
3961 */
3962 f_mand = 1;
Radek Krejci863c2852015-06-03 15:47:11 +02003963
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003964 GETVAL(value, sub, "value");
3965 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003966 anyxml->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003967 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003968 anyxml->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003969 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003970 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003971 goto error;
3972 }
3973 /* else false is the default value, so we can ignore it */
Radek Krejci3a5d9cf2017-01-18 14:06:25 +01003974
3975 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_MANDATORY, 0, unres)) {
3976 goto error;
3977 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003978 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003979 } else if (!strcmp(sub->name, "when")) {
3980 if (anyxml->when) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003981 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003982 goto error;
3983 }
3984
Radek Krejci5323b492017-01-16 15:40:11 +01003985 anyxml->when = read_yin_when(module, sub, unres);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003986 if (!anyxml->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003987 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003988 goto error;
3989 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003990 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003991 } else if (!strcmp(sub->name, "must")) {
3992 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003993 } else if (!strcmp(sub->name, "if-feature")) {
3994 c_ftrs++;
Radek Krejci863c2852015-06-03 15:47:11 +02003995
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003996 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01003997 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003998 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003999 }
4000 }
Radek Krejci863c2852015-06-03 15:47:11 +02004001
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004002 /* middle part - process nodes with cardinality of 0..n */
4003 if (c_must) {
4004 anyxml->must = calloc(c_must, sizeof *anyxml->must);
Michal Vasko253035f2015-12-17 16:58:13 +01004005 if (!anyxml->must) {
4006 LOGMEM;
4007 goto error;
4008 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004009 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004010 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004011 anyxml->iffeature = calloc(c_ftrs, sizeof *anyxml->iffeature);
4012 if (!anyxml->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01004013 LOGMEM;
4014 goto error;
4015 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004016 }
Radek Krejcie534c132016-11-23 13:32:31 +01004017 if (c_ext) {
Radek Krejci3a5d9cf2017-01-18 14:06:25 +01004018 /* some extensions may be already present from the substatements */
4019 reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
4020 if (!reallocated) {
Radek Krejcie534c132016-11-23 13:32:31 +01004021 LOGMEM;
4022 goto error;
4023 }
Radek Krejci3a5d9cf2017-01-18 14:06:25 +01004024 retval->ext = reallocated;
4025
4026 /* init memory */
4027 memset(&retval->ext[retval->ext_size], 0, c_ext * sizeof *retval->ext);
Radek Krejcie534c132016-11-23 13:32:31 +01004028 }
Radek Krejci863c2852015-06-03 15:47:11 +02004029
Radek Krejcie534c132016-11-23 13:32:31 +01004030 LY_TREE_FOR_SAFE(yin->child, next, sub) {
4031 if (strcmp(sub->ns->value, LY_NSYIN)) {
4032 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01004033 r = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01004034 retval->ext_size++;
4035 if (r) {
4036 goto error;
4037 }
4038 } else if (!strcmp(sub->name, "must")) {
Radek Krejcifccd1442017-01-16 10:26:57 +01004039 r = fill_yin_must(module, sub, &anyxml->must[anyxml->must_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004040 anyxml->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004041 if (r) {
4042 goto error;
4043 }
Radek Krejci0b24d752015-07-02 15:02:27 +02004044 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02004045 r = fill_yin_iffeature(retval, 0, sub, &anyxml->iffeature[anyxml->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004046 anyxml->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01004047 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004048 goto error;
4049 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004050 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004051 }
Radek Krejci863c2852015-06-03 15:47:11 +02004052
Michal Vasko508a50d2016-09-07 14:50:33 +02004053 /* check XPath dependencies */
4054 if ((anyxml->when || anyxml->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
4055 goto error;
4056 }
4057
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004058 return retval;
Radek Krejci863c2852015-06-03 15:47:11 +02004059
4060error:
4061
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004062 lys_node_free(retval, NULL, 0);
Radek Krejci863c2852015-06-03 15:47:11 +02004063
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004064 return NULL;
Radek Krejci863c2852015-06-03 15:47:11 +02004065}
4066
Michal Vasko0d343d12015-08-24 14:57:36 +02004067/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004068static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02004069read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02004070 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02004071{
Radek Krejci76512572015-08-04 09:47:08 +02004072 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004073 struct lys_node_leaf *leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004074 struct lyxml_elem *sub, *next;
4075 const char *value;
Radek Krejci48464ed2016-03-17 15:44:09 +01004076 int r, has_type = 0;
Radek Krejcie534c132016-11-23 13:32:31 +01004077 int c_must = 0, c_ftrs = 0, f_mand = 0, c_ext = 0;
Radek Krejcifccd1442017-01-16 10:26:57 +01004078 void *reallocated;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004079
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004080 leaf = calloc(1, sizeof *leaf);
Michal Vasko253035f2015-12-17 16:58:13 +01004081 if (!leaf) {
4082 LOGMEM;
4083 return NULL;
4084 }
Radek Krejci76512572015-08-04 09:47:08 +02004085 leaf->nodetype = LYS_LEAF;
4086 leaf->prev = (struct lys_node *)leaf;
4087 retval = (struct lys_node *)leaf;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004088
Radek Krejci07d0fb92017-01-13 14:11:05 +01004089 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
4090 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004091 goto error;
4092 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004093
Radek Krejcia9544502015-08-14 08:24:29 +02004094 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02004095
Radek Krejcic189a952016-07-11 15:27:07 +02004096 /* insert the node into the schema tree */
Radek Krejcic4283442016-04-22 09:19:27 +02004097 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004098 goto error;
4099 }
4100
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004101 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01004102 if (strcmp(sub->ns->value, LY_NSYIN)) {
4103 /* extension */
4104 c_ext++;
Radek Krejci0d70c372015-07-02 16:23:10 +02004105 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01004106 } else if (!strcmp(sub->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +01004107 if (has_type) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004108 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004109 goto error;
4110 }
Michal Vasko88c29542015-11-27 14:57:53 +01004111 /* HACK for unres */
4112 leaf->type.der = (struct lys_tpdf *)sub;
Radek Krejcicf509982015-12-15 09:22:44 +01004113 leaf->type.parent = (struct lys_tpdf *)leaf;
Radek Krejcicbb473e2016-09-16 14:48:32 +02004114 /* postpone type resolution when if-feature parsing is done since we need
4115 * if-feature for check_leafref_features() */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004116 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004117 } else if (!strcmp(sub->name, "default")) {
4118 if (leaf->dflt) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004119 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004120 goto error;
4121 }
4122 GETVAL(value, sub, "value");
4123 leaf->dflt = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci1abcdae2017-01-18 14:14:18 +01004124
4125 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_DEFAULT, 0, unres)) {
4126 goto error;
4127 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004128 } else if (!strcmp(sub->name, "units")) {
4129 if (leaf->units) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004130 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004131 goto error;
4132 }
4133 GETVAL(value, sub, "name");
4134 leaf->units = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci5323b492017-01-16 15:40:11 +01004135
Radek Krejciac00b2a2017-01-17 14:05:00 +01004136 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_UNITS, 0, unres)) {
Radek Krejci5323b492017-01-16 15:40:11 +01004137 goto error;
4138 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004139 } else if (!strcmp(sub->name, "mandatory")) {
4140 if (f_mand) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004141 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004142 goto error;
4143 }
4144 /* just checking the flags in leaf is not sufficient, we would allow
4145 * multiple mandatory statements with the "false" value
4146 */
4147 f_mand = 1;
Radek Krejci4c31f122015-06-02 14:51:22 +02004148
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004149 GETVAL(value, sub, "value");
4150 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02004151 leaf->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02004152 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02004153 leaf->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02004154 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004155 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004156 goto error;
4157 } /* else false is the default value, so we can ignore it */
Radek Krejcifccd1442017-01-16 10:26:57 +01004158
Radek Krejciac00b2a2017-01-17 14:05:00 +01004159 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_MANDATORY, 0, unres)) {
Radek Krejcifccd1442017-01-16 10:26:57 +01004160 goto error;
4161 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004162 } else if (!strcmp(sub->name, "when")) {
4163 if (leaf->when) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004164 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004165 goto error;
4166 }
4167
Radek Krejci5323b492017-01-16 15:40:11 +01004168 leaf->when = read_yin_when(module, sub, unres);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004169 if (!leaf->when) {
4170 goto error;
4171 }
4172
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004173 } else if (!strcmp(sub->name, "must")) {
Radek Krejci41882de2015-07-02 16:34:58 +02004174 c_must++;
4175 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004176 } else if (!strcmp(sub->name, "if-feature")) {
4177 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004178 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02004179
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004180 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004181 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004182 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004183 }
Radek Krejci4c31f122015-06-02 14:51:22 +02004184
Michal Vasko88c29542015-11-27 14:57:53 +01004185 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004186 }
Radek Krejci4c31f122015-06-02 14:51:22 +02004187
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004188 /* check mandatory parameters */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004189 if (!has_type) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004190 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004191 goto error;
4192 }
Michal Vasko478c4652016-07-21 12:55:01 +02004193 if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004194 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, retval, "mandatory", "leaf");
Michal Vasko51e5c582017-01-19 14:16:39 +01004195 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Michal Vasko478c4652016-07-21 12:55:01 +02004196 "The \"mandatory\" statement is forbidden on leaf with the \"default\" statement.");
4197 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004198 }
Radek Krejci4c31f122015-06-02 14:51:22 +02004199
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004200 /* middle part - process nodes with cardinality of 0..n */
4201 if (c_must) {
4202 leaf->must = calloc(c_must, sizeof *leaf->must);
Michal Vasko253035f2015-12-17 16:58:13 +01004203 if (!leaf->must) {
4204 LOGMEM;
4205 goto error;
4206 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004207 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004208 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004209 leaf->iffeature = calloc(c_ftrs, sizeof *leaf->iffeature);
4210 if (!leaf->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01004211 LOGMEM;
4212 goto error;
4213 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004214 }
Radek Krejcie534c132016-11-23 13:32:31 +01004215 if (c_ext) {
Radek Krejcifccd1442017-01-16 10:26:57 +01004216 /* some extensions may be already present from the substatements */
4217 reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
4218 if (!reallocated) {
Radek Krejcie534c132016-11-23 13:32:31 +01004219 LOGMEM;
4220 goto error;
4221 }
Radek Krejcifccd1442017-01-16 10:26:57 +01004222 retval->ext = reallocated;
4223
4224 /* init memory */
4225 memset(&retval->ext[retval->ext_size], 0, c_ext * sizeof *retval->ext);
Radek Krejcie534c132016-11-23 13:32:31 +01004226 }
Radek Krejci4c31f122015-06-02 14:51:22 +02004227
Radek Krejcie534c132016-11-23 13:32:31 +01004228 LY_TREE_FOR_SAFE(yin->child, next, sub) {
4229 if (strcmp(sub->ns->value, LY_NSYIN)) {
4230 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01004231 r = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01004232 retval->ext_size++;
4233 if (r) {
4234 goto error;
4235 }
4236 } else if (!strcmp(sub->name, "must")) {
Radek Krejcifccd1442017-01-16 10:26:57 +01004237 r = fill_yin_must(module, sub, &leaf->must[leaf->must_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004238 leaf->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004239 if (r) {
4240 goto error;
4241 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004242 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02004243 r = fill_yin_iffeature(retval, 0, sub, &leaf->iffeature[leaf->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004244 leaf->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01004245 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004246 goto error;
4247 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004248 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004249 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004250
Radek Krejcicbb473e2016-09-16 14:48:32 +02004251 /* finalize type parsing */
4252 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DER, retval) == -1) {
4253 leaf->type.der = NULL;
4254 goto error;
4255 }
4256
4257 /* check default value (if not defined, there still could be some restrictions
4258 * that need to be checked against a default value from a derived type) */
Radek Krejci51673202016-11-01 17:00:32 +01004259 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)(&leaf->dflt)) == -1) {
Radek Krejcicbb473e2016-09-16 14:48:32 +02004260 goto error;
4261 }
4262
Michal Vasko508a50d2016-09-07 14:50:33 +02004263 /* check XPath dependencies */
4264 if ((leaf->when || leaf->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
4265 goto error;
4266 }
4267
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004268 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004269
4270error:
4271
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004272 lys_node_free(retval, NULL, 0);
Radek Krejcida04f4a2015-05-21 12:54:09 +02004273
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004274 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004275}
4276
Michal Vasko0d343d12015-08-24 14:57:36 +02004277/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004278static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02004279read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02004280 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02004281{
Radek Krejci76512572015-08-04 09:47:08 +02004282 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004283 struct lys_node_leaflist *llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004284 struct lyxml_elem *sub, *next;
4285 const char *value;
4286 char *endptr;
4287 unsigned long val;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004288 int r, has_type = 0;
Radek Krejcie534c132016-11-23 13:32:31 +01004289 int c_must = 0, c_ftrs = 0, c_dflt = 0, c_ext = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004290 int f_ordr = 0, f_min = 0, f_max = 0;
Radek Krejci5323b492017-01-16 15:40:11 +01004291 void *reallocated;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004292
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004293 llist = calloc(1, sizeof *llist);
Michal Vasko253035f2015-12-17 16:58:13 +01004294 if (!llist) {
4295 LOGMEM;
4296 return NULL;
4297 }
Radek Krejci76512572015-08-04 09:47:08 +02004298 llist->nodetype = LYS_LEAFLIST;
4299 llist->prev = (struct lys_node *)llist;
4300 retval = (struct lys_node *)llist;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004301
Radek Krejci07d0fb92017-01-13 14:11:05 +01004302 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
4303 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004304 goto error;
4305 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004306
Radek Krejcia9544502015-08-14 08:24:29 +02004307 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02004308
Radek Krejcic189a952016-07-11 15:27:07 +02004309 /* insert the node into the schema tree */
Radek Krejcic4283442016-04-22 09:19:27 +02004310 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004311 goto error;
4312 }
4313
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004314 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01004315 if (strcmp(sub->ns->value, LY_NSYIN)) {
4316 /* extension */
4317 c_ext++;
Radek Krejci0d70c372015-07-02 16:23:10 +02004318 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01004319 } else if (!strcmp(sub->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +01004320 if (has_type) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004321 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004322 goto error;
4323 }
Michal Vasko88c29542015-11-27 14:57:53 +01004324 /* HACK for unres */
4325 llist->type.der = (struct lys_tpdf *)sub;
Radek Krejcicf509982015-12-15 09:22:44 +01004326 llist->type.parent = (struct lys_tpdf *)llist;
Radek Krejcicbb473e2016-09-16 14:48:32 +02004327 /* postpone type resolution when if-feature parsing is done since we need
4328 * if-feature for check_leafref_features() */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004329 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004330 } else if (!strcmp(sub->name, "units")) {
4331 if (llist->units) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004332 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004333 goto error;
4334 }
4335 GETVAL(value, sub, "name");
4336 llist->units = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci5323b492017-01-16 15:40:11 +01004337
Radek Krejciac00b2a2017-01-17 14:05:00 +01004338 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_UNITS, 0, unres)) {
Radek Krejci5323b492017-01-16 15:40:11 +01004339 goto error;
4340 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004341 } else if (!strcmp(sub->name, "ordered-by")) {
4342 if (f_ordr) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004343 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004344 goto error;
4345 }
4346 /* just checking the flags in llist is not sufficient, we would
4347 * allow multiple ordered-by statements with the "system" value
4348 */
4349 f_ordr = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004350
Radek Krejci1574a8d2015-08-03 14:16:52 +02004351 if (llist->flags & LYS_CONFIG_R) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004352 /* RFC 6020, 7.7.5 - ignore ordering when the list represents
4353 * state data
4354 */
Michal Vasko345da0a2015-12-02 10:35:55 +01004355 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004356 continue;
4357 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004358
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004359 GETVAL(value, sub, "value");
4360 if (!strcmp(value, "user")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02004361 llist->flags |= LYS_USERORDERED;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004362 } else if (strcmp(value, "system")) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004363 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004364 goto error;
Radek Krejci41882de2015-07-02 16:34:58 +02004365 } /* else system is the default value, so we can ignore it */
4366
Radek Krejciac00b2a2017-01-17 14:05:00 +01004367 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_ORDEREDBY, 0, unres)) {
4368 goto error;
4369 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004370 } else if (!strcmp(sub->name, "must")) {
4371 c_must++;
Radek Krejci41882de2015-07-02 16:34:58 +02004372 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004373 } else if (!strcmp(sub->name, "if-feature")) {
4374 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004375 continue;
Radek Krejcid5a5c282016-08-15 15:38:08 +02004376 } else if ((module->version >= 2) && !strcmp(sub->name, "default")) {
Radek Krejciac00b2a2017-01-17 14:05:00 +01004377 /* read the default's extension instances */
4378 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_DEFAULT, c_dflt, unres)) {
4379 goto error;
4380 }
4381
Radek Krejcid5a5c282016-08-15 15:38:08 +02004382 c_dflt++;
4383 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02004384
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004385 } else if (!strcmp(sub->name, "min-elements")) {
4386 if (f_min) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004387 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004388 goto error;
4389 }
4390 f_min = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004391
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004392 GETVAL(value, sub, "value");
4393 while (isspace(value[0])) {
4394 value++;
4395 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004396
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004397 /* convert it to uint32_t */
4398 errno = 0;
4399 endptr = NULL;
4400 val = strtoul(value, &endptr, 10);
4401 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004402 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004403 goto error;
4404 }
4405 llist->min = (uint32_t) val;
Michal Vasko6ea3e362016-03-11 10:25:36 +01004406 if (llist->max && (llist->min > llist->max)) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004407 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01004408 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Michal Vasko6ea3e362016-03-11 10:25:36 +01004409 goto error;
4410 }
Radek Krejci5323b492017-01-16 15:40:11 +01004411
Radek Krejciac00b2a2017-01-17 14:05:00 +01004412 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_MIN, 0, unres)) {
Radek Krejci5323b492017-01-16 15:40:11 +01004413 goto error;
4414 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004415 } else if (!strcmp(sub->name, "max-elements")) {
4416 if (f_max) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004417 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004418 goto error;
4419 }
4420 f_max = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004421
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004422 GETVAL(value, sub, "value");
4423 while (isspace(value[0])) {
4424 value++;
4425 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004426
Radek Krejci0d7b2472016-02-12 11:11:03 +01004427 if (!strcmp(value, "unbounded")) {
4428 llist->max = 0;
4429 } else {
4430 /* convert it to uint32_t */
4431 errno = 0;
4432 endptr = NULL;
4433 val = strtoul(value, &endptr, 10);
4434 if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004435 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01004436 goto error;
4437 }
4438 llist->max = (uint32_t) val;
Michal Vasko6ea3e362016-03-11 10:25:36 +01004439 if (llist->min > llist->max) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004440 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01004441 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"max-elements\" is smaller than \"min-elements\".");
Michal Vasko6ea3e362016-03-11 10:25:36 +01004442 goto error;
4443 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004444 }
Radek Krejci5323b492017-01-16 15:40:11 +01004445
Radek Krejciac00b2a2017-01-17 14:05:00 +01004446 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_MAX, 0, unres)) {
Radek Krejci5323b492017-01-16 15:40:11 +01004447 goto error;
4448 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004449 } else if (!strcmp(sub->name, "when")) {
4450 if (llist->when) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004451 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004452 goto error;
4453 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004454
Radek Krejci5323b492017-01-16 15:40:11 +01004455 llist->when = read_yin_when(module, sub, unres);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004456 if (!llist->when) {
4457 goto error;
4458 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004459 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004460 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004461 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004462 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004463
Michal Vasko88c29542015-11-27 14:57:53 +01004464 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004465 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004466
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004467 /* check constraints */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004468 if (!has_type) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004469 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004470 goto error;
4471 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004472
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004473 /* middle part - process nodes with cardinality of 0..n */
4474 if (c_must) {
4475 llist->must = calloc(c_must, sizeof *llist->must);
Michal Vasko253035f2015-12-17 16:58:13 +01004476 if (!llist->must) {
4477 LOGMEM;
4478 goto error;
4479 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004480 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004481 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004482 llist->iffeature = calloc(c_ftrs, sizeof *llist->iffeature);
4483 if (!llist->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01004484 LOGMEM;
4485 goto error;
4486 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004487 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02004488 if (c_dflt) {
4489 llist->dflt = calloc(c_dflt, sizeof *llist->dflt);
4490 if (!llist->dflt) {
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004491 LOGMEM;
4492 goto error;
4493 }
4494 }
Radek Krejcie534c132016-11-23 13:32:31 +01004495 if (c_ext) {
Radek Krejci5323b492017-01-16 15:40:11 +01004496 /* some extensions may be already present from the substatements */
4497 reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
4498 if (!reallocated) {
Radek Krejcie534c132016-11-23 13:32:31 +01004499 LOGMEM;
4500 goto error;
4501 }
Radek Krejci5323b492017-01-16 15:40:11 +01004502 retval->ext = reallocated;
4503
4504 /* init memory */
4505 memset(&retval->ext[retval->ext_size], 0, c_ext * sizeof *retval->ext);
Radek Krejcie534c132016-11-23 13:32:31 +01004506 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02004507
Radek Krejcie534c132016-11-23 13:32:31 +01004508 LY_TREE_FOR_SAFE(yin->child, next, sub) {
4509 if (strcmp(sub->ns->value, LY_NSYIN)) {
4510 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01004511 r = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01004512 retval->ext_size++;
4513 if (r) {
4514 goto error;
4515 }
4516 } else if (!strcmp(sub->name, "must")) {
Radek Krejcifccd1442017-01-16 10:26:57 +01004517 r = fill_yin_must(module, sub, &llist->must[llist->must_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004518 llist->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004519 if (r) {
4520 goto error;
4521 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004522 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02004523 r = fill_yin_iffeature(retval, 0, sub, &llist->iffeature[llist->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004524 llist->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01004525 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004526 goto error;
4527 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02004528 } else if (!strcmp(sub->name, "default")) {
4529 GETVAL(value, sub, "value");
4530
Radek Krejciac1a52c2016-09-15 14:42:40 +02004531 /* check for duplicity in case of configuration data,
4532 * in case of status data duplicities are allowed */
4533 if (llist->flags & LYS_CONFIG_W) {
4534 for (r = 0; r < llist->dflt_size; r++) {
4535 if (ly_strequal(llist->dflt[r], value, 1)) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004536 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, "default");
Michal Vasko51e5c582017-01-19 14:16:39 +01004537 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Duplicated default value \"%s\".", value);
Radek Krejciac1a52c2016-09-15 14:42:40 +02004538 goto error;
4539 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02004540 }
4541 }
4542 llist->dflt[llist->dflt_size++] = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004543 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004544 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004545
Radek Krejcicbb473e2016-09-16 14:48:32 +02004546 /* finalize type parsing */
4547 if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DER, retval) == -1) {
4548 llist->type.der = NULL;
4549 goto error;
4550 }
4551
Radek Krejcid5a5c282016-08-15 15:38:08 +02004552 if (llist->dflt_size && llist->min) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004553 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, retval, "min-elements", "leaf-list");
Michal Vasko51e5c582017-01-19 14:16:39 +01004554 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL,
Radek Krejcid5a5c282016-08-15 15:38:08 +02004555 "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
4556 goto error;
4557 }
4558
4559 /* check default value (if not defined, there still could be some restrictions
4560 * that need to be checked against a default value from a derived type) */
4561 for (r = 0; r < llist->dflt_size; r++) {
Radek Krejci51673202016-11-01 17:00:32 +01004562 if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT,
4563 (struct lys_node *)(&llist->dflt[r])) == -1) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02004564 goto error;
Radek Krejci25d782a2015-05-22 15:03:23 +02004565 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004566 }
4567
Michal Vasko508a50d2016-09-07 14:50:33 +02004568 /* check XPath dependencies */
4569 if ((llist->when || llist->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
4570 goto error;
4571 }
4572
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004573 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004574
4575error:
4576
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004577 lys_node_free(retval, NULL, 0);
Radek Krejcida04f4a2015-05-21 12:54:09 +02004578
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004579 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004580}
4581
Michal Vasko0d343d12015-08-24 14:57:36 +02004582/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004583static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02004584read_yin_list(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02004585 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02004586{
Radek Krejci1d82ef62015-08-07 14:44:40 +02004587 struct lys_node *retval, *node;
Radek Krejcib8048692015-08-05 13:36:34 +02004588 struct lys_node_list *list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004589 struct lyxml_elem *sub, *next, root, uniq;
Radek Krejci48464ed2016-03-17 15:44:09 +01004590 int r;
Radek Krejcie534c132016-11-23 13:32:31 +01004591 int c_tpdf = 0, c_must = 0, c_uniq = 0, c_ftrs = 0, c_ext = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004592 int f_ordr = 0, f_max = 0, f_min = 0;
Radek Krejci5c08a992016-11-02 13:30:04 +01004593 const char *value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004594 char *auxs;
4595 unsigned long val;
Radek Krejcie36d7c72017-01-17 16:12:30 +01004596 void *reallocated;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004597
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004598 /* init */
4599 memset(&root, 0, sizeof root);
4600 memset(&uniq, 0, sizeof uniq);
Radek Krejcie0674f82015-06-15 13:58:51 +02004601
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004602 list = calloc(1, sizeof *list);
Michal Vasko253035f2015-12-17 16:58:13 +01004603 if (!list) {
4604 LOGMEM;
4605 return NULL;
4606 }
Radek Krejci76512572015-08-04 09:47:08 +02004607 list->nodetype = LYS_LIST;
4608 list->prev = (struct lys_node *)list;
4609 retval = (struct lys_node *)list;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004610
Radek Krejci07d0fb92017-01-13 14:11:05 +01004611 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
4612 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004613 goto error;
4614 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004615
Radek Krejcia9544502015-08-14 08:24:29 +02004616 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4617
Radek Krejcic189a952016-07-11 15:27:07 +02004618 /* insert the node into the schema tree */
4619 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
4620 goto error;
4621 }
4622
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004623 /* process list's specific children */
4624 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01004625 if (strcmp(sub->ns->value, LY_NSYIN)) {
4626 /* extension */
4627 c_ext++;
Radek Krejci0d70c372015-07-02 16:23:10 +02004628 continue;
Radek Krejci0d70c372015-07-02 16:23:10 +02004629
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004630 /* data statements */
Radek Krejcie534c132016-11-23 13:32:31 +01004631 } else if (!strcmp(sub->name, "container") ||
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004632 !strcmp(sub->name, "leaf-list") ||
4633 !strcmp(sub->name, "leaf") ||
4634 !strcmp(sub->name, "list") ||
4635 !strcmp(sub->name, "choice") ||
4636 !strcmp(sub->name, "uses") ||
4637 !strcmp(sub->name, "grouping") ||
Michal Vaskoca7cbc42016-07-01 11:36:53 +02004638 !strcmp(sub->name, "anyxml") ||
Michal Vaskob4c608c2016-09-15 09:36:20 +02004639 !strcmp(sub->name, "action") ||
4640 !strcmp(sub->name, "notification")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004641 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004642 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02004643
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004644 /* array counters */
4645 } else if (!strcmp(sub->name, "key")) {
4646 /* check cardinality 0..1 */
4647 if (list->keys_size) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004648 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, list->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004649 goto error;
4650 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004651
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004652 /* count the number of keys */
4653 GETVAL(value, sub, "value");
Radek Krejci5c08a992016-11-02 13:30:04 +01004654 list->keys_str = lydict_insert(module->ctx, value, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004655 while ((value = strpbrk(value, " \t\n"))) {
4656 list->keys_size++;
4657 while (isspace(*value)) {
4658 value++;
4659 }
4660 }
4661 list->keys_size++;
4662 list->keys = calloc(list->keys_size, sizeof *list->keys);
Michal Vasko253035f2015-12-17 16:58:13 +01004663 if (!list->keys) {
4664 LOGMEM;
4665 goto error;
4666 }
Radek Krejcie36d7c72017-01-17 16:12:30 +01004667
4668 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_KEY, 0, unres)) {
4669 goto error;
4670 }
4671 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004672 } else if (!strcmp(sub->name, "unique")) {
4673 c_uniq++;
Michal Vaskof3930de2015-10-22 12:03:59 +02004674 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004675 lyxml_add_child(module->ctx, &uniq, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004676 } else if (!strcmp(sub->name, "typedef")) {
4677 c_tpdf++;
4678 } else if (!strcmp(sub->name, "must")) {
4679 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004680 } else if (!strcmp(sub->name, "if-feature")) {
4681 c_ftrs++;
Radek Krejci345ad742015-06-03 11:04:18 +02004682
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004683 /* optional stetments */
4684 } else if (!strcmp(sub->name, "ordered-by")) {
4685 if (f_ordr) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004686 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004687 goto error;
4688 }
4689 /* just checking the flags in llist is not sufficient, we would
4690 * allow multiple ordered-by statements with the "system" value
4691 */
4692 f_ordr = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02004693
Radek Krejci1574a8d2015-08-03 14:16:52 +02004694 if (list->flags & LYS_CONFIG_R) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004695 /* RFC 6020, 7.7.5 - ignore ordering when the list represents
4696 * state data
4697 */
Michal Vasko345da0a2015-12-02 10:35:55 +01004698 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004699 continue;
4700 }
Radek Krejci345ad742015-06-03 11:04:18 +02004701
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004702 GETVAL(value, sub, "value");
4703 if (!strcmp(value, "user")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02004704 list->flags |= LYS_USERORDERED;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004705 } else if (strcmp(value, "system")) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004706 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004707 goto error;
Radek Krejcie36d7c72017-01-17 16:12:30 +01004708 } /* else system is the default value, so we can ignore it */
4709
4710 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_ORDEREDBY, 0, unres)) {
4711 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004712 }
Michal Vasko345da0a2015-12-02 10:35:55 +01004713 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004714 } else if (!strcmp(sub->name, "min-elements")) {
4715 if (f_min) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004716 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004717 goto error;
4718 }
4719 f_min = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02004720
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004721 GETVAL(value, sub, "value");
4722 while (isspace(value[0])) {
4723 value++;
4724 }
Radek Krejci345ad742015-06-03 11:04:18 +02004725
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004726 /* convert it to uint32_t */
4727 errno = 0;
4728 auxs = NULL;
4729 val = strtoul(value, &auxs, 10);
4730 if (*auxs || value[0] == '-' || errno || val > UINT32_MAX) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004731 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004732 goto error;
4733 }
4734 list->min = (uint32_t) val;
Michal Vasko6ea3e362016-03-11 10:25:36 +01004735 if (list->max && (list->min > list->max)) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004736 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01004737 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Michal Vasko6ea3e362016-03-11 10:25:36 +01004738 lyxml_free(module->ctx, sub);
4739 goto error;
4740 }
Radek Krejcie36d7c72017-01-17 16:12:30 +01004741 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_MIN, 0, unres)) {
4742 goto error;
4743 }
Michal Vasko345da0a2015-12-02 10:35:55 +01004744 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004745 } else if (!strcmp(sub->name, "max-elements")) {
4746 if (f_max) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004747 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004748 goto error;
4749 }
4750 f_max = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02004751
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004752 GETVAL(value, sub, "value");
4753 while (isspace(value[0])) {
4754 value++;
4755 }
Radek Krejci345ad742015-06-03 11:04:18 +02004756
Radek Krejci0d7b2472016-02-12 11:11:03 +01004757 if (!strcmp(value, "unbounded")) {
4758 list->max = 0;;
4759 } else {
4760 /* convert it to uint32_t */
4761 errno = 0;
4762 auxs = NULL;
4763 val = strtoul(value, &auxs, 10);
4764 if (*auxs || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004765 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01004766 goto error;
4767 }
4768 list->max = (uint32_t) val;
Michal Vasko6ea3e362016-03-11 10:25:36 +01004769 if (list->min > list->max) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004770 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, value, sub->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01004771 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "\"max-elements\" is smaller than \"min-elements\".");
Michal Vasko6ea3e362016-03-11 10:25:36 +01004772 goto error;
4773 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004774 }
Radek Krejcie36d7c72017-01-17 16:12:30 +01004775 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_MAX, 0, unres)) {
4776 goto error;
4777 }
Michal Vasko345da0a2015-12-02 10:35:55 +01004778 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004779 } else if (!strcmp(sub->name, "when")) {
4780 if (list->when) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004781 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004782 goto error;
4783 }
4784
Radek Krejci5323b492017-01-16 15:40:11 +01004785 list->when = read_yin_when(module, sub, unres);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004786 if (!list->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004787 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004788 goto error;
4789 }
Michal Vasko345da0a2015-12-02 10:35:55 +01004790 lyxml_free(module->ctx, sub);
Radek Krejci3cf9e222015-06-18 11:37:50 +02004791 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01004792 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02004793 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004794 }
4795 }
Radek Krejci345ad742015-06-03 11:04:18 +02004796
Michal Vaskoe022a562016-09-27 14:24:15 +02004797 /* check - if list is configuration, key statement is mandatory
4798 * (but only if we are not in a grouping or augment, then the check is deferred) */
4799 for (node = retval; node && !(node->nodetype & (LYS_GROUPING | LYS_AUGMENT)); node = node->parent);
Radek Krejci5c08a992016-11-02 13:30:04 +01004800 if (!node && (list->flags & LYS_CONFIG_W) && !list->keys_str) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004801 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004802 goto error;
4803 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004804
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004805 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4806 if (c_tpdf) {
4807 list->tpdf = calloc(c_tpdf, sizeof *list->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004808 if (!list->tpdf) {
4809 LOGMEM;
4810 goto error;
4811 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004812 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004813 if (c_must) {
4814 list->must = calloc(c_must, sizeof *list->must);
Michal Vasko253035f2015-12-17 16:58:13 +01004815 if (!list->must) {
4816 LOGMEM;
4817 goto error;
4818 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004819 }
4820 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004821 list->iffeature = calloc(c_ftrs, sizeof *list->iffeature);
4822 if (!list->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01004823 LOGMEM;
4824 goto error;
4825 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004826 }
Radek Krejcie534c132016-11-23 13:32:31 +01004827 if (c_ext) {
Radek Krejcie36d7c72017-01-17 16:12:30 +01004828 /* some extensions may be already present from the substatements */
4829 reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
4830 if (!reallocated) {
Radek Krejcie534c132016-11-23 13:32:31 +01004831 LOGMEM;
4832 goto error;
4833 }
Radek Krejcie36d7c72017-01-17 16:12:30 +01004834 retval->ext = reallocated;
4835
4836 /* init memory */
4837 memset(&retval->ext[retval->ext_size], 0, c_ext * sizeof *retval->ext);
Radek Krejcie534c132016-11-23 13:32:31 +01004838 }
Radek Krejcie36d7c72017-01-17 16:12:30 +01004839
Radek Krejcie534c132016-11-23 13:32:31 +01004840 LY_TREE_FOR_SAFE(yin->child, next, sub) {
4841 if (strcmp(sub->ns->value, LY_NSYIN)) {
4842 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01004843 r = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01004844 retval->ext_size++;
4845 if (r) {
4846 goto error;
4847 }
4848 } else if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004849 r = fill_yin_typedef(module, retval, sub, &list->tpdf[list->tpdf_size], unres);
4850 list->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004851 if (r) {
4852 goto error;
4853 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004854 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02004855 r = fill_yin_iffeature(retval, 0, sub, &list->iffeature[list->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004856 list->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01004857 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004858 goto error;
4859 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004860 } else if (!strcmp(sub->name, "must")) {
Radek Krejcifccd1442017-01-16 10:26:57 +01004861 r = fill_yin_must(module, sub, &list->must[list->must_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004862 list->must_size++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004863 if (r) {
4864 goto error;
4865 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004866 }
4867 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004868
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004869 /* last part - process data nodes */
4870 LY_TREE_FOR_SAFE(root.child, next, sub) {
4871 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004872 node = read_yin_container(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004873 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004874 node = read_yin_leaflist(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004875 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004876 node = read_yin_leaf(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004877 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004878 node = read_yin_list(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004879 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004880 node = read_yin_choice(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004881 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02004882 node = read_yin_uses(module, retval, sub, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004883 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004884 node = read_yin_grouping(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004885 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004886 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02004887 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004888 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
Michal Vaskoca7cbc42016-07-01 11:36:53 +02004889 } else if (!strcmp(sub->name, "action")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004890 node = read_yin_rpc_action(module, retval, sub, unres);
Michal Vaskob4c608c2016-09-15 09:36:20 +02004891 } else if (!strcmp(sub->name, "notification")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004892 node = read_yin_notif(module, retval, sub, unres);
Michal Vaskoc07187d2015-08-13 15:20:57 +02004893 } else {
4894 LOGINT;
4895 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004896 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004897 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004898 goto error;
4899 }
Radek Krejci73adb602015-07-02 18:07:40 +02004900
Michal Vasko345da0a2015-12-02 10:35:55 +01004901 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004902 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004903
Radek Krejci5c08a992016-11-02 13:30:04 +01004904 if (list->keys_str) {
4905 /* check that we are not in grouping */
4906 for (node = parent; node && node->nodetype != LYS_GROUPING; node = lys_parent(node));
4907 if (!node && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
Radek Krejci461efb92016-02-12 15:52:18 +01004908 goto error;
4909 }
4910 } /* else config false list without a key, key_str presence in case of config true is checked earlier */
Radek Krejci812b10a2015-05-28 16:48:25 +02004911
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004912 /* process unique statements */
4913 if (c_uniq) {
4914 list->unique = calloc(c_uniq, sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01004915 if (!list->unique) {
4916 LOGMEM;
4917 goto error;
4918 }
Radek Krejci1e9b9992015-06-04 17:57:04 +02004919
Radek Krejci461efb92016-02-12 15:52:18 +01004920 LY_TREE_FOR_SAFE(uniq.child, next, sub) {
4921 r = fill_yin_unique(module, retval, sub, &list->unique[list->unique_size], unres);
4922 list->unique_size++;
4923 if (r) {
4924 goto error;
4925 }
4926
Radek Krejcie36d7c72017-01-17 16:12:30 +01004927 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub,
4928 LYEXT_SUBSTMT_UNIQUE, list->unique_size - 1, unres)) {
4929 goto error;
4930 }
Radek Krejci461efb92016-02-12 15:52:18 +01004931 lyxml_free(module->ctx, sub);
4932 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004933 }
Radek Krejci1e9b9992015-06-04 17:57:04 +02004934
Michal Vasko508a50d2016-09-07 14:50:33 +02004935 /* check XPath dependencies */
4936 if ((list->when || list->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
4937 goto error;
4938 }
4939
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004940 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004941
4942error:
4943
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004944 lys_node_free(retval, NULL, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004945 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004946 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004947 }
4948 while (uniq.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004949 lyxml_free(module->ctx, uniq.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004950 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004951
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004952 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004953}
4954
Michal Vasko0d343d12015-08-24 14:57:36 +02004955/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004956static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02004957read_yin_container(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02004958 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02004959{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004960 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004961 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02004962 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004963 struct lys_node_container *cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004964 const char *value;
Radek Krejci07d0fb92017-01-13 14:11:05 +01004965 void *reallocated;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004966 int r;
Radek Krejcie534c132016-11-23 13:32:31 +01004967 int c_tpdf = 0, c_must = 0, c_ftrs = 0, c_ext = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004968
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004969 /* init */
4970 memset(&root, 0, sizeof root);
Radek Krejcie0674f82015-06-15 13:58:51 +02004971
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004972 cont = calloc(1, sizeof *cont);
Michal Vasko253035f2015-12-17 16:58:13 +01004973 if (!cont) {
4974 LOGMEM;
4975 return NULL;
4976 }
Radek Krejci76512572015-08-04 09:47:08 +02004977 cont->nodetype = LYS_CONTAINER;
4978 cont->prev = (struct lys_node *)cont;
4979 retval = (struct lys_node *)cont;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004980
Radek Krejci07d0fb92017-01-13 14:11:05 +01004981 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
4982 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004983 goto error;
4984 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004985
Radek Krejcia9544502015-08-14 08:24:29 +02004986 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4987
Radek Krejcic189a952016-07-11 15:27:07 +02004988 /* insert the node into the schema tree */
4989 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
4990 goto error;
4991 }
4992
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004993 /* process container's specific children */
4994 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01004995 if (strcmp(sub->ns->value, LY_NSYIN)) {
4996 /* extension */
4997 c_ext++;
4998 } else if (!strcmp(sub->name, "presence")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004999 if (cont->presence) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005000 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005001 goto error;
5002 }
5003 GETVAL(value, sub, "value");
5004 cont->presence = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci800af702015-06-02 13:46:01 +02005005
Radek Krejciac00b2a2017-01-17 14:05:00 +01005006 if (read_yin_subnode_ext(module, retval, LYEXT_PAR_NODE, sub, LYEXT_SUBSTMT_PRESENCE, 0, unres)) {
Radek Krejcifccd1442017-01-16 10:26:57 +01005007 goto error;
5008 }
Michal Vasko345da0a2015-12-02 10:35:55 +01005009 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005010 } else if (!strcmp(sub->name, "when")) {
5011 if (cont->when) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005012 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005013 goto error;
5014 }
5015
Radek Krejci5323b492017-01-16 15:40:11 +01005016 cont->when = read_yin_when(module, sub, unres);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005017 if (!cont->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005018 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005019 goto error;
5020 }
Michal Vasko345da0a2015-12-02 10:35:55 +01005021 lyxml_free(module->ctx, sub);
Radek Krejci4c31f122015-06-02 14:51:22 +02005022
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005023 /* data statements */
5024 } else if (!strcmp(sub->name, "container") ||
5025 !strcmp(sub->name, "leaf-list") ||
5026 !strcmp(sub->name, "leaf") ||
5027 !strcmp(sub->name, "list") ||
5028 !strcmp(sub->name, "choice") ||
5029 !strcmp(sub->name, "uses") ||
5030 !strcmp(sub->name, "grouping") ||
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005031 !strcmp(sub->name, "anyxml") ||
Michal Vaskob4c608c2016-09-15 09:36:20 +02005032 !strcmp(sub->name, "action") ||
5033 !strcmp(sub->name, "notification")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02005034 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02005035 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02005036
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005037 /* array counters */
5038 } else if (!strcmp(sub->name, "typedef")) {
5039 c_tpdf++;
5040 } else if (!strcmp(sub->name, "must")) {
5041 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005042 } else if (!strcmp(sub->name, "if-feature")) {
5043 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005044 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005045 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005046 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005047 }
5048 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005049
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005050 /* middle part - process nodes with cardinality of 0..n except the data nodes */
5051 if (c_tpdf) {
5052 cont->tpdf = calloc(c_tpdf, sizeof *cont->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01005053 if (!cont->tpdf) {
5054 LOGMEM;
5055 goto error;
5056 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005057 }
5058 if (c_must) {
5059 cont->must = calloc(c_must, sizeof *cont->must);
Michal Vasko253035f2015-12-17 16:58:13 +01005060 if (!cont->must) {
5061 LOGMEM;
5062 goto error;
5063 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005064 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005065 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005066 cont->iffeature = calloc(c_ftrs, sizeof *cont->iffeature);
5067 if (!cont->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01005068 LOGMEM;
5069 goto error;
5070 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005071 }
Radek Krejcie534c132016-11-23 13:32:31 +01005072 if (c_ext) {
Radek Krejci07d0fb92017-01-13 14:11:05 +01005073 /* some extensions may be already present from the substatements */
5074 reallocated = realloc(retval->ext, (c_ext + retval->ext_size) * sizeof *retval->ext);
5075 if (!reallocated) {
Radek Krejcie534c132016-11-23 13:32:31 +01005076 LOGMEM;
5077 goto error;
5078 }
Radek Krejci07d0fb92017-01-13 14:11:05 +01005079 retval->ext = reallocated;
5080
5081 /* init memory */
5082 memset(&retval->ext[retval->ext_size], 0, c_ext * sizeof *retval->ext);
Radek Krejcie534c132016-11-23 13:32:31 +01005083 }
Radek Krejci800af702015-06-02 13:46:01 +02005084
Radek Krejcie534c132016-11-23 13:32:31 +01005085 LY_TREE_FOR_SAFE(yin->child, next, sub) {
5086 if (strcmp(sub->ns->value, LY_NSYIN)) {
5087 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01005088 r = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01005089 retval->ext_size++;
5090 if (r) {
5091 goto error;
5092 }
5093 } else if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01005094 r = fill_yin_typedef(module, retval, sub, &cont->tpdf[cont->tpdf_size], unres);
5095 cont->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005096 if (r) {
5097 goto error;
5098 }
5099 } else if (!strcmp(sub->name, "must")) {
Radek Krejcifccd1442017-01-16 10:26:57 +01005100 r = fill_yin_must(module, sub, &cont->must[cont->must_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01005101 cont->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005102 if (r) {
5103 goto error;
5104 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005105 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02005106 r = fill_yin_iffeature(retval, 0, sub, &cont->iffeature[cont->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005107 cont->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01005108 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005109 goto error;
5110 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005111 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005112 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005113
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005114 /* last part - process data nodes */
5115 LY_TREE_FOR_SAFE(root.child, next, sub) {
5116 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005117 node = read_yin_container(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005118 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005119 node = read_yin_leaflist(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005120 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005121 node = read_yin_leaf(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005122 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005123 node = read_yin_list(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005124 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005125 node = read_yin_choice(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005126 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02005127 node = read_yin_uses(module, retval, sub, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005128 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005129 node = read_yin_grouping(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005130 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005131 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02005132 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005133 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005134 } else if (!strcmp(sub->name, "action")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005135 node = read_yin_rpc_action(module, retval, sub, unres);
Michal Vaskob4c608c2016-09-15 09:36:20 +02005136 } else if (!strcmp(sub->name, "notification")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005137 node = read_yin_notif(module, retval, sub, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005138 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005139 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005140 goto error;
5141 }
Radek Krejci73adb602015-07-02 18:07:40 +02005142
Michal Vasko345da0a2015-12-02 10:35:55 +01005143 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005144 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005145
Michal Vasko508a50d2016-09-07 14:50:33 +02005146 /* check XPath dependencies */
5147 if ((cont->when || cont->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
5148 goto error;
5149 }
5150
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005151 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005152
5153error:
5154
Michal Vaskod51d6ad2016-02-16 13:24:31 +01005155 lys_node_free(retval, NULL, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005156 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005157 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005158 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005159
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005160 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005161}
5162
Michal Vasko0d343d12015-08-24 14:57:36 +02005163/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02005164static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02005165read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02005166 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02005167{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005168 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005169 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02005170 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02005171 struct lys_node_grp *grp;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005172 int r;
Radek Krejcie534c132016-11-23 13:32:31 +01005173 int c_tpdf = 0, c_ext = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005174
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005175 /* init */
5176 memset(&root, 0, sizeof root);
Radek Krejcie0674f82015-06-15 13:58:51 +02005177
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005178 grp = calloc(1, sizeof *grp);
Michal Vasko253035f2015-12-17 16:58:13 +01005179 if (!grp) {
5180 LOGMEM;
5181 return NULL;
5182 }
Radek Krejci76512572015-08-04 09:47:08 +02005183 grp->nodetype = LYS_GROUPING;
5184 grp->prev = (struct lys_node *)grp;
5185 retval = (struct lys_node *)grp;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005186
Radek Krejci07d0fb92017-01-13 14:11:05 +01005187 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin, OPT_IDENT | OPT_MODULE , unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005188 goto error;
5189 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005190
Radek Krejcia9544502015-08-14 08:24:29 +02005191 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
5192
Radek Krejcic189a952016-07-11 15:27:07 +02005193 /* insert the node into the schema tree */
5194 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
5195 goto error;
5196 }
5197
Radek Krejci1d82ef62015-08-07 14:44:40 +02005198 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01005199 if (strcmp(sub->ns->value, LY_NSYIN)) {
5200 /* extension */
5201 c_ext++;
Radek Krejci0d70c372015-07-02 16:23:10 +02005202
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005203 /* data statements */
Radek Krejcie534c132016-11-23 13:32:31 +01005204 } else if (!strcmp(sub->name, "container") ||
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005205 !strcmp(sub->name, "leaf-list") ||
5206 !strcmp(sub->name, "leaf") ||
5207 !strcmp(sub->name, "list") ||
5208 !strcmp(sub->name, "choice") ||
5209 !strcmp(sub->name, "uses") ||
5210 !strcmp(sub->name, "grouping") ||
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005211 !strcmp(sub->name, "anyxml") ||
Michal Vasko5acb5482016-09-16 14:35:14 +02005212 !strcmp(sub->name, "action") ||
5213 !strcmp(sub->name, "notification")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02005214 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02005215 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02005216
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005217 /* array counters */
5218 } else if (!strcmp(sub->name, "typedef")) {
5219 c_tpdf++;
5220 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005221 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005222 goto error;
5223 }
5224 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005225
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005226 /* middle part - process nodes with cardinality of 0..n except the data nodes */
5227 if (c_tpdf) {
5228 grp->tpdf = calloc(c_tpdf, sizeof *grp->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01005229 if (!grp->tpdf) {
5230 LOGMEM;
5231 goto error;
5232 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005233 }
Radek Krejcie534c132016-11-23 13:32:31 +01005234 if (c_ext) {
5235 grp->ext = calloc(c_ext, sizeof *grp->ext);
5236 if (!grp->ext) {
5237 LOGMEM;
Radek Krejci73adb602015-07-02 18:07:40 +02005238 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005239 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005240 }
Radek Krejcie534c132016-11-23 13:32:31 +01005241 LY_TREE_FOR_SAFE(yin->child, next, sub) {
5242 if (strcmp(sub->ns->value, LY_NSYIN)) {
5243 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01005244 r = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01005245 retval->ext_size++;
5246 if (r) {
5247 goto error;
5248 }
5249 } else {
5250 /* typedef */
5251 r = fill_yin_typedef(module, retval, sub, &grp->tpdf[grp->tpdf_size], unres);
5252 grp->tpdf_size++;
5253 if (r) {
5254 goto error;
5255 }
5256 }
5257 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005258
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005259 /* last part - process data nodes */
Michal Vasko919dbbc2016-05-19 15:22:45 +02005260 if (!root.child) {
5261 LOGWRN("Grouping \"%s\" without children.", retval->name);
5262 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005263 LY_TREE_FOR_SAFE(root.child, next, sub) {
5264 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005265 node = read_yin_container(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005266 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005267 node = read_yin_leaflist(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005268 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005269 node = read_yin_leaf(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005270 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005271 node = read_yin_list(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005272 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005273 node = read_yin_choice(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005274 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02005275 node = read_yin_uses(module, retval, sub, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005276 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005277 node = read_yin_grouping(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005278 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005279 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02005280 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005281 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005282 } else if (!strcmp(sub->name, "action")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005283 node = read_yin_rpc_action(module, retval, sub, unres);
Michal Vasko5acb5482016-09-16 14:35:14 +02005284 } else if (!strcmp(sub->name, "notification")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005285 node = read_yin_notif(module, retval, sub, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005286 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005287 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005288 goto error;
5289 }
Radek Krejci73adb602015-07-02 18:07:40 +02005290
Michal Vasko345da0a2015-12-02 10:35:55 +01005291 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005292 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005293
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005294 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005295
5296error:
5297
Michal Vaskod51d6ad2016-02-16 13:24:31 +01005298 lys_node_free(retval, NULL, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005299 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005300 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005301 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005302
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005303 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005304}
5305
Michal Vasko0d343d12015-08-24 14:57:36 +02005306/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02005307static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02005308read_yin_input_output(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +02005309 struct unres_schema *unres)
Michal Vasko38d01f72015-06-15 09:41:06 +02005310{
Radek Krejcie0674f82015-06-15 13:58:51 +02005311 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005312 struct lys_node *node = NULL;
Radek Krejci31fc30d2015-08-13 08:27:38 +02005313 struct lys_node *retval = NULL;
Michal Vasko44fb6382016-06-29 11:12:27 +02005314 struct lys_node_inout *inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02005315 int r;
Radek Krejcie534c132016-11-23 13:32:31 +01005316 int c_tpdf = 0, c_must = 0, c_ext = 0;
Michal Vasko38d01f72015-06-15 09:41:06 +02005317
Radek Krejcie0674f82015-06-15 13:58:51 +02005318 /* init */
5319 memset(&root, 0, sizeof root);
5320
Michal Vasko38d01f72015-06-15 09:41:06 +02005321 inout = calloc(1, sizeof *inout);
Michal Vasko253035f2015-12-17 16:58:13 +01005322 if (!inout) {
5323 LOGMEM;
5324 return NULL;
5325 }
Radek Krejci6acc8012015-08-13 09:07:04 +02005326 inout->prev = (struct lys_node *)inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02005327
5328 if (!strcmp(yin->name, "input")) {
Radek Krejci76512572015-08-04 09:47:08 +02005329 inout->nodetype = LYS_INPUT;
Michal Vasko0a1aaa42016-04-19 09:48:25 +02005330 inout->name = lydict_insert(module->ctx, "input", 0);
Michal Vasko38d01f72015-06-15 09:41:06 +02005331 } else if (!strcmp(yin->name, "output")) {
Radek Krejci76512572015-08-04 09:47:08 +02005332 inout->nodetype = LYS_OUTPUT;
Michal Vasko0a1aaa42016-04-19 09:48:25 +02005333 inout->name = lydict_insert(module->ctx, "output", 0);
Michal Vasko38d01f72015-06-15 09:41:06 +02005334 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02005335 LOGINT;
Radek Krejci6acc8012015-08-13 09:07:04 +02005336 free(inout);
Michal Vasko0c888fd2015-08-11 15:54:08 +02005337 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02005338 }
5339
Radek Krejci76512572015-08-04 09:47:08 +02005340 retval = (struct lys_node *)inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02005341
Radek Krejci07d0fb92017-01-13 14:11:05 +01005342 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin, OPT_MODULE, unres)) {
Michal Vaskoebeac942015-06-15 12:11:50 +02005343 goto error;
5344 }
5345
Radek Krejcia9544502015-08-14 08:24:29 +02005346 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
5347
Radek Krejcic189a952016-07-11 15:27:07 +02005348 /* insert the node into the schema tree */
5349 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
5350 goto error;
5351 }
5352
Michal Vasko38d01f72015-06-15 09:41:06 +02005353 /* data statements */
5354 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01005355 if (strcmp(sub->ns->value, LY_NSYIN)) {
5356 /* extension */
5357 c_ext++;
5358 } else if (!strcmp(sub->name, "container") ||
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005359 !strcmp(sub->name, "leaf-list") ||
5360 !strcmp(sub->name, "leaf") ||
5361 !strcmp(sub->name, "list") ||
5362 !strcmp(sub->name, "choice") ||
5363 !strcmp(sub->name, "uses") ||
5364 !strcmp(sub->name, "grouping") ||
5365 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02005366 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02005367 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02005368
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005369 /* array counters */
Michal Vasko38d01f72015-06-15 09:41:06 +02005370 } else if (!strcmp(sub->name, "typedef")) {
5371 c_tpdf++;
Radek Krejcid2bfa792015-07-02 16:45:29 +02005372
Radek Krejci1a31efe2016-07-29 11:04:16 +02005373 } else if ((module->version >= 2) && !strcmp(sub->name, "must")) {
Radek Krejci19332802016-07-29 10:39:46 +02005374 c_must++;
5375
Michal Vasko38d01f72015-06-15 09:41:06 +02005376 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005377 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02005378 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02005379 }
5380 }
5381
5382 /* middle part - process nodes with cardinality of 0..n except the data nodes */
5383 if (c_tpdf) {
5384 inout->tpdf = calloc(c_tpdf, sizeof *inout->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01005385 if (!inout->tpdf) {
5386 LOGMEM;
5387 goto error;
5388 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005389 }
Radek Krejci19332802016-07-29 10:39:46 +02005390 if (c_must) {
5391 inout->must = calloc(c_must, sizeof *inout->must);
5392 if (!inout->must) {
5393 LOGMEM;
5394 goto error;
5395 }
5396 }
Radek Krejcie534c132016-11-23 13:32:31 +01005397 if (c_ext) {
5398 inout->ext = calloc(c_ext, sizeof *inout->ext);
5399 if (!inout->ext) {
5400 LOGMEM;
5401 goto error;
5402 }
5403 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005404
Radek Krejcie534c132016-11-23 13:32:31 +01005405 LY_TREE_FOR_SAFE(yin->child, next, sub) {
5406 if (strcmp(sub->ns->value, LY_NSYIN)) {
5407 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01005408 r = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01005409 retval->ext_size++;
5410 if (r) {
5411 goto error;
5412 }
5413 } else if (!strcmp(sub->name, "must")) {
Radek Krejcifccd1442017-01-16 10:26:57 +01005414 r = fill_yin_must(module, sub, &inout->must[inout->must_size], unres);
Radek Krejci19332802016-07-29 10:39:46 +02005415 inout->must_size++;
5416 if (r) {
5417 goto error;
5418 }
5419 } else { /* typedef */
5420 r = fill_yin_typedef(module, retval, sub, &inout->tpdf[inout->tpdf_size], unres);
5421 inout->tpdf_size++;
5422 if (r) {
5423 goto error;
5424 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005425 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005426 }
5427
5428 /* last part - process data nodes */
5429 LY_TREE_FOR_SAFE(root.child, next, sub) {
5430 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005431 node = read_yin_container(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02005432 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005433 node = read_yin_leaflist(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02005434 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005435 node = read_yin_leaf(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02005436 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005437 node = read_yin_list(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02005438 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005439 node = read_yin_choice(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02005440 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02005441 node = read_yin_uses(module, retval, sub, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02005442 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005443 node = read_yin_grouping(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02005444 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005445 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, 0, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02005446 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005447 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02005448 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005449 if (!node) {
Michal Vasko38d01f72015-06-15 09:41:06 +02005450 goto error;
5451 }
Radek Krejci73adb602015-07-02 18:07:40 +02005452
Michal Vasko345da0a2015-12-02 10:35:55 +01005453 lyxml_free(module->ctx, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02005454 }
5455
Michal Vasko508a50d2016-09-07 14:50:33 +02005456 /* check XPath dependencies */
5457 if (inout->must_size && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
5458 goto error;
5459 }
5460
Michal Vasko38d01f72015-06-15 09:41:06 +02005461 return retval;
5462
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005463error:
Michal Vasko38d01f72015-06-15 09:41:06 +02005464
Michal Vaskod51d6ad2016-02-16 13:24:31 +01005465 lys_node_free(retval, NULL, 0);
Michal Vasko3a9abf82015-06-17 10:18:26 +02005466 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005467 lyxml_free(module->ctx, root.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02005468 }
5469
5470 return NULL;
5471}
5472
Michal Vasko0d343d12015-08-24 14:57:36 +02005473/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02005474static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02005475read_yin_notif(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +02005476 struct unres_schema *unres)
Michal Vasko0ea41032015-06-16 08:53:55 +02005477{
Michal Vaskoc6551b32015-06-16 10:51:43 +02005478 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005479 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02005480 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02005481 struct lys_node_notif *notif;
Michal Vasko0ea41032015-06-16 08:53:55 +02005482 int r;
Radek Krejcie534c132016-11-23 13:32:31 +01005483 int c_tpdf = 0, c_ftrs = 0, c_must = 0, c_ext = 0;
Michal Vasko0ea41032015-06-16 08:53:55 +02005484
Michal Vasko6bb7fc12016-09-21 14:17:22 +02005485 if (parent && (module->version < 2)) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005486 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, parent, "notification");
Michal Vasko6bb7fc12016-09-21 14:17:22 +02005487 return NULL;
5488 }
Michal Vasko0ea41032015-06-16 08:53:55 +02005489
Michal Vaskoc6551b32015-06-16 10:51:43 +02005490 memset(&root, 0, sizeof root);
5491
Michal Vasko0ea41032015-06-16 08:53:55 +02005492 notif = calloc(1, sizeof *notif);
Michal Vasko253035f2015-12-17 16:58:13 +01005493 if (!notif) {
5494 LOGMEM;
5495 return NULL;
5496 }
Radek Krejci76512572015-08-04 09:47:08 +02005497 notif->nodetype = LYS_NOTIF;
5498 notif->prev = (struct lys_node *)notif;
5499 retval = (struct lys_node *)notif;
Michal Vasko0ea41032015-06-16 08:53:55 +02005500
Radek Krejci07d0fb92017-01-13 14:11:05 +01005501 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin, OPT_IDENT | OPT_MODULE, unres)) {
Michal Vasko0ea41032015-06-16 08:53:55 +02005502 goto error;
5503 }
5504
Radek Krejcia9544502015-08-14 08:24:29 +02005505 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
5506
Radek Krejcic189a952016-07-11 15:27:07 +02005507 /* insert the node into the schema tree */
5508 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
5509 goto error;
5510 }
5511
Michal Vasko0ea41032015-06-16 08:53:55 +02005512 /* process rpc's specific children */
5513 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01005514 if (strcmp(sub->ns->value, LY_NSYIN)) {
5515 /* extension */
5516 c_ext++;
Radek Krejci0d70c372015-07-02 16:23:10 +02005517 continue;
Radek Krejci0d70c372015-07-02 16:23:10 +02005518
Michal Vasko0ea41032015-06-16 08:53:55 +02005519 /* data statements */
Radek Krejcie534c132016-11-23 13:32:31 +01005520 } else if (!strcmp(sub->name, "container") ||
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005521 !strcmp(sub->name, "leaf-list") ||
5522 !strcmp(sub->name, "leaf") ||
5523 !strcmp(sub->name, "list") ||
5524 !strcmp(sub->name, "choice") ||
5525 !strcmp(sub->name, "uses") ||
5526 !strcmp(sub->name, "grouping") ||
5527 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02005528 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02005529 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko0ea41032015-06-16 08:53:55 +02005530
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005531 /* array counters */
Michal Vasko0ea41032015-06-16 08:53:55 +02005532 } else if (!strcmp(sub->name, "typedef")) {
5533 c_tpdf++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005534 } else if (!strcmp(sub->name, "if-feature")) {
5535 c_ftrs++;
Radek Krejci1a31efe2016-07-29 11:04:16 +02005536 } else if ((module->version >= 2) && !strcmp(sub->name, "must")) {
Radek Krejci19332802016-07-29 10:39:46 +02005537 c_must++;
Michal Vasko0ea41032015-06-16 08:53:55 +02005538 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005539 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Michal Vasko0ea41032015-06-16 08:53:55 +02005540 goto error;
Michal Vasko0ea41032015-06-16 08:53:55 +02005541 }
5542 }
5543
5544 /* middle part - process nodes with cardinality of 0..n except the data nodes */
5545 if (c_tpdf) {
5546 notif->tpdf = calloc(c_tpdf, sizeof *notif->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01005547 if (!notif->tpdf) {
5548 LOGMEM;
5549 goto error;
5550 }
Michal Vasko0ea41032015-06-16 08:53:55 +02005551 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005552 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005553 notif->iffeature = calloc(c_ftrs, sizeof *notif->iffeature);
5554 if (!notif->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01005555 LOGMEM;
5556 goto error;
5557 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005558 }
Radek Krejci19332802016-07-29 10:39:46 +02005559 if (c_must) {
5560 notif->must = calloc(c_must, sizeof *notif->must);
5561 if (!notif->must) {
Michal Vasko0ea41032015-06-16 08:53:55 +02005562 LOGMEM;
5563 goto error;
5564 }
5565 }
Radek Krejcie534c132016-11-23 13:32:31 +01005566 if (c_ext) {
5567 notif->ext = calloc(c_ext, sizeof *notif->ext);
5568 if (!notif->ext) {
5569 LOGMEM;
5570 goto error;
5571 }
5572 }
Michal Vasko0ea41032015-06-16 08:53:55 +02005573
Radek Krejcie534c132016-11-23 13:32:31 +01005574 LY_TREE_FOR_SAFE(yin->child, next, sub) {
5575 if (strcmp(sub->ns->value, LY_NSYIN)) {
5576 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01005577 r = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01005578 retval->ext_size++;
5579 if (r) {
5580 goto error;
5581 }
5582 } else if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01005583 r = fill_yin_typedef(module, retval, sub, &notif->tpdf[notif->tpdf_size], unres);
5584 notif->tpdf_size++;
Michal Vasko0ea41032015-06-16 08:53:55 +02005585 if (r) {
5586 goto error;
5587 }
Radek Krejci96299152016-06-22 10:17:50 +02005588 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02005589 r = fill_yin_iffeature(retval, 0, sub, &notif->iffeature[notif->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005590 notif->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01005591 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005592 goto error;
5593 }
Radek Krejci19332802016-07-29 10:39:46 +02005594 } else if (!strcmp(sub->name, "must")) {
Radek Krejcifccd1442017-01-16 10:26:57 +01005595 r = fill_yin_must(module, sub, &notif->must[notif->must_size], unres);
Radek Krejci19332802016-07-29 10:39:46 +02005596 notif->must_size++;
Radek Krejci0b24d752015-07-02 15:02:27 +02005597 if (r) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02005598 goto error;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005599 }
Michal Vasko0ea41032015-06-16 08:53:55 +02005600 }
Michal Vasko0ea41032015-06-16 08:53:55 +02005601 }
5602
5603 /* last part - process data nodes */
5604 LY_TREE_FOR_SAFE(root.child, next, sub) {
5605 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005606 node = read_yin_container(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02005607 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005608 node = read_yin_leaflist(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02005609 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005610 node = read_yin_leaf(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02005611 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005612 node = read_yin_list(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02005613 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005614 node = read_yin_choice(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02005615 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02005616 node = read_yin_uses(module, retval, sub, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02005617 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005618 node = read_yin_grouping(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02005619 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005620 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, 0, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02005621 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005622 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02005623 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005624 if (!node) {
Michal Vasko0ea41032015-06-16 08:53:55 +02005625 goto error;
5626 }
Radek Krejci73adb602015-07-02 18:07:40 +02005627
Michal Vasko345da0a2015-12-02 10:35:55 +01005628 lyxml_free(module->ctx, sub);
Michal Vasko0ea41032015-06-16 08:53:55 +02005629 }
5630
Michal Vasko508a50d2016-09-07 14:50:33 +02005631 /* check XPath dependencies */
5632 if (notif->must_size && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
5633 goto error;
5634 }
5635
Michal Vasko0ea41032015-06-16 08:53:55 +02005636 return retval;
5637
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005638error:
Michal Vasko0ea41032015-06-16 08:53:55 +02005639
Michal Vaskod51d6ad2016-02-16 13:24:31 +01005640 lys_node_free(retval, NULL, 0);
Michal Vasko0ea41032015-06-16 08:53:55 +02005641 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005642 lyxml_free(module->ctx, root.child);
Michal Vasko0ea41032015-06-16 08:53:55 +02005643 }
5644
5645 return NULL;
5646}
5647
Michal Vasko0d343d12015-08-24 14:57:36 +02005648/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02005649static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02005650read_yin_rpc_action(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005651 struct unres_schema *unres)
Michal Vasko38d01f72015-06-15 09:41:06 +02005652{
Radek Krejcie0674f82015-06-15 13:58:51 +02005653 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005654 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02005655 struct lys_node *retval;
Michal Vasko44fb6382016-06-29 11:12:27 +02005656 struct lys_node_rpc_action *rpc;
Michal Vasko38d01f72015-06-15 09:41:06 +02005657 int r;
Radek Krejcie534c132016-11-23 13:32:31 +01005658 int c_tpdf = 0, c_ftrs = 0, c_input = 0, c_output = 0, c_ext = 0;
Michal Vasko38d01f72015-06-15 09:41:06 +02005659
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005660 if (!strcmp(yin->name, "action")) {
Radek Krejci1a31efe2016-07-29 11:04:16 +02005661 if (module->version < 2) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005662 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, parent, "action");
Michal Vaskobb174852016-07-25 11:00:21 +02005663 return NULL;
5664 }
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005665 for (node = parent; node; node = lys_parent(node)) {
Radek Krejcia3b19f72016-09-30 13:02:45 +02005666 if ((node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005667 || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys_size)) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005668 LOGVAL(LYE_INPAR, LY_VLOG_LYS, parent, strnodetype(node->nodetype), "action");
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005669 return NULL;
5670 }
5671 }
5672 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005673
Radek Krejcie0674f82015-06-15 13:58:51 +02005674 /* init */
5675 memset(&root, 0, sizeof root);
5676
Michal Vasko38d01f72015-06-15 09:41:06 +02005677 rpc = calloc(1, sizeof *rpc);
Michal Vasko253035f2015-12-17 16:58:13 +01005678 if (!rpc) {
5679 LOGMEM;
5680 return NULL;
5681 }
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005682 rpc->nodetype = (!strcmp(yin->name, "rpc") ? LYS_RPC : LYS_ACTION);
Radek Krejci76512572015-08-04 09:47:08 +02005683 rpc->prev = (struct lys_node *)rpc;
5684 retval = (struct lys_node *)rpc;
Michal Vasko38d01f72015-06-15 09:41:06 +02005685
Radek Krejci07d0fb92017-01-13 14:11:05 +01005686 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin, OPT_IDENT | OPT_MODULE, unres)) {
Michal Vasko38d01f72015-06-15 09:41:06 +02005687 goto error;
5688 }
5689
Radek Krejcia9544502015-08-14 08:24:29 +02005690 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
5691
Radek Krejcic189a952016-07-11 15:27:07 +02005692 /* insert the node into the schema tree */
5693 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
5694 goto error;
5695 }
5696
Michal Vasko38d01f72015-06-15 09:41:06 +02005697 /* process rpc's specific children */
5698 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01005699 if (strcmp(sub->ns->value, LY_NSYIN)) {
5700 /* extension */
5701 c_ext++;
Radek Krejci0d70c372015-07-02 16:23:10 +02005702 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01005703 } else if (!strcmp(sub->name, "input")) {
Pavol Vican3cb70c72016-09-05 11:32:52 +02005704 if (c_input) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005705 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02005706 goto error;
5707 }
Pavol Vican3cb70c72016-09-05 11:32:52 +02005708 c_input++;
Michal Vaskof3930de2015-10-22 12:03:59 +02005709 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02005710 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02005711 } else if (!strcmp(sub->name, "output")) {
Pavol Vican3cb70c72016-09-05 11:32:52 +02005712 if (c_output) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005713 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02005714 goto error;
5715 }
Pavol Vican3cb70c72016-09-05 11:32:52 +02005716 c_output++;
Michal Vaskof3930de2015-10-22 12:03:59 +02005717 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02005718 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02005719
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005720 /* data statements */
Michal Vasko38d01f72015-06-15 09:41:06 +02005721 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02005722 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02005723 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02005724
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005725 /* array counters */
Michal Vasko38d01f72015-06-15 09:41:06 +02005726 } else if (!strcmp(sub->name, "typedef")) {
5727 c_tpdf++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005728 } else if (!strcmp(sub->name, "if-feature")) {
5729 c_ftrs++;
Michal Vasko38d01f72015-06-15 09:41:06 +02005730 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005731 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02005732 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02005733 }
5734 }
5735
5736 /* middle part - process nodes with cardinality of 0..n except the data nodes */
5737 if (c_tpdf) {
5738 rpc->tpdf = calloc(c_tpdf, sizeof *rpc->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01005739 if (!rpc->tpdf) {
5740 LOGMEM;
5741 goto error;
5742 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005743 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005744 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005745 rpc->iffeature = calloc(c_ftrs, sizeof *rpc->iffeature);
5746 if (!rpc->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01005747 LOGMEM;
5748 goto error;
5749 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005750 }
Radek Krejcie534c132016-11-23 13:32:31 +01005751 if (c_ext) {
5752 rpc->ext = calloc(c_ext, sizeof *rpc->ext);
5753 if (!rpc->ext) {
5754 LOGMEM;
5755 goto error;
5756 }
5757 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005758
Radek Krejcie534c132016-11-23 13:32:31 +01005759 LY_TREE_FOR_SAFE(yin->child, next, sub) {
5760 if (strcmp(sub->ns->value, LY_NSYIN)) {
5761 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01005762 r = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01005763 retval->ext_size++;
5764 if (r) {
5765 goto error;
5766 }
5767 } else if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01005768 r = fill_yin_typedef(module, retval, sub, &rpc->tpdf[rpc->tpdf_size], unres);
5769 rpc->tpdf_size++;
Michal Vasko38d01f72015-06-15 09:41:06 +02005770 if (r) {
5771 goto error;
5772 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005773 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02005774 r = fill_yin_iffeature(retval, 0, sub, &rpc->iffeature[rpc->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005775 rpc->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01005776 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005777 goto error;
5778 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005779 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005780 }
5781
5782 /* last part - process data nodes */
5783 LY_TREE_FOR_SAFE(root.child, next, sub) {
5784 if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005785 node = read_yin_grouping(module, retval, sub, 0, unres);
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005786 } else if (!strcmp(sub->name, "input") || !strcmp(sub->name, "output")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005787 node = read_yin_input_output(module, retval, sub, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02005788 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005789 if (!node) {
Michal Vasko38d01f72015-06-15 09:41:06 +02005790 goto error;
5791 }
Radek Krejci73adb602015-07-02 18:07:40 +02005792
Michal Vasko345da0a2015-12-02 10:35:55 +01005793 lyxml_free(module->ctx, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02005794 }
5795
Michal Vasko38d01f72015-06-15 09:41:06 +02005796 return retval;
5797
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005798error:
Michal Vasko38d01f72015-06-15 09:41:06 +02005799
Michal Vaskod51d6ad2016-02-16 13:24:31 +01005800 lys_node_free(retval, NULL, 0);
Michal Vasko3a9abf82015-06-17 10:18:26 +02005801 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005802 lyxml_free(module->ctx, root.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02005803 }
5804
5805 return NULL;
5806}
5807
Michal Vasko0d343d12015-08-24 14:57:36 +02005808/* logs directly
5809 *
Radek Krejci74705112015-06-05 10:25:44 +02005810 * resolve - referenced grouping should be bounded to the namespace (resolved)
5811 * only when uses does not appear in grouping. In a case of grouping's uses,
5812 * we just get information but we do not apply augment or refine to it.
5813 */
Radek Krejci76512572015-08-04 09:47:08 +02005814static struct lys_node *
Radek Krejci3440cc52016-06-23 17:03:59 +02005815read_yin_uses(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct unres_schema *unres)
Radek Krejci74705112015-06-05 10:25:44 +02005816{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005817 struct lyxml_elem *sub, *next;
Radek Krejci76512572015-08-04 09:47:08 +02005818 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02005819 struct lys_node_uses *uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005820 const char *value;
Radek Krejcie534c132016-11-23 13:32:31 +01005821 int c_ref = 0, c_aug = 0, c_ftrs = 0, c_ext = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005822 int r;
Radek Krejci74705112015-06-05 10:25:44 +02005823
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005824 uses = calloc(1, sizeof *uses);
Michal Vasko253035f2015-12-17 16:58:13 +01005825 if (!uses) {
5826 LOGMEM;
5827 return NULL;
5828 }
Radek Krejci76512572015-08-04 09:47:08 +02005829 uses->nodetype = LYS_USES;
5830 uses->prev = (struct lys_node *)uses;
5831 retval = (struct lys_node *)uses;
Radek Krejci74705112015-06-05 10:25:44 +02005832
Radek Krejcia9544502015-08-14 08:24:29 +02005833 GETVAL(value, yin, "name");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005834 uses->name = lydict_insert(module->ctx, value, 0);
Radek Krejci106efc02015-06-10 14:36:27 +02005835
Radek Krejci07d0fb92017-01-13 14:11:05 +01005836 if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin, OPT_MODULE, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005837 goto error;
5838 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02005839
Radek Krejcia9544502015-08-14 08:24:29 +02005840 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
5841
Radek Krejcic189a952016-07-11 15:27:07 +02005842 /* insert the node into the schema tree */
5843 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
5844 goto error;
5845 }
5846
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005847 /* get other properties of uses */
Radek Krejcia9544502015-08-14 08:24:29 +02005848 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejcie534c132016-11-23 13:32:31 +01005849 if (strcmp(sub->ns->value, LY_NSYIN)) {
5850 /* extension */
5851 c_ext++;
Radek Krejci0d70c372015-07-02 16:23:10 +02005852 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01005853 } else if (!strcmp(sub->name, "refine")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005854 c_ref++;
5855 } else if (!strcmp(sub->name, "augment")) {
5856 c_aug++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005857 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci56e89772015-06-19 10:00:54 +02005858 c_ftrs++;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005859 } else if (!strcmp(sub->name, "when")) {
5860 if (uses->when) {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005861 LOGVAL(LYE_TOOMANY, LY_VLOG_LYS, retval, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005862 goto error;
5863 }
5864
Radek Krejci5323b492017-01-16 15:40:11 +01005865 uses->when = read_yin_when(module, sub, unres);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005866 if (!uses->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005867 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005868 goto error;
5869 }
Michal Vasko345da0a2015-12-02 10:35:55 +01005870 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005871 } else {
Radek Krejci6f3d6312016-11-04 17:23:18 +01005872 LOGVAL(LYE_INSTMT, LY_VLOG_LYS, retval, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005873 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005874 }
5875 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02005876
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005877 /* process properties with cardinality 0..n */
5878 if (c_ref) {
5879 uses->refine = calloc(c_ref, sizeof *uses->refine);
Michal Vasko253035f2015-12-17 16:58:13 +01005880 if (!uses->refine) {
5881 LOGMEM;
5882 goto error;
5883 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005884 }
5885 if (c_aug) {
5886 uses->augment = calloc(c_aug, sizeof *uses->augment);
Michal Vasko253035f2015-12-17 16:58:13 +01005887 if (!uses->augment) {
5888 LOGMEM;
5889 goto error;
5890 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005891 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005892 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005893 uses->iffeature = calloc(c_ftrs, sizeof *uses->iffeature);
5894 if (!uses->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01005895 LOGMEM;
5896 goto error;
5897 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005898 }
Radek Krejcie534c132016-11-23 13:32:31 +01005899 if (c_ext) {
5900 uses->ext = calloc(c_ext, sizeof *uses->ext);
5901 if (!uses->ext) {
5902 LOGMEM;
5903 goto error;
5904 }
5905 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02005906
Radek Krejcie534c132016-11-23 13:32:31 +01005907 LY_TREE_FOR_SAFE(yin->child, next, sub) {
5908 if (strcmp(sub->ns->value, LY_NSYIN)) {
5909 /* extension */
Radek Krejci2b999ac2017-01-18 16:22:12 +01005910 r = lyp_yin_fill_ext(retval, LYEXT_PAR_NODE, 0, 0, module, sub, &retval->ext, retval->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01005911 retval->ext_size++;
5912 if (r) {
5913 goto error;
5914 }
5915 } else if (!strcmp(sub->name, "refine")) {
Radek Krejci363bd4a2016-07-29 14:30:20 +02005916 r = fill_yin_refine(retval, sub, &uses->refine[uses->refine_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01005917 uses->refine_size++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005918 if (r) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02005919 goto error;
5920 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005921 } else if (!strcmp(sub->name, "augment")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01005922 r = fill_yin_augment(module, retval, sub, &uses->augment[uses->augment_size], unres);
5923 uses->augment_size++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005924 if (r) {
5925 goto error;
5926 }
5927 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02005928 r = fill_yin_iffeature(retval, 0, sub, &uses->iffeature[uses->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005929 uses->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01005930 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005931 goto error;
5932 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005933 }
5934 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02005935
Radek Krejci48464ed2016-03-17 15:44:09 +01005936 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005937 goto error;
5938 }
Radek Krejci74705112015-06-05 10:25:44 +02005939
Michal Vasko508a50d2016-09-07 14:50:33 +02005940 /* check XPath dependencies */
5941 if (uses->when && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
5942 goto error;
5943 }
5944
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005945 return retval;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02005946
5947error:
5948
Michal Vaskod51d6ad2016-02-16 13:24:31 +01005949 lys_node_free(retval, NULL, 0);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02005950
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005951 return NULL;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02005952}
5953
Michal Vasko0d343d12015-08-24 14:57:36 +02005954/* logs directly
5955 *
5956 * common code for yin_read_module() and yin_read_submodule()
5957 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005958static int
Radek Krejcic071c542016-01-27 14:57:51 +01005959read_sub_module(struct lys_module *module, struct lys_submodule *submodule, struct lyxml_elem *yin,
5960 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02005961{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005962 struct ly_ctx *ctx = module->ctx;
Radek Krejcie534c132016-11-23 13:32:31 +01005963 struct lyxml_elem *next, *child, *child2, root, grps, augs, exts;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005964 struct lys_node *node = NULL;
Radek Krejcic071c542016-01-27 14:57:51 +01005965 struct lys_module *trg;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005966 const char *value;
Pavol Vican974377b2016-03-23 00:38:53 +01005967 int i, r;
Radek Krejcic071c542016-01-27 14:57:51 +01005968 int version_flag = 0;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005969 /* counters */
Radek Krejcie534c132016-11-23 13:32:31 +01005970 int c_imp = 0, c_rev = 0, c_tpdf = 0, c_ident = 0, c_inc = 0, c_aug = 0, c_ftrs = 0, c_dev = 0;
5971 int c_ext = 0, c_extinst = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005972
Radek Krejcic071c542016-01-27 14:57:51 +01005973 /* to simplify code, store the module/submodule being processed as trg */
Michal Vaskofe7e5a72016-05-02 14:49:23 +02005974 trg = submodule ? (struct lys_module *)submodule : module;
Radek Krejcic071c542016-01-27 14:57:51 +01005975
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005976 /* init */
5977 memset(&root, 0, sizeof root);
5978 memset(&grps, 0, sizeof grps);
Michal Vasko2f7925f2015-10-21 15:06:56 +02005979 memset(&augs, 0, sizeof augs);
Radek Krejcie534c132016-11-23 13:32:31 +01005980 memset(&exts, 0, sizeof exts);
Radek Krejcie0674f82015-06-15 13:58:51 +02005981
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005982 /*
5983 * in the first run, we process elements with cardinality of 1 or 0..1 and
5984 * count elements with cardinality 0..n. Data elements (choices, containers,
5985 * leafs, lists, leaf-lists) are moved aside to be processed last, since we
5986 * need have all top-level and groupings already prepared at that time. In
5987 * the middle loop, we process other elements with carinality of 0..n since
5988 * we need to allocate arrays to store them.
5989 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005990 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejcie534c132016-11-23 13:32:31 +01005991 if (!child->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02005992 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01005993 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005994 continue;
Radek Krejcie534c132016-11-23 13:32:31 +01005995 } else if (strcmp(child->ns->value, LY_NSYIN)) {
5996 /* possible extension instance */
5997 lyxml_unlink_elem(module->ctx, child, 2);
5998 lyxml_add_child(module->ctx, &exts, child);
5999 c_extinst++;
6000 } else if (!submodule && !strcmp(child->name, "namespace")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006001 if (module->ns) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006002 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006003 goto error;
6004 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02006005 GETVAL(value, child, "uri");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006006 module->ns = lydict_insert(ctx, value, strlen(value));
Michal Vasko345da0a2015-12-02 10:35:55 +01006007 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01006008 } else if (!submodule && !strcmp(child->name, "prefix")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006009 if (module->prefix) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006010 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006011 goto error;
6012 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02006013 GETVAL(value, child, "value");
Radek Krejci48464ed2016-03-17 15:44:09 +01006014 if (lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006015 goto error;
6016 }
6017 module->prefix = lydict_insert(ctx, value, strlen(value));
Michal Vasko345da0a2015-12-02 10:35:55 +01006018 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01006019 } else if (submodule && !strcmp(child->name, "belongs-to")) {
6020 if (submodule->prefix) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006021 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006022 goto error;
6023 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02006024 GETVAL(value, child, "module");
Radek Krejci749190d2016-02-18 16:26:25 +01006025 if (!ly_strequal(value, submodule->belongsto->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006026 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006027 goto error;
6028 }
Radek Krejcif3886932015-06-04 17:36:06 +02006029
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006030 /* get the prefix substatement, start with checks */
Radek Krejci1d82ef62015-08-07 14:44:40 +02006031 if (!child->child) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006032 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006033 goto error;
Radek Krejci1d82ef62015-08-07 14:44:40 +02006034 } else if (strcmp(child->child->name, "prefix")) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006035 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006036 goto error;
Radek Krejci1d82ef62015-08-07 14:44:40 +02006037 } else if (child->child->next) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006038 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->child->next->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006039 goto error;
6040 }
6041 /* and now finally get the value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02006042 GETVAL(value, child->child, "value");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006043 /* check here differs from a generic prefix check, since this prefix
6044 * don't have to be unique
Michal Vasko38d01f72015-06-15 09:41:06 +02006045 */
Radek Krejci48464ed2016-03-17 15:44:09 +01006046 if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006047 goto error;
6048 }
Radek Krejcic071c542016-01-27 14:57:51 +01006049 submodule->prefix = lydict_insert(ctx, value, strlen(value));
Radek Krejci0af13872015-05-30 11:50:52 +02006050
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006051 /* we are done with belongs-to */
Michal Vasko345da0a2015-12-02 10:35:55 +01006052 lyxml_free(ctx, child);
Radek Krejcieb00f512015-07-01 16:44:58 +02006053
6054 /* counters (statements with n..1 cardinality) */
Radek Krejci1d82ef62015-08-07 14:44:40 +02006055 } else if (!strcmp(child->name, "import")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006056 c_imp++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02006057 } else if (!strcmp(child->name, "revision")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006058 c_rev++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02006059 } else if (!strcmp(child->name, "typedef")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006060 c_tpdf++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02006061 } else if (!strcmp(child->name, "identity")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006062 c_ident++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02006063 } else if (!strcmp(child->name, "include")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006064 c_inc++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02006065 } else if (!strcmp(child->name, "augment")) {
Radek Krejcif5be10f2015-06-16 13:29:36 +02006066 c_aug++;
Michal Vasko2f7925f2015-10-21 15:06:56 +02006067 /* keep augments separated, processed last */
Radek Krejcic071c542016-01-27 14:57:51 +01006068 lyxml_unlink_elem(ctx, child, 2);
6069 lyxml_add_child(ctx, &augs, child);
Michal Vasko2f7925f2015-10-21 15:06:56 +02006070
Radek Krejci1d82ef62015-08-07 14:44:40 +02006071 } else if (!strcmp(child->name, "feature")) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02006072 c_ftrs++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02006073
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006074 /* data statements */
Radek Krejci1d82ef62015-08-07 14:44:40 +02006075 } else if (!strcmp(child->name, "container") ||
6076 !strcmp(child->name, "leaf-list") ||
6077 !strcmp(child->name, "leaf") ||
6078 !strcmp(child->name, "list") ||
6079 !strcmp(child->name, "choice") ||
6080 !strcmp(child->name, "uses") ||
Michal Vasko7ffc3052015-10-21 15:05:56 +02006081 !strcmp(child->name, "anyxml") ||
6082 !strcmp(child->name, "rpc") ||
6083 !strcmp(child->name, "notification")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006084 lyxml_unlink_elem(ctx, child, 2);
6085 lyxml_add_child(ctx, &root, child);
Michal Vasko7ffc3052015-10-21 15:05:56 +02006086
Radek Krejci1d82ef62015-08-07 14:44:40 +02006087 } else if (!strcmp(child->name, "grouping")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006088 /* keep groupings separated and process them before other data statements */
Radek Krejcic071c542016-01-27 14:57:51 +01006089 lyxml_unlink_elem(ctx, child, 2);
6090 lyxml_add_child(ctx, &grps, child);
Radek Krejcida04f4a2015-05-21 12:54:09 +02006091
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006092 /* optional statements */
Radek Krejci1d82ef62015-08-07 14:44:40 +02006093 } else if (!strcmp(child->name, "description")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006094 if (trg->dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006095 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006096 goto error;
6097 }
Radek Krejcic071c542016-01-27 14:57:51 +01006098 trg->dsc = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01006099 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01006100 if (!trg->dsc) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006101 goto error;
6102 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02006103 } else if (!strcmp(child->name, "reference")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006104 if (trg->ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006105 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006106 goto error;
6107 }
Radek Krejcic071c542016-01-27 14:57:51 +01006108 trg->ref = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01006109 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01006110 if (!trg->ref) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006111 goto error;
6112 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02006113 } else if (!strcmp(child->name, "organization")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006114 if (trg->org) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006115 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006116 goto error;
6117 }
Radek Krejcic071c542016-01-27 14:57:51 +01006118 trg->org = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01006119 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01006120 if (!trg->org) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006121 goto error;
6122 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02006123 } else if (!strcmp(child->name, "contact")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006124 if (trg->contact) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006125 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006126 goto error;
6127 }
Radek Krejcic071c542016-01-27 14:57:51 +01006128 trg->contact = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01006129 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01006130 if (!trg->contact) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006131 goto error;
6132 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02006133 } else if (!strcmp(child->name, "yang-version")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006134 if (version_flag) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006135 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006136 goto error;
6137 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02006138 GETVAL(value, child, "value");
Michal Vasko88de3e42016-06-29 11:05:32 +02006139 if (strcmp(value, "1") && strcmp(value, "1.1")) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006140 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "yang-version");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006141 goto error;
6142 }
Radek Krejcic071c542016-01-27 14:57:51 +01006143 version_flag = 1;
Michal Vasko88de3e42016-06-29 11:05:32 +02006144 if (!strcmp(value, "1")) {
6145 if (submodule) {
6146 if (module->version > 1) {
6147 LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
6148 goto error;
6149 }
6150 } else {
6151 module->version = 1;
6152 }
6153 } else {
6154 if (submodule) {
Radek Krejci1a31efe2016-07-29 11:04:16 +02006155 if (module->version < 2) {
Michal Vasko88de3e42016-06-29 11:05:32 +02006156 LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
6157 goto error;
6158 }
6159 } else {
6160 module->version = 2;
6161 }
6162 }
6163
Michal Vasko345da0a2015-12-02 10:35:55 +01006164 lyxml_free(ctx, child);
Michal Vasko38d01f72015-06-15 09:41:06 +02006165
Radek Krejci1d82ef62015-08-07 14:44:40 +02006166 } else if (!strcmp(child->name, "extension")) {
Radek Krejcia1a6b762016-11-14 09:53:38 +09006167 c_ext++;
Radek Krejci5166a892015-07-02 16:44:24 +02006168
Radek Krejcia1a6b762016-11-14 09:53:38 +09006169 } else if (!strcmp(child->name, "deviation")) {
6170 c_dev++;
6171
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006172 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01006173 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006174 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006175 }
6176 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02006177
Radek Krejcic071c542016-01-27 14:57:51 +01006178 /* check for mandatory statements */
Radek Krejci6a1e2f42017-01-19 15:22:00 +01006179 if (submodule) {
6180 if (!submodule->prefix) {
6181 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "belongs-to", "submodule");
6182 goto error;
6183 }
6184 if (!version_flag) {
6185 /* check version compatibility with the main module */
6186 if (module->version > 1) {
6187 LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
6188 goto error;
6189 }
6190 }
6191 } else {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006192 if (!module->ns) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006193 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "namespace", "module");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006194 goto error;
6195 }
6196 if (!module->prefix) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006197 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", "module");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006198 goto error;
6199 }
6200 }
Radek Krejcibb3257d2015-05-21 23:03:51 +02006201
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006202 /* allocate arrays for elements with cardinality of 0..n */
6203 if (c_imp) {
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01006204 trg->imp = calloc(c_imp, sizeof *trg->imp);
Radek Krejcic071c542016-01-27 14:57:51 +01006205 if (!trg->imp) {
Michal Vasko253035f2015-12-17 16:58:13 +01006206 LOGMEM;
6207 goto error;
6208 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006209 }
6210 if (c_rev) {
Radek Krejcic071c542016-01-27 14:57:51 +01006211 trg->rev = calloc(c_rev, sizeof *trg->rev);
6212 if (!trg->rev) {
Michal Vasko253035f2015-12-17 16:58:13 +01006213 LOGMEM;
6214 goto error;
6215 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006216 }
6217 if (c_tpdf) {
Radek Krejcic071c542016-01-27 14:57:51 +01006218 trg->tpdf = calloc(c_tpdf, sizeof *trg->tpdf);
6219 if (!trg->tpdf) {
Michal Vasko253035f2015-12-17 16:58:13 +01006220 LOGMEM;
6221 goto error;
6222 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006223 }
6224 if (c_ident) {
Radek Krejcic071c542016-01-27 14:57:51 +01006225 trg->ident = calloc(c_ident, sizeof *trg->ident);
6226 if (!trg->ident) {
Michal Vasko253035f2015-12-17 16:58:13 +01006227 LOGMEM;
6228 goto error;
6229 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006230 }
6231 if (c_inc) {
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01006232 trg->inc = calloc(c_inc, sizeof *trg->inc);
Radek Krejcic071c542016-01-27 14:57:51 +01006233 if (!trg->inc) {
Michal Vasko253035f2015-12-17 16:58:13 +01006234 LOGMEM;
6235 goto error;
6236 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006237 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02006238 if (c_aug) {
Radek Krejcic071c542016-01-27 14:57:51 +01006239 trg->augment = calloc(c_aug, sizeof *trg->augment);
6240 if (!trg->augment) {
Michal Vasko253035f2015-12-17 16:58:13 +01006241 LOGMEM;
6242 goto error;
6243 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02006244 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02006245 if (c_ftrs) {
Radek Krejcic071c542016-01-27 14:57:51 +01006246 trg->features = calloc(c_ftrs, sizeof *trg->features);
6247 if (!trg->features) {
Michal Vasko253035f2015-12-17 16:58:13 +01006248 LOGMEM;
6249 goto error;
6250 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02006251 }
Radek Krejcieb00f512015-07-01 16:44:58 +02006252 if (c_dev) {
Radek Krejcic071c542016-01-27 14:57:51 +01006253 trg->deviation = calloc(c_dev, sizeof *trg->deviation);
6254 if (!trg->deviation) {
Michal Vasko253035f2015-12-17 16:58:13 +01006255 LOGMEM;
6256 goto error;
6257 }
Radek Krejcieb00f512015-07-01 16:44:58 +02006258 }
Radek Krejcia1a6b762016-11-14 09:53:38 +09006259 if (c_ext) {
6260 trg->extensions = calloc(c_ext, sizeof *trg->extensions);
6261 if (!trg->extensions) {
6262 LOGMEM;
6263 goto error;
6264 }
6265 }
Radek Krejcie534c132016-11-23 13:32:31 +01006266 if (c_extinst) {
6267 trg->ext = calloc(c_extinst, sizeof *trg->ext);
6268 if (!trg->ext) {
6269 LOGMEM;
6270 goto error;
6271 }
6272 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02006273
Michal Vasko2f7925f2015-10-21 15:06:56 +02006274 /* middle part - process nodes with cardinality of 0..n except the data nodes and augments */
6275 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02006276 if (!strcmp(child->name, "import")) {
Radek Krejcie534c132016-11-23 13:32:31 +01006277 r = fill_yin_import(trg, child, &trg->imp[trg->imp_size], unres);
Radek Krejcic071c542016-01-27 14:57:51 +01006278 trg->imp_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006279 if (r) {
6280 goto error;
6281 }
Radek Krejcice7fb782015-05-29 16:52:34 +02006282
Radek Krejci1d82ef62015-08-07 14:44:40 +02006283 } else if (!strcmp(child->name, "include")) {
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01006284 r = fill_yin_include(module, submodule, child, &trg->inc[trg->inc_size], unres);
6285 trg->inc_size++;
6286 if (r) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006287 goto error;
6288 }
Radek Krejcice7fb782015-05-29 16:52:34 +02006289
Radek Krejci1d82ef62015-08-07 14:44:40 +02006290 } else if (!strcmp(child->name, "revision")) {
6291 GETVAL(value, child, "date");
Radek Krejci48464ed2016-03-17 15:44:09 +01006292 if (lyp_check_date(value)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006293 goto error;
6294 }
Radek Krejcic071c542016-01-27 14:57:51 +01006295 memcpy(trg->rev[trg->rev_size].date, value, LY_REV_SIZE - 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006296 /* check uniqueness of the revision date - not required by RFC */
Radek Krejcic071c542016-01-27 14:57:51 +01006297 for (i = 0; i < trg->rev_size; i++) {
6298 if (!strcmp(value, trg->rev[i].date)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006299 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
6300 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Revision is not unique.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006301 }
6302 }
Radek Krejcice7fb782015-05-29 16:52:34 +02006303
Radek Krejci1d82ef62015-08-07 14:44:40 +02006304 LY_TREE_FOR(child->child, child2) {
6305 if (!strcmp(child2->name, "description")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006306 if (trg->rev[trg->rev_size].dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006307 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child2->name, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006308 goto error;
6309 }
Radek Krejcic071c542016-01-27 14:57:51 +01006310 trg->rev[trg->rev_size].dsc = read_yin_subnode(ctx, child2, "text");
6311 if (!trg->rev[trg->rev_size].dsc) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006312 goto error;
6313 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02006314 } else if (!strcmp(child2->name, "reference")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006315 if (trg->rev[trg->rev_size].ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006316 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child2->name, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006317 goto error;
6318 }
Radek Krejcic071c542016-01-27 14:57:51 +01006319 trg->rev[trg->rev_size].ref = read_yin_subnode(ctx, child2, "text");
6320 if (!trg->rev[trg->rev_size].ref) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006321 goto error;
6322 }
6323 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01006324 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child2->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006325 goto error;
6326 }
6327 }
Radek Krejcice7fb782015-05-29 16:52:34 +02006328
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006329 /* keep the latest revision at position 0 */
Radek Krejcic071c542016-01-27 14:57:51 +01006330 if (trg->rev_size && strcmp(trg->rev[trg->rev_size].date, trg->rev[0].date) > 0) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006331 /* switch their position */
Radek Krejcic071c542016-01-27 14:57:51 +01006332 value = strdup(trg->rev[0].date);
Michal Vasko253035f2015-12-17 16:58:13 +01006333 if (!value) {
6334 LOGMEM;
6335 goto error;
6336 }
Radek Krejcic071c542016-01-27 14:57:51 +01006337 memcpy(trg->rev[0].date, trg->rev[trg->rev_size].date, LY_REV_SIZE - 1);
6338 memcpy(trg->rev[trg->rev_size].date, value, LY_REV_SIZE - 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006339 free((char *)value);
Radek Krejcice7fb782015-05-29 16:52:34 +02006340
Radek Krejci749190d2016-02-18 16:26:25 +01006341 if (!ly_strequal(trg->rev[0].dsc, trg->rev[trg->rev_size].dsc, 1)) {
Radek Krejcic071c542016-01-27 14:57:51 +01006342 value = trg->rev[0].dsc;
6343 trg->rev[0].dsc = trg->rev[trg->rev_size].dsc;
6344 trg->rev[trg->rev_size].dsc = value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006345 }
Radek Krejcice7fb782015-05-29 16:52:34 +02006346
Radek Krejci749190d2016-02-18 16:26:25 +01006347 if (!ly_strequal(trg->rev[0].ref, trg->rev[trg->rev_size].ref, 1)) {
Radek Krejcic071c542016-01-27 14:57:51 +01006348 value = trg->rev[0].ref;
6349 trg->rev[0].ref = trg->rev[trg->rev_size].ref;
6350 trg->rev[trg->rev_size].ref = value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006351 }
6352 }
Radek Krejcice7fb782015-05-29 16:52:34 +02006353
Radek Krejcic071c542016-01-27 14:57:51 +01006354 trg->rev_size++;
Michal Vasko2f7925f2015-10-21 15:06:56 +02006355
Radek Krejci1d82ef62015-08-07 14:44:40 +02006356 } else if (!strcmp(child->name, "typedef")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006357 r = fill_yin_typedef(trg, NULL, child, &trg->tpdf[trg->tpdf_size], unres);
6358 trg->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006359 if (r) {
6360 goto error;
6361 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02006362
Radek Krejci1d82ef62015-08-07 14:44:40 +02006363 } else if (!strcmp(child->name, "identity")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006364 r = fill_yin_identity(trg, child, &trg->ident[trg->ident_size], unres);
6365 trg->ident_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006366 if (r) {
6367 goto error;
6368 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02006369
Radek Krejci1d82ef62015-08-07 14:44:40 +02006370 } else if (!strcmp(child->name, "feature")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006371 r = fill_yin_feature(trg, child, &trg->features[trg->features_size], unres);
6372 trg->features_size++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02006373 if (r) {
6374 goto error;
6375 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02006376
Radek Krejcia1a6b762016-11-14 09:53:38 +09006377 } else if (!strcmp(child->name, "extension")) {
Radek Krejcie534c132016-11-23 13:32:31 +01006378 r = fill_yin_extension(trg, child, &trg->extensions[trg->extensions_size], unres);
Radek Krejcia1a6b762016-11-14 09:53:38 +09006379 trg->extensions_size++;
6380 if (r) {
6381 goto error;
6382 }
6383
Radek Krejci1d82ef62015-08-07 14:44:40 +02006384 } else if (!strcmp(child->name, "deviation")) {
Radek Krejcic071c542016-01-27 14:57:51 +01006385 r = fill_yin_deviation(trg, child, &trg->deviation[trg->deviation_size], unres);
6386 trg->deviation_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02006387 if (r) {
6388 goto error;
6389 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006390 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006391 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02006392
Radek Krejcie534c132016-11-23 13:32:31 +01006393 /* process extension instances */
6394 LY_TREE_FOR_SAFE(exts.child, next, child) {
Radek Krejci2b999ac2017-01-18 16:22:12 +01006395 r = lyp_yin_fill_ext(trg, LYEXT_PAR_MODULE, 0, 0, module, child, &trg->ext, trg->ext_size, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01006396 trg->ext_size++;
6397 if (r) {
6398 goto error;
6399 }
6400 }
6401
Radek Krejcif5be10f2015-06-16 13:29:36 +02006402 /* process data nodes. Start with groupings to allow uses
Radek Krejcic071c542016-01-27 14:57:51 +01006403 * refer to them. Submodule's data nodes are stored in the
6404 * main module data tree.
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006405 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02006406 LY_TREE_FOR_SAFE(grps.child, next, child) {
Michal Vaskoe022a562016-09-27 14:24:15 +02006407 node = read_yin_grouping(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02006408 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006409 goto error;
6410 }
Radek Krejci74705112015-06-05 10:25:44 +02006411
Michal Vasko345da0a2015-12-02 10:35:55 +01006412 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006413 }
Radek Krejci74705112015-06-05 10:25:44 +02006414
Radek Krejcif5be10f2015-06-16 13:29:36 +02006415 /* parse data nodes, ... */
Radek Krejci1d82ef62015-08-07 14:44:40 +02006416 LY_TREE_FOR_SAFE(root.child, next, child) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02006417
Radek Krejci1d82ef62015-08-07 14:44:40 +02006418 if (!strcmp(child->name, "container")) {
Michal Vaskocd074222016-02-15 11:07:03 +01006419 node = read_yin_container(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02006420 } else if (!strcmp(child->name, "leaf-list")) {
Michal Vaskocd074222016-02-15 11:07:03 +01006421 node = read_yin_leaflist(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02006422 } else if (!strcmp(child->name, "leaf")) {
Michal Vaskocd074222016-02-15 11:07:03 +01006423 node = read_yin_leaf(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02006424 } else if (!strcmp(child->name, "list")) {
Michal Vaskocd074222016-02-15 11:07:03 +01006425 node = read_yin_list(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02006426 } else if (!strcmp(child->name, "choice")) {
Michal Vaskocd074222016-02-15 11:07:03 +01006427 node = read_yin_choice(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02006428 } else if (!strcmp(child->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02006429 node = read_yin_uses(trg, NULL, child, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02006430 } else if (!strcmp(child->name, "anyxml")) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02006431 node = read_yin_anydata(trg, NULL, child, LYS_ANYXML, 1, unres);
6432 } else if (!strcmp(child->name, "anydata")) {
6433 node = read_yin_anydata(trg, NULL, child, LYS_ANYDATA, 1, unres);
Michal Vasko7ffc3052015-10-21 15:05:56 +02006434 } else if (!strcmp(child->name, "rpc")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02006435 node = read_yin_rpc_action(trg, NULL, child, unres);
Michal Vasko7ffc3052015-10-21 15:05:56 +02006436 } else if (!strcmp(child->name, "notification")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02006437 node = read_yin_notif(trg, NULL, child, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006438 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02006439 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006440 goto error;
6441 }
Radek Krejci25d782a2015-05-22 15:03:23 +02006442
Michal Vasko345da0a2015-12-02 10:35:55 +01006443 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006444 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02006445
Michal Vasko2f7925f2015-10-21 15:06:56 +02006446 /* ... and finally augments (last, so we can augment our data, for instance) */
6447 LY_TREE_FOR_SAFE(augs.child, next, child) {
Radek Krejcic071c542016-01-27 14:57:51 +01006448 r = fill_yin_augment(trg, NULL, child, &trg->augment[trg->augment_size], unres);
6449 trg->augment_size++;
Radek Krejcif5be10f2015-06-16 13:29:36 +02006450
Michal Vasko2f7925f2015-10-21 15:06:56 +02006451 if (r) {
Radek Krejcif5be10f2015-06-16 13:29:36 +02006452 goto error;
6453 }
Michal Vasko345da0a2015-12-02 10:35:55 +01006454 lyxml_free(ctx, child);
Radek Krejcif5be10f2015-06-16 13:29:36 +02006455 }
6456
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006457 return EXIT_SUCCESS;
Radek Krejciefaeba32015-05-27 14:30:57 +02006458
6459error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006460 /* cleanup */
6461 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01006462 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006463 }
6464 while (grps.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01006465 lyxml_free(module->ctx, grps.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006466 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02006467 while (augs.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01006468 lyxml_free(module->ctx, augs.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02006469 }
Radek Krejcie534c132016-11-23 13:32:31 +01006470 while (exts.child) {
6471 lyxml_free(module->ctx, exts.child);
6472 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006473
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006474 return EXIT_FAILURE;
Radek Krejciefaeba32015-05-27 14:30:57 +02006475}
6476
Michal Vasko0d343d12015-08-24 14:57:36 +02006477/* logs directly */
Michal Vasko5a721fd2016-02-16 12:16:48 +01006478struct lys_submodule *
6479yin_read_submodule(struct lys_module *module, const char *data, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02006480{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006481 struct lyxml_elem *yin;
Michal Vasko5a721fd2016-02-16 12:16:48 +01006482 struct lys_submodule *submodule = NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006483 const char *value;
Radek Krejciefaeba32015-05-27 14:30:57 +02006484
Michal Vasko5a721fd2016-02-16 12:16:48 +01006485 assert(module->ctx);
Radek Krejciefaeba32015-05-27 14:30:57 +02006486
Radek Krejci722b0072016-02-01 17:09:45 +01006487 yin = lyxml_parse_mem(module->ctx, data, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006488 if (!yin) {
Michal Vasko5a721fd2016-02-16 12:16:48 +01006489 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006490 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006491
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006492 /* check root element */
6493 if (!yin->name || strcmp(yin->name, "submodule")) {
Radek Krejci48464ed2016-03-17 15:44:09 +01006494 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006495 goto error;
6496 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006497
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006498 GETVAL(value, yin, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +01006499 if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006500 goto error;
6501 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006502
Michal Vasko5a721fd2016-02-16 12:16:48 +01006503 submodule = calloc(1, sizeof *submodule);
6504 if (!submodule) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006505 LOGMEM;
6506 goto error;
6507 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006508
Michal Vasko5a721fd2016-02-16 12:16:48 +01006509 submodule->ctx = module->ctx;
6510 submodule->name = lydict_insert(submodule->ctx, value, strlen(value));
6511 submodule->type = 1;
6512 submodule->belongsto = module;
Radek Krejciefaeba32015-05-27 14:30:57 +02006513
Michal Vasko5a721fd2016-02-16 12:16:48 +01006514 LOGVRB("Reading submodule \"%s\".", submodule->name);
6515 if (read_sub_module(module, submodule, yin, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006516 goto error;
6517 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006518
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006519 /* cleanup */
Michal Vasko345da0a2015-12-02 10:35:55 +01006520 lyxml_free(module->ctx, yin);
Radek Krejciefaeba32015-05-27 14:30:57 +02006521
Michal Vasko5a721fd2016-02-16 12:16:48 +01006522 LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name);
Michal Vasko5a721fd2016-02-16 12:16:48 +01006523 return submodule;
Radek Krejciefaeba32015-05-27 14:30:57 +02006524
6525error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006526 /* cleanup */
Michal Vasko5a721fd2016-02-16 12:16:48 +01006527 unres_schema_free((struct lys_module *)submodule, &unres);
Michal Vasko345da0a2015-12-02 10:35:55 +01006528 lyxml_free(module->ctx, yin);
Michal Vasko9f258e42016-02-11 11:36:27 +01006529
Michal Vasko5a721fd2016-02-16 12:16:48 +01006530 if (!submodule) {
Radek Krejcidaea8212016-02-15 08:28:20 +01006531 LOGERR(ly_errno, "Submodule parsing failed.");
Michal Vasko5a721fd2016-02-16 12:16:48 +01006532 return NULL;
Radek Krejcidaea8212016-02-15 08:28:20 +01006533 }
6534
Michal Vasko5a721fd2016-02-16 12:16:48 +01006535 LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name);
Radek Krejcidaea8212016-02-15 08:28:20 +01006536
Michal Vasko9eb6dd02016-05-02 14:52:40 +02006537 lys_sub_module_remove_devs_augs((struct lys_module *)submodule);
6538 lys_submodule_module_data_free(submodule);
Michal Vasko5a721fd2016-02-16 12:16:48 +01006539 lys_submodule_free(submodule, NULL);
Michal Vasko5a721fd2016-02-16 12:16:48 +01006540 return NULL;
Radek Krejciefaeba32015-05-27 14:30:57 +02006541}
6542
Michal Vasko0d343d12015-08-24 14:57:36 +02006543/* logs directly */
Radek Krejcib8048692015-08-05 13:36:34 +02006544struct lys_module *
Radek Krejciff4874d2016-03-07 12:30:50 +01006545yin_read_module(struct ly_ctx *ctx, const char *data, const char *revision, int implement)
Radek Krejciefaeba32015-05-27 14:30:57 +02006546{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006547 struct lyxml_elem *yin;
Pavol Vicanc99b59e2016-03-22 15:46:39 +01006548 struct lys_module *module = NULL;
Radek Krejcic071c542016-01-27 14:57:51 +01006549 struct unres_schema *unres;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006550 const char *value;
Radek Krejciefaeba32015-05-27 14:30:57 +02006551
Radek Krejcic071c542016-01-27 14:57:51 +01006552 unres = calloc(1, sizeof *unres);
6553 if (!unres) {
6554 LOGMEM;
6555 return NULL;
6556 }
6557
Radek Krejci722b0072016-02-01 17:09:45 +01006558 yin = lyxml_parse_mem(ctx, data, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006559 if (!yin) {
Radek Krejcic071c542016-01-27 14:57:51 +01006560 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006561 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006562
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006563 /* check root element */
6564 if (!yin->name || strcmp(yin->name, "module")) {
Radek Krejci9a909bd2016-10-24 15:45:51 +02006565 if (ly_strequal("submodule", yin->name, 0)) {
Radek Krejci48cfa0f2016-11-08 19:18:34 +01006566 LOGVAL(LYE_SUBMODULE, LY_VLOG_NONE, NULL);
6567 } else {
6568 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, yin->name);
Radek Krejci9a909bd2016-10-24 15:45:51 +02006569 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006570 goto error;
6571 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006572
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006573 GETVAL(value, yin, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +01006574 if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006575 goto error;
6576 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006577
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006578 module = calloc(1, sizeof *module);
6579 if (!module) {
6580 LOGMEM;
6581 goto error;
6582 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006583
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006584 module->ctx = ctx;
6585 module->name = lydict_insert(ctx, value, strlen(value));
6586 module->type = 0;
Michal Vaskod8aa32d2015-07-24 11:50:01 +02006587 module->implemented = (implement ? 1 : 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02006588
Michal Vasko9f258e42016-02-11 11:36:27 +01006589 LOGVRB("Reading module \"%s\".", module->name);
Radek Krejcic071c542016-01-27 14:57:51 +01006590 if (read_sub_module(module, NULL, yin, unres)) {
6591 goto error;
6592 }
6593
6594 /* resolve rest of unres items */
6595 if (unres->count && resolve_unres_schema(module, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006596 goto error;
6597 }
Radek Krejciefaeba32015-05-27 14:30:57 +02006598
Radek Krejciff4874d2016-03-07 12:30:50 +01006599 if (revision) {
6600 /* check revision of the parsed model */
6601 if (!module->rev_size || strcmp(revision, module->rev[0].date)) {
Michal Vaskob24e7882016-03-21 16:13:25 +01006602 LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").",
6603 module->name, module->rev[0].date, revision);
Radek Krejciff4874d2016-03-07 12:30:50 +01006604 goto error;
6605 }
6606 }
6607
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01006608 /* check correctness of includes */
6609 if (lyp_check_include_missing(module)) {
6610 goto error;
6611 }
6612
Michal Vasko9eb6dd02016-05-02 14:52:40 +02006613 if (lyp_ctx_add_module(&module)) {
Pavol Vicanc99b59e2016-03-22 15:46:39 +01006614 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006615 }
Radek Krejci63a91a92015-07-29 13:31:04 +02006616
Radek Krejci27fe55e2016-09-13 17:13:35 +02006617 if (module->deviation_size && !module->implemented) {
6618 LOGVRB("Module \"%s\" includes deviations, changing its conformance to \"implement\".", module->name);
6619 /* deviations always causes target to be made implemented,
6620 * but augents and leafrefs not, so we have to apply them now */
6621 if (lys_set_implemented(module)) {
Michal Vasko26055752016-05-03 11:36:31 +02006622 goto error;
6623 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02006624 }
6625
Michal Vasko345da0a2015-12-02 10:35:55 +01006626 lyxml_free(ctx, yin);
Radek Krejcic071c542016-01-27 14:57:51 +01006627 unres_schema_free(NULL, &unres);
Michal Vasko9f258e42016-02-11 11:36:27 +01006628 LOGVRB("Module \"%s\" successfully parsed.", module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006629 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02006630
6631error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006632 /* cleanup */
Radek Krejcic071c542016-01-27 14:57:51 +01006633 lyxml_free(ctx, yin);
Radek Krejcib8c07b82016-02-12 11:11:55 +01006634 unres_schema_free(module, &unres);
6635
6636 if (!module) {
Radek Krejci48cfa0f2016-11-08 19:18:34 +01006637 if (ly_vecode != LYVE_SUBMODULE) {
6638 LOGERR(ly_errno, "Module parsing failed.");
6639 }
Radek Krejcib8c07b82016-02-12 11:11:55 +01006640 return NULL;
6641 }
6642
6643 LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name);
Radek Krejcida04f4a2015-05-21 12:54:09 +02006644
Michal Vasko9eb6dd02016-05-02 14:52:40 +02006645 lys_sub_module_remove_devs_augs(module);
Michal Vasko9f258e42016-02-11 11:36:27 +01006646 lys_free(module, NULL, 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02006647 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02006648}