blob: 10f727d82289297c85015325d6f8c029fc58a36a [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
47#define OPT_NACMEXT 0x20
Radek Krejcib8048692015-08-05 13:36:34 +020048static int read_yin_common(struct lys_module *, struct lys_node *, struct lys_node *, struct lyxml_elem *, int);
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);
Michal Vaskof02e3742015-08-05 16:27:02 +020072static struct lys_when *read_yin_when(struct lys_module *module, struct lyxml_elem *yin);
Radek Krejci74705112015-06-05 10:25:44 +020073
Michal Vasko0d343d12015-08-24 14:57:36 +020074/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020075static const char *
76read_yin_subnode(struct ly_ctx *ctx, struct lyxml_elem *node, const char *name)
Radek Krejcice7fb782015-05-29 16:52:34 +020077{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020078 int len;
Radek Krejcida04f4a2015-05-21 12:54:09 +020079
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020080 /* there should be <text> child */
81 if (!node->child || !node->child->name || strcmp(node->child->name, name)) {
Radek Krejci218436d2016-02-10 12:54:06 +010082 LOGERR(LY_EVALID, "Expected \"%s\" element in \"%s\" element.", name, node->name);
Radek Krejci48464ed2016-03-17 15:44:09 +010083 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, name, node->name);
Radek Krejci218436d2016-02-10 12:54:06 +010084 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020085 } else if (node->child->content) {
86 len = strlen(node->child->content);
87 return lydict_insert(ctx, node->child->content, len);
Radek Krejci218436d2016-02-10 12:54:06 +010088 } else {
89 return lydict_insert(ctx, "", 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020090 }
Radek Krejcida04f4a2015-05-21 12:54:09 +020091}
92
Michal Vasko0d343d12015-08-24 14:57:36 +020093/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020094static int
Radek Krejci9de2c042016-10-19 16:53:06 +020095fill_yin_iffeature(struct lys_node *parent, int parent_is_feature, struct lyxml_elem *yin, struct lys_iffeature *iffeat,
96 struct unres_schema *unres)
Michal Vasko1d337e12016-02-15 12:32:04 +010097{
98 int r;
99 const char *value;
100
101 GETVAL(value, yin, "name");
Michal Vasko97b32be2016-07-25 10:59:53 +0200102
103 if ((lys_node_module(parent)->version != 2) && ((value[0] == '(') || strchr(value, ' '))) {
104 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "if-feature");
Radek Krejci9ff0a922016-07-14 13:08:05 +0200105error:
Michal Vasko1d337e12016-02-15 12:32:04 +0100106 return EXIT_FAILURE;
107 }
108
Michal Vasko1d337e12016-02-15 12:32:04 +0100109 if (!(value = transform_schema2json(parent->module, value))) {
110 return EXIT_FAILURE;
111 }
112
Radek Krejci9de2c042016-10-19 16:53:06 +0200113 r = resolve_iffeature_compile(iffeat, value, parent, parent_is_feature, unres);
Michal Vasko1d337e12016-02-15 12:32:04 +0100114 lydict_remove(parent->module->ctx, value);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200115 if (r) {
116 return EXIT_FAILURE;
Michal Vasko1d337e12016-02-15 12:32:04 +0100117 }
118
Radek Krejci9ff0a922016-07-14 13:08:05 +0200119 return EXIT_SUCCESS;
Michal Vasko1d337e12016-02-15 12:32:04 +0100120}
121
122/* logs directly */
123static int
Michal Vaskof02e3742015-08-05 16:27:02 +0200124fill_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 +0200125{
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200126 struct lyxml_elem *node, *next;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200127 const char *value;
Radek Krejci018f1f52016-08-03 16:01:20 +0200128 int rc;
129 int c_ftrs = 0, c_base = 0;
Radek Krejci04581c62015-05-22 21:24:00 +0200130
Michal Vasko4cfcd252015-08-03 14:31:10 +0200131 GETVAL(value, yin, "name");
Michal Vaskoc94283a2015-10-29 09:07:20 +0100132 ident->name = value;
Michal Vasko4cfcd252015-08-03 14:31:10 +0200133
Radek Krejci76512572015-08-04 09:47:08 +0200134 if (read_yin_common(module, NULL, (struct lys_node *)ident, yin, OPT_IDENT | OPT_MODULE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200135 return EXIT_FAILURE;
136 }
Radek Krejci04581c62015-05-22 21:24:00 +0200137
Pavol Vicand6cda452016-07-13 15:08:29 +0200138 if (dup_identities_check(ident->name, module)) {
139 return EXIT_FAILURE;
140 }
141
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200142 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200143 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
144 /* garbage */
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200145 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200146 continue;
147 }
148
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200149 if (!strcmp(node->name, "base")) {
Radek Krejci018f1f52016-08-03 16:01:20 +0200150 if (c_base && (module->version < 2)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100151 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "base", "identity");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200152 return EXIT_FAILURE;
153 }
Radek Krejci018f1f52016-08-03 16:01:20 +0200154 c_base++;
Radek Krejciad73b6f2016-02-09 15:42:55 +0100155
Radek Krejci018f1f52016-08-03 16:01:20 +0200156 } else if ((module->version >= 2) && !strcmp(node->name, "if-feature")) {
157 c_ftrs++;
158
159 } else {
160 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name, "identity");
161 return EXIT_FAILURE;
162 }
163 }
164
165 if (c_base) {
166 ident->base_size = 0;
167 ident->base = calloc(c_base, sizeof *ident->base);
168 if (!ident->base) {
169 LOGMEM;
170 return EXIT_FAILURE;
171 }
172 }
173
174 if (c_ftrs) {
175 ident->iffeature = calloc(c_ftrs, sizeof *ident->iffeature);
176 if (!ident->iffeature) {
177 LOGMEM;
178 return EXIT_FAILURE;
179 }
180 }
181
182 LY_TREE_FOR(yin->child, node) {
183 if (!strcmp(node->name, "base")) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200184 GETVAL(value, node, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +0100185 value = transform_schema2json(module, value);
Michal Vaskoc94283a2015-10-29 09:07:20 +0100186 if (!value) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +0200187 return EXIT_FAILURE;
188 }
Michal Vaskoc94283a2015-10-29 09:07:20 +0100189
Radek Krejci48464ed2016-03-17 15:44:09 +0100190 if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, value) == -1) {
Michal Vaskoc94283a2015-10-29 09:07:20 +0100191 lydict_remove(module->ctx, value);
192 return EXIT_FAILURE;
193 }
194 lydict_remove(module->ctx, value);
Radek Krejci018f1f52016-08-03 16:01:20 +0200195 } else if (!strcmp(node->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +0200196 rc = fill_yin_iffeature((struct lys_node *)ident, 0, node, &ident->iffeature[ident->iffeature_size], unres);
Radek Krejci018f1f52016-08-03 16:01:20 +0200197 ident->iffeature_size++;
198 if (rc) {
199 return EXIT_FAILURE;
200 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200201 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200202 }
Radek Krejci04581c62015-05-22 21:24:00 +0200203
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200204 return EXIT_SUCCESS;
Michal Vasko2d851a92015-10-20 16:16:36 +0200205
206error:
207 return EXIT_FAILURE;
Radek Krejci04581c62015-05-22 21:24:00 +0200208}
209
Michal Vasko0d343d12015-08-24 14:57:36 +0200210/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200211static int
Radek Krejci1574a8d2015-08-03 14:16:52 +0200212read_restr_substmt(struct ly_ctx *ctx, struct lys_restr *restr, struct lyxml_elem *yin)
Radek Krejci41726f92015-06-19 13:11:05 +0200213{
Radek Krejci73adb602015-07-02 18:07:40 +0200214 struct lyxml_elem *child;
Radek Krejci461d1622015-06-30 14:06:28 +0200215 const char *value;
Radek Krejci41726f92015-06-19 13:11:05 +0200216
Radek Krejci73adb602015-07-02 18:07:40 +0200217 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200218 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
219 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200220 continue;
221 }
222
Radek Krejci41726f92015-06-19 13:11:05 +0200223 if (!strcmp(child->name, "description")) {
224 if (restr->dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100225 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200226 return EXIT_FAILURE;
227 }
228 restr->dsc = read_yin_subnode(ctx, child, "text");
229 if (!restr->dsc) {
230 return EXIT_FAILURE;
231 }
232 } else if (!strcmp(child->name, "reference")) {
233 if (restr->ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100234 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200235 return EXIT_FAILURE;
236 }
237 restr->ref = read_yin_subnode(ctx, child, "text");
238 if (!restr->ref) {
239 return EXIT_FAILURE;
240 }
241 } else if (!strcmp(child->name, "error-app-tag")) {
242 if (restr->eapptag) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100243 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200244 return EXIT_FAILURE;
245 }
Michal Vasko54e426f2015-07-07 15:38:02 +0200246 GETVAL(value, child, "value");
Radek Krejci461d1622015-06-30 14:06:28 +0200247 restr->eapptag = lydict_insert(ctx, value, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200248 } else if (!strcmp(child->name, "error-message")) {
249 if (restr->emsg) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100250 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200251 return EXIT_FAILURE;
252 }
253 restr->emsg = read_yin_subnode(ctx, child, "value");
254 if (!restr->emsg) {
255 return EXIT_FAILURE;
256 }
257 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100258 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200259 return EXIT_FAILURE;
260 }
Radek Krejci41726f92015-06-19 13:11:05 +0200261 }
262
263 return EXIT_SUCCESS;
Michal Vaskoc8ef47f2015-06-29 14:56:19 +0200264
265error:
266 return EXIT_FAILURE;
Radek Krejci41726f92015-06-19 13:11:05 +0200267}
268
Michal Vasko88c29542015-11-27 14:57:53 +0100269/* logs directly, returns EXIT_SUCCESS, EXIT_FAILURE, -1 */
270int
Radek Krejcib8048692015-08-05 13:36:34 +0200271fill_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 +0200272 int tpdftype, struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200273{
Michal Vasko0aee5c12016-06-17 14:27:26 +0200274 const char *value, *name;
Radek Krejci3a5501d2016-07-18 22:03:34 +0200275 struct lys_node *siter;
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200276 struct lyxml_elem *next, *next2, *node, *child;
Radek Krejci1574a8d2015-08-03 14:16:52 +0200277 struct lys_restr **restr;
Radek Krejci1c5890d2016-08-02 13:15:42 +0200278 struct lys_type_bit bit, *bits_sc = NULL;
279 struct lys_type_enum *enms_sc = NULL; /* shortcut */
Radek Krejcie663e012016-08-01 17:12:34 +0200280 struct lys_type *dertype;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200281 int i, j, rc, val_set, c_ftrs;
Radek Krejcidc008d72016-02-17 13:12:14 +0100282 int ret = -1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200283 int64_t v, v_;
Radek Krejci994b6f62015-06-18 16:47:27 +0200284 int64_t p, p_;
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200285 size_t len;
286 char *buf, modifier;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200287
Radek Krejci8de7b0f2015-07-02 11:43:42 +0200288 GETVAL(value, yin, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +0100289 value = transform_schema2json(module, value);
Michal Vasko1dca6882015-10-22 14:29:42 +0200290 if (!value) {
291 goto error;
Michal Vaskoa5835e92015-10-20 15:07:39 +0200292 }
Michal Vaskob362b4c2015-10-20 15:15:46 +0200293
294 i = parse_identifier(value);
295 if (i < 1) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100296 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, value[-i], &value[-i]);
Michal Vasko88c29542015-11-27 14:57:53 +0100297 lydict_remove(module->ctx, value);
Michal Vaskob362b4c2015-10-20 15:15:46 +0200298 goto error;
299 }
300 /* module name */
Radek Krejci225376f2016-02-16 17:36:22 +0100301 name = value;
Michal Vaskob362b4c2015-10-20 15:15:46 +0200302 if (value[i]) {
303 type->module_name = lydict_insert(module->ctx, value, i);
Radek Krejci225376f2016-02-16 17:36:22 +0100304 name += i;
305 if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100306 LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, name[0], name);
Michal Vasko88c29542015-11-27 14:57:53 +0100307 lydict_remove(module->ctx, value);
Michal Vaskob362b4c2015-10-20 15:15:46 +0200308 goto error;
309 }
Michal Vasko5b61c6d2016-06-06 15:15:30 +0200310 /* name is in dictionary, but moved */
Radek Krejci225376f2016-02-16 17:36:22 +0100311 ++name;
Michal Vaskob362b4c2015-10-20 15:15:46 +0200312 }
Michal Vaskoa5835e92015-10-20 15:07:39 +0200313
Radek Krejci225376f2016-02-16 17:36:22 +0100314 rc = resolve_superior_type(name, type->module_name, module, parent, &type->der);
Michal Vaskof7eee892015-08-24 15:03:11 +0200315 if (rc == -1) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100316 LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, type->module_name);
Michal Vasko5b61c6d2016-06-06 15:15:30 +0200317 lydict_remove(module->ctx, value);
Michal Vaskof7eee892015-08-24 15:03:11 +0200318 goto error;
Michal Vasko88c29542015-11-27 14:57:53 +0100319
320 /* the type could not be resolved or it was resolved to an unresolved typedef */
Michal Vaskof7eee892015-08-24 15:03:11 +0200321 } else if (rc == EXIT_FAILURE) {
Michal Vasko01c6fd22016-05-20 11:43:05 +0200322 LOGVAL(LYE_NORESOLV, LY_VLOG_NONE, NULL, "type", name);
Michal Vasko5b61c6d2016-06-06 15:15:30 +0200323 lydict_remove(module->ctx, value);
Radek Krejcidc008d72016-02-17 13:12:14 +0100324 ret = EXIT_FAILURE;
325 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200326 }
Michal Vasko5b61c6d2016-06-06 15:15:30 +0200327 lydict_remove(module->ctx, value);
Radek Krejcic13db382016-08-16 10:52:42 +0200328
Radek Krejci9b6aad22016-09-20 15:55:51 +0200329 if (type->base == LY_TYPE_ERR) {
Radek Krejcic13db382016-08-16 10:52:42 +0200330 /* resolved type in grouping, decrease the grouping's nacm number to indicate that one less
Radek Krejci9b6aad22016-09-20 15:55:51 +0200331 * 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 +0200332 for (siter = parent; siter && (siter->nodetype != LYS_GROUPING); siter = lys_parent(siter));
333 if (siter) {
334 if (!((struct lys_node_grp *)siter)->nacm) {
335 LOGINT;
336 goto error;
337 }
338 ((struct lys_node_grp *)siter)->nacm--;
339 } else {
340 LOGINT;
341 goto error;
342 }
343 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200344 type->base = type->der->type.base;
Radek Krejci25d782a2015-05-22 15:03:23 +0200345
Radek Krejcicf509982015-12-15 09:22:44 +0100346 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +0100347 if (lyp_check_status(type->parent->flags, type->parent->module, type->parent->name,
Radek Krejci48464ed2016-03-17 15:44:09 +0100348 type->der->flags, type->der->module, type->der->name, parent)) {
Radek Krejcicf509982015-12-15 09:22:44 +0100349 return -1;
350 }
351
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200352 switch (type->base) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200353 case LY_TYPE_BITS:
Radek Krejci994b6f62015-06-18 16:47:27 +0200354 /* RFC 6020 9.7.4 - bit */
355
356 /* get bit specifications, at least one must be present */
357 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200358 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
359 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100360 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200361 continue;
362 }
363
Radek Krejci994b6f62015-06-18 16:47:27 +0200364 if (!strcmp(node->name, "bit")) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200365 type->info.bits.count++;
Radek Krejci41726f92015-06-19 13:11:05 +0200366 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100367 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci69794c62016-08-02 11:01:21 +0200368 type->info.bits.count = 0;
Radek Krejci41726f92015-06-19 13:11:05 +0200369 goto error;
Radek Krejci994b6f62015-06-18 16:47:27 +0200370 }
371 }
Radek Krejcie663e012016-08-01 17:12:34 +0200372 dertype = &type->der->type;
373 if (!dertype->der) {
374 if (!type->info.bits.count) {
375 /* type is derived directly from buit-in bits type and bit statement is required */
376 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "bit", "type");
377 goto error;
378 }
379 } else {
380 for (; !dertype->info.enums.count; dertype = &dertype->der->type);
381 if (module->version < 2 && type->info.bits.count) {
382 /* type is not directly derived from buit-in bits type and bit statement is prohibited,
383 * since YANG 1.1 the bit statements can be used to restrict the base bits type */
384 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "bit");
Radek Krejci69794c62016-08-02 11:01:21 +0200385 type->info.bits.count = 0;
Radek Krejcie663e012016-08-01 17:12:34 +0200386 goto error;
387 }
Radek Krejciac781922015-07-09 15:35:14 +0200388 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200389
390 type->info.bits.bit = calloc(type->info.bits.count, sizeof *type->info.bits.bit);
Michal Vasko253035f2015-12-17 16:58:13 +0100391 if (!type->info.bits.bit) {
392 LOGMEM;
393 goto error;
394 }
Radek Krejci73adb602015-07-02 18:07:40 +0200395 p = 0;
396 i = -1;
397 LY_TREE_FOR(yin->child, next) {
398 i++;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200399 c_ftrs = 0;
Radek Krejci73adb602015-07-02 18:07:40 +0200400
401 GETVAL(value, next, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +0100402 if (lyp_check_identifier(value, LY_IDENT_SIMPLE, NULL, NULL)) {
Michal Vasko2d26a022015-12-07 09:27:21 +0100403 goto error;
404 }
405
Radek Krejci994b6f62015-06-18 16:47:27 +0200406 type->info.bits.bit[i].name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci76512572015-08-04 09:47:08 +0200407 if (read_yin_common(module, NULL, (struct lys_node *)&type->info.bits.bit[i], next, 0)) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200408 type->info.bits.count = i + 1;
409 goto error;
410 }
411
Radek Krejcie663e012016-08-01 17:12:34 +0200412 if (!dertype->der) { /* directly derived type from bits built-in type */
413 /* check the name uniqueness */
414 for (j = 0; j < i; j++) {
415 if (!strcmp(type->info.bits.bit[j].name, type->info.bits.bit[i].name)) {
416 LOGVAL(LYE_BITS_DUPNAME, LY_VLOG_NONE, NULL, type->info.bits.bit[i].name);
417 type->info.bits.count = i + 1;
418 goto error;
419 }
420 }
421 } else {
422 /* restricted bits type - the name MUST be used in the base type */
423 bits_sc = dertype->info.bits.bit;
424 for (j = 0; j < dertype->info.bits.count; j++) {
425 if (ly_strequal(bits_sc[j].name, value, 1)) {
426 break;
427 }
428 }
429 if (j == dertype->info.bits.count) {
430 LOGVAL(LYE_BITS_INNAME, LY_VLOG_NONE, NULL, value);
Radek Krejci994b6f62015-06-18 16:47:27 +0200431 type->info.bits.count = i + 1;
432 goto error;
433 }
434 }
435
Radek Krejcie663e012016-08-01 17:12:34 +0200436
Radek Krejci0d70c372015-07-02 16:23:10 +0200437 p_ = -1;
Radek Krejci73adb602015-07-02 18:07:40 +0200438 LY_TREE_FOR(next->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200439 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
440 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200441 continue;
Radek Krejci994b6f62015-06-18 16:47:27 +0200442 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200443
Radek Krejci0d70c372015-07-02 16:23:10 +0200444 if (!strcmp(node->name, "position")) {
Radek Krejcie663e012016-08-01 17:12:34 +0200445 if (p_ != -1) {
446 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, next->name);
447 type->info.bits.count = i + 1;
448 goto error;
449 }
450
Radek Krejci0d70c372015-07-02 16:23:10 +0200451 GETVAL(value, node, "value");
Radek Krejci7511f402015-07-10 09:56:30 +0200452 p_ = strtoll(value, NULL, 10);
Radek Krejci0d70c372015-07-02 16:23:10 +0200453
454 /* range check */
Radek Krejcib8ca1082015-07-10 11:24:11 +0200455 if (p_ < 0 || p_ > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100456 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "bit/position");
Radek Krejci0d70c372015-07-02 16:23:10 +0200457 type->info.bits.count = i + 1;
458 goto error;
459 }
460 type->info.bits.bit[i].pos = (uint32_t)p_;
461
Radek Krejcie663e012016-08-01 17:12:34 +0200462 if (!dertype->der) { /* directly derived type from bits built-in type */
463 /* keep the highest enum value for automatic increment */
464 if (type->info.bits.bit[i].pos >= p) {
465 p = type->info.bits.bit[i].pos;
466 p++;
467 } else {
468 /* check that the value is unique */
469 for (j = 0; j < i; j++) {
470 if (type->info.bits.bit[j].pos == type->info.bits.bit[i].pos) {
471 LOGVAL(LYE_BITS_DUPVAL, LY_VLOG_NONE, NULL,
472 type->info.bits.bit[i].pos, type->info.bits.bit[i].name,
473 type->info.bits.bit[j].name);
474 type->info.bits.count = i + 1;
475 goto error;
476 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200477 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200478 }
479 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200480
481 } else if ((module->version >= 2) && !strcmp(node->name, "if-feature")) {
482 c_ftrs++;
Radek Krejci0d70c372015-07-02 16:23:10 +0200483 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100484 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200485 goto error;
Radek Krejci994b6f62015-06-18 16:47:27 +0200486 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200487 }
Radek Krejcie663e012016-08-01 17:12:34 +0200488
489 if (!dertype->der) { /* directly derived type from bits built-in type */
490 if (p_ == -1) {
491 /* assign value automatically */
492 if (p > UINT32_MAX) {
493 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "4294967295", "bit/position");
494 type->info.bits.count = i + 1;
495 goto error;
496 }
497 type->info.bits.bit[i].pos = (uint32_t)p;
498 type->info.bits.bit[i].flags |= LYS_AUTOASSIGNED;
499 p++;
Radek Krejci994b6f62015-06-18 16:47:27 +0200500 }
Radek Krejcie663e012016-08-01 17:12:34 +0200501 } else { /* restricted bits type */
502 if (p_ == -1) {
503 /* automatically assign position from base type */
504 type->info.bits.bit[i].pos = bits_sc[j].pos;
505 type->info.bits.bit[i].flags |= LYS_AUTOASSIGNED;
506 } else {
507 /* check that the assigned position corresponds to the original
508 * position of the bit in the base type */
509 if (p_ != bits_sc[j].pos) {
510 /* p_ - assigned position in restricted bits
511 * bits_sc[j].pos - position assigned to the corresponding bit (detected above) in base type */
512 LOGVAL(LYE_BITS_INVAL, LY_VLOG_NONE, NULL, type->info.bits.bit[i].pos,
Radek Krejci541a45d2016-08-02 13:12:07 +0200513 type->info.bits.bit[i].name, bits_sc[j].pos);
Radek Krejcie663e012016-08-01 17:12:34 +0200514 type->info.bits.count = i + 1;
515 goto error;
516 }
517 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200518 }
Radek Krejci9a1b95a2015-07-09 15:32:21 +0200519
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200520 /* if-features */
521 if (c_ftrs) {
522 bits_sc = &type->info.bits.bit[i];
523 bits_sc->iffeature = calloc(c_ftrs, sizeof *bits_sc->iffeature);
524 if (!bits_sc->iffeature) {
525 LOGMEM;
Radek Krejci994b6f62015-06-18 16:47:27 +0200526 type->info.bits.count = i + 1;
527 goto error;
528 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200529
530 LY_TREE_FOR(next->child, node) {
531 if (!strcmp(node->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +0200532 rc = fill_yin_iffeature((struct lys_node *)type->parent, 0, node,
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200533 &bits_sc->iffeature[bits_sc->iffeature_size], unres);
534 bits_sc->iffeature_size++;
535 if (rc) {
536 type->info.bits.count = i + 1;
537 goto error;
538 }
539 }
540 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200541 }
Radek Krejci9a1b95a2015-07-09 15:32:21 +0200542
543 /* keep them ordered by position */
544 j = i;
545 while (j && type->info.bits.bit[j - 1].pos > type->info.bits.bit[j].pos) {
546 /* switch them */
547 memcpy(&bit, &type->info.bits.bit[j], sizeof bit);
548 memcpy(&type->info.bits.bit[j], &type->info.bits.bit[j - 1], sizeof bit);
549 memcpy(&type->info.bits.bit[j - 1], &bit, sizeof bit);
550 j--;
551 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200552 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200553 break;
Radek Krejci25d782a2015-05-22 15:03:23 +0200554
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200555 case LY_TYPE_DEC64:
Radek Krejcif9401c32015-06-26 16:47:36 +0200556 /* RFC 6020 9.2.4 - range and 9.3.4 - fraction-digits */
Radek Krejci73adb602015-07-02 18:07:40 +0200557 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200558 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
559 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200560 continue;
561 }
562
Radek Krejcif9401c32015-06-26 16:47:36 +0200563 if (!strcmp(node->name, "range")) {
564 if (type->info.dec64.range) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100565 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200566 goto error;
567 }
568
569 GETVAL(value, node, "value");
Radek Krejcif9401c32015-06-26 16:47:36 +0200570 type->info.dec64.range = calloc(1, sizeof *type->info.dec64.range);
Michal Vasko253035f2015-12-17 16:58:13 +0100571 if (!type->info.dec64.range) {
572 LOGMEM;
573 goto error;
574 }
Radek Krejcif9401c32015-06-26 16:47:36 +0200575 type->info.dec64.range->expr = lydict_insert(module->ctx, value, 0);
576
577 /* get possible substatements */
578 if (read_restr_substmt(module->ctx, type->info.dec64.range, node)) {
579 goto error;
580 }
581 } else if (!strcmp(node->name, "fraction-digits")) {
582 if (type->info.dec64.dig) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100583 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200584 goto error;
585 }
586 GETVAL(value, node, "value");
587 v = strtol(value, NULL, 10);
588
589 /* range check */
590 if (v < 1 || v > 18) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100591 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200592 goto error;
593 }
594 type->info.dec64.dig = (uint8_t)v;
Radek Krejci8c3b4b62016-06-17 14:32:12 +0200595 type->info.dec64.div = 10;
596 for (i = 1; i < v; i++) {
597 type->info.dec64.div *= 10;
598 }
Radek Krejcif9401c32015-06-26 16:47:36 +0200599 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100600 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200601 goto error;
602 }
Radek Krejcif9401c32015-06-26 16:47:36 +0200603 }
604
605 /* mandatory sub-statement(s) check */
606 if (!type->info.dec64.dig && !type->der->type.der) {
607 /* decimal64 type directly derived from built-in type requires fraction-digits */
Radek Krejci48464ed2016-03-17 15:44:09 +0100608 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "fraction-digits", "type");
Radek Krejcif9401c32015-06-26 16:47:36 +0200609 goto error;
610 }
Radek Krejci7511f402015-07-10 09:56:30 +0200611 if (type->info.dec64.dig && type->der->type.der) {
612 /* type is not directly derived from buit-in type and fraction-digits statement is prohibited */
Radek Krejci48464ed2016-03-17 15:44:09 +0100613 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "fraction-digits");
Radek Krejci7511f402015-07-10 09:56:30 +0200614 goto error;
615 }
Radek Krejci4800f652016-09-08 14:02:52 +0200616
617 /* copy fraction-digits specification from parent type for easier internal use */
618 if (type->der->type.der) {
619 type->info.dec64.dig = type->der->type.info.dec64.dig;
620 type->info.dec64.div = type->der->type.info.dec64.div;
621 }
622
Pavol Vican3c8ee2b2016-09-29 13:18:13 +0200623 if (type->info.dec64.range && lyp_check_length_range(type->info.dec64.range->expr, type)) {
624 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "range");
625 goto error;
626 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200627 break;
Radek Krejci25d782a2015-05-22 15:03:23 +0200628
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200629 case LY_TYPE_ENUM:
Radek Krejci994b6f62015-06-18 16:47:27 +0200630 /* RFC 6020 9.6 - enum */
Radek Krejci25d782a2015-05-22 15:03:23 +0200631
Radek Krejci994b6f62015-06-18 16:47:27 +0200632 /* get enum specifications, at least one must be present */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200633 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200634 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
635 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100636 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200637 continue;
638 }
639
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200640 if (!strcmp(node->name, "enum")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200641 type->info.enums.count++;
Radek Krejci5fbc9162015-06-19 14:11:11 +0200642 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100643 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci69794c62016-08-02 11:01:21 +0200644 type->info.enums.count = 0;
Radek Krejci5fbc9162015-06-19 14:11:11 +0200645 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200646 }
647 }
Radek Krejcie663e012016-08-01 17:12:34 +0200648 dertype = &type->der->type;
649 if (!dertype->der) {
650 if (!type->info.enums.count) {
651 /* type is derived directly from buit-in enumeartion type and enum statement is required */
652 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "enum", "type");
653 goto error;
654 }
655 } else {
656 for (; !dertype->info.enums.count; dertype = &dertype->der->type);
657 if (module->version < 2 && type->info.enums.count) {
658 /* type is not directly derived from built-in enumeration type and enum statement is prohibited
659 * in YANG 1.0, since YANG 1.1 enum statements can be used to restrict the base enumeration type */
660 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "enum");
Radek Krejci69794c62016-08-02 11:01:21 +0200661 type->info.enums.count = 0;
Radek Krejcie663e012016-08-01 17:12:34 +0200662 goto error;
663 }
Radek Krejcib54bcb12015-07-10 13:16:40 +0200664 }
Radek Krejci25d782a2015-05-22 15:03:23 +0200665
Radek Krejci1574a8d2015-08-03 14:16:52 +0200666 type->info.enums.enm = calloc(type->info.enums.count, sizeof *type->info.enums.enm);
Michal Vasko253035f2015-12-17 16:58:13 +0100667 if (!type->info.enums.enm) {
668 LOGMEM;
669 goto error;
670 }
Radek Krejcifc8d8322016-06-24 11:23:23 +0200671
Radek Krejcie663e012016-08-01 17:12:34 +0200672 v = 0;
Radek Krejci73adb602015-07-02 18:07:40 +0200673 i = -1;
674 LY_TREE_FOR(yin->child, next) {
675 i++;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200676 c_ftrs = 0;
Radek Krejci73adb602015-07-02 18:07:40 +0200677
678 GETVAL(value, next, "name");
Michal Vasko0fb58e92015-12-07 09:27:44 +0100679 if (!value[0]) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100680 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum name");
681 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Enum name must not be empty.");
Michal Vasko0fb58e92015-12-07 09:27:44 +0100682 goto error;
683 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200684 type->info.enums.enm[i].name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci76512572015-08-04 09:47:08 +0200685 if (read_yin_common(module, NULL, (struct lys_node *)&type->info.enums.enm[i], next, 0)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200686 type->info.enums.count = i + 1;
687 goto error;
688 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200689
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200690 /* the assigned name MUST NOT have any leading or trailing whitespace characters */
Radek Krejci1574a8d2015-08-03 14:16:52 +0200691 value = type->info.enums.enm[i].name;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200692 if (isspace(value[0]) || isspace(value[strlen(value) - 1])) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100693 LOGVAL(LYE_ENUM_WS, LY_VLOG_NONE, NULL, value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200694 type->info.enums.count = i + 1;
695 goto error;
696 }
Radek Krejci25d782a2015-05-22 15:03:23 +0200697
Radek Krejcie663e012016-08-01 17:12:34 +0200698 if (!dertype->der) { /* directly derived type from enumeration built-in type */
699 /* check the name uniqueness */
700 for (j = 0; j < i; j++) {
701 if (ly_strequal(type->info.enums.enm[j].name, value, 1)) {
702 LOGVAL(LYE_ENUM_DUPNAME, LY_VLOG_NONE, NULL, value);
703 type->info.enums.count = i + 1;
704 goto error;
705 }
706 }
707 } else {
708 /* restricted enumeration type - the name MUST be used in the base type */
709 enms_sc = dertype->info.enums.enm;
710 for (j = 0; j < dertype->info.enums.count; j++) {
711 if (ly_strequal(enms_sc[j].name, value, 1)) {
712 break;
713 }
714 }
715 if (j == dertype->info.enums.count) {
716 LOGVAL(LYE_ENUM_INNAME, LY_VLOG_NONE, NULL, value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200717 type->info.enums.count = i + 1;
718 goto error;
719 }
720 }
Radek Krejci04581c62015-05-22 21:24:00 +0200721
Radek Krejcie663e012016-08-01 17:12:34 +0200722 val_set = 0;
Radek Krejci73adb602015-07-02 18:07:40 +0200723 LY_TREE_FOR(next->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200724 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
725 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200726 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200727 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200728
Radek Krejci0d70c372015-07-02 16:23:10 +0200729 if (!strcmp(node->name, "value")) {
Radek Krejcie663e012016-08-01 17:12:34 +0200730 if (val_set) {
731 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, next->name);
732 type->info.enums.count = i + 1;
733 goto error;
734 }
735
Radek Krejci0d70c372015-07-02 16:23:10 +0200736 GETVAL(value, node, "value");
Radek Krejci7511f402015-07-10 09:56:30 +0200737 v_ = strtoll(value, NULL, 10);
Radek Krejci0d70c372015-07-02 16:23:10 +0200738
739 /* range check */
740 if (v_ < INT32_MIN || v_ > INT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100741 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "enum/value");
Radek Krejci0d70c372015-07-02 16:23:10 +0200742 type->info.enums.count = i + 1;
743 goto error;
744 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200745 type->info.enums.enm[i].value = v_;
Radek Krejci0d70c372015-07-02 16:23:10 +0200746
Radek Krejcie663e012016-08-01 17:12:34 +0200747 if (!dertype->der) { /* directly derived type from enumeration built-in type */
Pavol Vican5de389c2016-08-30 08:55:15 +0200748 if (!i) {
749 /* change value, which is assigned automatically, if first enum has value. */
Radek Krejcie663e012016-08-01 17:12:34 +0200750 v = type->info.enums.enm[i].value;
751 v++;
752 } else {
Pavol Vican5de389c2016-08-30 08:55:15 +0200753 /* keep the highest enum value for automatic increment */
754 if (type->info.enums.enm[i].value >= v) {
755 v = type->info.enums.enm[i].value;
756 v++;
757 } else {
758 /* check that the value is unique */
759 for (j = 0; j < i; j++) {
760 if (type->info.enums.enm[j].value == type->info.enums.enm[i].value) {
761 LOGVAL(LYE_ENUM_DUPVAL, LY_VLOG_NONE, NULL,
762 type->info.enums.enm[i].value, type->info.enums.enm[i].name,
763 type->info.enums.enm[j].name);
764 type->info.enums.count = i + 1;
765 goto error;
766 }
Radek Krejcie663e012016-08-01 17:12:34 +0200767 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200768 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200769 }
770 }
Radek Krejcifc8d8322016-06-24 11:23:23 +0200771 val_set = 1;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200772
773 } else if ((module->version >= 2) && !strcmp(node->name, "if-feature")) {
774 c_ftrs++;
775
Radek Krejci0d70c372015-07-02 16:23:10 +0200776 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100777 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200778 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200779 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200780 }
Radek Krejcie663e012016-08-01 17:12:34 +0200781
782 if (!dertype->der) { /* directly derived type from enumeration */
783 if (!val_set) {
784 /* assign value automatically */
785 if (v > INT32_MAX) {
786 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "2147483648", "enum/value");
787 type->info.enums.count = i + 1;
788 goto error;
789 }
790 type->info.enums.enm[i].value = v;
791 type->info.enums.enm[i].flags |= LYS_AUTOASSIGNED;
792 v++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200793 }
Radek Krejcie663e012016-08-01 17:12:34 +0200794 } else { /* restricted enum type */
795 if (!val_set) {
796 /* automatically assign value from base type */
797 type->info.enums.enm[i].value = enms_sc[j].value;
798 type->info.enums.enm[i].flags |= LYS_AUTOASSIGNED;
799 } else {
800 /* check that the assigned value corresponds to the original
801 * value of the enum in the base type */
802 if (v_ != enms_sc[j].value) {
803 /* v_ - assigned value in restricted enum
804 * enms_sc[j].value - value assigned to the corresponding enum (detected above) in base type */
805 LOGVAL(LYE_ENUM_INVAL, LY_VLOG_NONE, NULL,
Radek Krejci541a45d2016-08-02 13:12:07 +0200806 type->info.enums.enm[i].value, type->info.enums.enm[i].name, enms_sc[j].value);
Radek Krejcie663e012016-08-01 17:12:34 +0200807 type->info.enums.count = i + 1;
808 goto error;
809 }
810 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200811 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200812
813 /* if-features */
814 if (c_ftrs) {
815 enms_sc = &type->info.enums.enm[i];
816 enms_sc->iffeature = calloc(c_ftrs, sizeof *enms_sc->iffeature);
817 if (!enms_sc->iffeature) {
818 LOGMEM;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200819 type->info.enums.count = i + 1;
820 goto error;
821 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200822
823 LY_TREE_FOR(next->child, node) {
824 if (!strcmp(node->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +0200825 rc = fill_yin_iffeature((struct lys_node *)type->parent, 0, node,
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200826 &enms_sc->iffeature[enms_sc->iffeature_size], unres);
827 enms_sc->iffeature_size++;
828 if (rc) {
829 type->info.enums.count = i + 1;
830 goto error;
831 }
832 }
833 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200834 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200835
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200836 }
837 break;
838
839 case LY_TYPE_IDENT:
Radek Krejci994b6f62015-06-18 16:47:27 +0200840 /* RFC 6020 9.10 - base */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200841
Michal Vaskof2d43962016-09-02 11:10:16 +0200842 /* get base specification, at least one must be present */
Radek Krejci0d70c372015-07-02 16:23:10 +0200843 LY_TREE_FOR_SAFE(yin->child, next, node) {
844 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
845 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100846 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200847 continue;
848 }
849
Michal Vaskoe29c6622015-11-27 15:02:31 +0100850 if (strcmp(node->name, "base")) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100851 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200852 goto error;
853 }
Michal Vaskof2d43962016-09-02 11:10:16 +0200854
855 GETVAL(value, yin->child, "name");
856 /* store in the JSON format */
857 value = transform_schema2json(module, value);
858 if (!value) {
859 goto error;
860 }
861 rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value);
862 lydict_remove(module->ctx, value);
863
864 if (rc == -1) {
865 goto error;
866 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200867 }
868
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200869 if (!yin->child) {
Radek Krejci65c889c2015-06-22 10:17:22 +0200870 if (type->der->type.der) {
Michal Vaskof2d43962016-09-02 11:10:16 +0200871 /* this is just a derived type with no base required */
Radek Krejci65c889c2015-06-22 10:17:22 +0200872 break;
873 }
Radek Krejci48464ed2016-03-17 15:44:09 +0100874 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "base", "type");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200875 goto error;
Pavol Vicanf0046f42016-09-07 15:11:09 +0200876 } else {
877 if (type->der->type.der) {
878 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "base");
879 goto error;
880 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200881 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200882 if (yin->child->next) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100883 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, yin->child->next->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200884 goto error;
885 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200886 break;
887
888 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200889 /* RFC 6020 9.13.2 - require-instance */
Radek Krejci73adb602015-07-02 18:07:40 +0200890 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200891 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
892 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200893 continue;
894 }
895
Radek Krejciaf351422015-06-19 14:49:38 +0200896 if (!strcmp(node->name, "require-instance")) {
897 if (type->info.inst.req) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100898 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejciaf351422015-06-19 14:49:38 +0200899 goto error;
900 }
901 GETVAL(value, node, "value");
Michal Vasko25fa5ac2015-07-17 10:53:38 +0200902 if (!strcmp(value, "true")) {
Radek Krejciaf351422015-06-19 14:49:38 +0200903 type->info.inst.req = 1;
Michal Vasko25fa5ac2015-07-17 10:53:38 +0200904 } else if (!strcmp(value, "false")) {
Radek Krejciaf351422015-06-19 14:49:38 +0200905 type->info.inst.req = -1;
906 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100907 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
Radek Krejciaf351422015-06-19 14:49:38 +0200908 goto error;
909 }
910 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100911 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejciaf351422015-06-19 14:49:38 +0200912 goto error;
913 }
Radek Krejciaf351422015-06-19 14:49:38 +0200914 }
Michal Vasko8548cf92015-07-20 15:17:53 +0200915
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200916 break;
917
Radek Krejcif2860132015-06-20 12:37:20 +0200918 case LY_TYPE_BINARY:
919 /* RFC 6020 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200920 case LY_TYPE_INT8:
921 case LY_TYPE_INT16:
922 case LY_TYPE_INT32:
923 case LY_TYPE_INT64:
924 case LY_TYPE_UINT8:
925 case LY_TYPE_UINT16:
926 case LY_TYPE_UINT32:
927 case LY_TYPE_UINT64:
Radek Krejcif2860132015-06-20 12:37:20 +0200928 /* RFC 6020 9.2.4 - range */
929
930 /* length and range are actually the same restriction, so process
931 * them by this common code, we just need to differ the name and
932 * structure where the information will be stored
933 */
934 if (type->base == LY_TYPE_BINARY) {
935 restr = &type->info.binary.length;
936 name = "length";
937 } else {
938 restr = &type->info.num.range;
939 name = "range";
940 }
941
Radek Krejci73adb602015-07-02 18:07:40 +0200942 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200943 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
944 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200945 continue;
946 }
947
Radek Krejcif2860132015-06-20 12:37:20 +0200948 if (!strcmp(node->name, name)) {
949 if (*restr) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100950 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcif2860132015-06-20 12:37:20 +0200951 goto error;
952 }
953
954 GETVAL(value, node, "value");
Radek Krejci6dc53a22015-08-17 13:27:59 +0200955 if (lyp_check_length_range(value, type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100956 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, name);
Radek Krejcif2860132015-06-20 12:37:20 +0200957 goto error;
958 }
959 *restr = calloc(1, sizeof **restr);
Michal Vasko253035f2015-12-17 16:58:13 +0100960 if (!(*restr)) {
961 LOGMEM;
962 goto error;
963 }
Radek Krejcif2860132015-06-20 12:37:20 +0200964 (*restr)->expr = lydict_insert(module->ctx, value, 0);
965
966 /* get possible substatements */
967 if (read_restr_substmt(module->ctx, *restr, node)) {
968 goto error;
969 }
970 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +0100971 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejcif2860132015-06-20 12:37:20 +0200972 goto error;
973 }
Radek Krejcif2860132015-06-20 12:37:20 +0200974 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200975 break;
976
977 case LY_TYPE_LEAFREF:
Radek Krejci3a5501d2016-07-18 22:03:34 +0200978 /* flag resolving for later use */
979 if (!tpdftype) {
980 for (siter = parent; siter && siter->nodetype != LYS_GROUPING; siter = lys_parent(siter));
981 if (siter) {
982 /* just a flag - do not resolve */
983 tpdftype = 1;
984 }
985 }
986
Radek Krejcidc4c1412015-06-19 15:39:54 +0200987 /* RFC 6020 9.9.2 - path */
Radek Krejci73adb602015-07-02 18:07:40 +0200988 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200989 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
990 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200991 continue;
992 }
993
Michal Vasko88c29542015-11-27 14:57:53 +0100994 if (!strcmp(node->name, "path") && !type->der->type.der) {
Radek Krejcidc4c1412015-06-19 15:39:54 +0200995 if (type->info.lref.path) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100996 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcidc4c1412015-06-19 15:39:54 +0200997 goto error;
998 }
999
1000 GETVAL(value, node, "value");
Michal Vasko1dca6882015-10-22 14:29:42 +02001001 /* store in the JSON format */
Radek Krejci48464ed2016-03-17 15:44:09 +01001002 type->info.lref.path = transform_schema2json(module, value);
Michal Vasko1dca6882015-10-22 14:29:42 +02001003 if (!type->info.lref.path) {
1004 goto error;
1005 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001006
1007 /* try to resolve leafref path only when this is instantiated
1008 * leaf, so it is not:
1009 * - typedef's type,
1010 * - in grouping definition,
1011 * - just instantiated in a grouping definition,
1012 * because in those cases the nodes referenced in path might not be present
1013 * and it is not a bug. */
1014 if (!tpdftype && unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001015 goto error;
1016 }
Radek Krejcid6353ed2016-09-15 13:30:45 +02001017 } else if (module->version >= 2 && !strcmp(node->name, "require-instance")) {
Michal Vasko08ae53e2016-09-02 12:40:04 +02001018 if (type->info.lref.req) {
1019 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
1020 goto error;
1021 }
1022 GETVAL(value, node, "value");
1023 if (!strcmp(value, "true")) {
1024 type->info.lref.req = 1;
1025 } else if (!strcmp(value, "false")) {
1026 type->info.lref.req = -1;
1027 } else {
1028 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
1029 goto error;
1030 }
Radek Krejcidc4c1412015-06-19 15:39:54 +02001031 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001032 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejcidc4c1412015-06-19 15:39:54 +02001033 goto error;
1034 }
Radek Krejci73adb602015-07-02 18:07:40 +02001035 }
1036
Radek Krejci742be352016-07-17 12:18:54 +02001037 if (!type->info.lref.path) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001038 if (!type->der->type.der) {
1039 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "path", "type");
1040 goto error;
1041 } else {
1042 /* copy leafref definition into the derived type */
1043 type->info.lref.path = lydict_insert(module->ctx, type->der->type.info.lref.path, 0);
1044 /* and resolve the path at the place we are (if not in grouping/typedef) */
1045 if (!tpdftype && unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent) == -1) {
1046 goto error;
1047 }
1048
1049 /* add pointer to leafref target, only on leaves (not in typedefs) */
1050 if (type->info.lref.target && lys_leaf_add_leafref_target(type->info.lref.target, (struct lys_node *)type->parent)) {
1051 goto error;
1052 }
1053 }
Radek Krejci742be352016-07-17 12:18:54 +02001054 }
1055
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001056 break;
1057
1058 case LY_TYPE_STRING:
Radek Krejci3733a802015-06-19 13:43:21 +02001059 /* RFC 6020 9.4.4 - length */
1060 /* RFC 6020 9.4.6 - pattern */
Radek Krejci73adb602015-07-02 18:07:40 +02001061 i = 0;
Radek Krejci3733a802015-06-19 13:43:21 +02001062 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001063 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
1064 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01001065 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +02001066 continue;
1067 }
1068
Radek Krejci3733a802015-06-19 13:43:21 +02001069 if (!strcmp(node->name, "length")) {
1070 if (type->info.str.length) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001071 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci3733a802015-06-19 13:43:21 +02001072 goto error;
1073 }
1074
1075 GETVAL(value, node, "value");
Radek Krejci6dc53a22015-08-17 13:27:59 +02001076 if (lyp_check_length_range(value, type)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001077 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "length");
Radek Krejci3733a802015-06-19 13:43:21 +02001078 goto error;
1079 }
1080 type->info.str.length = calloc(1, sizeof *type->info.str.length);
Michal Vasko253035f2015-12-17 16:58:13 +01001081 if (!type->info.str.length) {
1082 LOGMEM;
1083 goto error;
1084 }
Radek Krejci3733a802015-06-19 13:43:21 +02001085 type->info.str.length->expr = lydict_insert(module->ctx, value, 0);
1086
Radek Krejci5fbc9162015-06-19 14:11:11 +02001087 /* get possible sub-statements */
1088 if (read_restr_substmt(module->ctx, type->info.str.length, node)) {
Radek Krejci3733a802015-06-19 13:43:21 +02001089 goto error;
1090 }
Michal Vasko345da0a2015-12-02 10:35:55 +01001091 lyxml_free(module->ctx, node);
Radek Krejci3733a802015-06-19 13:43:21 +02001092 } else if (!strcmp(node->name, "pattern")) {
Radek Krejci73adb602015-07-02 18:07:40 +02001093 i++;
Radek Krejci3733a802015-06-19 13:43:21 +02001094 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001095 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejci3733a802015-06-19 13:43:21 +02001096 goto error;
1097 }
1098 }
Radek Krejci5fbc9162015-06-19 14:11:11 +02001099 /* store patterns in array */
Radek Krejci73adb602015-07-02 18:07:40 +02001100 if (i) {
1101 type->info.str.patterns = calloc(i, sizeof *type->info.str.patterns);
Michal Vasko253035f2015-12-17 16:58:13 +01001102 if (!type->info.str.patterns) {
1103 LOGMEM;
1104 goto error;
1105 }
Radek Krejci73adb602015-07-02 18:07:40 +02001106 LY_TREE_FOR(yin->child, node) {
Michal Vasko5b64da22015-11-23 15:22:30 +01001107 GETVAL(value, node, "value");
Michal Vasko0aee5c12016-06-17 14:27:26 +02001108 if (lyp_check_pattern(value, NULL)) {
Michal Vasko69068852015-07-13 14:34:31 +02001109 free(type->info.str.patterns);
Radek Krejci9f1e8532016-06-16 11:18:21 +02001110 type->info.str.patterns = NULL;
Michal Vasko69068852015-07-13 14:34:31 +02001111 goto error;
1112 }
Michal Vasko69068852015-07-13 14:34:31 +02001113
Radek Krejci0d23e7a2016-08-04 12:46:17 +02001114 modifier = 0x06; /* ACK */
1115 name = NULL;
1116 LY_TREE_FOR_SAFE(node->child, next2, child) {
1117 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1118 /* garbage */
1119 lyxml_free(module->ctx, child);
1120 continue;
1121 }
1122
1123 if (module->version >= 2 && !strcmp(child->name, "modifier")) {
1124 if (name) {
1125 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "modifier", node->name);
1126 goto error;
1127 }
1128
1129 GETVAL(name, child, "value");
1130 if (!strcmp(name, "invert-match")) {
1131 modifier = 0x15; /* NACK */
1132 } else {
1133 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, name, "modifier");
1134 goto error;
1135 }
1136 lyxml_free(module->ctx, child);
1137 }
1138 }
1139
1140 len = strlen(value);
1141 buf = malloc((len + 2) * sizeof *buf); /* modifier byte + value + terminating NULL byte */
1142 buf[0] = modifier;
1143 strcpy(&buf[1], value);
1144
1145 type->info.str.patterns[type->info.str.pat_count].expr = lydict_insert_zc(module->ctx, buf);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001146
1147 /* get possible sub-statements */
Michal Vasko5b64da22015-11-23 15:22:30 +01001148 if (read_restr_substmt(module->ctx, &type->info.str.patterns[type->info.str.pat_count], node)) {
Michal Vasko69068852015-07-13 14:34:31 +02001149 free(type->info.str.patterns);
Pavol Vican9fc5d642016-08-30 15:23:19 +02001150 type->info.str.patterns = NULL;
Radek Krejci5fbc9162015-06-19 14:11:11 +02001151 goto error;
1152 }
Radek Krejci73adb602015-07-02 18:07:40 +02001153 type->info.str.pat_count++;
Radek Krejci5fbc9162015-06-19 14:11:11 +02001154 }
1155 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001156 break;
1157
1158 case LY_TYPE_UNION:
Radek Krejcie4c366b2015-07-02 10:11:31 +02001159 /* RFC 6020 7.4 - type */
1160 /* count number of types in union */
1161 i = 0;
Radek Krejci0d70c372015-07-02 16:23:10 +02001162 LY_TREE_FOR_SAFE(yin->child, next, node) {
1163 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
1164 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01001165 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +02001166 continue;
1167 }
1168
Radek Krejcie4c366b2015-07-02 10:11:31 +02001169 if (!strcmp(node->name, "type")) {
Radek Krejci038d5d92016-09-12 15:07:15 +02001170 if (type->der->type.der) {
1171 /* type can be a substatement only in "union" type, not in derived types */
1172 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "derived type");
1173 goto error;
1174 }
Radek Krejcie4c366b2015-07-02 10:11:31 +02001175 i++;
1176 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001177 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001178 goto error;
1179 }
1180 }
1181
Radek Krejci038d5d92016-09-12 15:07:15 +02001182 if (!i && !type->der->type.der) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001183 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", "(union) type");
Radek Krejcie4c366b2015-07-02 10:11:31 +02001184 goto error;
1185 }
1186
1187 /* allocate array for union's types ... */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001188 type->info.uni.types = calloc(i, sizeof *type->info.uni.types);
Michal Vasko253035f2015-12-17 16:58:13 +01001189 if (!type->info.uni.types) {
1190 LOGMEM;
1191 goto error;
1192 }
Radek Krejcie4c366b2015-07-02 10:11:31 +02001193 /* ... and fill the structures */
Radek Krejci73adb602015-07-02 18:07:40 +02001194 LY_TREE_FOR(yin->child, node) {
Radek Krejcicf509982015-12-15 09:22:44 +01001195 type->info.uni.types[type->info.uni.count].parent = type->parent;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001196 rc = fill_yin_type(module, parent, node, &type->info.uni.types[type->info.uni.count], tpdftype, unres);
Michal Vasko88c29542015-11-27 14:57:53 +01001197 if (!rc) {
1198 type->info.uni.count++;
1199
Radek Krejci7de36cf2016-09-12 16:18:50 +02001200 if (module->version < 2) {
1201 /* union's type cannot be empty or leafref */
1202 if (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_EMPTY) {
1203 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "empty", node->name);
1204 rc = -1;
1205 } else if (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_LEAFREF) {
1206 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, "leafref", node->name);
1207 rc = -1;
1208 }
Michal Vasko88c29542015-11-27 14:57:53 +01001209 }
1210 }
1211 if (rc) {
1212 /* even if we got EXIT_FAILURE, throw it all away, too much trouble doing something else */
1213 for (i = 0; i < type->info.uni.count; ++i) {
1214 lys_type_free(module->ctx, &type->info.uni.types[i]);
1215 }
1216 free(type->info.uni.types);
1217 type->info.uni.types = NULL;
1218 type->info.uni.count = 0;
Michal Vaskoeac08182016-07-21 12:16:32 +02001219 type->der = NULL;
1220 type->base = LY_TYPE_DER;
Michal Vasko88c29542015-11-27 14:57:53 +01001221
1222 if (rc == EXIT_FAILURE) {
Radek Krejcidc008d72016-02-17 13:12:14 +01001223 ret = EXIT_FAILURE;
Michal Vasko88c29542015-11-27 14:57:53 +01001224 }
Radek Krejcie4c366b2015-07-02 10:11:31 +02001225 goto error;
1226 }
Michal Vasko88c29542015-11-27 14:57:53 +01001227 }
1228 break;
Radek Krejcie4c366b2015-07-02 10:11:31 +02001229
Michal Vasko88c29542015-11-27 14:57:53 +01001230 case LY_TYPE_BOOL:
1231 case LY_TYPE_EMPTY:
1232 /* no sub-statement allowed */
1233 LY_TREE_FOR(yin->child, node) {
1234 if (node->ns && !strcmp(node->ns->value, LY_NSYIN)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001235 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001236 goto error;
1237 }
Radek Krejcie4c366b2015-07-02 10:11:31 +02001238 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001239 break;
1240
1241 default:
Michal Vasko88c29542015-11-27 14:57:53 +01001242 LOGINT;
1243 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001244 }
1245
1246 return EXIT_SUCCESS;
Radek Krejci25d782a2015-05-22 15:03:23 +02001247
1248error:
Radek Krejcidc008d72016-02-17 13:12:14 +01001249 if (type->module_name) {
1250 lydict_remove(module->ctx, type->module_name);
1251 type->module_name = NULL;
1252 }
1253 return ret;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001254}
1255
Michal Vasko0d343d12015-08-24 14:57:36 +02001256/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001257static int
Michal Vaskof02e3742015-08-05 16:27:02 +02001258fill_yin_typedef(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_tpdf *tpdf, struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001259{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001260 const char *value;
Michal Vasko88c29542015-11-27 14:57:53 +01001261 struct lyxml_elem *node, *next;
Radek Krejci48464ed2016-03-17 15:44:09 +01001262 int has_type = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001263
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001264 GETVAL(value, yin, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +01001265 if (lyp_check_identifier(value, LY_IDENT_TYPE, module, parent)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001266 goto error;
1267 }
1268 tpdf->name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejcida04f4a2015-05-21 12:54:09 +02001269
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001270 /* generic part - status, description, reference */
Radek Krejci225376f2016-02-16 17:36:22 +01001271 if (read_yin_common(module, NULL, (struct lys_node *)tpdf, yin, OPT_MODULE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001272 goto error;
1273 }
Radek Krejcieac35532015-05-31 19:09:15 +02001274
Michal Vasko88c29542015-11-27 14:57:53 +01001275 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001276 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
1277 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +02001278 continue;
1279 }
1280
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001281 if (!strcmp(node->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +01001282 if (has_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001283 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001284 goto error;
1285 }
Michal Vasko88c29542015-11-27 14:57:53 +01001286 /* HACK for unres */
1287 tpdf->type.der = (struct lys_tpdf *)node;
Radek Krejcicf509982015-12-15 09:22:44 +01001288 tpdf->type.parent = tpdf;
Michal Vasko5d631402016-07-21 13:15:15 +02001289 if (unres_schema_add_node(module, unres, &tpdf->type, UNRES_TYPE_DER_TPDF, parent) == -1) {
Radek Krejci73adb602015-07-02 18:07:40 +02001290 goto error;
1291 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001292 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001293 } else if (!strcmp(node->name, "default")) {
1294 if (tpdf->dflt) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001295 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001296 goto error;
1297 }
1298 GETVAL(value, node, "value");
1299 tpdf->dflt = lydict_insert(module->ctx, value, strlen(value));
1300 } else if (!strcmp(node->name, "units")) {
1301 if (tpdf->units) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001302 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001303 goto error;
1304 }
1305 GETVAL(value, node, "name");
1306 tpdf->units = lydict_insert(module->ctx, value, strlen(value));
1307 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001308 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001309 goto error;
1310 }
1311 }
Radek Krejci25d782a2015-05-22 15:03:23 +02001312
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001313 /* check mandatory value */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001314 if (!has_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001315 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001316 goto error;
1317 }
Radek Krejcieac35532015-05-31 19:09:15 +02001318
Michal Vasko478c4652016-07-21 12:55:01 +02001319 /* check default value (if not defined, there still could be some restrictions
1320 * that need to be checked against a default value from a derived type) */
1321 if (unres_schema_add_str(module, unres, &tpdf->type, UNRES_TYPE_DFLT, tpdf->dflt) == -1) {
1322 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001323 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001324
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001325 return EXIT_SUCCESS;
Radek Krejcieac35532015-05-31 19:09:15 +02001326
1327error:
1328
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001329 return EXIT_FAILURE;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001330}
1331
Michal Vasko0d343d12015-08-24 14:57:36 +02001332/* logs directly */
Radek Krejci3cf9e222015-06-18 11:37:50 +02001333static int
Michal Vaskof02e3742015-08-05 16:27:02 +02001334fill_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 +02001335{
1336 const char *value;
1337 struct lyxml_elem *child, *next;
Michal Vasko2d851a92015-10-20 16:16:36 +02001338 int c = 0, ret;
Radek Krejci3cf9e222015-06-18 11:37:50 +02001339
Radek Krejcib05774c2015-06-18 13:52:59 +02001340 GETVAL(value, yin, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +01001341 if (lyp_check_identifier(value, LY_IDENT_FEATURE, module, NULL)) {
Radek Krejcib05774c2015-06-18 13:52:59 +02001342 goto error;
1343 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001344 f->name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci6a113852015-07-03 16:04:20 +02001345 f->module = module;
Radek Krejcib05774c2015-06-18 13:52:59 +02001346
Radek Krejci76512572015-08-04 09:47:08 +02001347 if (read_yin_common(module, NULL, (struct lys_node *)f, yin, 0)) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02001348 goto error;
1349 }
1350
1351 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001352 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1353 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01001354 lyxml_free(module->ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001355 continue;
1356 }
1357
Radek Krejci3cf9e222015-06-18 11:37:50 +02001358 if (!strcmp(child->name, "if-feature")) {
1359 c++;
1360 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001361 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001362 goto error;
1363 }
1364 }
1365
1366 if (c) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001367 f->iffeature = calloc(c, sizeof *f->iffeature);
1368 if (!f->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01001369 LOGMEM;
1370 goto error;
1371 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02001372 }
Radek Krejci73adb602015-07-02 18:07:40 +02001373 LY_TREE_FOR(yin->child, child) {
Radek Krejci9de2c042016-10-19 16:53:06 +02001374 ret = fill_yin_iffeature((struct lys_node *)f, 1, child, &f->iffeature[f->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001375 f->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01001376 if (ret) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001377 goto error;
1378 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02001379 }
1380
Radek Krejcic79c6b12016-07-26 15:11:49 +02001381 /* check for circular dependencies */
1382 if (f->iffeature_size) {
1383 if (unres_schema_add_node(module, unres, f, UNRES_FEATURE, NULL) == -1) {
1384 goto error;
1385 }
1386 }
1387
Radek Krejci3cf9e222015-06-18 11:37:50 +02001388 return EXIT_SUCCESS;
1389
1390error:
1391
1392 return EXIT_FAILURE;
1393}
1394
Michal Vasko0d343d12015-08-24 14:57:36 +02001395/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001396static int
Radek Krejcib8048692015-08-05 13:36:34 +02001397fill_yin_must(struct lys_module *module, struct lyxml_elem *yin, struct lys_restr *must)
Radek Krejci800af702015-06-02 13:46:01 +02001398{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001399 const char *value;
Radek Krejci800af702015-06-02 13:46:01 +02001400
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001401 GETVAL(value, yin, "condition");
Radek Krejci48464ed2016-03-17 15:44:09 +01001402 must->expr = transform_schema2json(module, value);
Michal Vaskof9893382015-10-09 14:03:04 +02001403 if (!must->expr) {
1404 goto error;
1405 }
Radek Krejci800af702015-06-02 13:46:01 +02001406
Radek Krejci41726f92015-06-19 13:11:05 +02001407 return read_restr_substmt(module->ctx, must, yin);
Radek Krejci800af702015-06-02 13:46:01 +02001408
Michal Vasko77dc5652016-02-15 12:32:42 +01001409error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001410 return EXIT_FAILURE;
Radek Krejci800af702015-06-02 13:46:01 +02001411}
1412
Radek Krejci581ce772015-11-10 17:22:40 +01001413static int
Michal Vasko88c29542015-11-27 14:57:53 +01001414fill_yin_unique(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_unique *unique,
1415 struct unres_schema *unres)
Radek Krejci581ce772015-11-10 17:22:40 +01001416{
1417 int i, j;
1418 const char *value, *vaux;
Radek Krejcid09d1a52016-08-11 14:05:45 +02001419 struct unres_list_uniq *unique_info;
Radek Krejci581ce772015-11-10 17:22:40 +01001420
1421 /* get unique value (list of leafs supposed to be unique */
1422 GETVAL(value, yin, "tag");
1423
1424 /* count the number of unique leafs in the value */
1425 vaux = value;
1426 while ((vaux = strpbrk(vaux, " \t\n"))) {
Michal Vasko98645db2016-03-07 14:38:49 +01001427 unique->expr_size++;
Radek Krejci581ce772015-11-10 17:22:40 +01001428 while (isspace(*vaux)) {
1429 vaux++;
1430 }
1431 }
1432 unique->expr_size++;
1433 unique->expr = calloc(unique->expr_size, sizeof *unique->expr);
Michal Vasko253035f2015-12-17 16:58:13 +01001434 if (!unique->expr) {
1435 LOGMEM;
1436 goto error;
1437 }
Radek Krejci581ce772015-11-10 17:22:40 +01001438
1439 for (i = 0; i < unique->expr_size; i++) {
1440 vaux = strpbrk(value, " \t\n");
1441 if (!vaux) {
1442 /* the last token, lydict_insert() will count its size on its own */
1443 vaux = value;
1444 }
1445
1446 /* store token into unique structure */
1447 unique->expr[i] = lydict_insert(module->ctx, value, vaux - value);
1448
1449 /* check that the expression does not repeat */
1450 for (j = 0; j < i; j++) {
Radek Krejci749190d2016-02-18 16:26:25 +01001451 if (ly_strequal(unique->expr[j], unique->expr[i], 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001452 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, unique->expr[i], "unique");
1453 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The identifier is not unique");
Radek Krejci581ce772015-11-10 17:22:40 +01001454 goto error;
1455 }
1456 }
1457
1458 /* try to resolve leaf */
1459 if (unres) {
Radek Krejcid09d1a52016-08-11 14:05:45 +02001460 unique_info = malloc(sizeof *unique_info);
1461 unique_info->list = parent;
1462 unique_info->expr = unique->expr[i];
1463 unique_info->trg_type = &unique->trg_type;
1464 if (unres_schema_add_node(module, unres, unique_info, UNRES_LIST_UNIQ, NULL) == -1){
Pavol Vican18b10212016-04-11 15:41:52 +02001465 goto error;
1466 }
Radek Krejci581ce772015-11-10 17:22:40 +01001467 } else {
Radek Krejcid09d1a52016-08-11 14:05:45 +02001468 if (resolve_unique(parent, unique->expr[i], &unique->trg_type)) {
Radek Krejci581ce772015-11-10 17:22:40 +01001469 goto error;
1470 }
1471 }
1472
1473 /* move to next token */
1474 value = vaux;
1475 while(isspace(*value)) {
1476 value++;
1477 }
1478 }
1479
1480 return EXIT_SUCCESS;
1481
1482error:
1483 return EXIT_FAILURE;
1484}
1485
Michal Vasko0d343d12015-08-24 14:57:36 +02001486/* logs directly
1487 *
Radek Krejcieb00f512015-07-01 16:44:58 +02001488 * type: 0 - min, 1 - max
1489 */
1490static int
Radek Krejcia52656e2015-08-05 13:41:50 +02001491deviate_minmax(struct lys_node *target, struct lyxml_elem *node, struct lys_deviate *d, int type)
Radek Krejcieb00f512015-07-01 16:44:58 +02001492{
1493 const char *value;
1494 char *endptr;
1495 unsigned long val;
Pavol Vican09adcc32016-08-25 10:51:36 +02001496 uint32_t *ui32val, *min, *max;
Radek Krejcieb00f512015-07-01 16:44:58 +02001497
Michal Vaskof7e57d52016-03-07 11:31:09 +01001498 /* del min/max is forbidden */
1499 if (d->mod == LY_DEVIATE_DEL) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001500 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, (type ? "max-elements" : "min-elements"), "deviate delete");
Michal Vaskof7e57d52016-03-07 11:31:09 +01001501 goto error;
1502 }
1503
Radek Krejcieb00f512015-07-01 16:44:58 +02001504 /* check target node type */
Radek Krejci76512572015-08-04 09:47:08 +02001505 if (target->nodetype == LYS_LEAFLIST) {
Pavol Vican09adcc32016-08-25 10:51:36 +02001506 max = &((struct lys_node_leaflist *)target)->max;
1507 min = &((struct lys_node_leaflist *)target)->min;
Radek Krejci76512572015-08-04 09:47:08 +02001508 } else if (target->nodetype == LYS_LIST) {
Pavol Vican09adcc32016-08-25 10:51:36 +02001509 max = &((struct lys_node_list *)target)->max;
1510 min = &((struct lys_node_list *)target)->min;
Radek Krejcieb00f512015-07-01 16:44:58 +02001511 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001512 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
1513 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", node->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001514 goto error;
1515 }
1516
1517 GETVAL(value, node, "value");
1518 while (isspace(value[0])) {
1519 value++;
1520 }
1521
Radek Krejci0d7b2472016-02-12 11:11:03 +01001522 if (type && !strcmp(value, "unbounded")) {
1523 d->max = val = 0;
Radek Krejcia31ca7a2016-03-07 15:05:20 +01001524 d->max_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02001525 ui32val = max;
Radek Krejcieb00f512015-07-01 16:44:58 +02001526 } else {
Radek Krejci0d7b2472016-02-12 11:11:03 +01001527 /* convert it to uint32_t */
1528 errno = 0;
1529 endptr = NULL;
1530 val = strtoul(value, &endptr, 10);
1531 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001532 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, node->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01001533 goto error;
1534 }
1535 if (type) {
1536 d->max = (uint32_t)val;
Radek Krejcia31ca7a2016-03-07 15:05:20 +01001537 d->max_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02001538 ui32val = max;
Radek Krejci0d7b2472016-02-12 11:11:03 +01001539 } else {
1540 d->min = (uint32_t)val;
Radek Krejcia31ca7a2016-03-07 15:05:20 +01001541 d->min_set = 1;
Pavol Vican09adcc32016-08-25 10:51:36 +02001542 ui32val = min;
Radek Krejci0d7b2472016-02-12 11:11:03 +01001543 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001544 }
1545
1546 if (d->mod == LY_DEVIATE_ADD) {
1547 /* check that there is no current value */
1548 if (*ui32val) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001549 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, node->name);
1550 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001551 goto error;
1552 }
Michal Vasko21be1b32016-03-07 12:31:34 +01001553 } else if (d->mod == LY_DEVIATE_RPL) {
1554 /* unfortunately, there is no way to check reliably that there
1555 * was a value before, it could have been the default */
Radek Krejcieb00f512015-07-01 16:44:58 +02001556 }
1557
Michal Vaskof7e57d52016-03-07 11:31:09 +01001558 /* add (already checked) and replace */
1559 /* set new value specified in deviation */
1560 *ui32val = (uint32_t)val;
Radek Krejcieb00f512015-07-01 16:44:58 +02001561
Pavol Vican09adcc32016-08-25 10:51:36 +02001562 /* check min-elements is smaller than max-elements */
1563 if (*max && *min > *max) {
1564 if (type) {
Radek Krejci73f96012016-10-03 13:53:53 +02001565 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "max-elements");
Pavol Vican09adcc32016-08-25 10:51:36 +02001566 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\".");
1567 } else {
Radek Krejci73f96012016-10-03 13:53:53 +02001568 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "min-elements");
Pavol Vican09adcc32016-08-25 10:51:36 +02001569 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
1570 }
1571 goto error;
1572 }
1573
Radek Krejcieb00f512015-07-01 16:44:58 +02001574 return EXIT_SUCCESS;
1575
1576error:
1577
1578 return EXIT_FAILURE;
1579}
1580
Michal Vasko0d343d12015-08-24 14:57:36 +02001581/* logs directly */
Radek Krejcieb00f512015-07-01 16:44:58 +02001582static int
Michal Vasko88c29542015-11-27 14:57:53 +01001583fill_yin_deviation(struct lys_module *module, struct lyxml_elem *yin, struct lys_deviation *dev,
1584 struct unres_schema *unres)
Radek Krejcieb00f512015-07-01 16:44:58 +02001585{
1586 const char *value, **stritem;
1587 struct lyxml_elem *next, *child, *develem;
Radek Krejcid5a5c282016-08-15 15:38:08 +02001588 int c_dev = 0, c_must, c_uniq, c_dflt;
Radek Krejci0d7b2472016-02-12 11:11:03 +01001589 int f_min = 0, f_max = 0; /* flags */
Michal Vaskob40b4512016-02-11 11:35:37 +01001590 int i, j, rc;
Radek Krejcid5a5c282016-08-15 15:38:08 +02001591 unsigned int u;
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001592 struct ly_ctx *ctx;
Radek Krejcia52656e2015-08-05 13:41:50 +02001593 struct lys_deviate *d = NULL;
Radek Krejcie00d2312016-08-12 15:27:49 +02001594 struct lys_node *node = NULL, *parent, *dev_target = NULL;
Radek Krejcib8048692015-08-05 13:36:34 +02001595 struct lys_node_choice *choice = NULL;
Radek Krejcid5a5c282016-08-15 15:38:08 +02001596 struct lys_node_leaf *leaf = NULL;
1597 struct ly_set *dflt_check = ly_set_new();
Radek Krejcib8048692015-08-05 13:36:34 +02001598 struct lys_node_list *list = NULL;
Radek Krejcid5a5c282016-08-15 15:38:08 +02001599 struct lys_node_leaflist *llist = NULL;
Radek Krejci1574a8d2015-08-03 14:16:52 +02001600 struct lys_type *t = NULL;
Radek Krejcid5a5c282016-08-15 15:38:08 +02001601 uint8_t *trg_must_size = NULL;
Radek Krejci1574a8d2015-08-03 14:16:52 +02001602 struct lys_restr **trg_must = NULL;
Michal Vaskoff006c12016-02-17 11:15:19 +01001603 struct unres_schema tmp_unres;
Radek Krejci27fe55e2016-09-13 17:13:35 +02001604 struct lys_module *mod;
Radek Krejcieb00f512015-07-01 16:44:58 +02001605
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001606 ctx = module->ctx;
1607
Radek Krejcieb00f512015-07-01 16:44:58 +02001608 GETVAL(value, yin, "target-node");
Radek Krejci48464ed2016-03-17 15:44:09 +01001609 dev->target_name = transform_schema2json(module, value);
Michal Vaskoa8b25952015-10-20 15:30:25 +02001610 if (!dev->target_name) {
1611 goto error;
1612 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001613
1614 /* resolve target node */
Michal Vasko60f4b452016-02-12 11:02:55 +01001615 rc = resolve_augment_schema_nodeid(dev->target_name, NULL, module, (const struct lys_node **)&dev_target);
1616 if (rc || !dev_target) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001617 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001618 goto error;
1619 }
Radek Krejcic4283442016-04-22 09:19:27 +02001620 if (dev_target->module == lys_main_module(module)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001621 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, dev->target_name, yin->name);
1622 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Deviating own module is not allowed.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001623 goto error;
1624 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001625
1626 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001627 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1628 /* garbage */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001629 lyxml_free(ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001630 continue;
1631 }
1632
Radek Krejcieb00f512015-07-01 16:44:58 +02001633 if (!strcmp(child->name, "description")) {
1634 if (dev->dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001635 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001636 goto error;
1637 }
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001638 dev->dsc = read_yin_subnode(ctx, child, "text");
Radek Krejcieb00f512015-07-01 16:44:58 +02001639 if (!dev->dsc) {
1640 goto error;
1641 }
1642 } else if (!strcmp(child->name, "reference")) {
1643 if (dev->ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001644 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001645 goto error;
1646 }
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001647 dev->ref = read_yin_subnode(ctx, child, "text");
Radek Krejcieb00f512015-07-01 16:44:58 +02001648 if (!dev->ref) {
1649 goto error;
1650 }
1651 } else if (!strcmp(child->name, "deviate")) {
1652 c_dev++;
1653
Michal Vasko345da0a2015-12-02 10:35:55 +01001654 /* skip lyxml_free() at the end of the loop, node will be
Radek Krejcieb00f512015-07-01 16:44:58 +02001655 * further processed later
1656 */
1657 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02001658
Radek Krejcieb00f512015-07-01 16:44:58 +02001659 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001660 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001661 goto error;
1662 }
1663
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001664 lyxml_free(ctx, child);
Radek Krejcieb00f512015-07-01 16:44:58 +02001665 }
1666
1667 if (c_dev) {
1668 dev->deviate = calloc(c_dev, sizeof *dev->deviate);
Michal Vasko253035f2015-12-17 16:58:13 +01001669 if (!dev->deviate) {
1670 LOGMEM;
1671 goto error;
1672 }
Pavol Vican09adcc32016-08-25 10:51:36 +02001673 } else {
1674 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "deviate", "deviation");
1675 goto error;
Radek Krejcieb00f512015-07-01 16:44:58 +02001676 }
1677
1678 LY_TREE_FOR(yin->child, develem) {
1679 /* init */
1680 f_min = 0;
Radek Krejci0d7b2472016-02-12 11:11:03 +01001681 f_max = 0;
Radek Krejcieb00f512015-07-01 16:44:58 +02001682 c_must = 0;
1683 c_uniq = 0;
Radek Krejcid5a5c282016-08-15 15:38:08 +02001684 c_dflt = 0;
Radek Krejcieb00f512015-07-01 16:44:58 +02001685
1686 /* get deviation type */
1687 GETVAL(value, develem, "value");
1688 if (!strcmp(value, "not-supported")) {
1689 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_NO;
1690 /* no property expected in this case */
1691 if (develem->child) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001692 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, develem->child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001693 goto error;
1694 }
1695
Radek Krejci5b917642015-07-02 09:03:13 +02001696 /* and neither any other deviate statement is expected,
1697 * not-supported deviation must be the only deviation of the target
1698 */
1699 if (dev->deviate_size || develem->next) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001700 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
1701 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
Radek Krejci5b917642015-07-02 09:03:13 +02001702 goto error;
1703 }
1704
Michal Vaskoad1f7b72016-02-17 11:13:58 +01001705 /* you cannot remove a key leaf */
Michal Vasko43c94772016-05-03 11:47:44 +02001706 if ((dev_target->nodetype == LYS_LEAF) && lys_parent(dev_target) && (lys_parent(dev_target)->nodetype == LYS_LIST)) {
1707 for (i = 0; i < ((struct lys_node_list *)lys_parent(dev_target))->keys_size; ++i) {
1708 if (((struct lys_node_list *)lys_parent(dev_target))->keys[i] == (struct lys_node_leaf *)dev_target) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001709 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
1710 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"not-supported\" deviation cannot remove a list key.");
Michal Vaskoad1f7b72016-02-17 11:13:58 +01001711 goto error;
1712 }
1713 }
1714 }
Radek Krejci5b917642015-07-02 09:03:13 +02001715
Michal Vaskoff006c12016-02-17 11:15:19 +01001716 /* unlink and store the original node */
1717 lys_node_unlink(dev_target);
1718 dev->orig_node = dev_target;
Radek Krejcieb00f512015-07-01 16:44:58 +02001719
Radek Krejci5b917642015-07-02 09:03:13 +02001720 dev->deviate_size = 1;
Pavol Vican85991ec2016-08-16 14:58:12 +02001721 ly_set_free(dflt_check);
Radek Krejci5b917642015-07-02 09:03:13 +02001722 return EXIT_SUCCESS;
Radek Krejcieb00f512015-07-01 16:44:58 +02001723 } else if (!strcmp(value, "add")) {
1724 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_ADD;
1725 } else if (!strcmp(value, "replace")) {
1726 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_RPL;
1727 } else if (!strcmp(value, "delete")) {
1728 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_DEL;
1729 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001730 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, develem->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001731 goto error;
1732 }
1733 d = &dev->deviate[dev->deviate_size];
Michal Vasko0f7d7ee2016-03-08 09:20:25 +01001734 dev->deviate_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02001735
Michal Vaskoff006c12016-02-17 11:15:19 +01001736 /* store a shallow copy of the original node */
1737 if (!dev->orig_node) {
1738 memset(&tmp_unres, 0, sizeof tmp_unres);
Michal Vaskoe022a562016-09-27 14:24:15 +02001739 dev->orig_node = lys_node_dup(dev_target->module, NULL, dev_target, 0, &tmp_unres, 1);
Michal Vaskoff006c12016-02-17 11:15:19 +01001740 /* just to be safe */
1741 if (tmp_unres.count) {
1742 LOGINT;
1743 goto error;
1744 }
1745 }
1746
Radek Krejcieb00f512015-07-01 16:44:58 +02001747 /* process deviation properties */
1748 LY_TREE_FOR_SAFE(develem->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001749 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1750 /* garbage */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001751 lyxml_free(ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001752 continue;
1753 }
1754
Radek Krejcieb00f512015-07-01 16:44:58 +02001755 if (!strcmp(child->name, "config")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001756 if (d->flags & LYS_CONFIG_MASK) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001757 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001758 goto error;
1759 }
1760
1761 /* for we deviate from RFC 6020 and allow config property even it is/is not
1762 * specified in the target explicitly since config property inherits. So we expect
1763 * that config is specified in every node. But for delete, we check that the value
1764 * is the same as here in deviation
1765 */
1766 GETVAL(value, child, "value");
1767 if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001768 d->flags |= LYS_CONFIG_R;
Radek Krejcieb00f512015-07-01 16:44:58 +02001769 } else if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001770 d->flags |= LYS_CONFIG_W;
Radek Krejcieb00f512015-07-01 16:44:58 +02001771 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001772 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001773 goto error;
1774 }
1775
1776 if (d->mod == LY_DEVIATE_DEL) {
Michal Vaskof7e57d52016-03-07 11:31:09 +01001777 /* del config is forbidden */
Radek Krejci48464ed2016-03-17 15:44:09 +01001778 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "config", "deviate delete");
Michal Vaskof7e57d52016-03-07 11:31:09 +01001779 goto error;
Radek Krejcieb00f512015-07-01 16:44:58 +02001780 } else { /* add and replace are the same in this case */
1781 /* remove current config value of the target ... */
Michal Vasko60f4b452016-02-12 11:02:55 +01001782 dev_target->flags &= ~LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001783
1784 /* ... and replace it with the value specified in deviation */
Michal Vasko60f4b452016-02-12 11:02:55 +01001785 dev_target->flags |= d->flags & LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001786 }
1787 } else if (!strcmp(child->name, "default")) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02001788 c_dflt++;
1789
1790 /* check target node type */
1791 if (module->version < 2 && dev_target->nodetype == LYS_LEAFLIST) {
1792 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1793 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
1794 goto error;
1795 } else if (c_dflt > 1 && dev_target->nodetype != LYS_LEAFLIST) { /* from YANG 1.1 */
1796 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1797 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow multiple \"default\" properties.");
1798 goto error;
1799 } else if (c_dflt == 1 && (!(dev_target->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE)))) {
1800 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
1801 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"default\" property.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001802 goto error;
1803 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001804
Radek Krejcid5a5c282016-08-15 15:38:08 +02001805 /* skip lyxml_free() at the end of the loop, this node will be processed later */
1806 continue;
Radek Krejcieb00f512015-07-01 16:44:58 +02001807
Radek Krejcieb00f512015-07-01 16:44:58 +02001808 } else if (!strcmp(child->name, "mandatory")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001809 if (d->flags & LYS_MAND_MASK) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001810 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001811 goto error;
1812 }
1813
1814 /* check target node type */
Radek Krejcibf2abff2016-08-23 15:51:52 +02001815 if (!(dev_target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA))) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001816 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
1817 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001818 goto error;
1819 }
1820
1821 GETVAL(value, child, "value");
1822 if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001823 d->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02001824 } else if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001825 d->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02001826 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001827 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001828 goto error;
1829 }
1830
1831 if (d->mod == LY_DEVIATE_ADD) {
1832 /* check that there is no current value */
Michal Vasko60f4b452016-02-12 11:02:55 +01001833 if (dev_target->flags & LYS_MAND_MASK) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001834 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
1835 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001836 goto error;
1837 }
Radek Krejcie00d2312016-08-12 15:27:49 +02001838
Radek Krejci841ec082016-04-05 13:05:17 +02001839 /* check collision with default-stmt */
Radek Krejcie00d2312016-08-12 15:27:49 +02001840 if (d->flags & LYS_MAND_TRUE) {
1841 if (dev_target->nodetype == LYS_CHOICE) {
1842 if (((struct lys_node_choice *)(dev_target))->dflt) {
1843 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
1844 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
1845 "Adding the \"mandatory\" statement is forbidden on choice with the \"default\" statement.");
1846 goto error;
1847 }
1848 } else if (dev_target->nodetype == LYS_LEAF) {
1849 if (((struct lys_node_leaf *)(dev_target))->dflt) {
1850 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
1851 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
1852 "Adding the \"mandatory\" statement is forbidden on leaf with the \"default\" statement.");
1853 goto error;
1854 }
1855 }
Radek Krejci841ec082016-04-05 13:05:17 +02001856 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001857
Michal Vasko21be1b32016-03-07 12:31:34 +01001858 dev_target->flags |= d->flags & LYS_MAND_MASK;
1859 } else if (d->mod == LY_DEVIATE_RPL) {
1860 /* check that there was a value before */
1861 if (!(dev_target->flags & LYS_MAND_MASK)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001862 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
1863 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001864 goto error;
1865 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001866
Michal Vasko21be1b32016-03-07 12:31:34 +01001867 dev_target->flags &= ~LYS_MAND_MASK;
Michal Vasko60f4b452016-02-12 11:02:55 +01001868 dev_target->flags |= d->flags & LYS_MAND_MASK;
Michal Vasko21be1b32016-03-07 12:31:34 +01001869 } else if (d->mod == LY_DEVIATE_DEL) {
Michal Vaskof7e57d52016-03-07 11:31:09 +01001870 /* del mandatory is forbidden */
Radek Krejci48464ed2016-03-17 15:44:09 +01001871 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "deviate delete");
Michal Vaskof7e57d52016-03-07 11:31:09 +01001872 goto error;
Radek Krejcieb00f512015-07-01 16:44:58 +02001873 }
Radek Krejcie00d2312016-08-12 15:27:49 +02001874
1875 /* check for mandatory node in default case, first find the closest parent choice to the changed node */
1876 for (parent = dev_target->parent;
1877 parent && !(parent->nodetype & (LYS_CHOICE | LYS_GROUPING | LYS_ACTION));
1878 parent = parent->parent) {
1879 if (parent->nodetype == LYS_CONTAINER && ((struct lys_node_container *)parent)->presence) {
1880 /* stop also on presence containers */
1881 break;
1882 }
1883 }
1884 /* and if it is a choice with the default case, check it for presence of a mandatory node in it */
1885 if (parent && parent->nodetype == LYS_CHOICE && ((struct lys_node_choice *)parent)->dflt) {
1886 if (lyp_check_mandatory_choice(parent)) {
1887 goto error;
1888 }
1889 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001890 } else if (!strcmp(child->name, "min-elements")) {
1891 if (f_min) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001892 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001893 goto error;
1894 }
1895 f_min = 1;
1896
Michal Vasko60f4b452016-02-12 11:02:55 +01001897 if (deviate_minmax(dev_target, child, d, 0)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001898 goto error;
1899 }
1900 } else if (!strcmp(child->name, "max-elements")) {
Radek Krejci0d7b2472016-02-12 11:11:03 +01001901 if (f_max) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001902 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001903 goto error;
1904 }
Radek Krejci0d7b2472016-02-12 11:11:03 +01001905 f_max = 1;
Radek Krejcieb00f512015-07-01 16:44:58 +02001906
Michal Vasko60f4b452016-02-12 11:02:55 +01001907 if (deviate_minmax(dev_target, child, d, 1)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001908 goto error;
1909 }
1910 } else if (!strcmp(child->name, "must")) {
1911 c_must++;
Michal Vasko345da0a2015-12-02 10:35:55 +01001912 /* skip lyxml_free() at the end of the loop, this node will be processed later */
Radek Krejcieb00f512015-07-01 16:44:58 +02001913 continue;
1914 } else if (!strcmp(child->name, "type")) {
1915 if (d->type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001916 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001917 goto error;
1918 }
1919
Michal Vaskof7e57d52016-03-07 11:31:09 +01001920 /* add, del type is forbidden */
1921 if (d->mod == LY_DEVIATE_ADD) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001922 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "deviate add");
Michal Vaskof7e57d52016-03-07 11:31:09 +01001923 goto error;
1924 } else if (d->mod == LY_DEVIATE_DEL) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001925 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "type", "deviate delete");
Michal Vaskof7e57d52016-03-07 11:31:09 +01001926 goto error;
1927 }
1928
Radek Krejcieb00f512015-07-01 16:44:58 +02001929 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01001930 if (dev_target->nodetype == LYS_LEAF) {
1931 t = &((struct lys_node_leaf *)dev_target)->type;
Pavol Vican2e322822016-09-07 15:48:13 +02001932 if (((struct lys_node_leaf *)dev_target)->dflt) {
1933 ly_set_add(dflt_check, dev_target, 0);
1934 }
Michal Vasko60f4b452016-02-12 11:02:55 +01001935 } else if (dev_target->nodetype == LYS_LEAFLIST) {
1936 t = &((struct lys_node_leaflist *)dev_target)->type;
Pavol Vican2e322822016-09-07 15:48:13 +02001937 if (((struct lys_node_leaflist *)dev_target)->dflt) {
1938 ly_set_add(dflt_check, dev_target, 0);
1939 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001940 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001941 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
1942 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001943 goto error;
1944 }
1945
Radek Krejcieb00f512015-07-01 16:44:58 +02001946 /* replace */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001947 lys_type_free(ctx, t);
Michal Vasko88c29542015-11-27 14:57:53 +01001948 /* HACK for unres */
1949 t->der = (struct lys_tpdf *)child;
Michal Vasko3767fb22016-07-21 12:10:57 +02001950 if (unres_schema_add_node(module, unres, t, UNRES_TYPE_DER, dev_target) == -1) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001951 goto error;
1952 }
1953 d->type = t;
1954 } else if (!strcmp(child->name, "unique")) {
1955 c_uniq++;
Michal Vasko345da0a2015-12-02 10:35:55 +01001956 /* skip lyxml_free() at the end of the loop, this node will be processed later */
Radek Krejcieb00f512015-07-01 16:44:58 +02001957 continue;
1958 } else if (!strcmp(child->name, "units")) {
1959 if (d->units) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001960 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001961 goto error;
1962 }
1963
1964 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01001965 if (dev_target->nodetype == LYS_LEAFLIST) {
1966 stritem = &((struct lys_node_leaflist *)dev_target)->units;
1967 } else if (dev_target->nodetype == LYS_LEAF) {
1968 stritem = &((struct lys_node_leaf *)dev_target)->units;
Radek Krejcieb00f512015-07-01 16:44:58 +02001969 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01001970 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
1971 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001972 goto error;
1973 }
1974
1975 /* get units value */
1976 GETVAL(value, child, "name");
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001977 d->units = lydict_insert(ctx, value, 0);
Radek Krejcieb00f512015-07-01 16:44:58 +02001978
1979 /* apply to target */
1980 if (d->mod == LY_DEVIATE_ADD) {
1981 /* check that there is no current value */
1982 if (*stritem) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001983 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
1984 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001985 goto error;
1986 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001987
Michal Vasko21be1b32016-03-07 12:31:34 +01001988 *stritem = lydict_insert(ctx, value, 0);
1989 } else if (d->mod == LY_DEVIATE_RPL) {
1990 /* check that there was a value before */
1991 if (!*stritem) {
Radek Krejci48464ed2016-03-17 15:44:09 +01001992 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
1993 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
Michal Vasko21be1b32016-03-07 12:31:34 +01001994 goto error;
1995 }
1996
1997 lydict_remove(ctx, *stritem);
1998 *stritem = lydict_insert(ctx, value, 0);
1999 } else if (d->mod == LY_DEVIATE_DEL) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002000 /* check values */
Michal Vaskob42b6972016-06-06 14:21:30 +02002001 if (!ly_strequal(*stritem, d->units, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002002 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
2003 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002004 goto error;
2005 }
2006 /* remove current units value of the target */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002007 lydict_remove(ctx, *stritem);
Radek Krejcieb00f512015-07-01 16:44:58 +02002008 }
2009 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002010 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02002011 goto error;
2012 }
2013
Michal Vasko88c29542015-11-27 14:57:53 +01002014 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejcieb00f512015-07-01 16:44:58 +02002015 }
2016
2017 if (c_must) {
2018 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01002019 switch (dev_target->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002020 case LYS_LEAF:
Michal Vasko60f4b452016-02-12 11:02:55 +01002021 trg_must = &((struct lys_node_leaf *)dev_target)->must;
2022 trg_must_size = &((struct lys_node_leaf *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02002023 break;
Radek Krejci76512572015-08-04 09:47:08 +02002024 case LYS_CONTAINER:
Michal Vasko60f4b452016-02-12 11:02:55 +01002025 trg_must = &((struct lys_node_container *)dev_target)->must;
2026 trg_must_size = &((struct lys_node_container *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02002027 break;
Radek Krejci76512572015-08-04 09:47:08 +02002028 case LYS_LEAFLIST:
Michal Vasko60f4b452016-02-12 11:02:55 +01002029 trg_must = &((struct lys_node_leaflist *)dev_target)->must;
2030 trg_must_size = &((struct lys_node_leaflist *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02002031 break;
Radek Krejci76512572015-08-04 09:47:08 +02002032 case LYS_LIST:
Michal Vasko60f4b452016-02-12 11:02:55 +01002033 trg_must = &((struct lys_node_list *)dev_target)->must;
2034 trg_must_size = &((struct lys_node_list *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02002035 break;
Radek Krejci76512572015-08-04 09:47:08 +02002036 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002037 case LYS_ANYDATA:
2038 trg_must = &((struct lys_node_anydata *)dev_target)->must;
2039 trg_must_size = &((struct lys_node_anydata *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02002040 break;
2041 default:
Radek Krejcid5a5c282016-08-15 15:38:08 +02002042 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "must");
2043 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"must\" property.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002044 goto error;
2045 }
2046
Michal Vaskoe9914d12016-10-07 14:32:37 +02002047 dev_target->flags &= ~LYS_VALID_DEP;
Michal Vasko508a50d2016-09-07 14:50:33 +02002048
Radek Krejcieb00f512015-07-01 16:44:58 +02002049 if (d->mod == LY_DEVIATE_RPL) {
Michal Vaskof7e57d52016-03-07 11:31:09 +01002050 /* replace must is forbidden */
Radek Krejci48464ed2016-03-17 15:44:09 +01002051 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "must", "deviate replace");
Michal Vaskof7e57d52016-03-07 11:31:09 +01002052 goto error;
Radek Krejcieb00f512015-07-01 16:44:58 +02002053 } else if (d->mod == LY_DEVIATE_ADD) {
2054 /* reallocate the must array of the target */
Michal Vasko253035f2015-12-17 16:58:13 +01002055 d->must = ly_realloc(*trg_must, (c_must + *trg_must_size) * sizeof *d->must);
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002056 if (!d->must) {
2057 LOGMEM;
2058 goto error;
2059 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002060 *trg_must = d->must;
Michal Vasko979ad5b2015-10-23 10:12:55 +02002061 d->must = &((*trg_must)[*trg_must_size]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002062 d->must_size = c_must;
2063 } else { /* LY_DEVIATE_DEL */
2064 d->must = calloc(c_must, sizeof *d->must);
2065 }
Michal Vasko253035f2015-12-17 16:58:13 +01002066 if (!d->must) {
2067 LOGMEM;
2068 goto error;
2069 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002070 }
2071 if (c_uniq) {
Michal Vaskof7e57d52016-03-07 11:31:09 +01002072 /* replace unique is forbidden */
2073 if (d->mod == LY_DEVIATE_RPL) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002074 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "unique", "deviate replace");
Michal Vaskof7e57d52016-03-07 11:31:09 +01002075 goto error;
2076 }
2077
Radek Krejcieb00f512015-07-01 16:44:58 +02002078 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01002079 if (dev_target->nodetype != LYS_LIST) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02002080 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "unique");
2081 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Target node does not allow \"unique\" property.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002082 goto error;
2083 }
2084
Michal Vasko60f4b452016-02-12 11:02:55 +01002085 list = (struct lys_node_list *)dev_target;
Michal Vaskof7e57d52016-03-07 11:31:09 +01002086 if (d->mod == LY_DEVIATE_ADD) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002087 /* reallocate the unique array of the target */
Michal Vasko253035f2015-12-17 16:58:13 +01002088 d->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *d->unique);
Radek Krejcieb00f512015-07-01 16:44:58 +02002089 list->unique = d->unique;
2090 d->unique = &list->unique[list->unique_size];
2091 d->unique_size = c_uniq;
2092 } else { /* LY_DEVIATE_DEL */
2093 d->unique = calloc(c_uniq, sizeof *d->unique);
2094 }
Michal Vasko253035f2015-12-17 16:58:13 +01002095 if (!d->unique) {
2096 LOGMEM;
2097 goto error;
2098 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002099 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02002100 if (c_dflt) {
2101 if (d->mod == LY_DEVIATE_ADD) {
2102 /* check that there is no current value */
2103 if ((dev_target->nodetype == LYS_LEAF && ((struct lys_node_leaf *)dev_target)->dflt) ||
2104 (dev_target->nodetype == LYS_CHOICE && ((struct lys_node_choice *)dev_target)->dflt)) {
2105 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "default");
2106 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Adding property that already exists.");
2107 goto error;
2108 }
2109
2110 /* check collision with mandatory/min-elements */
2111 if ((dev_target->flags & LYS_MAND_TRUE) ||
2112 (dev_target->nodetype == LYS_LEAFLIST && ((struct lys_node_leaflist *)dev_target)->min)) {
2113 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, child->name, child->parent->name);
2114 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
2115 "Adding the \"default\" statement is forbidden on %s statement.",
2116 (dev_target->flags & LYS_MAND_TRUE) ? "nodes with the \"mandatory\"" : "leaflists with non-zero \"min-elements\"");
2117 goto error;
2118 }
2119 } else if (d->mod == LY_DEVIATE_RPL) {
2120 /* check that there was a value before */
2121 if (((dev_target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && !((struct lys_node_leaf *)dev_target)->dflt) ||
2122 (dev_target->nodetype == LYS_CHOICE && !((struct lys_node_choice *)dev_target)->dflt)) {
2123 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
2124 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Replacing a property that does not exist.");
2125 goto error;
2126 }
2127 }
2128
2129 if (dev_target->nodetype == LYS_LEAFLIST) {
2130 /* reallocate default list in the target */
2131 llist = (struct lys_node_leaflist *)dev_target;
2132 if (d->mod == LY_DEVIATE_ADD) {
2133 /* reallocate (enlarge) the unique array of the target */
2134 llist->dflt = ly_realloc(llist->dflt, (c_dflt + llist->dflt_size) * sizeof *d->dflt);
2135 } else if (d->mod == LY_DEVIATE_RPL) {
2136 /* reallocate (replace) the unique array of the target */
2137 for (i = 0; i < llist->dflt_size; i++) {
2138 lydict_remove(llist->module->ctx, llist->dflt[i]);
2139 }
2140 llist->dflt = ly_realloc(llist->dflt, c_dflt * sizeof *d->dflt);
2141 llist->dflt_size = 0;
2142 }
2143 }
2144 d->dflt = calloc(c_dflt, sizeof *d->dflt);
2145 if (!d->dflt) {
2146 LOGMEM;
2147 goto error;
2148 }
2149 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002150
2151 /* process deviation properties with 0..n cardinality */
Radek Krejci73adb602015-07-02 18:07:40 +02002152 LY_TREE_FOR(develem->child, child) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002153 if (!strcmp(child->name, "must")) {
2154 if (d->mod == LY_DEVIATE_DEL) {
2155 if (fill_yin_must(module, child, &d->must[d->must_size])) {
2156 goto error;
2157 }
2158
2159 /* find must to delete, we are ok with just matching conditions */
2160 for (i = 0; i < *trg_must_size; i++) {
Radek Krejci749190d2016-02-18 16:26:25 +01002161 if (ly_strequal(d->must[d->must_size].expr, (*trg_must)[i].expr, 1)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002162 /* we have a match, free the must structure ... */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002163 lys_restr_free(ctx, &((*trg_must)[i]));
Radek Krejcieb00f512015-07-01 16:44:58 +02002164 /* ... and maintain the array */
2165 (*trg_must_size)--;
2166 if (i != *trg_must_size) {
2167 (*trg_must)[i].expr = (*trg_must)[*trg_must_size].expr;
2168 (*trg_must)[i].dsc = (*trg_must)[*trg_must_size].dsc;
2169 (*trg_must)[i].ref = (*trg_must)[*trg_must_size].ref;
2170 (*trg_must)[i].eapptag = (*trg_must)[*trg_must_size].eapptag;
2171 (*trg_must)[i].emsg = (*trg_must)[*trg_must_size].emsg;
2172 }
2173 if (!(*trg_must_size)) {
2174 free(*trg_must);
2175 *trg_must = NULL;
2176 } else {
2177 (*trg_must)[*trg_must_size].expr = NULL;
2178 (*trg_must)[*trg_must_size].dsc = NULL;
2179 (*trg_must)[*trg_must_size].ref = NULL;
2180 (*trg_must)[*trg_must_size].eapptag = NULL;
2181 (*trg_must)[*trg_must_size].emsg = NULL;
2182 }
2183
2184 i = -1; /* set match flag */
2185 break;
2186 }
2187 }
2188 d->must_size++;
2189 if (i != -1) {
2190 /* no match found */
Radek Krejci48464ed2016-03-17 15:44:09 +01002191 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL,
Radek Krejciadb57612016-02-16 13:34:34 +01002192 d->must[d->must_size - 1].expr, child->name);
Radek Krejci48464ed2016-03-17 15:44:09 +01002193 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value does not match any must from the target.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002194 goto error;
2195 }
2196 } else { /* replace or add */
Michal Vaskof92a7282016-02-11 12:35:57 +01002197 memset(&((*trg_must)[*trg_must_size]), 0, sizeof **trg_must);
2198 if (fill_yin_must(module, child, &((*trg_must)[*trg_must_size]))) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002199 goto error;
2200 }
2201 (*trg_must_size)++;
2202 }
Michal Vasko508a50d2016-09-07 14:50:33 +02002203
2204 /* check XPath dependencies again */
Michal Vaskoe8734262016-09-29 14:12:06 +02002205 if (*trg_must_size && unres_schema_add_node(module, unres, dev_target, UNRES_XPATH, NULL)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02002206 goto error;
2207 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002208 } else if (!strcmp(child->name, "unique")) {
2209 if (d->mod == LY_DEVIATE_DEL) {
Michal Vaskoa0a10ab2016-03-07 14:41:23 +01002210 memset(&d->unique[d->unique_size], 0, sizeof *d->unique);
Michal Vasko60f4b452016-02-12 11:02:55 +01002211 if (fill_yin_unique(module, dev_target, child, &d->unique[d->unique_size], NULL)) {
Radek Krejci581ce772015-11-10 17:22:40 +01002212 d->unique_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02002213 goto error;
2214 }
2215
2216 /* find unique structures to delete */
2217 for (i = 0; i < list->unique_size; i++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002218 if (list->unique[i].expr_size != d->unique[d->unique_size].expr_size) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002219 continue;
2220 }
2221
Radek Krejci581ce772015-11-10 17:22:40 +01002222 for (j = 0; j < d->unique[d->unique_size].expr_size; j++) {
Radek Krejci749190d2016-02-18 16:26:25 +01002223 if (!ly_strequal(list->unique[i].expr[j], d->unique[d->unique_size].expr[j], 1)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002224 break;
2225 }
2226 }
2227
Radek Krejci581ce772015-11-10 17:22:40 +01002228 if (j == d->unique[d->unique_size].expr_size) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002229 /* we have a match, free the unique structure ... */
Radek Krejci581ce772015-11-10 17:22:40 +01002230 for (j = 0; j < list->unique[i].expr_size; j++) {
Michal Vaskodc48e7f2016-02-11 12:35:27 +01002231 lydict_remove(ctx, list->unique[i].expr[j]);
Radek Krejci581ce772015-11-10 17:22:40 +01002232 }
2233 free(list->unique[i].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02002234 /* ... and maintain the array */
2235 list->unique_size--;
2236 if (i != list->unique_size) {
Radek Krejci581ce772015-11-10 17:22:40 +01002237 list->unique[i].expr_size = list->unique[list->unique_size].expr_size;
2238 list->unique[i].expr = list->unique[list->unique_size].expr;
Radek Krejcieb00f512015-07-01 16:44:58 +02002239 }
2240
2241 if (!list->unique_size) {
2242 free(list->unique);
2243 list->unique = NULL;
2244 } else {
Radek Krejci581ce772015-11-10 17:22:40 +01002245 list->unique[list->unique_size].expr_size = 0;
2246 list->unique[list->unique_size].expr = NULL;
Radek Krejcieb00f512015-07-01 16:44:58 +02002247 }
2248
2249 i = -1; /* set match flag */
2250 break;
2251 }
2252 }
2253
2254 d->unique_size++;
2255 if (i != -1) {
2256 /* no match found */
Radek Krejci48464ed2016-03-17 15:44:09 +01002257 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, lyxml_get_attr(child, "tag", NULL), child->name);
2258 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02002259 goto error;
2260 }
2261 } else { /* replace or add */
Michal Vaskoa0a10ab2016-03-07 14:41:23 +01002262 memset(&list->unique[list->unique_size], 0, sizeof *list->unique);
Michal Vasko60f4b452016-02-12 11:02:55 +01002263 i = fill_yin_unique(module, dev_target, child, &list->unique[list->unique_size], NULL);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002264 list->unique_size++;
2265 if (i) {
Radek Krejcieb00f512015-07-01 16:44:58 +02002266 goto error;
2267 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002268 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02002269 } else if (!strcmp(child->name, "default")) {
2270 GETVAL(value, child, "value");
2271 u = strlen(value);
2272 d->dflt[d->dflt_size++] = lydict_insert(module->ctx, value, u);
2273
2274 if (dev_target->nodetype == LYS_CHOICE) {
2275 choice = (struct lys_node_choice *)dev_target;
2276 rc = resolve_choice_default_schema_nodeid(value, choice->child, (const struct lys_node **)&node);
2277 if (rc || !node) {
2278 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2279 goto error;
2280 }
2281 if (d->mod == LY_DEVIATE_DEL) {
2282 if (!choice->dflt || (choice->dflt != node)) {
2283 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2284 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
2285 goto error;
2286 }
2287 } else { /* add or replace */
2288 choice->dflt = node;
2289 if (!choice->dflt) {
2290 /* default branch not found */
2291 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2292 goto error;
2293 }
2294 }
2295 } else if (dev_target->nodetype == LYS_LEAF) {
2296 leaf = (struct lys_node_leaf *)dev_target;
2297 if (d->mod == LY_DEVIATE_DEL) {
2298 if (!leaf->dflt || !ly_strequal(leaf->dflt, value, 1)) {
2299 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2300 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Value differs from the target being deleted.");
2301 goto error;
2302 }
2303 /* remove value */
2304 lydict_remove(ctx, leaf->dflt);
2305 leaf->dflt = NULL;
2306 } else { /* add (already checked) and replace */
2307 /* remove value */
2308 lydict_remove(ctx, leaf->dflt);
2309
2310 /* set new value */
2311 leaf->dflt = lydict_insert(ctx, value, u);
2312
2313 /* remember to check it later (it may not fit now, but the type can be deviated too) */
2314 ly_set_add(dflt_check, dev_target, 0);
2315 }
2316 } else { /* LYS_LEAFLIST */
2317 llist = (struct lys_node_leaflist *)dev_target;
2318 if (d->mod == LY_DEVIATE_DEL) {
2319 /* find and remove the value in target list */
2320 for (i = 0; i < llist->dflt_size; i++) {
2321 if (llist->dflt[i] && ly_strequal(llist->dflt[i], value, 1)) {
2322 /* match, remove the value */
2323 lydict_remove(llist->module->ctx, llist->dflt[i]);
2324 llist->dflt[i] = NULL;
2325 break;
2326 }
2327 }
2328 if (i == llist->dflt_size) {
2329 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2330 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The default value to delete not found in the target node.");
2331 goto error;
2332 }
2333 } else {
2334 /* add or replace, anyway we place items into the deviate's list
2335 which propagates to the target */
2336 /* we just want to check that the value isn't already in the list */
2337 for (i = 0; i < llist->dflt_size; i++) {
2338 if (ly_strequal(llist->dflt[i], value, 1)) {
2339 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2340 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
2341 goto error;
2342 }
2343 }
2344 /* store it in target node */
2345 llist->dflt[llist->dflt_size++] = lydict_insert(module->ctx, value, u);
2346
2347 /* remember to check it later (it may not fit now, but the type can be deviated too) */
2348 ly_set_add(dflt_check, dev_target, 0);
2349 }
2350 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002351 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002352 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02002353
2354 if (c_dflt && dev_target->nodetype == LYS_LEAFLIST && d->mod == LY_DEVIATE_DEL) {
2355 /* consolidate the final list in the target after removing items from it */
2356 llist = (struct lys_node_leaflist *)dev_target;
2357 for (i = j = 0; j < llist->dflt_size; j++) {
2358 llist->dflt[i] = llist->dflt[j];
2359 if (llist->dflt[i]) {
2360 i++;
2361 }
2362 }
2363 llist->dflt_size = i + 1;
2364 }
Radek Krejcieb00f512015-07-01 16:44:58 +02002365 }
2366
Michal Vasko43a1feb2016-03-07 12:03:02 +01002367 /* now check whether default value, if any, matches the type */
Pavol Vican85991ec2016-08-16 14:58:12 +02002368 for (u = 0; u < dflt_check->number; ++u) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02002369 value = NULL;
2370 rc = EXIT_SUCCESS;
2371 if (dflt_check->set.s[u]->nodetype == LYS_LEAF) {
2372 leaf = (struct lys_node_leaf *)dflt_check->set.s[u];
2373 rc = unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, value = leaf->dflt);
2374 } else { /* LYS_LEAFLIST */
2375 llist = (struct lys_node_leaflist *)dflt_check->set.s[u];
2376 for (j = 0; j < llist->dflt_size; j++) {
2377 rc = unres_schema_add_str(module, unres, &llist->type, UNRES_TYPE_DFLT, value = llist->dflt[j]);
2378 if (rc == -1) {
2379 break;
2380 }
Michal Vasko43a1feb2016-03-07 12:03:02 +01002381 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02002382
2383 }
2384 if (rc == -1) {
2385 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2386 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
2387 "The default value \"%s\" of the deviated node \"%s\"no longer matches its type.",
2388 dev->target_name);
2389 goto error;
Michal Vasko43a1feb2016-03-07 12:03:02 +01002390 }
2391 }
Michal Vasko43a1feb2016-03-07 12:03:02 +01002392
Radek Krejci27fe55e2016-09-13 17:13:35 +02002393 /* mark all the affected modules as deviated and implemented */
2394 for(parent = dev_target; parent; parent = lys_parent(parent)) {
2395 mod = lys_node_module(parent);
2396 if (module != mod) {
2397 mod->deviated = 1;
2398 lys_set_implemented(mod);
2399 }
2400 }
2401
Radek Krejcid5a5c282016-08-15 15:38:08 +02002402 ly_set_free(dflt_check);
Radek Krejcieb00f512015-07-01 16:44:58 +02002403 return EXIT_SUCCESS;
2404
2405error:
Radek Krejcid5a5c282016-08-15 15:38:08 +02002406 ly_set_free(dflt_check);
Radek Krejcieb00f512015-07-01 16:44:58 +02002407 return EXIT_FAILURE;
2408}
2409
Michal Vasko0d343d12015-08-24 14:57:36 +02002410/* logs directly */
Radek Krejcieb00f512015-07-01 16:44:58 +02002411static int
Radek Krejcib8048692015-08-05 13:36:34 +02002412fill_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 +02002413 struct unres_schema *unres)
Radek Krejci106efc02015-06-10 14:36:27 +02002414{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002415 const char *value;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002416 struct lyxml_elem *child, *next;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002417 struct lys_node *node;
Michal Vasko2d851a92015-10-20 16:16:36 +02002418 int c = 0, ret;
Radek Krejci106efc02015-06-10 14:36:27 +02002419
Michal Vasko591e0b22015-08-13 13:53:43 +02002420 aug->nodetype = LYS_AUGMENT;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002421 GETVAL(value, yin, "target-node");
Radek Krejci48464ed2016-03-17 15:44:09 +01002422 aug->target_name = transform_schema2json(module, value);
Michal Vasko488c19e2015-10-20 15:21:00 +02002423 if (!aug->target_name) {
2424 goto error;
2425 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002426 aug->parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02002427
Michal Vasko1d87a922015-08-21 12:57:16 +02002428 if (read_yin_common(module, NULL, (struct lys_node *)aug, yin, OPT_MODULE | OPT_NACMEXT)) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02002429 goto error;
2430 }
2431
2432 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002433 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
2434 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002435 lyxml_free(module->ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02002436 continue;
2437 }
2438
Radek Krejci3cf9e222015-06-18 11:37:50 +02002439 if (!strcmp(child->name, "if-feature")) {
2440 c++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002441 continue;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002442 } else if (!strcmp(child->name, "when")) {
2443 if (aug->when) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002444 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002445 goto error;
2446 }
2447
2448 aug->when = read_yin_when(module, child);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002449 if (!aug->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01002450 lyxml_free(module->ctx, child);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002451 goto error;
2452 }
Michal Vasko345da0a2015-12-02 10:35:55 +01002453 lyxml_free(module->ctx, child);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002454 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02002455
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002456 /* check allowed data sub-statements */
2457 } else if (!strcmp(child->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002458 node = read_yin_container(module, (struct lys_node *)aug, child, 1, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002459 } else if (!strcmp(child->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002460 node = read_yin_leaflist(module, (struct lys_node *)aug, child, 1, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002461 } else if (!strcmp(child->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002462 node = read_yin_leaf(module, (struct lys_node *)aug, child, 1, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002463 } else if (!strcmp(child->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002464 node = read_yin_list(module, (struct lys_node *)aug, child, 1, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002465 } else if (!strcmp(child->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02002466 node = read_yin_uses(module, (struct lys_node *)aug, child, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002467 } else if (!strcmp(child->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002468 node = read_yin_case(module, (struct lys_node *)aug, child, 1, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002469 } else if (!strcmp(child->name, "case")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002470 node = read_yin_case(module, (struct lys_node *)aug, child, 1, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002471 } else if (!strcmp(child->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002472 node = read_yin_anydata(module, (struct lys_node *)aug, child, LYS_ANYXML, 1, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02002473 } else if (!strcmp(child->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002474 node = read_yin_anydata(module, (struct lys_node *)aug, child, LYS_ANYDATA, 1, unres);
Michal Vaskoca7cbc42016-07-01 11:36:53 +02002475 } else if (!strcmp(child->name, "action")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002476 node = read_yin_rpc_action(module, (struct lys_node *)aug, child, unres);
Michal Vasko5acb5482016-09-16 14:35:14 +02002477 } else if (!strcmp(child->name, "notification")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002478 node = read_yin_notif(module, (struct lys_node *)aug, child, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002479 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002480 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002481 goto error;
2482 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002483
Radek Krejci1d82ef62015-08-07 14:44:40 +02002484 if (!node) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002485 goto error;
2486 }
2487
Radek Krejci1d82ef62015-08-07 14:44:40 +02002488 node = NULL;
Michal Vasko345da0a2015-12-02 10:35:55 +01002489 lyxml_free(module->ctx, child);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002490 }
2491
2492 if (c) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002493 aug->iffeature = calloc(c, sizeof *aug->iffeature);
2494 if (!aug->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01002495 LOGMEM;
2496 goto error;
2497 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002498 }
2499
2500 LY_TREE_FOR_SAFE(yin->child, next, child) {
2501 if (!strcmp(child->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02002502 ret = fill_yin_iffeature((struct lys_node *)aug, 0, child, &aug->iffeature[aug->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002503 aug->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01002504 if (ret) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002505 goto error;
2506 }
Michal Vasko345da0a2015-12-02 10:35:55 +01002507 lyxml_free(module->ctx, child);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002508 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002509 }
2510
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002511 /* aug->child points to the parsed nodes, they must now be
Michal Vasko49291b32015-08-06 09:49:41 +02002512 * connected to the tree and adjusted (if possible right now).
Radek Krejci27fe55e2016-09-13 17:13:35 +02002513 * However, if this is augment in a uses (parent is NULL), it gets resolved
Michal Vasko49291b32015-08-06 09:49:41 +02002514 * when the uses does and cannot be resolved now for sure
2515 * (the grouping was not yet copied into uses).
2516 */
Radek Krejci27fe55e2016-09-13 17:13:35 +02002517 if (!parent) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002518 if (unres_schema_add_node(module, unres, aug, UNRES_AUGMENT, NULL) == -1) {
Michal Vasko4adc10f2015-08-11 15:26:17 +02002519 goto error;
2520 }
Michal Vasko49291b32015-08-06 09:49:41 +02002521 }
Radek Krejci106efc02015-06-10 14:36:27 +02002522
Michal Vasko508a50d2016-09-07 14:50:33 +02002523 /* check XPath dependencies */
2524 if (aug->when && (unres_schema_add_node(module, unres, (struct lys_node *)aug, UNRES_XPATH, NULL) == -1)) {
2525 goto error;
2526 }
2527
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002528 return EXIT_SUCCESS;
Radek Krejci106efc02015-06-10 14:36:27 +02002529
2530error:
2531
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002532 return EXIT_FAILURE;
Radek Krejci106efc02015-06-10 14:36:27 +02002533}
2534
Michal Vasko0d343d12015-08-24 14:57:36 +02002535/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002536static int
Radek Krejci363bd4a2016-07-29 14:30:20 +02002537fill_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 +02002538{
Radek Krejci363bd4a2016-07-29 14:30:20 +02002539 struct lys_module *module;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002540 struct lyxml_elem *sub, *next;
2541 const char *value;
2542 char *endptr;
2543 int f_mand = 0, f_min = 0, f_max = 0;
Radek Krejci200bf712016-08-16 17:11:04 +02002544 int c_must = 0, c_ftrs = 0, c_dflt = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002545 int r;
2546 unsigned long int val;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002547
Radek Krejci363bd4a2016-07-29 14:30:20 +02002548 assert(uses);
2549 module = uses->module; /* shorthand */
2550
Michal Vaskoe022a562016-09-27 14:24:15 +02002551 if (read_yin_common(module, NULL, (struct lys_node *)rfn, yin, OPT_CFG_PARSE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002552 goto error;
2553 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002554
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002555 GETVAL(value, yin, "target-node");
Radek Krejci48464ed2016-03-17 15:44:09 +01002556 rfn->target_name = transform_schema2json(module, value);
Michal Vaskoa8b25952015-10-20 15:30:25 +02002557 if (!rfn->target_name) {
2558 goto error;
2559 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002560
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002561 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002562 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
2563 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002564 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02002565 continue;
2566 }
2567
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002568 /* limited applicability */
2569 if (!strcmp(sub->name, "default")) {
Radek Krejci200bf712016-08-16 17:11:04 +02002570 /* leaf, leaf-list or choice */
Radek Krejci3bde87f2015-06-05 16:51:58 +02002571
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002572 /* check possibility of statements combination */
2573 if (rfn->target_type) {
Radek Krejci200bf712016-08-16 17:11:04 +02002574 if (c_dflt) {
2575 /* multiple defaults are allowed only in leaf-list */
Pavol Vican35aa9ea2016-08-17 10:27:43 +02002576 if (module->version < 2) {
2577 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
2578 goto error;
2579 }
Radek Krejci200bf712016-08-16 17:11:04 +02002580 rfn->target_type &= LYS_LEAFLIST;
2581 } else {
Pavol Vican35aa9ea2016-08-17 10:27:43 +02002582 if (module->version < 2) {
2583 rfn->target_type &= (LYS_LEAF | LYS_CHOICE);
2584 } else {
2585 /* YANG 1.1 */
2586 rfn->target_type &= (LYS_LEAFLIST | LYS_LEAF | LYS_CHOICE);
2587 }
Radek Krejci200bf712016-08-16 17:11:04 +02002588 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002589 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002590 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
2591 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002592 goto error;
2593 }
2594 } else {
Pavol Vican35aa9ea2016-08-17 10:27:43 +02002595 if (module->version < 2) {
2596 rfn->target_type = LYS_LEAF | LYS_CHOICE;
2597 } else {
2598 /* YANG 1.1 */
2599 rfn->target_type = LYS_LEAFLIST | LYS_LEAF | LYS_CHOICE;
2600 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002601 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002602
Radek Krejci200bf712016-08-16 17:11:04 +02002603 c_dflt++;
Pavol Vican35aa9ea2016-08-17 10:27:43 +02002604 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002605 } else if (!strcmp(sub->name, "mandatory")) {
2606 /* leaf, choice or anyxml */
2607 if (f_mand) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002608 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002609 goto error;
2610 }
2611 /* just checking the flags in leaf is not sufficient, we would allow
2612 * multiple mandatory statements with the "false" value
2613 */
2614 f_mand = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002615
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002616 /* check possibility of statements combination */
2617 if (rfn->target_type) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002618 rfn->target_type &= (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002619 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002620 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
2621 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002622 goto error;
2623 }
2624 } else {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002625 rfn->target_type = LYS_LEAF | LYS_CHOICE | LYS_ANYDATA;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002626 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002627
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002628 GETVAL(value, sub, "value");
2629 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002630 rfn->flags |= LYS_MAND_TRUE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002631 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002632 rfn->flags |= LYS_MAND_FALSE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002633 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002634 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002635 goto error;
2636 }
2637 } else if (!strcmp(sub->name, "min-elements")) {
2638 /* list or leaf-list */
2639 if (f_min) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002640 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002641 goto error;
2642 }
2643 f_min = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002644
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002645 /* check possibility of statements combination */
2646 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002647 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002648 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002649 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
2650 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002651 goto error;
2652 }
2653 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002654 rfn->target_type = LYS_LIST | LYS_LEAFLIST;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002655 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002656
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002657 GETVAL(value, sub, "value");
2658 while (isspace(value[0])) {
2659 value++;
2660 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002661
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002662 /* convert it to uint32_t */
2663 errno = 0;
2664 endptr = NULL;
2665 val = strtoul(value, &endptr, 10);
2666 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002667 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002668 goto error;
2669 }
2670 rfn->mod.list.min = (uint32_t) val;
Radek Krejci0f04a6c2016-04-14 16:16:36 +02002671 rfn->flags |= LYS_RFN_MINSET;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002672 } else if (!strcmp(sub->name, "max-elements")) {
2673 /* list or leaf-list */
2674 if (f_max) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002675 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002676 goto error;
2677 }
2678 f_max = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002679
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002680 /* check possibility of statements combination */
2681 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002682 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002683 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002684 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
2685 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002686 goto error;
2687 }
2688 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002689 rfn->target_type = LYS_LIST | LYS_LEAFLIST;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002690 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002691
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002692 GETVAL(value, sub, "value");
2693 while (isspace(value[0])) {
2694 value++;
2695 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002696
Radek Krejci0d7b2472016-02-12 11:11:03 +01002697 if (!strcmp(value, "unbounded")) {
2698 rfn->mod.list.max = 0;
2699 } else {
2700 /* convert it to uint32_t */
2701 errno = 0;
2702 endptr = NULL;
2703 val = strtoul(value, &endptr, 10);
2704 if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002705 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01002706 goto error;
2707 }
2708 rfn->mod.list.max = (uint32_t) val;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002709 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +02002710 rfn->flags |= LYS_RFN_MAXSET;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002711 } else if (!strcmp(sub->name, "presence")) {
2712 /* container */
2713 if (rfn->mod.presence) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002714 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002715 goto error;
2716 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002717
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002718 /* check possibility of statements combination */
2719 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002720 rfn->target_type &= LYS_CONTAINER;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002721 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002722 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
2723 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002724 goto error;
2725 }
2726 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002727 rfn->target_type = LYS_CONTAINER;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002728 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002729
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002730 GETVAL(value, sub, "value");
2731 rfn->mod.presence = lydict_insert(module->ctx, value, strlen(value));
2732 } else if (!strcmp(sub->name, "must")) {
Radek Krejci363bd4a2016-07-29 14:30:20 +02002733 /* leafm leaf-list, list, container or anyxml */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002734 /* check possibility of statements combination */
2735 if (rfn->target_type) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002736 rfn->target_type &= (LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002737 if (!rfn->target_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002738 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
2739 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002740 goto error;
2741 }
2742 } else {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002743 rfn->target_type = LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002744 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002745
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002746 c_must++;
Radek Krejci41882de2015-07-02 16:34:58 +02002747 continue;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002748
Radek Krejci363bd4a2016-07-29 14:30:20 +02002749 } else if ((module->version >= 2) && !strcmp(sub->name, "if-feature")) {
2750 /* leaf, leaf-list, list, container or anyxml */
2751 /* check possibility of statements combination */
2752 if (rfn->target_type) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002753 rfn->target_type &= (LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA);
Radek Krejci363bd4a2016-07-29 14:30:20 +02002754 if (!rfn->target_type) {
2755 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, sub->name, yin->name);
2756 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid refine target nodetype for the substatements.");
2757 goto error;
2758 }
2759 } else {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002760 rfn->target_type = LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYDATA;
Radek Krejci363bd4a2016-07-29 14:30:20 +02002761 }
2762
2763 c_ftrs++;
2764 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002765 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002766 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002767 goto error;
2768 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002769
Michal Vasko345da0a2015-12-02 10:35:55 +01002770 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002771 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002772
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002773 /* process nodes with cardinality of 0..n */
2774 if (c_must) {
2775 rfn->must = calloc(c_must, sizeof *rfn->must);
Michal Vasko253035f2015-12-17 16:58:13 +01002776 if (!rfn->must) {
2777 LOGMEM;
2778 goto error;
2779 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002780 }
Radek Krejci363bd4a2016-07-29 14:30:20 +02002781 if (c_ftrs) {
Radek Krejci947e0342016-08-15 09:42:56 +02002782 rfn->iffeature = calloc(c_ftrs, sizeof *rfn->iffeature);
Radek Krejci363bd4a2016-07-29 14:30:20 +02002783 if (!rfn->iffeature) {
2784 LOGMEM;
Radek Krejci73adb602015-07-02 18:07:40 +02002785 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002786 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002787 }
Radek Krejci200bf712016-08-16 17:11:04 +02002788 if (c_dflt) {
Pavol Vican35aa9ea2016-08-17 10:27:43 +02002789 rfn->dflt = calloc(c_dflt, sizeof *rfn->dflt);
Radek Krejci200bf712016-08-16 17:11:04 +02002790 if (!rfn->dflt) {
2791 LOGMEM;
2792 goto error;
2793 }
2794 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002795
Radek Krejci363bd4a2016-07-29 14:30:20 +02002796 LY_TREE_FOR(yin->child, sub) {
2797 if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02002798 r = fill_yin_iffeature(uses, 0, sub, &rfn->iffeature[rfn->iffeature_size], unres);
Radek Krejci363bd4a2016-07-29 14:30:20 +02002799 rfn->iffeature_size++;
2800 if (r) {
2801 goto error;
2802 }
Radek Krejci200bf712016-08-16 17:11:04 +02002803 } else if (!strcmp(sub->name, "must")) {
Radek Krejci363bd4a2016-07-29 14:30:20 +02002804 r = fill_yin_must(module, sub, &rfn->must[rfn->must_size]);
2805 rfn->must_size++;
2806 if (r) {
2807 goto error;
2808 }
Radek Krejci200bf712016-08-16 17:11:04 +02002809 } else { /* default */
2810 GETVAL(value, sub, "value");
2811
2812 /* check for duplicity */
2813 for (r = 0; r < rfn->dflt_size; r++) {
2814 if (ly_strequal(rfn->dflt[r], value, 1)) {
2815 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
2816 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
2817 goto error;
2818 }
2819 }
2820 rfn->dflt[rfn->dflt_size++] = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci363bd4a2016-07-29 14:30:20 +02002821 }
2822 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002823
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002824 return EXIT_SUCCESS;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002825
2826error:
2827
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002828 return EXIT_FAILURE;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002829}
2830
Michal Vasko0d343d12015-08-24 14:57:36 +02002831/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002832static int
Radek Krejcib8048692015-08-05 13:36:34 +02002833fill_yin_import(struct lys_module *module, struct lyxml_elem *yin, struct lys_import *imp)
Radek Krejciefaeba32015-05-27 14:30:57 +02002834{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002835 struct lyxml_elem *child;
2836 const char *value;
Radek Krejciefaeba32015-05-27 14:30:57 +02002837
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002838 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002839 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
2840 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +02002841 continue;
2842 }
2843
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002844 if (!strcmp(child->name, "prefix")) {
2845 GETVAL(value, child, "value");
Radek Krejci48464ed2016-03-17 15:44:09 +01002846 if (lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002847 goto error;
2848 }
2849 imp->prefix = lydict_insert(module->ctx, value, strlen(value));
2850 } else if (!strcmp(child->name, "revision-date")) {
2851 if (imp->rev[0]) {
Radek Krejcid52195b2016-06-22 11:18:49 +02002852 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
2853 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002854 }
2855 GETVAL(value, child, "date");
Radek Krejci48464ed2016-03-17 15:44:09 +01002856 if (lyp_check_date(value)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002857 goto error;
2858 }
2859 memcpy(imp->rev, value, LY_REV_SIZE - 1);
Radek Krejci1a31efe2016-07-29 11:04:16 +02002860 } else if ((module->version >= 2) && !strcmp(child->name, "description")) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002861 if (imp->dsc) {
2862 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
2863 goto error;
2864 }
2865 imp->dsc = read_yin_subnode(module->ctx, child, "text");
2866 if (!imp->dsc) {
2867 goto error;
2868 }
Radek Krejci1a31efe2016-07-29 11:04:16 +02002869 } else if ((module->version >= 2) && !strcmp(child->name, "reference")) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002870 if (imp->ref) {
2871 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
2872 goto error;
2873 }
2874 imp->ref = read_yin_subnode(module->ctx, child, "text");
2875 if (!imp->ref) {
2876 goto error;
2877 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002878 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002879 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002880 goto error;
2881 }
2882 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002883
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002884 /* check mandatory information */
2885 if (!imp->prefix) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002886 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002887 goto error;
2888 }
Radek Krejcice7fb782015-05-29 16:52:34 +02002889
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002890 GETVAL(value, yin, "module");
Michal Vasko1b882eb2015-10-22 11:43:14 +02002891
Pavol Vicane994fda2016-03-22 10:47:58 +01002892 return lyp_check_import(module, value, imp);
Radek Krejcice7fb782015-05-29 16:52:34 +02002893
2894error:
2895
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002896 return EXIT_FAILURE;
Radek Krejciefaeba32015-05-27 14:30:57 +02002897}
2898
Radek Krejci5b2c8a82016-06-17 15:36:03 +02002899/* logs directly
2900 * returns:
2901 * 0 - inc successfully filled
2902 * -1 - error, inc is cleaned
2903 * 1 - duplication, ignore the inc structure, inc is cleaned
2904 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002905static int
Michal Vasko5ff78822016-02-12 09:33:31 +01002906fill_yin_include(struct lys_module *module, struct lys_submodule *submodule, struct lyxml_elem *yin,
2907 struct lys_include *inc, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02002908{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002909 struct lyxml_elem *child;
2910 const char *value;
Radek Krejciefaeba32015-05-27 14:30:57 +02002911
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002912 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002913 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
2914 /* garbage */
2915 continue;
2916 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002917 if (!strcmp(child->name, "revision-date")) {
2918 if (inc->rev[0]) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002919 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, "revision-date", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002920 goto error;
2921 }
2922 GETVAL(value, child, "date");
Radek Krejci48464ed2016-03-17 15:44:09 +01002923 if (lyp_check_date(value)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002924 goto error;
2925 }
2926 memcpy(inc->rev, value, LY_REV_SIZE - 1);
Radek Krejci1a31efe2016-07-29 11:04:16 +02002927 } else if ((module->version >= 2) && !strcmp(child->name, "description")) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002928 if (inc->dsc) {
2929 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
2930 goto error;
2931 }
2932 inc->dsc = read_yin_subnode(module->ctx, child, "text");
2933 if (!inc->dsc) {
2934 goto error;
2935 }
Radek Krejci1a31efe2016-07-29 11:04:16 +02002936 } else if ((module->version >= 2) && !strcmp(child->name, "reference")) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002937 if (inc->ref) {
2938 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
2939 goto error;
2940 }
2941 inc->ref = read_yin_subnode(module->ctx, child, "text");
2942 if (!inc->ref) {
2943 goto error;
2944 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002945 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01002946 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002947 goto error;
2948 }
2949 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002950
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002951 GETVAL(value, yin, "module");
Michal Vasko1b882eb2015-10-22 11:43:14 +02002952
Pavol Vican0adf01d2016-03-22 12:29:33 +01002953 return lyp_check_include(module, submodule, value, inc, unres);
Radek Krejcice7fb782015-05-29 16:52:34 +02002954
2955error:
2956
Radek Krejci83e3f5b2016-06-24 14:55:25 +02002957 return -1;
Radek Krejciefaeba32015-05-27 14:30:57 +02002958}
2959
Michal Vasko0d343d12015-08-24 14:57:36 +02002960/* logs directly
2961 *
Radek Krejcida04f4a2015-05-21 12:54:09 +02002962 * Covers:
Radek Krejci25d782a2015-05-22 15:03:23 +02002963 * description, reference, status, optionaly config
Radek Krejcib388c152015-06-04 17:03:03 +02002964 *
Radek Krejcida04f4a2015-05-21 12:54:09 +02002965 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002966static int
Radek Krejcib8048692015-08-05 13:36:34 +02002967read_yin_common(struct lys_module *module, struct lys_node *parent,
Radek Krejci1d82ef62015-08-07 14:44:40 +02002968 struct lys_node *node, struct lyxml_elem *xmlnode, int opt)
Radek Krejcida04f4a2015-05-21 12:54:09 +02002969{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002970 const char *value;
2971 struct lyxml_elem *sub, *next;
2972 struct ly_ctx *const ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002973
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002974 if (opt & OPT_MODULE) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002975 node->module = module;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002976 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002977
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002978 if (opt & OPT_IDENT) {
2979 GETVAL(value, xmlnode, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +01002980 if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002981 goto error;
2982 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02002983 node->name = lydict_insert(ctx, value, strlen(value));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002984 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002985
Radek Krejci6764bb32015-07-03 15:16:04 +02002986 /* inherit NACM flags */
Radek Krejci6a113852015-07-03 16:04:20 +02002987 if ((opt & OPT_NACMEXT) && parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002988 node->nacm = parent->nacm;
Radek Krejci6764bb32015-07-03 15:16:04 +02002989 }
2990
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002991 /* process local parameters */
2992 LY_TREE_FOR_SAFE(xmlnode->child, next, sub) {
Radek Krejci6764bb32015-07-03 15:16:04 +02002993 if (!sub->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002994 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002995 lyxml_free(ctx, sub);
Radek Krejci6764bb32015-07-03 15:16:04 +02002996 continue;
2997 }
2998 if (strcmp(sub->ns->value, LY_NSYIN)) {
2999 /* NACM extensions */
Radek Krejci6a113852015-07-03 16:04:20 +02003000 if ((opt & OPT_NACMEXT) && !strcmp(sub->ns->value, LY_NSNACM)) {
Radek Krejci6764bb32015-07-03 15:16:04 +02003001 if (!strcmp(sub->name, "default-deny-write")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003002 node->nacm |= LYS_NACM_DENYW;
Radek Krejci6764bb32015-07-03 15:16:04 +02003003 } else if (!strcmp(sub->name, "default-deny-all")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003004 node->nacm |= LYS_NACM_DENYA;
Radek Krejci6764bb32015-07-03 15:16:04 +02003005 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003006 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6764bb32015-07-03 15:16:04 +02003007 goto error;
3008 }
3009 }
3010
3011 /* else garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003012 lyxml_free(ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003013 continue;
3014 }
3015
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003016 if (!strcmp(sub->name, "description")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003017 if (node->dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003018 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003019 goto error;
3020 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003021 node->dsc = read_yin_subnode(ctx, sub, "text");
3022 if (!node->dsc) {
Radek Krejci73adb602015-07-02 18:07:40 +02003023 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003024 }
3025 } else if (!strcmp(sub->name, "reference")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003026 if (node->ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003027 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003028 goto error;
3029 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003030 node->ref = read_yin_subnode(ctx, sub, "text");
3031 if (!node->ref) {
Radek Krejci73adb602015-07-02 18:07:40 +02003032 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003033 }
3034 } else if (!strcmp(sub->name, "status")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003035 if (node->flags & LYS_STATUS_MASK) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003036 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003037 goto error;
3038 }
3039 GETVAL(value, sub, "value");
3040 if (!strcmp(value, "current")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003041 node->flags |= LYS_STATUS_CURR;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003042 } else if (!strcmp(value, "deprecated")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003043 node->flags |= LYS_STATUS_DEPRC;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003044 } else if (!strcmp(value, "obsolete")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003045 node->flags |= LYS_STATUS_OBSLT;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003046 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003047 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci73adb602015-07-02 18:07:40 +02003048 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003049 }
Michal Vaskoe022a562016-09-27 14:24:15 +02003050 } else if ((opt & (OPT_CFG_PARSE | OPT_CFG_IGNORE)) && !strcmp(sub->name, "config")) {
3051 if (opt & OPT_CFG_PARSE) {
3052 if (node->flags & LYS_CONFIG_MASK) {
3053 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
3054 goto error;
3055 }
3056 GETVAL(value, sub, "value");
3057 if (!strcmp(value, "false")) {
3058 node->flags |= LYS_CONFIG_R;
3059 } else if (!strcmp(value, "true")) {
3060 node->flags |= LYS_CONFIG_W;
3061 } else {
3062 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
3063 goto error;
3064 }
3065 node->flags |= LYS_CONFIG_SET;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003066 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003067 } else {
Michal Vasko345da0a2015-12-02 10:35:55 +01003068 /* skip the lyxml_free */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003069 continue;
3070 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003071 lyxml_free(ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003072 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003073
Michal Vaskoe022a562016-09-27 14:24:15 +02003074 if ((opt & OPT_CFG_INHERIT) && !(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003075 /* get config flag from parent */
Radek Krejci32c7bd62016-04-14 17:47:04 +02003076 if (parent && (parent->flags & LYS_CONFIG_R)) {
3077 node->flags |= LYS_CONFIG_R;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003078 } else {
3079 /* default config is true */
Radek Krejci1d82ef62015-08-07 14:44:40 +02003080 node->flags |= LYS_CONFIG_W;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003081 }
3082 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003083
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003084 return EXIT_SUCCESS;
Radek Krejcieac35532015-05-31 19:09:15 +02003085
3086error:
3087
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003088 return EXIT_FAILURE;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003089}
3090
Michal Vasko0d343d12015-08-24 14:57:36 +02003091/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003092static struct lys_when *
Radek Krejcib8048692015-08-05 13:36:34 +02003093read_yin_when(struct lys_module *module, struct lyxml_elem *yin)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003094{
Radek Krejci76512572015-08-04 09:47:08 +02003095 struct lys_when *retval = NULL;
Radek Krejci73adb602015-07-02 18:07:40 +02003096 struct lyxml_elem *child;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003097 const char *value;
3098
3099 retval = calloc(1, sizeof *retval);
Michal Vasko253035f2015-12-17 16:58:13 +01003100 if (!retval) {
3101 LOGMEM;
3102 return NULL;
3103 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003104
3105 GETVAL(value, yin, "condition");
Radek Krejci48464ed2016-03-17 15:44:09 +01003106 retval->cond = transform_schema2json(module, value);
Michal Vaskof9893382015-10-09 14:03:04 +02003107 if (!retval->cond) {
3108 goto error;
3109 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003110
Radek Krejci73adb602015-07-02 18:07:40 +02003111 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003112 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
3113 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +02003114 continue;
3115 }
3116
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003117 if (!strcmp(child->name, "description")) {
3118 if (retval->dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003119 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003120 goto error;
3121 }
3122 retval->dsc = read_yin_subnode(module->ctx, child, "text");
3123 if (!retval->dsc) {
3124 goto error;
3125 }
3126 } else if (!strcmp(child->name, "reference")) {
3127 if (retval->ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003128 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003129 goto error;
3130 }
3131 retval->ref = read_yin_subnode(module->ctx, child, "text");
3132 if (!retval->ref) {
3133 goto error;
3134 }
3135 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003136 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003137 goto error;
3138 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003139 }
3140
3141 return retval;
3142
3143error:
3144
Michal Vasko0308dd62015-10-07 09:14:40 +02003145 lys_when_free(module->ctx, retval);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003146 return NULL;
3147}
3148
Michal Vasko0d343d12015-08-24 14:57:36 +02003149/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003150static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02003151read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02003152 struct unres_schema *unres)
Radek Krejcib4cf2022015-06-03 14:40:05 +02003153{
Michal Vasko29fc0182015-08-24 15:02:39 +02003154 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02003155 struct lys_node_case *cs;
3156 struct lys_node *retval, *node = NULL;
Michal Vasko2d851a92015-10-20 16:16:36 +02003157 int c_ftrs = 0, ret;
Radek Krejcib4cf2022015-06-03 14:40:05 +02003158
Radek Krejcie867c852015-08-27 09:52:34 +02003159 /* init */
3160 memset(&root, 0, sizeof root);
3161
Radek Krejci1d82ef62015-08-07 14:44:40 +02003162 cs = calloc(1, sizeof *cs);
Michal Vasko253035f2015-12-17 16:58:13 +01003163 if (!cs) {
3164 LOGMEM;
3165 return NULL;
3166 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003167 cs->nodetype = LYS_CASE;
3168 cs->prev = (struct lys_node *)cs;
3169 retval = (struct lys_node *)cs;
Radek Krejcib4cf2022015-06-03 14:40:05 +02003170
Michal Vaskoe022a562016-09-27 14:24:15 +02003171 if (read_yin_common(module, parent, retval, yin,
3172 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_INHERIT : 0) | OPT_NACMEXT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003173 goto error;
3174 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02003175
Radek Krejcia9544502015-08-14 08:24:29 +02003176 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
3177
Michal Vasko3a0043f2015-08-12 12:11:30 +02003178 /* insert the node into the schema tree */
Radek Krejcic4283442016-04-22 09:19:27 +02003179 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003180 goto error;
3181 }
3182
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003183 /* process choice's specific children */
3184 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003185 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3186 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003187 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003188 continue;
3189 }
3190
Michal Vasko29fc0182015-08-24 15:02:39 +02003191 if (!strcmp(sub->name, "container") ||
3192 !strcmp(sub->name, "leaf-list") ||
3193 !strcmp(sub->name, "leaf") ||
3194 !strcmp(sub->name, "list") ||
3195 !strcmp(sub->name, "uses") ||
3196 !strcmp(sub->name, "choice") ||
3197 !strcmp(sub->name, "anyxml")) {
3198
Michal Vaskof3930de2015-10-22 12:03:59 +02003199 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vasko29fc0182015-08-24 15:02:39 +02003200 lyxml_add_child(module->ctx, &root, sub);
Radek Krejci3cf9e222015-06-18 11:37:50 +02003201 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko29fc0182015-08-24 15:02:39 +02003202 c_ftrs++;
Michal Vasko345da0a2015-12-02 10:35:55 +01003203 /* skip lyxml_free() at the end of the loop, sub is processed later */
Michal Vasko29fc0182015-08-24 15:02:39 +02003204 continue;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003205 } else if (!strcmp(sub->name, "when")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003206 if (cs->when) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003207 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003208 goto error;
3209 }
3210
Radek Krejci1d82ef62015-08-07 14:44:40 +02003211 cs->when = read_yin_when(module, sub);
3212 if (!cs->when) {
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003213 goto error;
3214 }
Michal Vasko29fc0182015-08-24 15:02:39 +02003215
Michal Vasko345da0a2015-12-02 10:35:55 +01003216 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003217 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003218 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003219 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003220 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003221 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02003222
Radek Krejci3cf9e222015-06-18 11:37:50 +02003223 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003224 cs->iffeature = calloc(c_ftrs, sizeof *cs->iffeature);
3225 if (!cs->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01003226 LOGMEM;
3227 goto error;
3228 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003229 }
Radek Krejci73adb602015-07-02 18:07:40 +02003230 LY_TREE_FOR(yin->child, sub) {
Radek Krejci9de2c042016-10-19 16:53:06 +02003231 ret = fill_yin_iffeature(retval, 0, sub, &cs->iffeature[cs->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003232 cs->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01003233 if (ret) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003234 goto error;
3235 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003236 }
Radek Krejcib388c152015-06-04 17:03:03 +02003237
Michal Vasko29fc0182015-08-24 15:02:39 +02003238 /* last part - process data nodes */
3239 LY_TREE_FOR_SAFE(root.child, next, sub) {
3240 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003241 node = read_yin_container(module, retval, sub, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003242 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003243 node = read_yin_leaflist(module, retval, sub, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003244 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003245 node = read_yin_leaf(module, retval, sub, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003246 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003247 node = read_yin_list(module, retval, sub, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003248 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003249 node = read_yin_choice(module, retval, sub, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003250 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02003251 node = read_yin_uses(module, retval, sub, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003252 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003253 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02003254 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003255 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
Michal Vasko29fc0182015-08-24 15:02:39 +02003256 }
3257 if (!node) {
3258 goto error;
3259 }
3260
Michal Vasko345da0a2015-12-02 10:35:55 +01003261 lyxml_free(module->ctx, sub);
Michal Vasko29fc0182015-08-24 15:02:39 +02003262 }
3263
Michal Vasko508a50d2016-09-07 14:50:33 +02003264 /* check XPath dependencies */
3265 if (cs->when && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
3266 goto error;
3267 }
3268
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003269 return retval;
Radek Krejcib4cf2022015-06-03 14:40:05 +02003270
3271error:
3272
Michal Vasko29fc0182015-08-24 15:02:39 +02003273 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003274 lyxml_free(module->ctx, root.child);
Michal Vasko29fc0182015-08-24 15:02:39 +02003275 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003276 lys_node_free(retval, NULL, 0);
Radek Krejcib4cf2022015-06-03 14:40:05 +02003277
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003278 return NULL;
Radek Krejcib4cf2022015-06-03 14:40:05 +02003279}
3280
Michal Vasko0d343d12015-08-24 14:57:36 +02003281/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003282static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02003283read_yin_choice(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
3284 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003285{
Radek Krejci629cdef2016-06-06 15:06:36 +02003286 struct lyxml_elem *sub, *next, *dflt = NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003287 struct ly_ctx *const ctx = module->ctx;
Radek Krejci1d82ef62015-08-07 14:44:40 +02003288 struct lys_node *retval, *node = NULL;
Radek Krejcib8048692015-08-05 13:36:34 +02003289 struct lys_node_choice *choice;
Radek Krejci629cdef2016-06-06 15:06:36 +02003290 const char *value;
Michal Vasko2d851a92015-10-20 16:16:36 +02003291 int f_mand = 0, c_ftrs = 0, ret;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003292
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003293 choice = calloc(1, sizeof *choice);
Michal Vasko253035f2015-12-17 16:58:13 +01003294 if (!choice) {
3295 LOGMEM;
3296 return NULL;
3297 }
Radek Krejci76512572015-08-04 09:47:08 +02003298 choice->nodetype = LYS_CHOICE;
3299 choice->prev = (struct lys_node *)choice;
3300 retval = (struct lys_node *)choice;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003301
Michal Vaskoe022a562016-09-27 14:24:15 +02003302 if (read_yin_common(module, parent, retval, yin,
3303 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE)
3304 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003305 goto error;
3306 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003307
Radek Krejcia9544502015-08-14 08:24:29 +02003308 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
3309
Michal Vasko3a0043f2015-08-12 12:11:30 +02003310 /* insert the node into the schema tree */
Radek Krejcic4283442016-04-22 09:19:27 +02003311 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003312 goto error;
3313 }
3314
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003315 /* process choice's specific children */
3316 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003317 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3318 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003319 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003320 continue;
3321 }
3322
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003323 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003324 if (!(node = read_yin_container(module, retval, sub, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003325 goto error;
3326 }
3327 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003328 if (!(node = read_yin_leaflist(module, retval, sub, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003329 goto error;
3330 }
3331 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003332 if (!(node = read_yin_leaf(module, retval, sub, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003333 goto error;
3334 }
3335 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003336 if (!(node = read_yin_list(module, retval, sub, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003337 goto error;
3338 }
3339 } else if (!strcmp(sub->name, "case")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003340 if (!(node = read_yin_case(module, retval, sub, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003341 goto error;
3342 }
3343 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003344 if (!(node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres))) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02003345 goto error;
3346 }
3347 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003348 if (!(node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003349 goto error;
3350 }
3351 } else if (!strcmp(sub->name, "default")) {
Radek Krejci629cdef2016-06-06 15:06:36 +02003352 if (dflt) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003353 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003354 goto error;
3355 }
Radek Krejci629cdef2016-06-06 15:06:36 +02003356 dflt = sub;
3357 lyxml_unlink_elem(ctx, dflt, 0);
3358
Radek Krejcif9a312c2016-06-06 15:14:30 +02003359 continue;
3360 /* skip lyxml_free() at the end of the loop, the sub node is processed later as dflt */
3361
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003362 } else if (!strcmp(sub->name, "mandatory")) {
3363 if (f_mand) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003364 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003365 goto error;
3366 }
3367 /* just checking the flags in leaf is not sufficient, we would allow
3368 * multiple mandatory statements with the "false" value
3369 */
3370 f_mand = 1;
Radek Krejcib4cf2022015-06-03 14:40:05 +02003371
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003372 GETVAL(value, sub, "value");
3373 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003374 choice->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003375 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003376 choice->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003377 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003378 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003379 goto error;
3380 } /* else false is the default value, so we can ignore it */
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003381 } else if (!strcmp(sub->name, "when")) {
3382 if (choice->when) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003383 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003384 goto error;
3385 }
3386
3387 choice->when = read_yin_when(module, sub);
3388 if (!choice->when) {
3389 goto error;
3390 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003391 } else if (!strcmp(sub->name, "if-feature")) {
3392 c_ftrs++;
3393
Michal Vasko345da0a2015-12-02 10:35:55 +01003394 /* skip lyxml_free() at the end of the loop, the sub node is processed later */
Radek Krejci3cf9e222015-06-18 11:37:50 +02003395 continue;
Radek Krejci2f792db2016-09-12 10:52:33 +02003396 } else if (module->version >= 2 && !strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02003397 if (!(node = read_yin_choice(module, retval, sub, valid_config, unres))) {
Radek Krejci2f792db2016-09-12 10:52:33 +02003398 goto error;
3399 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003400 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003401 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003402 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003403 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02003404
Radek Krejci1d82ef62015-08-07 14:44:40 +02003405 node = NULL;
Michal Vasko345da0a2015-12-02 10:35:55 +01003406 lyxml_free(ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003407 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02003408
Radek Krejci3cf9e222015-06-18 11:37:50 +02003409 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003410 choice->iffeature = calloc(c_ftrs, sizeof *choice->iffeature);
3411 if (!choice->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01003412 LOGMEM;
3413 goto error;
3414 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003415 }
3416
Radek Krejci73adb602015-07-02 18:07:40 +02003417 LY_TREE_FOR(yin->child, sub) {
Radek Krejci9de2c042016-10-19 16:53:06 +02003418 ret = fill_yin_iffeature(retval, 0, sub, &choice->iffeature[choice->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003419 choice->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01003420 if (ret) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003421 goto error;
3422 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003423 }
3424
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003425 /* check - default is prohibited in combination with mandatory */
Radek Krejci629cdef2016-06-06 15:06:36 +02003426 if (dflt && (choice->flags & LYS_MAND_TRUE)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003427 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "default", "choice");
3428 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "The \"default\" statement is forbidden on choices with \"mandatory\".");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003429 goto error;
3430 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02003431
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003432 /* link default with the case */
Radek Krejci629cdef2016-06-06 15:06:36 +02003433 if (dflt) {
3434 GETVAL(value, dflt, "value");
3435 if (unres_schema_add_str(module, unres, choice, UNRES_CHOICE_DFLT, value) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003436 goto error;
3437 }
Radek Krejci629cdef2016-06-06 15:06:36 +02003438 lyxml_free(ctx, dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003439 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003440
Michal Vasko508a50d2016-09-07 14:50:33 +02003441 /* check XPath dependencies */
3442 if (choice->when && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
3443 goto error;
3444 }
3445
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003446 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003447
3448error:
3449
Radek Krejci629cdef2016-06-06 15:06:36 +02003450 lyxml_free(ctx, dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003451 lys_node_free(retval, NULL, 0);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003452
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003453 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003454}
3455
Michal Vasko0d343d12015-08-24 14:57:36 +02003456/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003457static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02003458read_yin_anydata(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, LYS_NODE type,
3459 int valid_config, struct unres_schema *unres)
Radek Krejci863c2852015-06-03 15:47:11 +02003460{
Radek Krejci76512572015-08-04 09:47:08 +02003461 struct lys_node *retval;
Radek Krejcibf2abff2016-08-23 15:51:52 +02003462 struct lys_node_anydata *anyxml;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003463 struct lyxml_elem *sub, *next;
3464 const char *value;
3465 int r;
3466 int f_mand = 0;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003467 int c_must = 0, c_ftrs = 0;
Radek Krejci863c2852015-06-03 15:47:11 +02003468
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003469 anyxml = calloc(1, sizeof *anyxml);
Michal Vasko253035f2015-12-17 16:58:13 +01003470 if (!anyxml) {
3471 LOGMEM;
3472 return NULL;
3473 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02003474 anyxml->nodetype = type;
Radek Krejci76512572015-08-04 09:47:08 +02003475 anyxml->prev = (struct lys_node *)anyxml;
3476 retval = (struct lys_node *)anyxml;
Radek Krejci863c2852015-06-03 15:47:11 +02003477
Michal Vaskoe022a562016-09-27 14:24:15 +02003478 if (read_yin_common(module, parent, retval, yin,
3479 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE)
3480 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003481 goto error;
3482 }
Radek Krejci863c2852015-06-03 15:47:11 +02003483
Radek Krejcia9544502015-08-14 08:24:29 +02003484 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02003485
Radek Krejcic189a952016-07-11 15:27:07 +02003486 /* insert the node into the schema tree */
Radek Krejcic4283442016-04-22 09:19:27 +02003487 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003488 goto error;
3489 }
3490
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003491 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003492 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3493 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003494 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003495 continue;
3496 }
3497
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003498 if (!strcmp(sub->name, "mandatory")) {
3499 if (f_mand) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003500 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003501 goto error;
3502 }
3503 /* just checking the flags in leaf is not sufficient, we would allow
3504 * multiple mandatory statements with the "false" value
3505 */
3506 f_mand = 1;
Radek Krejci863c2852015-06-03 15:47:11 +02003507
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003508 GETVAL(value, sub, "value");
3509 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003510 anyxml->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003511 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003512 anyxml->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003513 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003514 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003515 goto error;
3516 }
3517 /* else false is the default value, so we can ignore it */
Michal Vasko345da0a2015-12-02 10:35:55 +01003518 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003519 } else if (!strcmp(sub->name, "when")) {
3520 if (anyxml->when) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003521 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003522 goto error;
3523 }
3524
3525 anyxml->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003526 if (!anyxml->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003527 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003528 goto error;
3529 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003530 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003531 } else if (!strcmp(sub->name, "must")) {
3532 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003533 } else if (!strcmp(sub->name, "if-feature")) {
3534 c_ftrs++;
Radek Krejci863c2852015-06-03 15:47:11 +02003535
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003536 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003537 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003538 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003539 }
3540 }
Radek Krejci863c2852015-06-03 15:47:11 +02003541
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003542 /* middle part - process nodes with cardinality of 0..n */
3543 if (c_must) {
3544 anyxml->must = calloc(c_must, sizeof *anyxml->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003545 if (!anyxml->must) {
3546 LOGMEM;
3547 goto error;
3548 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003549 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003550 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003551 anyxml->iffeature = calloc(c_ftrs, sizeof *anyxml->iffeature);
3552 if (!anyxml->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01003553 LOGMEM;
3554 goto error;
3555 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003556 }
Radek Krejci863c2852015-06-03 15:47:11 +02003557
Radek Krejci73adb602015-07-02 18:07:40 +02003558 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003559 if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003560 r = fill_yin_must(module, sub, &anyxml->must[anyxml->must_size]);
3561 anyxml->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003562 if (r) {
3563 goto error;
3564 }
Radek Krejci0b24d752015-07-02 15:02:27 +02003565 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02003566 r = fill_yin_iffeature(retval, 0, sub, &anyxml->iffeature[anyxml->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003567 anyxml->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01003568 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003569 goto error;
3570 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003571 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003572 }
Radek Krejci863c2852015-06-03 15:47:11 +02003573
Michal Vasko508a50d2016-09-07 14:50:33 +02003574 /* check XPath dependencies */
3575 if ((anyxml->when || anyxml->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
3576 goto error;
3577 }
3578
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003579 return retval;
Radek Krejci863c2852015-06-03 15:47:11 +02003580
3581error:
3582
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003583 lys_node_free(retval, NULL, 0);
Radek Krejci863c2852015-06-03 15:47:11 +02003584
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003585 return NULL;
Radek Krejci863c2852015-06-03 15:47:11 +02003586}
3587
Michal Vasko0d343d12015-08-24 14:57:36 +02003588/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003589static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02003590read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02003591 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003592{
Radek Krejci76512572015-08-04 09:47:08 +02003593 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003594 struct lys_node_leaf *leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003595 struct lyxml_elem *sub, *next;
3596 const char *value;
Radek Krejci48464ed2016-03-17 15:44:09 +01003597 int r, has_type = 0;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003598 int c_must = 0, c_ftrs = 0, f_mand = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003599
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003600 leaf = calloc(1, sizeof *leaf);
Michal Vasko253035f2015-12-17 16:58:13 +01003601 if (!leaf) {
3602 LOGMEM;
3603 return NULL;
3604 }
Radek Krejci76512572015-08-04 09:47:08 +02003605 leaf->nodetype = LYS_LEAF;
3606 leaf->prev = (struct lys_node *)leaf;
3607 retval = (struct lys_node *)leaf;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003608
Michal Vaskoe022a562016-09-27 14:24:15 +02003609 if (read_yin_common(module, parent, retval, yin,
3610 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE)
3611 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003612 goto error;
3613 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003614
Radek Krejcia9544502015-08-14 08:24:29 +02003615 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02003616
Radek Krejcic189a952016-07-11 15:27:07 +02003617 /* insert the node into the schema tree */
Radek Krejcic4283442016-04-22 09:19:27 +02003618 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003619 goto error;
3620 }
3621
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003622 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003623 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3624 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003625 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003626 continue;
3627 }
3628
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003629 if (!strcmp(sub->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +01003630 if (has_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003631 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003632 goto error;
3633 }
Michal Vasko88c29542015-11-27 14:57:53 +01003634 /* HACK for unres */
3635 leaf->type.der = (struct lys_tpdf *)sub;
Radek Krejcicf509982015-12-15 09:22:44 +01003636 leaf->type.parent = (struct lys_tpdf *)leaf;
Radek Krejcicbb473e2016-09-16 14:48:32 +02003637 /* postpone type resolution when if-feature parsing is done since we need
3638 * if-feature for check_leafref_features() */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003639 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003640 } else if (!strcmp(sub->name, "default")) {
3641 if (leaf->dflt) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003642 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003643 goto error;
3644 }
3645 GETVAL(value, sub, "value");
3646 leaf->dflt = lydict_insert(module->ctx, value, strlen(value));
3647 } else if (!strcmp(sub->name, "units")) {
3648 if (leaf->units) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003649 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003650 goto error;
3651 }
3652 GETVAL(value, sub, "name");
3653 leaf->units = lydict_insert(module->ctx, value, strlen(value));
3654 } else if (!strcmp(sub->name, "mandatory")) {
3655 if (f_mand) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003656 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003657 goto error;
3658 }
3659 /* just checking the flags in leaf is not sufficient, we would allow
3660 * multiple mandatory statements with the "false" value
3661 */
3662 f_mand = 1;
Radek Krejci4c31f122015-06-02 14:51:22 +02003663
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003664 GETVAL(value, sub, "value");
3665 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003666 leaf->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003667 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003668 leaf->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003669 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003670 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003671 goto error;
3672 } /* else false is the default value, so we can ignore it */
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003673 } else if (!strcmp(sub->name, "when")) {
3674 if (leaf->when) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003675 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003676 goto error;
3677 }
3678
3679 leaf->when = read_yin_when(module, sub);
3680 if (!leaf->when) {
3681 goto error;
3682 }
3683
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003684 } else if (!strcmp(sub->name, "must")) {
Radek Krejci41882de2015-07-02 16:34:58 +02003685 c_must++;
3686 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003687 } else if (!strcmp(sub->name, "if-feature")) {
3688 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003689 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02003690
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003691 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003692 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003693 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003694 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003695
Michal Vasko88c29542015-11-27 14:57:53 +01003696 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003697 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003698
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003699 /* check mandatory parameters */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003700 if (!has_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003701 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003702 goto error;
3703 }
Michal Vasko478c4652016-07-21 12:55:01 +02003704 if (leaf->dflt && (leaf->flags & LYS_MAND_TRUE)) {
3705 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "mandatory", "leaf");
3706 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
3707 "The \"mandatory\" statement is forbidden on leaf with the \"default\" statement.");
3708 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003709 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003710
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003711 /* middle part - process nodes with cardinality of 0..n */
3712 if (c_must) {
3713 leaf->must = calloc(c_must, sizeof *leaf->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003714 if (!leaf->must) {
3715 LOGMEM;
3716 goto error;
3717 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003718 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003719 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003720 leaf->iffeature = calloc(c_ftrs, sizeof *leaf->iffeature);
3721 if (!leaf->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01003722 LOGMEM;
3723 goto error;
3724 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003725 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003726
Radek Krejci73adb602015-07-02 18:07:40 +02003727 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003728 if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003729 r = fill_yin_must(module, sub, &leaf->must[leaf->must_size]);
3730 leaf->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003731 if (r) {
3732 goto error;
3733 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003734 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02003735 r = fill_yin_iffeature(retval, 0, sub, &leaf->iffeature[leaf->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003736 leaf->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01003737 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003738 goto error;
3739 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003740 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003741 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003742
Radek Krejcicbb473e2016-09-16 14:48:32 +02003743 /* finalize type parsing */
3744 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DER, retval) == -1) {
3745 leaf->type.der = NULL;
3746 goto error;
3747 }
3748
3749 /* check default value (if not defined, there still could be some restrictions
3750 * that need to be checked against a default value from a derived type) */
3751 if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt) == -1) {
3752 goto error;
3753 }
3754
Michal Vasko508a50d2016-09-07 14:50:33 +02003755 /* check XPath dependencies */
3756 if ((leaf->when || leaf->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
3757 goto error;
3758 }
3759
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003760 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003761
3762error:
3763
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003764 lys_node_free(retval, NULL, 0);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003765
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003766 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003767}
3768
Michal Vasko0d343d12015-08-24 14:57:36 +02003769/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003770static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02003771read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02003772 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003773{
Radek Krejci76512572015-08-04 09:47:08 +02003774 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003775 struct lys_node_leaflist *llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003776 struct lyxml_elem *sub, *next;
3777 const char *value;
3778 char *endptr;
3779 unsigned long val;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003780 int r, has_type = 0;
Radek Krejcid5a5c282016-08-15 15:38:08 +02003781 int c_must = 0, c_ftrs = 0, c_dflt = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003782 int f_ordr = 0, f_min = 0, f_max = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003783
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003784 llist = calloc(1, sizeof *llist);
Michal Vasko253035f2015-12-17 16:58:13 +01003785 if (!llist) {
3786 LOGMEM;
3787 return NULL;
3788 }
Radek Krejci76512572015-08-04 09:47:08 +02003789 llist->nodetype = LYS_LEAFLIST;
3790 llist->prev = (struct lys_node *)llist;
3791 retval = (struct lys_node *)llist;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003792
Michal Vaskoe022a562016-09-27 14:24:15 +02003793 if (read_yin_common(module, parent, retval, yin,
3794 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE)
3795 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003796 goto error;
3797 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003798
Radek Krejcia9544502015-08-14 08:24:29 +02003799 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02003800
Radek Krejcic189a952016-07-11 15:27:07 +02003801 /* insert the node into the schema tree */
Radek Krejcic4283442016-04-22 09:19:27 +02003802 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003803 goto error;
3804 }
3805
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003806 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003807 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3808 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003809 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003810 continue;
3811 }
3812
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003813 if (!strcmp(sub->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +01003814 if (has_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003815 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003816 goto error;
3817 }
Michal Vasko88c29542015-11-27 14:57:53 +01003818 /* HACK for unres */
3819 llist->type.der = (struct lys_tpdf *)sub;
Radek Krejcicf509982015-12-15 09:22:44 +01003820 llist->type.parent = (struct lys_tpdf *)llist;
Radek Krejcicbb473e2016-09-16 14:48:32 +02003821 /* postpone type resolution when if-feature parsing is done since we need
3822 * if-feature for check_leafref_features() */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003823 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003824 } else if (!strcmp(sub->name, "units")) {
3825 if (llist->units) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003826 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003827 goto error;
3828 }
3829 GETVAL(value, sub, "name");
3830 llist->units = lydict_insert(module->ctx, value, strlen(value));
3831 } else if (!strcmp(sub->name, "ordered-by")) {
3832 if (f_ordr) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003833 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003834 goto error;
3835 }
3836 /* just checking the flags in llist is not sufficient, we would
3837 * allow multiple ordered-by statements with the "system" value
3838 */
3839 f_ordr = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003840
Radek Krejci1574a8d2015-08-03 14:16:52 +02003841 if (llist->flags & LYS_CONFIG_R) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003842 /* RFC 6020, 7.7.5 - ignore ordering when the list represents
3843 * state data
3844 */
Michal Vasko345da0a2015-12-02 10:35:55 +01003845 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003846 continue;
3847 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003848
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003849 GETVAL(value, sub, "value");
3850 if (!strcmp(value, "user")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003851 llist->flags |= LYS_USERORDERED;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003852 } else if (strcmp(value, "system")) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003853 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003854 goto error;
Radek Krejci41882de2015-07-02 16:34:58 +02003855 } /* else system is the default value, so we can ignore it */
3856
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003857 } else if (!strcmp(sub->name, "must")) {
3858 c_must++;
Radek Krejci41882de2015-07-02 16:34:58 +02003859 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003860 } else if (!strcmp(sub->name, "if-feature")) {
3861 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003862 continue;
Radek Krejcid5a5c282016-08-15 15:38:08 +02003863 } else if ((module->version >= 2) && !strcmp(sub->name, "default")) {
3864 c_dflt++;
3865 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02003866
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003867 } else if (!strcmp(sub->name, "min-elements")) {
3868 if (f_min) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003869 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003870 goto error;
3871 }
3872 f_min = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003873
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003874 GETVAL(value, sub, "value");
3875 while (isspace(value[0])) {
3876 value++;
3877 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003878
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003879 /* convert it to uint32_t */
3880 errno = 0;
3881 endptr = NULL;
3882 val = strtoul(value, &endptr, 10);
3883 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003884 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003885 goto error;
3886 }
3887 llist->min = (uint32_t) val;
Michal Vasko6ea3e362016-03-11 10:25:36 +01003888 if (llist->max && (llist->min > llist->max)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003889 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
3890 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Michal Vasko6ea3e362016-03-11 10:25:36 +01003891 goto error;
3892 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003893 } else if (!strcmp(sub->name, "max-elements")) {
3894 if (f_max) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003895 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003896 goto error;
3897 }
3898 f_max = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003899
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003900 GETVAL(value, sub, "value");
3901 while (isspace(value[0])) {
3902 value++;
3903 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003904
Radek Krejci0d7b2472016-02-12 11:11:03 +01003905 if (!strcmp(value, "unbounded")) {
3906 llist->max = 0;
3907 } else {
3908 /* convert it to uint32_t */
3909 errno = 0;
3910 endptr = NULL;
3911 val = strtoul(value, &endptr, 10);
3912 if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003913 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01003914 goto error;
3915 }
3916 llist->max = (uint32_t) val;
Michal Vasko6ea3e362016-03-11 10:25:36 +01003917 if (llist->min > llist->max) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003918 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
3919 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\".");
Michal Vasko6ea3e362016-03-11 10:25:36 +01003920 goto error;
3921 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003922 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003923 } else if (!strcmp(sub->name, "when")) {
3924 if (llist->when) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003925 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003926 goto error;
3927 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003928
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003929 llist->when = read_yin_when(module, sub);
3930 if (!llist->when) {
3931 goto error;
3932 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003933 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01003934 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003935 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003936 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003937
Michal Vasko88c29542015-11-27 14:57:53 +01003938 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003939 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003940
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003941 /* check constraints */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003942 if (!has_type) {
Radek Krejci48464ed2016-03-17 15:44:09 +01003943 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003944 goto error;
3945 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003946
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003947 /* middle part - process nodes with cardinality of 0..n */
3948 if (c_must) {
3949 llist->must = calloc(c_must, sizeof *llist->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003950 if (!llist->must) {
3951 LOGMEM;
3952 goto error;
3953 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003954 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003955 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003956 llist->iffeature = calloc(c_ftrs, sizeof *llist->iffeature);
3957 if (!llist->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01003958 LOGMEM;
3959 goto error;
3960 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003961 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02003962 if (c_dflt) {
3963 llist->dflt = calloc(c_dflt, sizeof *llist->dflt);
3964 if (!llist->dflt) {
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003965 LOGMEM;
3966 goto error;
3967 }
3968 }
3969
Radek Krejci73adb602015-07-02 18:07:40 +02003970 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003971 if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003972 r = fill_yin_must(module, sub, &llist->must[llist->must_size]);
3973 llist->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003974 if (r) {
3975 goto error;
3976 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003977 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02003978 r = fill_yin_iffeature(retval, 0, sub, &llist->iffeature[llist->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02003979 llist->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01003980 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003981 goto error;
3982 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02003983 } else if (!strcmp(sub->name, "default")) {
3984 GETVAL(value, sub, "value");
3985
Radek Krejciac1a52c2016-09-15 14:42:40 +02003986 /* check for duplicity in case of configuration data,
3987 * in case of status data duplicities are allowed */
3988 if (llist->flags & LYS_CONFIG_W) {
3989 for (r = 0; r < llist->dflt_size; r++) {
3990 if (ly_strequal(llist->dflt[r], value, 1)) {
3991 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "default");
3992 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Duplicated default value \"%s\".", value);
3993 goto error;
3994 }
Radek Krejcid5a5c282016-08-15 15:38:08 +02003995 }
3996 }
3997 llist->dflt[llist->dflt_size++] = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003998 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003999 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004000
Radek Krejcicbb473e2016-09-16 14:48:32 +02004001 /* finalize type parsing */
4002 if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DER, retval) == -1) {
4003 llist->type.der = NULL;
4004 goto error;
4005 }
4006
Radek Krejcid5a5c282016-08-15 15:38:08 +02004007 if (llist->dflt_size && llist->min) {
Radek Krejci200bf712016-08-16 17:11:04 +02004008 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_NONE, NULL, "min-elements", "leaf-list");
Radek Krejcid5a5c282016-08-15 15:38:08 +02004009 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL,
4010 "The \"min-elements\" statement with non-zero value is forbidden on leaf-lists with the \"default\" statement.");
4011 goto error;
4012 }
4013
4014 /* check default value (if not defined, there still could be some restrictions
4015 * that need to be checked against a default value from a derived type) */
4016 for (r = 0; r < llist->dflt_size; r++) {
4017 if (unres_schema_add_str(module, unres, &llist->type, UNRES_TYPE_DFLT, llist->dflt[r]) == -1) {
4018 goto error;
Radek Krejci25d782a2015-05-22 15:03:23 +02004019 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004020 }
4021
Michal Vasko508a50d2016-09-07 14:50:33 +02004022 /* check XPath dependencies */
4023 if ((llist->when || llist->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
4024 goto error;
4025 }
4026
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004027 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004028
4029error:
4030
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004031 lys_node_free(retval, NULL, 0);
Radek Krejcida04f4a2015-05-21 12:54:09 +02004032
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004033 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004034}
4035
Michal Vasko0d343d12015-08-24 14:57:36 +02004036/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004037static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02004038read_yin_list(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02004039 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02004040{
Radek Krejci1d82ef62015-08-07 14:44:40 +02004041 struct lys_node *retval, *node;
Radek Krejcib8048692015-08-05 13:36:34 +02004042 struct lys_node_list *list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004043 struct lyxml_elem *sub, *next, root, uniq;
Radek Krejci48464ed2016-03-17 15:44:09 +01004044 int r;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004045 int c_tpdf = 0, c_must = 0, c_uniq = 0, c_ftrs = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004046 int f_ordr = 0, f_max = 0, f_min = 0;
Radek Krejcieb00f512015-07-01 16:44:58 +02004047 const char *key_str = NULL, *value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004048 char *auxs;
4049 unsigned long val;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004050
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004051 /* init */
4052 memset(&root, 0, sizeof root);
4053 memset(&uniq, 0, sizeof uniq);
Radek Krejcie0674f82015-06-15 13:58:51 +02004054
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004055 list = calloc(1, sizeof *list);
Michal Vasko253035f2015-12-17 16:58:13 +01004056 if (!list) {
4057 LOGMEM;
4058 return NULL;
4059 }
Radek Krejci76512572015-08-04 09:47:08 +02004060 list->nodetype = LYS_LIST;
4061 list->prev = (struct lys_node *)list;
4062 retval = (struct lys_node *)list;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004063
Michal Vaskoe022a562016-09-27 14:24:15 +02004064 if (read_yin_common(module, parent, retval, yin,
4065 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE)
4066 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004067 goto error;
4068 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004069
Radek Krejcia9544502015-08-14 08:24:29 +02004070 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4071
Radek Krejcic189a952016-07-11 15:27:07 +02004072 /* insert the node into the schema tree */
4073 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
4074 goto error;
4075 }
4076
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004077 /* process list's specific children */
4078 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004079 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4080 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004081 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004082 continue;
4083 }
4084
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004085 /* data statements */
4086 if (!strcmp(sub->name, "container") ||
4087 !strcmp(sub->name, "leaf-list") ||
4088 !strcmp(sub->name, "leaf") ||
4089 !strcmp(sub->name, "list") ||
4090 !strcmp(sub->name, "choice") ||
4091 !strcmp(sub->name, "uses") ||
4092 !strcmp(sub->name, "grouping") ||
Michal Vaskoca7cbc42016-07-01 11:36:53 +02004093 !strcmp(sub->name, "anyxml") ||
Michal Vaskob4c608c2016-09-15 09:36:20 +02004094 !strcmp(sub->name, "action") ||
4095 !strcmp(sub->name, "notification")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004096 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004097 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02004098
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004099 /* array counters */
4100 } else if (!strcmp(sub->name, "key")) {
4101 /* check cardinality 0..1 */
4102 if (list->keys_size) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004103 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, list->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004104 goto error;
4105 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004106
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004107 /* count the number of keys */
4108 GETVAL(value, sub, "value");
4109 key_str = value;
4110 while ((value = strpbrk(value, " \t\n"))) {
4111 list->keys_size++;
4112 while (isspace(*value)) {
4113 value++;
4114 }
4115 }
4116 list->keys_size++;
4117 list->keys = calloc(list->keys_size, sizeof *list->keys);
Michal Vasko253035f2015-12-17 16:58:13 +01004118 if (!list->keys) {
4119 LOGMEM;
4120 goto error;
4121 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004122 } else if (!strcmp(sub->name, "unique")) {
4123 c_uniq++;
Michal Vaskof3930de2015-10-22 12:03:59 +02004124 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004125 lyxml_add_child(module->ctx, &uniq, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004126 } else if (!strcmp(sub->name, "typedef")) {
4127 c_tpdf++;
4128 } else if (!strcmp(sub->name, "must")) {
4129 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004130 } else if (!strcmp(sub->name, "if-feature")) {
4131 c_ftrs++;
Radek Krejci345ad742015-06-03 11:04:18 +02004132
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004133 /* optional stetments */
4134 } else if (!strcmp(sub->name, "ordered-by")) {
4135 if (f_ordr) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004136 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004137 goto error;
4138 }
4139 /* just checking the flags in llist is not sufficient, we would
4140 * allow multiple ordered-by statements with the "system" value
4141 */
4142 f_ordr = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02004143
Radek Krejci1574a8d2015-08-03 14:16:52 +02004144 if (list->flags & LYS_CONFIG_R) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004145 /* RFC 6020, 7.7.5 - ignore ordering when the list represents
4146 * state data
4147 */
Michal Vasko345da0a2015-12-02 10:35:55 +01004148 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004149 continue;
4150 }
Radek Krejci345ad742015-06-03 11:04:18 +02004151
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004152 GETVAL(value, sub, "value");
4153 if (!strcmp(value, "user")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02004154 list->flags |= LYS_USERORDERED;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004155 } else if (strcmp(value, "system")) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004156 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004157 goto error;
4158 }
4159 /* else system is the default value, so we can ignore it */
Michal Vasko345da0a2015-12-02 10:35:55 +01004160 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004161 } else if (!strcmp(sub->name, "min-elements")) {
4162 if (f_min) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004163 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004164 goto error;
4165 }
4166 f_min = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02004167
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004168 GETVAL(value, sub, "value");
4169 while (isspace(value[0])) {
4170 value++;
4171 }
Radek Krejci345ad742015-06-03 11:04:18 +02004172
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004173 /* convert it to uint32_t */
4174 errno = 0;
4175 auxs = NULL;
4176 val = strtoul(value, &auxs, 10);
4177 if (*auxs || value[0] == '-' || errno || val > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004178 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004179 goto error;
4180 }
4181 list->min = (uint32_t) val;
Michal Vasko6ea3e362016-03-11 10:25:36 +01004182 if (list->max && (list->min > list->max)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004183 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
4184 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Michal Vasko6ea3e362016-03-11 10:25:36 +01004185 lyxml_free(module->ctx, sub);
4186 goto error;
4187 }
Michal Vasko345da0a2015-12-02 10:35:55 +01004188 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004189 } else if (!strcmp(sub->name, "max-elements")) {
4190 if (f_max) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004191 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004192 goto error;
4193 }
4194 f_max = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02004195
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004196 GETVAL(value, sub, "value");
4197 while (isspace(value[0])) {
4198 value++;
4199 }
Radek Krejci345ad742015-06-03 11:04:18 +02004200
Radek Krejci0d7b2472016-02-12 11:11:03 +01004201 if (!strcmp(value, "unbounded")) {
4202 list->max = 0;;
4203 } else {
4204 /* convert it to uint32_t */
4205 errno = 0;
4206 auxs = NULL;
4207 val = strtoul(value, &auxs, 10);
4208 if (*auxs || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004209 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01004210 goto error;
4211 }
4212 list->max = (uint32_t) val;
Michal Vasko6ea3e362016-03-11 10:25:36 +01004213 if (list->min > list->max) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004214 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, sub->name);
4215 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "\"max-elements\" is smaller than \"min-elements\".");
Michal Vasko6ea3e362016-03-11 10:25:36 +01004216 goto error;
4217 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004218 }
Michal Vasko345da0a2015-12-02 10:35:55 +01004219 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004220 } else if (!strcmp(sub->name, "when")) {
4221 if (list->when) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004222 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004223 goto error;
4224 }
4225
4226 list->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004227 if (!list->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004228 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004229 goto error;
4230 }
Michal Vasko345da0a2015-12-02 10:35:55 +01004231 lyxml_free(module->ctx, sub);
Radek Krejci3cf9e222015-06-18 11:37:50 +02004232 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004233 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02004234 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004235 }
4236 }
Radek Krejci345ad742015-06-03 11:04:18 +02004237
Michal Vaskoe022a562016-09-27 14:24:15 +02004238 /* check - if list is configuration, key statement is mandatory
4239 * (but only if we are not in a grouping or augment, then the check is deferred) */
4240 for (node = retval; node && !(node->nodetype & (LYS_GROUPING | LYS_AUGMENT)); node = node->parent);
4241 if (!node && (list->flags & LYS_CONFIG_W) && !key_str) {
4242 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004243 goto error;
4244 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004245
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004246 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4247 if (c_tpdf) {
4248 list->tpdf = calloc(c_tpdf, sizeof *list->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004249 if (!list->tpdf) {
4250 LOGMEM;
4251 goto error;
4252 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004253 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004254 if (c_must) {
4255 list->must = calloc(c_must, sizeof *list->must);
Michal Vasko253035f2015-12-17 16:58:13 +01004256 if (!list->must) {
4257 LOGMEM;
4258 goto error;
4259 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004260 }
4261 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004262 list->iffeature = calloc(c_ftrs, sizeof *list->iffeature);
4263 if (!list->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01004264 LOGMEM;
4265 goto error;
4266 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004267 }
Radek Krejci73adb602015-07-02 18:07:40 +02004268 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004269 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004270 r = fill_yin_typedef(module, retval, sub, &list->tpdf[list->tpdf_size], unres);
4271 list->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004272 if (r) {
4273 goto error;
4274 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004275 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02004276 r = fill_yin_iffeature(retval, 0, sub, &list->iffeature[list->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004277 list->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01004278 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004279 goto error;
4280 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004281 } else if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004282 r = fill_yin_must(module, sub, &list->must[list->must_size]);
4283 list->must_size++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004284 if (r) {
4285 goto error;
4286 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004287 }
4288 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004289
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004290 /* last part - process data nodes */
4291 LY_TREE_FOR_SAFE(root.child, next, sub) {
4292 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004293 node = read_yin_container(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004294 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004295 node = read_yin_leaflist(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004296 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004297 node = read_yin_leaf(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004298 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004299 node = read_yin_list(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004300 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004301 node = read_yin_choice(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004302 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02004303 node = read_yin_uses(module, retval, sub, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004304 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004305 node = read_yin_grouping(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004306 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004307 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02004308 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004309 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
Michal Vaskoca7cbc42016-07-01 11:36:53 +02004310 } else if (!strcmp(sub->name, "action")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004311 node = read_yin_rpc_action(module, retval, sub, unres);
Michal Vaskob4c608c2016-09-15 09:36:20 +02004312 } else if (!strcmp(sub->name, "notification")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004313 node = read_yin_notif(module, retval, sub, unres);
Michal Vaskoc07187d2015-08-13 15:20:57 +02004314 } else {
4315 LOGINT;
4316 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004317 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004318 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004319 goto error;
4320 }
Radek Krejci73adb602015-07-02 18:07:40 +02004321
Michal Vasko345da0a2015-12-02 10:35:55 +01004322 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004323 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004324
Radek Krejci461efb92016-02-12 15:52:18 +01004325 if (key_str) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004326 if (unres_schema_add_str(module, unres, list, UNRES_LIST_KEYS, key_str) == -1) {
Radek Krejci461efb92016-02-12 15:52:18 +01004327 goto error;
4328 }
4329 } /* 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 +02004330
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004331 /* process unique statements */
4332 if (c_uniq) {
4333 list->unique = calloc(c_uniq, sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01004334 if (!list->unique) {
4335 LOGMEM;
4336 goto error;
4337 }
Radek Krejci1e9b9992015-06-04 17:57:04 +02004338
Radek Krejci461efb92016-02-12 15:52:18 +01004339 LY_TREE_FOR_SAFE(uniq.child, next, sub) {
4340 r = fill_yin_unique(module, retval, sub, &list->unique[list->unique_size], unres);
4341 list->unique_size++;
4342 if (r) {
4343 goto error;
4344 }
4345
4346 lyxml_free(module->ctx, sub);
4347 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004348 }
Radek Krejci1e9b9992015-06-04 17:57:04 +02004349
Michal Vasko508a50d2016-09-07 14:50:33 +02004350 /* check XPath dependencies */
4351 if ((list->when || list->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
4352 goto error;
4353 }
4354
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004355 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004356
4357error:
4358
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004359 lys_node_free(retval, NULL, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004360 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004361 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004362 }
4363 while (uniq.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004364 lyxml_free(module->ctx, uniq.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004365 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004366
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004367 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004368}
4369
Michal Vasko0d343d12015-08-24 14:57:36 +02004370/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004371static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02004372read_yin_container(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02004373 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02004374{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004375 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004376 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02004377 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004378 struct lys_node_container *cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004379 const char *value;
4380 int r;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004381 int c_tpdf = 0, c_must = 0, c_ftrs = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004382
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004383 /* init */
4384 memset(&root, 0, sizeof root);
Radek Krejcie0674f82015-06-15 13:58:51 +02004385
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004386 cont = calloc(1, sizeof *cont);
Michal Vasko253035f2015-12-17 16:58:13 +01004387 if (!cont) {
4388 LOGMEM;
4389 return NULL;
4390 }
Radek Krejci76512572015-08-04 09:47:08 +02004391 cont->nodetype = LYS_CONTAINER;
4392 cont->prev = (struct lys_node *)cont;
4393 retval = (struct lys_node *)cont;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004394
Michal Vaskoe022a562016-09-27 14:24:15 +02004395 if (read_yin_common(module, parent, retval, yin,
4396 OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE)
4397 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004398 goto error;
4399 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004400
Radek Krejcia9544502015-08-14 08:24:29 +02004401 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4402
Radek Krejcic189a952016-07-11 15:27:07 +02004403 /* insert the node into the schema tree */
4404 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
4405 goto error;
4406 }
4407
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004408 /* process container's specific children */
4409 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci6764bb32015-07-03 15:16:04 +02004410 if (!sub->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004411 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004412 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004413 continue;
4414 }
4415
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004416 if (!strcmp(sub->name, "presence")) {
4417 if (cont->presence) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004418 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004419 goto error;
4420 }
4421 GETVAL(value, sub, "value");
4422 cont->presence = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci800af702015-06-02 13:46:01 +02004423
Michal Vasko345da0a2015-12-02 10:35:55 +01004424 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004425 } else if (!strcmp(sub->name, "when")) {
4426 if (cont->when) {
Radek Krejci48464ed2016-03-17 15:44:09 +01004427 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004428 goto error;
4429 }
4430
4431 cont->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004432 if (!cont->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004433 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004434 goto error;
4435 }
Michal Vasko345da0a2015-12-02 10:35:55 +01004436 lyxml_free(module->ctx, sub);
Radek Krejci4c31f122015-06-02 14:51:22 +02004437
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004438 /* data statements */
4439 } else if (!strcmp(sub->name, "container") ||
4440 !strcmp(sub->name, "leaf-list") ||
4441 !strcmp(sub->name, "leaf") ||
4442 !strcmp(sub->name, "list") ||
4443 !strcmp(sub->name, "choice") ||
4444 !strcmp(sub->name, "uses") ||
4445 !strcmp(sub->name, "grouping") ||
Michal Vaskoca7cbc42016-07-01 11:36:53 +02004446 !strcmp(sub->name, "anyxml") ||
Michal Vaskob4c608c2016-09-15 09:36:20 +02004447 !strcmp(sub->name, "action") ||
4448 !strcmp(sub->name, "notification")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004449 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004450 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02004451
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004452 /* array counters */
4453 } else if (!strcmp(sub->name, "typedef")) {
4454 c_tpdf++;
4455 } else if (!strcmp(sub->name, "must")) {
4456 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004457 } else if (!strcmp(sub->name, "if-feature")) {
4458 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004459 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004460 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004461 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004462 }
4463 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004464
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004465 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4466 if (c_tpdf) {
4467 cont->tpdf = calloc(c_tpdf, sizeof *cont->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004468 if (!cont->tpdf) {
4469 LOGMEM;
4470 goto error;
4471 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004472 }
4473 if (c_must) {
4474 cont->must = calloc(c_must, sizeof *cont->must);
Michal Vasko253035f2015-12-17 16:58:13 +01004475 if (!cont->must) {
4476 LOGMEM;
4477 goto error;
4478 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004479 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004480 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004481 cont->iffeature = calloc(c_ftrs, sizeof *cont->iffeature);
4482 if (!cont->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01004483 LOGMEM;
4484 goto error;
4485 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004486 }
Radek Krejci800af702015-06-02 13:46:01 +02004487
Radek Krejci73adb602015-07-02 18:07:40 +02004488 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004489 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004490 r = fill_yin_typedef(module, retval, sub, &cont->tpdf[cont->tpdf_size], unres);
4491 cont->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004492 if (r) {
4493 goto error;
4494 }
4495 } else if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004496 r = fill_yin_must(module, sub, &cont->must[cont->must_size]);
4497 cont->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004498 if (r) {
4499 goto error;
4500 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004501 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02004502 r = fill_yin_iffeature(retval, 0, sub, &cont->iffeature[cont->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004503 cont->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01004504 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004505 goto error;
4506 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004507 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004508 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004509
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004510 /* last part - process data nodes */
4511 LY_TREE_FOR_SAFE(root.child, next, sub) {
4512 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004513 node = read_yin_container(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004514 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004515 node = read_yin_leaflist(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004516 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004517 node = read_yin_leaf(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004518 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004519 node = read_yin_list(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004520 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004521 node = read_yin_choice(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004522 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02004523 node = read_yin_uses(module, retval, sub, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004524 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004525 node = read_yin_grouping(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004526 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004527 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02004528 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004529 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
Michal Vaskoca7cbc42016-07-01 11:36:53 +02004530 } else if (!strcmp(sub->name, "action")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004531 node = read_yin_rpc_action(module, retval, sub, unres);
Michal Vaskob4c608c2016-09-15 09:36:20 +02004532 } else if (!strcmp(sub->name, "notification")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004533 node = read_yin_notif(module, retval, sub, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004534 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004535 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004536 goto error;
4537 }
Radek Krejci73adb602015-07-02 18:07:40 +02004538
Michal Vasko345da0a2015-12-02 10:35:55 +01004539 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004540 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004541
Michal Vasko508a50d2016-09-07 14:50:33 +02004542 /* check XPath dependencies */
4543 if ((cont->when || cont->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
4544 goto error;
4545 }
4546
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004547 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004548
4549error:
4550
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004551 lys_node_free(retval, NULL, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004552 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004553 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004554 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004555
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004556 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004557}
4558
Michal Vasko0d343d12015-08-24 14:57:36 +02004559/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004560static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02004561read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
Michal Vaskof02e3742015-08-05 16:27:02 +02004562 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02004563{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004564 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004565 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02004566 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004567 struct lys_node_grp *grp;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004568 int r;
4569 int c_tpdf = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004570
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004571 /* init */
4572 memset(&root, 0, sizeof root);
Radek Krejcie0674f82015-06-15 13:58:51 +02004573
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004574 grp = calloc(1, sizeof *grp);
Michal Vasko253035f2015-12-17 16:58:13 +01004575 if (!grp) {
4576 LOGMEM;
4577 return NULL;
4578 }
Radek Krejci76512572015-08-04 09:47:08 +02004579 grp->nodetype = LYS_GROUPING;
4580 grp->prev = (struct lys_node *)grp;
4581 retval = (struct lys_node *)grp;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004582
Michal Vaskoe022a562016-09-27 14:24:15 +02004583 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004584 goto error;
4585 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004586
Radek Krejcia9544502015-08-14 08:24:29 +02004587 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4588
Radek Krejcic189a952016-07-11 15:27:07 +02004589 /* insert the node into the schema tree */
4590 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
4591 goto error;
4592 }
4593
Radek Krejci1d82ef62015-08-07 14:44:40 +02004594 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004595 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4596 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004597 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004598 continue;
4599 }
4600
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004601 /* data statements */
4602 if (!strcmp(sub->name, "container") ||
4603 !strcmp(sub->name, "leaf-list") ||
4604 !strcmp(sub->name, "leaf") ||
4605 !strcmp(sub->name, "list") ||
4606 !strcmp(sub->name, "choice") ||
4607 !strcmp(sub->name, "uses") ||
4608 !strcmp(sub->name, "grouping") ||
Michal Vaskoca7cbc42016-07-01 11:36:53 +02004609 !strcmp(sub->name, "anyxml") ||
Michal Vasko5acb5482016-09-16 14:35:14 +02004610 !strcmp(sub->name, "action") ||
4611 !strcmp(sub->name, "notification")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004612 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004613 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02004614
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004615 /* array counters */
4616 } else if (!strcmp(sub->name, "typedef")) {
4617 c_tpdf++;
4618 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004619 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004620 goto error;
4621 }
4622 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004623
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004624 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4625 if (c_tpdf) {
4626 grp->tpdf = calloc(c_tpdf, sizeof *grp->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004627 if (!grp->tpdf) {
4628 LOGMEM;
4629 goto error;
4630 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004631 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004632 LY_TREE_FOR(yin->child, sub) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004633 r = fill_yin_typedef(module, retval, sub, &grp->tpdf[grp->tpdf_size], unres);
4634 grp->tpdf_size++;
Radek Krejci73adb602015-07-02 18:07:40 +02004635 if (r) {
4636 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004637 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004638 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004639
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004640 /* last part - process data nodes */
Michal Vasko919dbbc2016-05-19 15:22:45 +02004641 if (!root.child) {
4642 LOGWRN("Grouping \"%s\" without children.", retval->name);
4643 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004644 LY_TREE_FOR_SAFE(root.child, next, sub) {
4645 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004646 node = read_yin_container(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004647 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004648 node = read_yin_leaflist(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004649 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004650 node = read_yin_leaf(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004651 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004652 node = read_yin_list(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004653 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004654 node = read_yin_choice(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004655 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02004656 node = read_yin_uses(module, retval, sub, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004657 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004658 node = read_yin_grouping(module, retval, sub, valid_config, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004659 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004660 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02004661 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004662 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
Michal Vaskoca7cbc42016-07-01 11:36:53 +02004663 } else if (!strcmp(sub->name, "action")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004664 node = read_yin_rpc_action(module, retval, sub, unres);
Michal Vasko5acb5482016-09-16 14:35:14 +02004665 } else if (!strcmp(sub->name, "notification")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004666 node = read_yin_notif(module, retval, sub, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004667 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004668 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004669 goto error;
4670 }
Radek Krejci73adb602015-07-02 18:07:40 +02004671
Michal Vasko345da0a2015-12-02 10:35:55 +01004672 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004673 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004674
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004675 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004676
4677error:
4678
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004679 lys_node_free(retval, NULL, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004680 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004681 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004682 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004683
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004684 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004685}
4686
Michal Vasko0d343d12015-08-24 14:57:36 +02004687/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004688static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02004689read_yin_input_output(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +02004690 struct unres_schema *unres)
Michal Vasko38d01f72015-06-15 09:41:06 +02004691{
Radek Krejcie0674f82015-06-15 13:58:51 +02004692 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004693 struct lys_node *node = NULL;
Radek Krejci31fc30d2015-08-13 08:27:38 +02004694 struct lys_node *retval = NULL;
Michal Vasko44fb6382016-06-29 11:12:27 +02004695 struct lys_node_inout *inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02004696 int r;
Radek Krejci19332802016-07-29 10:39:46 +02004697 int c_tpdf = 0, c_must = 0;
Michal Vasko38d01f72015-06-15 09:41:06 +02004698
Radek Krejcie0674f82015-06-15 13:58:51 +02004699 /* init */
4700 memset(&root, 0, sizeof root);
4701
Michal Vasko38d01f72015-06-15 09:41:06 +02004702 inout = calloc(1, sizeof *inout);
Michal Vasko253035f2015-12-17 16:58:13 +01004703 if (!inout) {
4704 LOGMEM;
4705 return NULL;
4706 }
Radek Krejci6acc8012015-08-13 09:07:04 +02004707 inout->prev = (struct lys_node *)inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02004708
4709 if (!strcmp(yin->name, "input")) {
Radek Krejci76512572015-08-04 09:47:08 +02004710 inout->nodetype = LYS_INPUT;
Michal Vasko0a1aaa42016-04-19 09:48:25 +02004711 inout->name = lydict_insert(module->ctx, "input", 0);
Michal Vasko38d01f72015-06-15 09:41:06 +02004712 } else if (!strcmp(yin->name, "output")) {
Radek Krejci76512572015-08-04 09:47:08 +02004713 inout->nodetype = LYS_OUTPUT;
Michal Vasko0a1aaa42016-04-19 09:48:25 +02004714 inout->name = lydict_insert(module->ctx, "output", 0);
Michal Vasko38d01f72015-06-15 09:41:06 +02004715 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02004716 LOGINT;
Radek Krejci6acc8012015-08-13 09:07:04 +02004717 free(inout);
Michal Vasko0c888fd2015-08-11 15:54:08 +02004718 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02004719 }
4720
Radek Krejci76512572015-08-04 09:47:08 +02004721 retval = (struct lys_node *)inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02004722
Radek Krejci6a113852015-07-03 16:04:20 +02004723 if (read_yin_common(module, parent, retval, yin, OPT_MODULE | OPT_NACMEXT)) {
Michal Vaskoebeac942015-06-15 12:11:50 +02004724 goto error;
4725 }
4726
Radek Krejcia9544502015-08-14 08:24:29 +02004727 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4728
Radek Krejcic189a952016-07-11 15:27:07 +02004729 /* insert the node into the schema tree */
4730 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
4731 goto error;
4732 }
4733
Michal Vasko38d01f72015-06-15 09:41:06 +02004734 /* data statements */
4735 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004736 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4737 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004738 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004739 continue;
4740 }
4741
Michal Vasko38d01f72015-06-15 09:41:06 +02004742 if (!strcmp(sub->name, "container") ||
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004743 !strcmp(sub->name, "leaf-list") ||
4744 !strcmp(sub->name, "leaf") ||
4745 !strcmp(sub->name, "list") ||
4746 !strcmp(sub->name, "choice") ||
4747 !strcmp(sub->name, "uses") ||
4748 !strcmp(sub->name, "grouping") ||
4749 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004750 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004751 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004752
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004753 /* array counters */
Michal Vasko38d01f72015-06-15 09:41:06 +02004754 } else if (!strcmp(sub->name, "typedef")) {
4755 c_tpdf++;
Radek Krejcid2bfa792015-07-02 16:45:29 +02004756
Radek Krejci1a31efe2016-07-29 11:04:16 +02004757 } else if ((module->version >= 2) && !strcmp(sub->name, "must")) {
Radek Krejci19332802016-07-29 10:39:46 +02004758 c_must++;
4759
Michal Vasko38d01f72015-06-15 09:41:06 +02004760 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004761 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02004762 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02004763 }
4764 }
4765
4766 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4767 if (c_tpdf) {
4768 inout->tpdf = calloc(c_tpdf, sizeof *inout->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004769 if (!inout->tpdf) {
4770 LOGMEM;
4771 goto error;
4772 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004773 }
Radek Krejci19332802016-07-29 10:39:46 +02004774 if (c_must) {
4775 inout->must = calloc(c_must, sizeof *inout->must);
4776 if (!inout->must) {
4777 LOGMEM;
4778 goto error;
4779 }
4780 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004781
Radek Krejci73adb602015-07-02 18:07:40 +02004782 LY_TREE_FOR(yin->child, sub) {
Radek Krejci19332802016-07-29 10:39:46 +02004783 if (!strcmp(sub->name, "must")) {
4784 r = fill_yin_must(module, sub, &inout->must[inout->must_size]);
4785 inout->must_size++;
4786 if (r) {
4787 goto error;
4788 }
4789 } else { /* typedef */
4790 r = fill_yin_typedef(module, retval, sub, &inout->tpdf[inout->tpdf_size], unres);
4791 inout->tpdf_size++;
4792 if (r) {
4793 goto error;
4794 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004795 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004796 }
4797
4798 /* last part - process data nodes */
4799 LY_TREE_FOR_SAFE(root.child, next, sub) {
4800 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004801 node = read_yin_container(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004802 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004803 node = read_yin_leaflist(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004804 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004805 node = read_yin_leaf(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004806 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004807 node = read_yin_list(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004808 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004809 node = read_yin_choice(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004810 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02004811 node = read_yin_uses(module, retval, sub, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004812 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004813 node = read_yin_grouping(module, retval, sub, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004814 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004815 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, 0, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02004816 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004817 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, 0, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004818 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004819 if (!node) {
Michal Vasko38d01f72015-06-15 09:41:06 +02004820 goto error;
4821 }
Radek Krejci73adb602015-07-02 18:07:40 +02004822
Michal Vasko345da0a2015-12-02 10:35:55 +01004823 lyxml_free(module->ctx, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004824 }
4825
Michal Vasko508a50d2016-09-07 14:50:33 +02004826 /* check XPath dependencies */
4827 if (inout->must_size && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
4828 goto error;
4829 }
4830
Michal Vasko38d01f72015-06-15 09:41:06 +02004831 return retval;
4832
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004833error:
Michal Vasko38d01f72015-06-15 09:41:06 +02004834
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004835 lys_node_free(retval, NULL, 0);
Michal Vasko3a9abf82015-06-17 10:18:26 +02004836 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004837 lyxml_free(module->ctx, root.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02004838 }
4839
4840 return NULL;
4841}
4842
Michal Vasko0d343d12015-08-24 14:57:36 +02004843/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004844static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02004845read_yin_notif(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +02004846 struct unres_schema *unres)
Michal Vasko0ea41032015-06-16 08:53:55 +02004847{
Michal Vaskoc6551b32015-06-16 10:51:43 +02004848 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004849 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02004850 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004851 struct lys_node_notif *notif;
Michal Vasko0ea41032015-06-16 08:53:55 +02004852 int r;
Radek Krejci19332802016-07-29 10:39:46 +02004853 int c_tpdf = 0, c_ftrs = 0, c_must = 0;
Michal Vasko0ea41032015-06-16 08:53:55 +02004854
Michal Vasko6bb7fc12016-09-21 14:17:22 +02004855 if (parent && (module->version < 2)) {
4856 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "notification");
4857 return NULL;
4858 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004859
Michal Vaskoc6551b32015-06-16 10:51:43 +02004860 memset(&root, 0, sizeof root);
4861
Michal Vasko0ea41032015-06-16 08:53:55 +02004862 notif = calloc(1, sizeof *notif);
Michal Vasko253035f2015-12-17 16:58:13 +01004863 if (!notif) {
4864 LOGMEM;
4865 return NULL;
4866 }
Radek Krejci76512572015-08-04 09:47:08 +02004867 notif->nodetype = LYS_NOTIF;
4868 notif->prev = (struct lys_node *)notif;
4869 retval = (struct lys_node *)notif;
Michal Vasko0ea41032015-06-16 08:53:55 +02004870
Radek Krejci6a113852015-07-03 16:04:20 +02004871 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_NACMEXT)) {
Michal Vasko0ea41032015-06-16 08:53:55 +02004872 goto error;
4873 }
4874
Radek Krejcia9544502015-08-14 08:24:29 +02004875 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4876
Radek Krejcic189a952016-07-11 15:27:07 +02004877 /* insert the node into the schema tree */
4878 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
4879 goto error;
4880 }
4881
Michal Vasko0ea41032015-06-16 08:53:55 +02004882 /* process rpc's specific children */
4883 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004884 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4885 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004886 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004887 continue;
4888 }
4889
Michal Vasko0ea41032015-06-16 08:53:55 +02004890 /* data statements */
4891 if (!strcmp(sub->name, "container") ||
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004892 !strcmp(sub->name, "leaf-list") ||
4893 !strcmp(sub->name, "leaf") ||
4894 !strcmp(sub->name, "list") ||
4895 !strcmp(sub->name, "choice") ||
4896 !strcmp(sub->name, "uses") ||
4897 !strcmp(sub->name, "grouping") ||
4898 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004899 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004900 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko0ea41032015-06-16 08:53:55 +02004901
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004902 /* array counters */
Michal Vasko0ea41032015-06-16 08:53:55 +02004903 } else if (!strcmp(sub->name, "typedef")) {
4904 c_tpdf++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004905 } else if (!strcmp(sub->name, "if-feature")) {
4906 c_ftrs++;
Radek Krejci1a31efe2016-07-29 11:04:16 +02004907 } else if ((module->version >= 2) && !strcmp(sub->name, "must")) {
Radek Krejci19332802016-07-29 10:39:46 +02004908 c_must++;
Michal Vasko0ea41032015-06-16 08:53:55 +02004909 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01004910 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Michal Vasko0ea41032015-06-16 08:53:55 +02004911 goto error;
Michal Vasko0ea41032015-06-16 08:53:55 +02004912 }
4913 }
4914
4915 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4916 if (c_tpdf) {
4917 notif->tpdf = calloc(c_tpdf, sizeof *notif->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004918 if (!notif->tpdf) {
4919 LOGMEM;
4920 goto error;
4921 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004922 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004923 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004924 notif->iffeature = calloc(c_ftrs, sizeof *notif->iffeature);
4925 if (!notif->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01004926 LOGMEM;
4927 goto error;
4928 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004929 }
Radek Krejci19332802016-07-29 10:39:46 +02004930 if (c_must) {
4931 notif->must = calloc(c_must, sizeof *notif->must);
4932 if (!notif->must) {
Michal Vasko0ea41032015-06-16 08:53:55 +02004933 LOGMEM;
4934 goto error;
4935 }
4936 }
4937
Radek Krejci73adb602015-07-02 18:07:40 +02004938 LY_TREE_FOR(yin->child, sub) {
Michal Vasko0ea41032015-06-16 08:53:55 +02004939 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004940 r = fill_yin_typedef(module, retval, sub, &notif->tpdf[notif->tpdf_size], unres);
4941 notif->tpdf_size++;
Michal Vasko0ea41032015-06-16 08:53:55 +02004942 if (r) {
4943 goto error;
4944 }
Radek Krejci96299152016-06-22 10:17:50 +02004945 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02004946 r = fill_yin_iffeature(retval, 0, sub, &notif->iffeature[notif->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02004947 notif->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01004948 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004949 goto error;
4950 }
Radek Krejci19332802016-07-29 10:39:46 +02004951 } else if (!strcmp(sub->name, "must")) {
4952 r = fill_yin_must(module, sub, &notif->must[notif->must_size]);
4953 notif->must_size++;
Radek Krejci0b24d752015-07-02 15:02:27 +02004954 if (r) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02004955 goto error;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004956 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004957 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004958 }
4959
4960 /* last part - process data nodes */
4961 LY_TREE_FOR_SAFE(root.child, next, sub) {
4962 if (!strcmp(sub->name, "container")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004963 node = read_yin_container(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004964 } else if (!strcmp(sub->name, "leaf-list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004965 node = read_yin_leaflist(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004966 } else if (!strcmp(sub->name, "leaf")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004967 node = read_yin_leaf(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004968 } else if (!strcmp(sub->name, "list")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004969 node = read_yin_list(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004970 } else if (!strcmp(sub->name, "choice")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004971 node = read_yin_choice(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004972 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02004973 node = read_yin_uses(module, retval, sub, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004974 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004975 node = read_yin_grouping(module, retval, sub, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004976 } else if (!strcmp(sub->name, "anyxml")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004977 node = read_yin_anydata(module, retval, sub, LYS_ANYXML, 0, unres);
Radek Krejcibf2abff2016-08-23 15:51:52 +02004978 } else if (!strcmp(sub->name, "anydata")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02004979 node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, 0, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004980 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004981 if (!node) {
Michal Vasko0ea41032015-06-16 08:53:55 +02004982 goto error;
4983 }
Radek Krejci73adb602015-07-02 18:07:40 +02004984
Michal Vasko345da0a2015-12-02 10:35:55 +01004985 lyxml_free(module->ctx, sub);
Michal Vasko0ea41032015-06-16 08:53:55 +02004986 }
4987
Michal Vasko508a50d2016-09-07 14:50:33 +02004988 /* check XPath dependencies */
4989 if (notif->must_size && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
4990 goto error;
4991 }
4992
Michal Vasko0ea41032015-06-16 08:53:55 +02004993 return retval;
4994
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004995error:
Michal Vasko0ea41032015-06-16 08:53:55 +02004996
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004997 lys_node_free(retval, NULL, 0);
Michal Vasko0ea41032015-06-16 08:53:55 +02004998 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004999 lyxml_free(module->ctx, root.child);
Michal Vasko0ea41032015-06-16 08:53:55 +02005000 }
5001
5002 return NULL;
5003}
5004
Michal Vasko0d343d12015-08-24 14:57:36 +02005005/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02005006static struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02005007read_yin_rpc_action(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005008 struct unres_schema *unres)
Michal Vasko38d01f72015-06-15 09:41:06 +02005009{
Radek Krejcie0674f82015-06-15 13:58:51 +02005010 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005011 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02005012 struct lys_node *retval;
Michal Vasko44fb6382016-06-29 11:12:27 +02005013 struct lys_node_rpc_action *rpc;
Michal Vasko38d01f72015-06-15 09:41:06 +02005014 int r;
Pavol Vican3cb70c72016-09-05 11:32:52 +02005015 int c_tpdf = 0, c_ftrs = 0, c_input = 0, c_output = 0;
Michal Vasko38d01f72015-06-15 09:41:06 +02005016
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005017 if (!strcmp(yin->name, "action")) {
Radek Krejci1a31efe2016-07-29 11:04:16 +02005018 if (module->version < 2) {
Michal Vaskobb174852016-07-25 11:00:21 +02005019 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, "action");
5020 return NULL;
5021 }
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005022 for (node = parent; node; node = lys_parent(node)) {
Radek Krejcia3b19f72016-09-30 13:02:45 +02005023 if ((node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005024 || ((node->nodetype == LYS_LIST) && !((struct lys_node_list *)node)->keys_size)) {
5025 LOGVAL(LYE_INPAR, LY_VLOG_NONE, NULL, strnodetype(node->nodetype), "action");
5026 return NULL;
5027 }
5028 }
5029 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005030
Radek Krejcie0674f82015-06-15 13:58:51 +02005031 /* init */
5032 memset(&root, 0, sizeof root);
5033
Michal Vasko38d01f72015-06-15 09:41:06 +02005034 rpc = calloc(1, sizeof *rpc);
Michal Vasko253035f2015-12-17 16:58:13 +01005035 if (!rpc) {
5036 LOGMEM;
5037 return NULL;
5038 }
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005039 rpc->nodetype = (!strcmp(yin->name, "rpc") ? LYS_RPC : LYS_ACTION);
Radek Krejci76512572015-08-04 09:47:08 +02005040 rpc->prev = (struct lys_node *)rpc;
5041 retval = (struct lys_node *)rpc;
Michal Vasko38d01f72015-06-15 09:41:06 +02005042
Radek Krejci6a113852015-07-03 16:04:20 +02005043 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_NACMEXT)) {
Michal Vasko38d01f72015-06-15 09:41:06 +02005044 goto error;
5045 }
5046
Radek Krejcia9544502015-08-14 08:24:29 +02005047 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
5048
Radek Krejcic189a952016-07-11 15:27:07 +02005049 /* insert the node into the schema tree */
5050 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
5051 goto error;
5052 }
5053
Michal Vasko38d01f72015-06-15 09:41:06 +02005054 /* process rpc's specific children */
5055 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02005056 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
5057 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01005058 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02005059 continue;
5060 }
5061
Michal Vasko38d01f72015-06-15 09:41:06 +02005062 if (!strcmp(sub->name, "input")) {
Pavol Vican3cb70c72016-09-05 11:32:52 +02005063 if (c_input) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005064 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02005065 goto error;
5066 }
Pavol Vican3cb70c72016-09-05 11:32:52 +02005067 c_input++;
Michal Vaskof3930de2015-10-22 12:03:59 +02005068 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02005069 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02005070 } else if (!strcmp(sub->name, "output")) {
Pavol Vican3cb70c72016-09-05 11:32:52 +02005071 if (c_output) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005072 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02005073 goto error;
5074 }
Pavol Vican3cb70c72016-09-05 11:32:52 +02005075 c_output++;
Michal Vaskof3930de2015-10-22 12:03:59 +02005076 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02005077 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02005078
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005079 /* data statements */
Michal Vasko38d01f72015-06-15 09:41:06 +02005080 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02005081 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02005082 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02005083
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005084 /* array counters */
Michal Vasko38d01f72015-06-15 09:41:06 +02005085 } else if (!strcmp(sub->name, "typedef")) {
5086 c_tpdf++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005087 } else if (!strcmp(sub->name, "if-feature")) {
5088 c_ftrs++;
Michal Vasko38d01f72015-06-15 09:41:06 +02005089 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01005090 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02005091 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02005092 }
5093 }
5094
5095 /* middle part - process nodes with cardinality of 0..n except the data nodes */
5096 if (c_tpdf) {
5097 rpc->tpdf = calloc(c_tpdf, sizeof *rpc->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01005098 if (!rpc->tpdf) {
5099 LOGMEM;
5100 goto error;
5101 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005102 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005103 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005104 rpc->iffeature = calloc(c_ftrs, sizeof *rpc->iffeature);
5105 if (!rpc->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01005106 LOGMEM;
5107 goto error;
5108 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005109 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005110
Radek Krejci73adb602015-07-02 18:07:40 +02005111 LY_TREE_FOR(yin->child, sub) {
Michal Vasko38d01f72015-06-15 09:41:06 +02005112 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01005113 r = fill_yin_typedef(module, retval, sub, &rpc->tpdf[rpc->tpdf_size], unres);
5114 rpc->tpdf_size++;
Michal Vasko38d01f72015-06-15 09:41:06 +02005115 if (r) {
5116 goto error;
5117 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005118 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02005119 r = fill_yin_iffeature(retval, 0, sub, &rpc->iffeature[rpc->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005120 rpc->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01005121 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005122 goto error;
5123 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005124 }
Michal Vasko38d01f72015-06-15 09:41:06 +02005125 }
5126
5127 /* last part - process data nodes */
5128 LY_TREE_FOR_SAFE(root.child, next, sub) {
5129 if (!strcmp(sub->name, "grouping")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005130 node = read_yin_grouping(module, retval, sub, 0, unres);
Michal Vaskoca7cbc42016-07-01 11:36:53 +02005131 } else if (!strcmp(sub->name, "input") || !strcmp(sub->name, "output")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005132 node = read_yin_input_output(module, retval, sub, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02005133 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005134 if (!node) {
Michal Vasko38d01f72015-06-15 09:41:06 +02005135 goto error;
5136 }
Radek Krejci73adb602015-07-02 18:07:40 +02005137
Michal Vasko345da0a2015-12-02 10:35:55 +01005138 lyxml_free(module->ctx, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02005139 }
5140
Michal Vasko38d01f72015-06-15 09:41:06 +02005141 return retval;
5142
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005143error:
Michal Vasko38d01f72015-06-15 09:41:06 +02005144
Michal Vaskod51d6ad2016-02-16 13:24:31 +01005145 lys_node_free(retval, NULL, 0);
Michal Vasko3a9abf82015-06-17 10:18:26 +02005146 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005147 lyxml_free(module->ctx, root.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02005148 }
5149
5150 return NULL;
5151}
5152
Michal Vasko0d343d12015-08-24 14:57:36 +02005153/* logs directly
5154 *
Radek Krejci74705112015-06-05 10:25:44 +02005155 * resolve - referenced grouping should be bounded to the namespace (resolved)
5156 * only when uses does not appear in grouping. In a case of grouping's uses,
5157 * we just get information but we do not apply augment or refine to it.
5158 */
Radek Krejci76512572015-08-04 09:47:08 +02005159static struct lys_node *
Radek Krejci3440cc52016-06-23 17:03:59 +02005160read_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 +02005161{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005162 struct lyxml_elem *sub, *next;
Radek Krejci76512572015-08-04 09:47:08 +02005163 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02005164 struct lys_node_uses *uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005165 const char *value;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005166 int c_ref = 0, c_aug = 0, c_ftrs = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005167 int r;
Radek Krejci74705112015-06-05 10:25:44 +02005168
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005169 uses = calloc(1, sizeof *uses);
Michal Vasko253035f2015-12-17 16:58:13 +01005170 if (!uses) {
5171 LOGMEM;
5172 return NULL;
5173 }
Radek Krejci76512572015-08-04 09:47:08 +02005174 uses->nodetype = LYS_USES;
5175 uses->prev = (struct lys_node *)uses;
5176 retval = (struct lys_node *)uses;
Radek Krejci74705112015-06-05 10:25:44 +02005177
Radek Krejcia9544502015-08-14 08:24:29 +02005178 GETVAL(value, yin, "name");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005179 uses->name = lydict_insert(module->ctx, value, 0);
Radek Krejci106efc02015-06-10 14:36:27 +02005180
Michal Vaskoe0c59842015-09-24 13:52:20 +02005181 if (read_yin_common(module, parent, retval, yin, OPT_MODULE
Radek Krejci3440cc52016-06-23 17:03:59 +02005182 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005183 goto error;
5184 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02005185
Radek Krejcia9544502015-08-14 08:24:29 +02005186 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
5187
Radek Krejcic189a952016-07-11 15:27:07 +02005188 /* insert the node into the schema tree */
5189 if (lys_node_addchild(parent, lys_main_module(module), retval)) {
5190 goto error;
5191 }
5192
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005193 /* get other properties of uses */
Radek Krejcia9544502015-08-14 08:24:29 +02005194 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02005195 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
5196 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01005197 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02005198 continue;
5199 }
5200
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005201 if (!strcmp(sub->name, "refine")) {
5202 c_ref++;
5203 } else if (!strcmp(sub->name, "augment")) {
5204 c_aug++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005205 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci56e89772015-06-19 10:00:54 +02005206 c_ftrs++;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005207 } else if (!strcmp(sub->name, "when")) {
5208 if (uses->when) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005209 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005210 goto error;
5211 }
5212
5213 uses->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005214 if (!uses->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005215 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02005216 goto error;
5217 }
Michal Vasko345da0a2015-12-02 10:35:55 +01005218 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005219 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01005220 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005221 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005222 }
5223 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02005224
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005225 /* process properties with cardinality 0..n */
5226 if (c_ref) {
5227 uses->refine = calloc(c_ref, sizeof *uses->refine);
Michal Vasko253035f2015-12-17 16:58:13 +01005228 if (!uses->refine) {
5229 LOGMEM;
5230 goto error;
5231 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005232 }
5233 if (c_aug) {
5234 uses->augment = calloc(c_aug, sizeof *uses->augment);
Michal Vasko253035f2015-12-17 16:58:13 +01005235 if (!uses->augment) {
5236 LOGMEM;
5237 goto error;
5238 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005239 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005240 if (c_ftrs) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005241 uses->iffeature = calloc(c_ftrs, sizeof *uses->iffeature);
5242 if (!uses->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01005243 LOGMEM;
5244 goto error;
5245 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005246 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02005247
Radek Krejcia9544502015-08-14 08:24:29 +02005248 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005249 if (!strcmp(sub->name, "refine")) {
Radek Krejci363bd4a2016-07-29 14:30:20 +02005250 r = fill_yin_refine(retval, sub, &uses->refine[uses->refine_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01005251 uses->refine_size++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005252 if (r) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02005253 goto error;
5254 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005255 } else if (!strcmp(sub->name, "augment")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01005256 r = fill_yin_augment(module, retval, sub, &uses->augment[uses->augment_size], unres);
5257 uses->augment_size++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02005258 if (r) {
5259 goto error;
5260 }
5261 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci9de2c042016-10-19 16:53:06 +02005262 r = fill_yin_iffeature(retval, 0, sub, &uses->iffeature[uses->iffeature_size], unres);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02005263 uses->iffeature_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01005264 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005265 goto error;
5266 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005267 }
5268 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02005269
Radek Krejci48464ed2016-03-17 15:44:09 +01005270 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02005271 goto error;
5272 }
Radek Krejci74705112015-06-05 10:25:44 +02005273
Michal Vasko508a50d2016-09-07 14:50:33 +02005274 /* check XPath dependencies */
5275 if (uses->when && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
5276 goto error;
5277 }
5278
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005279 return retval;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02005280
5281error:
5282
Michal Vaskod51d6ad2016-02-16 13:24:31 +01005283 lys_node_free(retval, NULL, 0);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02005284
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005285 return NULL;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02005286}
5287
Michal Vasko0d343d12015-08-24 14:57:36 +02005288/* logs directly
5289 *
5290 * common code for yin_read_module() and yin_read_submodule()
5291 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005292static int
Radek Krejcic071c542016-01-27 14:57:51 +01005293read_sub_module(struct lys_module *module, struct lys_submodule *submodule, struct lyxml_elem *yin,
5294 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02005295{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005296 struct ly_ctx *ctx = module->ctx;
Michal Vasko2f7925f2015-10-21 15:06:56 +02005297 struct lyxml_elem *next, *child, *child2, root, grps, augs;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005298 struct lys_node *node = NULL;
Radek Krejcic071c542016-01-27 14:57:51 +01005299 struct lys_module *trg;
Pavol Vican974377b2016-03-23 00:38:53 +01005300 struct lys_include inc;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005301 const char *value;
Pavol Vican974377b2016-03-23 00:38:53 +01005302 int i, r;
Radek Krejci4dcd3392016-06-22 10:28:40 +02005303 size_t size;
Radek Krejcic071c542016-01-27 14:57:51 +01005304 int version_flag = 0;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005305 /* counters */
Radek Krejcieb00f512015-07-01 16:44:58 +02005306 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;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005307
Radek Krejcic071c542016-01-27 14:57:51 +01005308 /* to simplify code, store the module/submodule being processed as trg */
Michal Vaskofe7e5a72016-05-02 14:49:23 +02005309 trg = submodule ? (struct lys_module *)submodule : module;
Radek Krejcic071c542016-01-27 14:57:51 +01005310
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005311 /* init */
5312 memset(&root, 0, sizeof root);
5313 memset(&grps, 0, sizeof grps);
Michal Vasko2f7925f2015-10-21 15:06:56 +02005314 memset(&augs, 0, sizeof augs);
Radek Krejcie0674f82015-06-15 13:58:51 +02005315
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005316 /*
5317 * in the first run, we process elements with cardinality of 1 or 0..1 and
5318 * count elements with cardinality 0..n. Data elements (choices, containers,
5319 * leafs, lists, leaf-lists) are moved aside to be processed last, since we
5320 * need have all top-level and groupings already prepared at that time. In
5321 * the middle loop, we process other elements with carinality of 0..n since
5322 * we need to allocate arrays to store them.
5323 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005324 LY_TREE_FOR_SAFE(yin->child, next, child) {
5325 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
Radek Krejci0d70c372015-07-02 16:23:10 +02005326 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01005327 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005328 continue;
5329 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005330
Radek Krejcic071c542016-01-27 14:57:51 +01005331 if (!submodule && !strcmp(child->name, "namespace")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005332 if (module->ns) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005333 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005334 goto error;
5335 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005336 GETVAL(value, child, "uri");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005337 module->ns = lydict_insert(ctx, value, strlen(value));
Michal Vasko345da0a2015-12-02 10:35:55 +01005338 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01005339 } else if (!submodule && !strcmp(child->name, "prefix")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005340 if (module->prefix) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005341 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005342 goto error;
5343 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005344 GETVAL(value, child, "value");
Radek Krejci48464ed2016-03-17 15:44:09 +01005345 if (lyp_check_identifier(value, LY_IDENT_PREFIX, module, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005346 goto error;
5347 }
5348 module->prefix = lydict_insert(ctx, value, strlen(value));
Michal Vasko345da0a2015-12-02 10:35:55 +01005349 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01005350 } else if (submodule && !strcmp(child->name, "belongs-to")) {
5351 if (submodule->prefix) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005352 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005353 goto error;
5354 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005355 GETVAL(value, child, "module");
Radek Krejci749190d2016-02-18 16:26:25 +01005356 if (!ly_strequal(value, submodule->belongsto->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005357 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005358 goto error;
5359 }
Radek Krejcif3886932015-06-04 17:36:06 +02005360
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005361 /* get the prefix substatement, start with checks */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005362 if (!child->child) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005363 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005364 goto error;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005365 } else if (strcmp(child->child->name, "prefix")) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005366 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005367 goto error;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005368 } else if (child->child->next) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005369 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->child->next->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005370 goto error;
5371 }
5372 /* and now finally get the value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005373 GETVAL(value, child->child, "value");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005374 /* check here differs from a generic prefix check, since this prefix
5375 * don't have to be unique
Michal Vasko38d01f72015-06-15 09:41:06 +02005376 */
Radek Krejci48464ed2016-03-17 15:44:09 +01005377 if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005378 goto error;
5379 }
Radek Krejcic071c542016-01-27 14:57:51 +01005380 submodule->prefix = lydict_insert(ctx, value, strlen(value));
Radek Krejci0af13872015-05-30 11:50:52 +02005381
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005382 /* we are done with belongs-to */
Michal Vasko345da0a2015-12-02 10:35:55 +01005383 lyxml_free(ctx, child);
Radek Krejcieb00f512015-07-01 16:44:58 +02005384
5385 /* counters (statements with n..1 cardinality) */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005386 } else if (!strcmp(child->name, "import")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005387 c_imp++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005388 } else if (!strcmp(child->name, "revision")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005389 c_rev++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005390 } else if (!strcmp(child->name, "typedef")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005391 c_tpdf++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005392 } else if (!strcmp(child->name, "identity")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005393 c_ident++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005394 } else if (!strcmp(child->name, "include")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005395 c_inc++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005396 } else if (!strcmp(child->name, "augment")) {
Radek Krejcif5be10f2015-06-16 13:29:36 +02005397 c_aug++;
Michal Vasko2f7925f2015-10-21 15:06:56 +02005398 /* keep augments separated, processed last */
Radek Krejcic071c542016-01-27 14:57:51 +01005399 lyxml_unlink_elem(ctx, child, 2);
5400 lyxml_add_child(ctx, &augs, child);
Michal Vasko2f7925f2015-10-21 15:06:56 +02005401
Radek Krejci1d82ef62015-08-07 14:44:40 +02005402 } else if (!strcmp(child->name, "feature")) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02005403 c_ftrs++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02005404 } else if (!strcmp(child->name, "deviation")) {
Radek Krejcieb00f512015-07-01 16:44:58 +02005405 c_dev++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005406
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005407 /* data statements */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005408 } else if (!strcmp(child->name, "container") ||
5409 !strcmp(child->name, "leaf-list") ||
5410 !strcmp(child->name, "leaf") ||
5411 !strcmp(child->name, "list") ||
5412 !strcmp(child->name, "choice") ||
5413 !strcmp(child->name, "uses") ||
Michal Vasko7ffc3052015-10-21 15:05:56 +02005414 !strcmp(child->name, "anyxml") ||
5415 !strcmp(child->name, "rpc") ||
5416 !strcmp(child->name, "notification")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005417 lyxml_unlink_elem(ctx, child, 2);
5418 lyxml_add_child(ctx, &root, child);
Michal Vasko7ffc3052015-10-21 15:05:56 +02005419
Radek Krejci1d82ef62015-08-07 14:44:40 +02005420 } else if (!strcmp(child->name, "grouping")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005421 /* keep groupings separated and process them before other data statements */
Radek Krejcic071c542016-01-27 14:57:51 +01005422 lyxml_unlink_elem(ctx, child, 2);
5423 lyxml_add_child(ctx, &grps, child);
Radek Krejcida04f4a2015-05-21 12:54:09 +02005424
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005425 /* optional statements */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005426 } else if (!strcmp(child->name, "description")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005427 if (trg->dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005428 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005429 goto error;
5430 }
Radek Krejcic071c542016-01-27 14:57:51 +01005431 trg->dsc = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01005432 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01005433 if (!trg->dsc) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005434 goto error;
5435 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005436 } else if (!strcmp(child->name, "reference")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005437 if (trg->ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005438 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005439 goto error;
5440 }
Radek Krejcic071c542016-01-27 14:57:51 +01005441 trg->ref = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01005442 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01005443 if (!trg->ref) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005444 goto error;
5445 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005446 } else if (!strcmp(child->name, "organization")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005447 if (trg->org) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005448 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005449 goto error;
5450 }
Radek Krejcic071c542016-01-27 14:57:51 +01005451 trg->org = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01005452 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01005453 if (!trg->org) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005454 goto error;
5455 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005456 } else if (!strcmp(child->name, "contact")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005457 if (trg->contact) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005458 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005459 goto error;
5460 }
Radek Krejcic071c542016-01-27 14:57:51 +01005461 trg->contact = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01005462 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01005463 if (!trg->contact) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005464 goto error;
5465 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005466 } else if (!strcmp(child->name, "yang-version")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005467 if (version_flag) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005468 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005469 goto error;
5470 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005471 GETVAL(value, child, "value");
Michal Vasko88de3e42016-06-29 11:05:32 +02005472 if (strcmp(value, "1") && strcmp(value, "1.1")) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005473 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, "yang-version");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005474 goto error;
5475 }
Radek Krejcic071c542016-01-27 14:57:51 +01005476 version_flag = 1;
Michal Vasko88de3e42016-06-29 11:05:32 +02005477 if (!strcmp(value, "1")) {
5478 if (submodule) {
5479 if (module->version > 1) {
5480 LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
5481 goto error;
5482 }
5483 } else {
5484 module->version = 1;
5485 }
5486 } else {
5487 if (submodule) {
Radek Krejci1a31efe2016-07-29 11:04:16 +02005488 if (module->version < 2) {
Michal Vasko88de3e42016-06-29 11:05:32 +02005489 LOGVAL(LYE_INVER, LY_VLOG_NONE, NULL);
5490 goto error;
5491 }
5492 } else {
5493 module->version = 2;
5494 }
5495 }
5496
Michal Vasko345da0a2015-12-02 10:35:55 +01005497 lyxml_free(ctx, child);
Michal Vasko38d01f72015-06-15 09:41:06 +02005498
Radek Krejci1d82ef62015-08-07 14:44:40 +02005499 } else if (!strcmp(child->name, "extension")) {
5500 GETVAL(value, child, "name");
Radek Krejci5166a892015-07-02 16:44:24 +02005501
Radek Krejci3d468122015-10-02 13:36:12 +02005502 /* we have the following supported (hardcoded) extensions: */
5503 /* ietf-netconf's get-filter-element-attributes */
5504 if (!strcmp(module->ns, LY_NSNC) &&
5505 !strcmp(value, "get-filter-element-attributes")) {
5506 LOGDBG("NETCONF filter extension found");
5507 /* NACM's default-deny-write and default-deny-all */
5508 } else if (!strcmp(module->ns, LY_NSNACM) &&
5509 (!strcmp(value, "default-deny-write") || !strcmp(value, "default-deny-all"))) {
5510 LOGDBG("NACM extension found");
5511 /* other extensions are not supported, so inform about such an extension */
5512 } else {
Radek Krejci6764bb32015-07-03 15:16:04 +02005513 LOGWRN("Not supported \"%s\" extension statement found, ignoring.", value);
Michal Vasko345da0a2015-12-02 10:35:55 +01005514 lyxml_free(ctx, child);
Radek Krejci6764bb32015-07-03 15:16:04 +02005515 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005516 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01005517 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005518 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005519 }
5520 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005521
Radek Krejcic071c542016-01-27 14:57:51 +01005522 /* check for mandatory statements */
5523 if (submodule && !submodule->prefix) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005524 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "belongs-to", "submodule");
Michal Vaskobdf51ef2015-12-10 12:11:21 +01005525 goto error;
Radek Krejcic071c542016-01-27 14:57:51 +01005526 } else if (!submodule) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005527 if (!module->ns) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005528 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "namespace", "module");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005529 goto error;
5530 }
5531 if (!module->prefix) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005532 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_NONE, NULL, "prefix", "module");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005533 goto error;
5534 }
5535 }
Radek Krejcibb3257d2015-05-21 23:03:51 +02005536
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005537 /* allocate arrays for elements with cardinality of 0..n */
5538 if (c_imp) {
Radek Krejci4dcd3392016-06-22 10:28:40 +02005539 size = (c_imp * sizeof *trg->imp) + sizeof(void*);
5540 trg->imp = calloc(1, size);
Radek Krejcic071c542016-01-27 14:57:51 +01005541 if (!trg->imp) {
Michal Vasko253035f2015-12-17 16:58:13 +01005542 LOGMEM;
5543 goto error;
5544 }
Radek Krejci4dcd3392016-06-22 10:28:40 +02005545 /* set stop block for possible realloc */
5546 trg->imp[c_imp].module = (void*)0x1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005547 }
5548 if (c_rev) {
Radek Krejcic071c542016-01-27 14:57:51 +01005549 trg->rev = calloc(c_rev, sizeof *trg->rev);
5550 if (!trg->rev) {
Michal Vasko253035f2015-12-17 16:58:13 +01005551 LOGMEM;
5552 goto error;
5553 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005554 }
5555 if (c_tpdf) {
Radek Krejcic071c542016-01-27 14:57:51 +01005556 trg->tpdf = calloc(c_tpdf, sizeof *trg->tpdf);
5557 if (!trg->tpdf) {
Michal Vasko253035f2015-12-17 16:58:13 +01005558 LOGMEM;
5559 goto error;
5560 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005561 }
5562 if (c_ident) {
Radek Krejcic071c542016-01-27 14:57:51 +01005563 trg->ident = calloc(c_ident, sizeof *trg->ident);
5564 if (!trg->ident) {
Michal Vasko253035f2015-12-17 16:58:13 +01005565 LOGMEM;
5566 goto error;
5567 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005568 }
5569 if (c_inc) {
Radek Krejci4dcd3392016-06-22 10:28:40 +02005570 size = (c_inc * sizeof *trg->inc) + sizeof(void*);
5571 trg->inc = calloc(1, size);
Radek Krejcic071c542016-01-27 14:57:51 +01005572 if (!trg->inc) {
Michal Vasko253035f2015-12-17 16:58:13 +01005573 LOGMEM;
5574 goto error;
5575 }
Radek Krejci4dcd3392016-06-22 10:28:40 +02005576 /* set stop block for possible realloc */
5577 trg->inc[c_inc].submodule = (void*)0x1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005578 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02005579 if (c_aug) {
Radek Krejcic071c542016-01-27 14:57:51 +01005580 trg->augment = calloc(c_aug, sizeof *trg->augment);
5581 if (!trg->augment) {
Michal Vasko253035f2015-12-17 16:58:13 +01005582 LOGMEM;
5583 goto error;
5584 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02005585 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005586 if (c_ftrs) {
Radek Krejcic071c542016-01-27 14:57:51 +01005587 trg->features = calloc(c_ftrs, sizeof *trg->features);
5588 if (!trg->features) {
Michal Vasko253035f2015-12-17 16:58:13 +01005589 LOGMEM;
5590 goto error;
5591 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02005592 }
Radek Krejcieb00f512015-07-01 16:44:58 +02005593 if (c_dev) {
Radek Krejcic071c542016-01-27 14:57:51 +01005594 trg->deviation = calloc(c_dev, sizeof *trg->deviation);
5595 if (!trg->deviation) {
Michal Vasko253035f2015-12-17 16:58:13 +01005596 LOGMEM;
5597 goto error;
5598 }
Radek Krejcieb00f512015-07-01 16:44:58 +02005599 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005600
Michal Vasko2f7925f2015-10-21 15:06:56 +02005601 /* middle part - process nodes with cardinality of 0..n except the data nodes and augments */
5602 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02005603 if (!strcmp(child->name, "import")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005604 r = fill_yin_import(trg, child, &trg->imp[trg->imp_size]);
5605 trg->imp_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005606 if (r) {
5607 goto error;
5608 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005609
Radek Krejci1d82ef62015-08-07 14:44:40 +02005610 } else if (!strcmp(child->name, "include")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005611 memset(&inc, 0, sizeof inc);
5612 /* 1) pass module, not trg, since we want to pass the main module
5613 * 2) we cannot pass directly the structure in the array since
5614 * submodule parser can realloc our array of includes */
Michal Vasko5ff78822016-02-12 09:33:31 +01005615 r = fill_yin_include(module, submodule, child, &inc, unres);
Radek Krejci5b2c8a82016-06-17 15:36:03 +02005616 if (!r) {
5617 /* success, copy the filled data into the final array */
Radek Krejci4dcd3392016-06-22 10:28:40 +02005618 memcpy(&trg->inc[trg->inc_size], &inc, sizeof inc);
5619 trg->inc_size++;
Radek Krejci5b2c8a82016-06-17 15:36:03 +02005620 } else if (r == -1) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005621 goto error;
5622 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005623
Radek Krejci1d82ef62015-08-07 14:44:40 +02005624 } else if (!strcmp(child->name, "revision")) {
5625 GETVAL(value, child, "date");
Radek Krejci48464ed2016-03-17 15:44:09 +01005626 if (lyp_check_date(value)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005627 goto error;
5628 }
Radek Krejcic071c542016-01-27 14:57:51 +01005629 memcpy(trg->rev[trg->rev_size].date, value, LY_REV_SIZE - 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005630 /* check uniqueness of the revision date - not required by RFC */
Radek Krejcic071c542016-01-27 14:57:51 +01005631 for (i = 0; i < trg->rev_size; i++) {
5632 if (!strcmp(value, trg->rev[i].date)) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005633 LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, value, child->name);
5634 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Revision is not unique.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005635 }
5636 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005637
Radek Krejci1d82ef62015-08-07 14:44:40 +02005638 LY_TREE_FOR(child->child, child2) {
5639 if (!strcmp(child2->name, "description")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005640 if (trg->rev[trg->rev_size].dsc) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005641 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child2->name, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005642 goto error;
5643 }
Radek Krejcic071c542016-01-27 14:57:51 +01005644 trg->rev[trg->rev_size].dsc = read_yin_subnode(ctx, child2, "text");
5645 if (!trg->rev[trg->rev_size].dsc) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005646 goto error;
5647 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005648 } else if (!strcmp(child2->name, "reference")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005649 if (trg->rev[trg->rev_size].ref) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005650 LOGVAL(LYE_TOOMANY, LY_VLOG_NONE, NULL, child2->name, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005651 goto error;
5652 }
Radek Krejcic071c542016-01-27 14:57:51 +01005653 trg->rev[trg->rev_size].ref = read_yin_subnode(ctx, child2, "text");
5654 if (!trg->rev[trg->rev_size].ref) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005655 goto error;
5656 }
5657 } else {
Radek Krejci48464ed2016-03-17 15:44:09 +01005658 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, child2->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005659 goto error;
5660 }
5661 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005662
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005663 /* keep the latest revision at position 0 */
Radek Krejcic071c542016-01-27 14:57:51 +01005664 if (trg->rev_size && strcmp(trg->rev[trg->rev_size].date, trg->rev[0].date) > 0) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005665 /* switch their position */
Radek Krejcic071c542016-01-27 14:57:51 +01005666 value = strdup(trg->rev[0].date);
Michal Vasko253035f2015-12-17 16:58:13 +01005667 if (!value) {
5668 LOGMEM;
5669 goto error;
5670 }
Radek Krejcic071c542016-01-27 14:57:51 +01005671 memcpy(trg->rev[0].date, trg->rev[trg->rev_size].date, LY_REV_SIZE - 1);
5672 memcpy(trg->rev[trg->rev_size].date, value, LY_REV_SIZE - 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005673 free((char *)value);
Radek Krejcice7fb782015-05-29 16:52:34 +02005674
Radek Krejci749190d2016-02-18 16:26:25 +01005675 if (!ly_strequal(trg->rev[0].dsc, trg->rev[trg->rev_size].dsc, 1)) {
Radek Krejcic071c542016-01-27 14:57:51 +01005676 value = trg->rev[0].dsc;
5677 trg->rev[0].dsc = trg->rev[trg->rev_size].dsc;
5678 trg->rev[trg->rev_size].dsc = value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005679 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005680
Radek Krejci749190d2016-02-18 16:26:25 +01005681 if (!ly_strequal(trg->rev[0].ref, trg->rev[trg->rev_size].ref, 1)) {
Radek Krejcic071c542016-01-27 14:57:51 +01005682 value = trg->rev[0].ref;
5683 trg->rev[0].ref = trg->rev[trg->rev_size].ref;
5684 trg->rev[trg->rev_size].ref = value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005685 }
5686 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005687
Radek Krejcic071c542016-01-27 14:57:51 +01005688 trg->rev_size++;
Michal Vasko2f7925f2015-10-21 15:06:56 +02005689
Radek Krejci1d82ef62015-08-07 14:44:40 +02005690 } else if (!strcmp(child->name, "typedef")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005691 r = fill_yin_typedef(trg, NULL, child, &trg->tpdf[trg->tpdf_size], unres);
5692 trg->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005693 if (r) {
5694 goto error;
5695 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02005696
Radek Krejci1d82ef62015-08-07 14:44:40 +02005697 } else if (!strcmp(child->name, "identity")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005698 r = fill_yin_identity(trg, child, &trg->ident[trg->ident_size], unres);
5699 trg->ident_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005700 if (r) {
5701 goto error;
5702 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02005703
Radek Krejci1d82ef62015-08-07 14:44:40 +02005704 } else if (!strcmp(child->name, "feature")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005705 r = fill_yin_feature(trg, child, &trg->features[trg->features_size], unres);
5706 trg->features_size++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005707 if (r) {
5708 goto error;
5709 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02005710
Radek Krejci1d82ef62015-08-07 14:44:40 +02005711 } else if (!strcmp(child->name, "deviation")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005712 r = fill_yin_deviation(trg, child, &trg->deviation[trg->deviation_size], unres);
5713 trg->deviation_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02005714 if (r) {
5715 goto error;
5716 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005717 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005718 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005719
Radek Krejcib8f98c12016-06-24 10:30:46 +02005720 if (!submodule) {
5721 /* update the size of the arrays, they can be smaller due to possible duplicities
Radek Krejci4dcd3392016-06-22 10:28:40 +02005722 * found in submodules */
Radek Krejcib8f98c12016-06-24 10:30:46 +02005723 if (module->inc_size) {
5724 module->inc = ly_realloc(module->inc, module->inc_size * sizeof *module->inc);
5725 if (!module->inc) {
5726 LOGMEM;
5727 goto error;
5728 }
5729 }
5730 if (module->imp_size) {
5731 module->imp = ly_realloc(module->imp, module->imp_size * sizeof *module->imp);
5732 if (!module->imp) {
5733 LOGMEM;
5734 goto error;
5735 }
Radek Krejci4dcd3392016-06-22 10:28:40 +02005736 }
5737 }
Radek Krejcic071c542016-01-27 14:57:51 +01005738
Radek Krejcif5be10f2015-06-16 13:29:36 +02005739 /* process data nodes. Start with groupings to allow uses
Radek Krejcic071c542016-01-27 14:57:51 +01005740 * refer to them. Submodule's data nodes are stored in the
5741 * main module data tree.
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005742 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005743 LY_TREE_FOR_SAFE(grps.child, next, child) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005744 node = read_yin_grouping(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005745 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005746 goto error;
5747 }
Radek Krejci74705112015-06-05 10:25:44 +02005748
Michal Vasko345da0a2015-12-02 10:35:55 +01005749 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005750 }
Radek Krejci74705112015-06-05 10:25:44 +02005751
Radek Krejcif5be10f2015-06-16 13:29:36 +02005752 /* parse data nodes, ... */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005753 LY_TREE_FOR_SAFE(root.child, next, child) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02005754
Radek Krejci1d82ef62015-08-07 14:44:40 +02005755 if (!strcmp(child->name, "container")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005756 node = read_yin_container(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005757 } else if (!strcmp(child->name, "leaf-list")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005758 node = read_yin_leaflist(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005759 } else if (!strcmp(child->name, "leaf")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005760 node = read_yin_leaf(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005761 } else if (!strcmp(child->name, "list")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005762 node = read_yin_list(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005763 } else if (!strcmp(child->name, "choice")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005764 node = read_yin_choice(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005765 } else if (!strcmp(child->name, "uses")) {
Radek Krejci3440cc52016-06-23 17:03:59 +02005766 node = read_yin_uses(trg, NULL, child, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005767 } else if (!strcmp(child->name, "anyxml")) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02005768 node = read_yin_anydata(trg, NULL, child, LYS_ANYXML, 1, unres);
5769 } else if (!strcmp(child->name, "anydata")) {
5770 node = read_yin_anydata(trg, NULL, child, LYS_ANYDATA, 1, unres);
Michal Vasko7ffc3052015-10-21 15:05:56 +02005771 } else if (!strcmp(child->name, "rpc")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005772 node = read_yin_rpc_action(trg, NULL, child, unres);
Michal Vasko7ffc3052015-10-21 15:05:56 +02005773 } else if (!strcmp(child->name, "notification")) {
Michal Vaskoe022a562016-09-27 14:24:15 +02005774 node = read_yin_notif(trg, NULL, child, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005775 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005776 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005777 goto error;
5778 }
Radek Krejci25d782a2015-05-22 15:03:23 +02005779
Michal Vasko345da0a2015-12-02 10:35:55 +01005780 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005781 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02005782
Michal Vasko2f7925f2015-10-21 15:06:56 +02005783 /* ... and finally augments (last, so we can augment our data, for instance) */
5784 LY_TREE_FOR_SAFE(augs.child, next, child) {
Radek Krejcic071c542016-01-27 14:57:51 +01005785 r = fill_yin_augment(trg, NULL, child, &trg->augment[trg->augment_size], unres);
5786 trg->augment_size++;
Radek Krejcif5be10f2015-06-16 13:29:36 +02005787
Michal Vasko2f7925f2015-10-21 15:06:56 +02005788 if (r) {
Radek Krejcif5be10f2015-06-16 13:29:36 +02005789 goto error;
5790 }
Michal Vasko345da0a2015-12-02 10:35:55 +01005791 lyxml_free(ctx, child);
Radek Krejcif5be10f2015-06-16 13:29:36 +02005792 }
5793
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005794 return EXIT_SUCCESS;
Radek Krejciefaeba32015-05-27 14:30:57 +02005795
5796error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005797 /* cleanup */
5798 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005799 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005800 }
5801 while (grps.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005802 lyxml_free(module->ctx, grps.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005803 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02005804 while (augs.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005805 lyxml_free(module->ctx, augs.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02005806 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005807
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005808 return EXIT_FAILURE;
Radek Krejciefaeba32015-05-27 14:30:57 +02005809}
5810
Michal Vasko0d343d12015-08-24 14:57:36 +02005811/* logs directly */
Michal Vasko5a721fd2016-02-16 12:16:48 +01005812struct lys_submodule *
5813yin_read_submodule(struct lys_module *module, const char *data, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02005814{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005815 struct lyxml_elem *yin;
Michal Vasko5a721fd2016-02-16 12:16:48 +01005816 struct lys_submodule *submodule = NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005817 const char *value;
Radek Krejciefaeba32015-05-27 14:30:57 +02005818
Michal Vasko5a721fd2016-02-16 12:16:48 +01005819 assert(module->ctx);
Radek Krejciefaeba32015-05-27 14:30:57 +02005820
Radek Krejci722b0072016-02-01 17:09:45 +01005821 yin = lyxml_parse_mem(module->ctx, data, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005822 if (!yin) {
Michal Vasko5a721fd2016-02-16 12:16:48 +01005823 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005824 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005825
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005826 /* check root element */
5827 if (!yin->name || strcmp(yin->name, "submodule")) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005828 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005829 goto error;
5830 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005831
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005832 GETVAL(value, yin, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +01005833 if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005834 goto error;
5835 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005836
Michal Vasko5a721fd2016-02-16 12:16:48 +01005837 submodule = calloc(1, sizeof *submodule);
5838 if (!submodule) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005839 LOGMEM;
5840 goto error;
5841 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005842
Michal Vasko5a721fd2016-02-16 12:16:48 +01005843 submodule->ctx = module->ctx;
5844 submodule->name = lydict_insert(submodule->ctx, value, strlen(value));
5845 submodule->type = 1;
5846 submodule->belongsto = module;
Radek Krejciefaeba32015-05-27 14:30:57 +02005847
Michal Vasko5a721fd2016-02-16 12:16:48 +01005848 LOGVRB("Reading submodule \"%s\".", submodule->name);
5849 if (read_sub_module(module, submodule, yin, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005850 goto error;
5851 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005852
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005853 /* cleanup */
Michal Vasko345da0a2015-12-02 10:35:55 +01005854 lyxml_free(module->ctx, yin);
Radek Krejciefaeba32015-05-27 14:30:57 +02005855
Michal Vasko5a721fd2016-02-16 12:16:48 +01005856 LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name);
Michal Vasko5a721fd2016-02-16 12:16:48 +01005857 return submodule;
Radek Krejciefaeba32015-05-27 14:30:57 +02005858
5859error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005860 /* cleanup */
Michal Vasko5a721fd2016-02-16 12:16:48 +01005861 unres_schema_free((struct lys_module *)submodule, &unres);
Michal Vasko345da0a2015-12-02 10:35:55 +01005862 lyxml_free(module->ctx, yin);
Michal Vasko9f258e42016-02-11 11:36:27 +01005863
Michal Vasko5a721fd2016-02-16 12:16:48 +01005864 if (!submodule) {
Radek Krejcidaea8212016-02-15 08:28:20 +01005865 LOGERR(ly_errno, "Submodule parsing failed.");
Michal Vasko5a721fd2016-02-16 12:16:48 +01005866 return NULL;
Radek Krejcidaea8212016-02-15 08:28:20 +01005867 }
5868
Michal Vasko5a721fd2016-02-16 12:16:48 +01005869 LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name);
Radek Krejcidaea8212016-02-15 08:28:20 +01005870
Michal Vasko9eb6dd02016-05-02 14:52:40 +02005871 lys_sub_module_remove_devs_augs((struct lys_module *)submodule);
5872 lys_submodule_module_data_free(submodule);
Michal Vasko5a721fd2016-02-16 12:16:48 +01005873 lys_submodule_free(submodule, NULL);
Michal Vasko5a721fd2016-02-16 12:16:48 +01005874 return NULL;
Radek Krejciefaeba32015-05-27 14:30:57 +02005875}
5876
Michal Vasko0d343d12015-08-24 14:57:36 +02005877/* logs directly */
Radek Krejcib8048692015-08-05 13:36:34 +02005878struct lys_module *
Radek Krejciff4874d2016-03-07 12:30:50 +01005879yin_read_module(struct ly_ctx *ctx, const char *data, const char *revision, int implement)
Radek Krejciefaeba32015-05-27 14:30:57 +02005880{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005881 struct lyxml_elem *yin;
Pavol Vicanc99b59e2016-03-22 15:46:39 +01005882 struct lys_module *module = NULL;
Radek Krejcic071c542016-01-27 14:57:51 +01005883 struct unres_schema *unres;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005884 const char *value;
Radek Krejciefaeba32015-05-27 14:30:57 +02005885
Radek Krejcic071c542016-01-27 14:57:51 +01005886 unres = calloc(1, sizeof *unres);
5887 if (!unres) {
5888 LOGMEM;
5889 return NULL;
5890 }
5891
Radek Krejci722b0072016-02-01 17:09:45 +01005892 yin = lyxml_parse_mem(ctx, data, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005893 if (!yin) {
Radek Krejcic071c542016-01-27 14:57:51 +01005894 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005895 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005896
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005897 /* check root element */
5898 if (!yin->name || strcmp(yin->name, "module")) {
Radek Krejci48464ed2016-03-17 15:44:09 +01005899 LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005900 goto error;
5901 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005902
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005903 GETVAL(value, yin, "name");
Radek Krejci48464ed2016-03-17 15:44:09 +01005904 if (lyp_check_identifier(value, LY_IDENT_NAME, NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005905 goto error;
5906 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005907
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005908 module = calloc(1, sizeof *module);
5909 if (!module) {
5910 LOGMEM;
5911 goto error;
5912 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005913
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005914 module->ctx = ctx;
5915 module->name = lydict_insert(ctx, value, strlen(value));
5916 module->type = 0;
Michal Vaskod8aa32d2015-07-24 11:50:01 +02005917 module->implemented = (implement ? 1 : 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02005918
Michal Vasko9f258e42016-02-11 11:36:27 +01005919 LOGVRB("Reading module \"%s\".", module->name);
Radek Krejcic071c542016-01-27 14:57:51 +01005920 if (read_sub_module(module, NULL, yin, unres)) {
5921 goto error;
5922 }
5923
5924 /* resolve rest of unres items */
5925 if (unres->count && resolve_unres_schema(module, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005926 goto error;
5927 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005928
Radek Krejciff4874d2016-03-07 12:30:50 +01005929 if (revision) {
5930 /* check revision of the parsed model */
5931 if (!module->rev_size || strcmp(revision, module->rev[0].date)) {
Michal Vaskob24e7882016-03-21 16:13:25 +01005932 LOGVRB("Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").",
5933 module->name, module->rev[0].date, revision);
Radek Krejciff4874d2016-03-07 12:30:50 +01005934 goto error;
5935 }
5936 }
5937
Michal Vasko9eb6dd02016-05-02 14:52:40 +02005938 if (lyp_ctx_add_module(&module)) {
Pavol Vicanc99b59e2016-03-22 15:46:39 +01005939 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005940 }
Radek Krejci63a91a92015-07-29 13:31:04 +02005941
Radek Krejci27fe55e2016-09-13 17:13:35 +02005942 if (module->deviation_size && !module->implemented) {
5943 LOGVRB("Module \"%s\" includes deviations, changing its conformance to \"implement\".", module->name);
5944 /* deviations always causes target to be made implemented,
5945 * but augents and leafrefs not, so we have to apply them now */
5946 if (lys_set_implemented(module)) {
Michal Vasko26055752016-05-03 11:36:31 +02005947 goto error;
5948 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02005949 }
5950
Michal Vasko345da0a2015-12-02 10:35:55 +01005951 lyxml_free(ctx, yin);
Radek Krejcic071c542016-01-27 14:57:51 +01005952 unres_schema_free(NULL, &unres);
Michal Vasko9f258e42016-02-11 11:36:27 +01005953 LOGVRB("Module \"%s\" successfully parsed.", module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005954 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005955
5956error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005957 /* cleanup */
Radek Krejcic071c542016-01-27 14:57:51 +01005958 lyxml_free(ctx, yin);
Radek Krejcib8c07b82016-02-12 11:11:55 +01005959 unres_schema_free(module, &unres);
5960
5961 if (!module) {
Radek Krejcidaea8212016-02-15 08:28:20 +01005962 LOGERR(ly_errno, "Module parsing failed.");
Radek Krejcib8c07b82016-02-12 11:11:55 +01005963 return NULL;
5964 }
5965
5966 LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name);
Radek Krejcida04f4a2015-05-21 12:54:09 +02005967
Michal Vasko9eb6dd02016-05-02 14:52:40 +02005968 lys_sub_module_remove_devs_augs(module);
Michal Vasko9f258e42016-02-11 11:36:27 +01005969 lys_free(module, NULL, 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005970 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005971}