blob: 34e31fb7de908933b953e528906ac66a5c4662b4 [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
11 *
12 * 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>
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020024#include <pcre.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020025
Radek Krejci998a0b82015-08-17 13:14:36 +020026#include "libyang.h"
27#include "common.h"
28#include "context.h"
Radek Krejci41912fe2015-10-22 10:22:12 +020029#include "dict_private.h"
Michal Vaskofcdac172015-10-07 09:35:05 +020030#include "xpath.h"
Radek Krejci998a0b82015-08-17 13:14:36 +020031#include "parser.h"
Radek Krejci998a0b82015-08-17 13:14:36 +020032#include "resolve.h"
33#include "tree_internal.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020034#include "xml_internal.h"
Radek Krejciefdd0ce2015-05-26 16:48:29 +020035
Radek Krejciadb57612016-02-16 13:34:34 +010036#define GETVAL(value, node, arg) \
37 value = lyxml_get_attr(node, arg, NULL); \
38 if (!value) { \
Michal Vaskoe5e06292016-02-26 09:56:41 +010039 LOGVAL(LYE_MISSARG, LOGLINE(node), LY_VLOG_NONE, NULL, arg, node->name); \
Radek Krejciadb57612016-02-16 13:34:34 +010040 goto error; \
Michal Vasko2d710f32016-02-05 12:29:21 +010041 }
Radek Krejcice7fb782015-05-29 16:52:34 +020042
Radek Krejcic6556022016-01-27 15:16:45 +010043/* parser.c */
44int dup_prefix_check(const char *prefix, struct lys_module *module);
45
Radek Krejcib388c152015-06-04 17:03:03 +020046#define OPT_IDENT 0x01
47#define OPT_CONFIG 0x02
48#define OPT_MODULE 0x04
49#define OPT_INHERIT 0x08
Radek Krejci6764bb32015-07-03 15:16:04 +020050#define OPT_NACMEXT 0x10
Radek Krejcib8048692015-08-05 13:36:34 +020051static int read_yin_common(struct lys_module *, struct lys_node *, struct lys_node *, struct lyxml_elem *, int);
Radek Krejcib388c152015-06-04 17:03:03 +020052
Radek Krejcib8048692015-08-05 13:36:34 +020053static struct lys_node *read_yin_choice(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +020054 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020055static struct lys_node *read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +020056 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020057static struct lys_node *read_yin_anyxml(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +020058 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020059static struct lys_node *read_yin_container(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +020060 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020061static struct lys_node *read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +020062 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020063static struct lys_node *read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +020064 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020065static struct lys_node *read_yin_list(struct lys_module *module,struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +020066 int resolve, struct unres_schema *unres);
Radek Krejcia9544502015-08-14 08:24:29 +020067static struct lys_node *read_yin_uses(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +020068 int resolve, struct unres_schema *unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +020069static struct lys_node *read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
Michal Vaskof02e3742015-08-05 16:27:02 +020070 int resolve, struct unres_schema *unres);
71static struct lys_when *read_yin_when(struct lys_module *module, struct lyxml_elem *yin);
Radek Krejci74705112015-06-05 10:25:44 +020072
Michal Vasko0d343d12015-08-24 14:57:36 +020073/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020074static const char *
75read_yin_subnode(struct ly_ctx *ctx, struct lyxml_elem *node, const char *name)
Radek Krejcice7fb782015-05-29 16:52:34 +020076{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020077 int len;
Radek Krejcida04f4a2015-05-21 12:54:09 +020078
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020079 /* there should be <text> child */
80 if (!node->child || !node->child->name || strcmp(node->child->name, name)) {
Radek Krejci218436d2016-02-10 12:54:06 +010081 LOGERR(LY_EVALID, "Expected \"%s\" element in \"%s\" element.", name, node->name);
Michal Vaskoe5e06292016-02-26 09:56:41 +010082 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, name, node->name);
Radek Krejci218436d2016-02-10 12:54:06 +010083 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020084 } else if (node->child->content) {
85 len = strlen(node->child->content);
86 return lydict_insert(ctx, node->child->content, len);
Radek Krejci218436d2016-02-10 12:54:06 +010087 } else {
88 return lydict_insert(ctx, "", 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020089 }
Radek Krejcida04f4a2015-05-21 12:54:09 +020090}
91
Michal Vasko0d343d12015-08-24 14:57:36 +020092/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020093static int
Michal Vasko1d337e12016-02-15 12:32:04 +010094fill_yin_iffeature(struct lys_node *parent, struct lyxml_elem *yin, struct lys_feature **iffeat, struct unres_schema *unres)
95{
96 int r;
97 const char *value;
98
99 GETVAL(value, yin, "name");
100 if (!(value = transform_schema2json(parent->module, value, LOGLINE(yin)))) {
101 return EXIT_FAILURE;
102 }
103
104 /* HACK - store pointer to the parent node for later status check */
105 *iffeat = (struct lys_feature *)parent;
106 r = unres_schema_add_str(parent->module, unres, iffeat, UNRES_IFFEAT, value,
107 LOGLINE(yin));
108 lydict_remove(parent->module->ctx, value);
109 if (!r) {
110 return EXIT_SUCCESS;
111 }
112
113error:
114 return EXIT_FAILURE;
115}
116
117/* logs directly */
118static int
Michal Vaskof02e3742015-08-05 16:27:02 +0200119fill_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 +0200120{
Radek Krejci73adb602015-07-02 18:07:40 +0200121 struct lyxml_elem *node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200122 const char *value;
Radek Krejciad73b6f2016-02-09 15:42:55 +0100123 int base_flag = 0;
Radek Krejci04581c62015-05-22 21:24:00 +0200124
Michal Vasko4cfcd252015-08-03 14:31:10 +0200125 GETVAL(value, yin, "name");
Michal Vaskoc94283a2015-10-29 09:07:20 +0100126 ident->name = value;
Michal Vasko4cfcd252015-08-03 14:31:10 +0200127
Radek Krejci76512572015-08-04 09:47:08 +0200128 if (read_yin_common(module, NULL, (struct lys_node *)ident, yin, OPT_IDENT | OPT_MODULE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200129 return EXIT_FAILURE;
130 }
Radek Krejci04581c62015-05-22 21:24:00 +0200131
Radek Krejci73adb602015-07-02 18:07:40 +0200132 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200133 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
134 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200135 continue;
136 }
137
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200138 if (!strcmp(node->name, "base")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +0100139 if (base_flag) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100140 LOGVAL(LYE_TOOMANY, LOGLINE(node), LY_VLOG_NONE, NULL, "base", "identity");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200141 return EXIT_FAILURE;
142 }
Radek Krejciad73b6f2016-02-09 15:42:55 +0100143 base_flag = 1;
144
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200145 GETVAL(value, node, "name");
Michal Vaskoc94283a2015-10-29 09:07:20 +0100146 value = transform_schema2json(module, value, LOGLINE(node));
147 if (!value) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +0200148 return EXIT_FAILURE;
149 }
Michal Vaskoc94283a2015-10-29 09:07:20 +0100150
151 if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, value, LOGLINE(node)) == -1) {
152 lydict_remove(module->ctx, value);
153 return EXIT_FAILURE;
154 }
155 lydict_remove(module->ctx, value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200156 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100157 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name, "identity");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200158 return EXIT_FAILURE;
159 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200160 }
Radek Krejci04581c62015-05-22 21:24:00 +0200161
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200162 return EXIT_SUCCESS;
Michal Vasko2d851a92015-10-20 16:16:36 +0200163
164error:
165 return EXIT_FAILURE;
Radek Krejci04581c62015-05-22 21:24:00 +0200166}
167
Michal Vasko0d343d12015-08-24 14:57:36 +0200168/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200169static int
Radek Krejci1574a8d2015-08-03 14:16:52 +0200170read_restr_substmt(struct ly_ctx *ctx, struct lys_restr *restr, struct lyxml_elem *yin)
Radek Krejci41726f92015-06-19 13:11:05 +0200171{
Radek Krejci73adb602015-07-02 18:07:40 +0200172 struct lyxml_elem *child;
Radek Krejci461d1622015-06-30 14:06:28 +0200173 const char *value;
Radek Krejci41726f92015-06-19 13:11:05 +0200174
Radek Krejci73adb602015-07-02 18:07:40 +0200175 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200176 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
177 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200178 continue;
179 }
180
Radek Krejci41726f92015-06-19 13:11:05 +0200181 if (!strcmp(child->name, "description")) {
182 if (restr->dsc) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100183 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200184 return EXIT_FAILURE;
185 }
186 restr->dsc = read_yin_subnode(ctx, child, "text");
187 if (!restr->dsc) {
188 return EXIT_FAILURE;
189 }
190 } else if (!strcmp(child->name, "reference")) {
191 if (restr->ref) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100192 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200193 return EXIT_FAILURE;
194 }
195 restr->ref = read_yin_subnode(ctx, child, "text");
196 if (!restr->ref) {
197 return EXIT_FAILURE;
198 }
199 } else if (!strcmp(child->name, "error-app-tag")) {
200 if (restr->eapptag) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100201 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200202 return EXIT_FAILURE;
203 }
Michal Vasko54e426f2015-07-07 15:38:02 +0200204 GETVAL(value, child, "value");
Radek Krejci461d1622015-06-30 14:06:28 +0200205 restr->eapptag = lydict_insert(ctx, value, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200206 } else if (!strcmp(child->name, "error-message")) {
207 if (restr->emsg) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100208 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200209 return EXIT_FAILURE;
210 }
211 restr->emsg = read_yin_subnode(ctx, child, "value");
212 if (!restr->emsg) {
213 return EXIT_FAILURE;
214 }
215 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100216 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200217 return EXIT_FAILURE;
218 }
Radek Krejci41726f92015-06-19 13:11:05 +0200219 }
220
221 return EXIT_SUCCESS;
Michal Vaskoc8ef47f2015-06-29 14:56:19 +0200222
223error:
224 return EXIT_FAILURE;
Radek Krejci41726f92015-06-19 13:11:05 +0200225}
226
Michal Vasko88c29542015-11-27 14:57:53 +0100227/* logs directly, returns EXIT_SUCCESS, EXIT_FAILURE, -1 */
228int
Radek Krejcib8048692015-08-05 13:36:34 +0200229fill_yin_type(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_type *type,
Michal Vaskof02e3742015-08-05 16:27:02 +0200230 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200231{
Michal Vasko1dca6882015-10-22 14:29:42 +0200232 const char *value, *name, *err_ptr;
Radek Krejci5fbc9162015-06-19 14:11:11 +0200233 struct lyxml_elem *next, *node;
Radek Krejci1574a8d2015-08-03 14:16:52 +0200234 struct lys_restr **restr;
235 struct lys_type_bit bit;
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200236 pcre *precomp;
237 int i, j, rc, err_offset;
Radek Krejcidc008d72016-02-17 13:12:14 +0100238 int ret = -1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200239 int64_t v, v_;
Radek Krejci994b6f62015-06-18 16:47:27 +0200240 int64_t p, p_;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200241
Radek Krejci8de7b0f2015-07-02 11:43:42 +0200242 GETVAL(value, yin, "name");
Michal Vaskofba15262015-10-21 12:10:28 +0200243 value = transform_schema2json(module, value, LOGLINE(yin));
Michal Vasko1dca6882015-10-22 14:29:42 +0200244 if (!value) {
245 goto error;
Michal Vaskoa5835e92015-10-20 15:07:39 +0200246 }
Michal Vaskob362b4c2015-10-20 15:15:46 +0200247
248 i = parse_identifier(value);
249 if (i < 1) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100250 LOGVAL(LYE_INCHAR, LOGLINE(yin), LY_VLOG_NONE, NULL, value[-i], &value[-i]);
Michal Vasko88c29542015-11-27 14:57:53 +0100251 lydict_remove(module->ctx, value);
Michal Vaskob362b4c2015-10-20 15:15:46 +0200252 goto error;
253 }
254 /* module name */
Radek Krejci225376f2016-02-16 17:36:22 +0100255 name = value;
Michal Vaskob362b4c2015-10-20 15:15:46 +0200256 if (value[i]) {
257 type->module_name = lydict_insert(module->ctx, value, i);
Radek Krejci225376f2016-02-16 17:36:22 +0100258 name += i;
259 if ((name[0] != ':') || (parse_identifier(name + 1) < 1)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100260 LOGVAL(LYE_INCHAR, LOGLINE(yin), LY_VLOG_NONE, NULL, name[0], name);
Michal Vasko88c29542015-11-27 14:57:53 +0100261 lydict_remove(module->ctx, value);
Michal Vaskob362b4c2015-10-20 15:15:46 +0200262 goto error;
263 }
Radek Krejci225376f2016-02-16 17:36:22 +0100264 ++name;
Michal Vaskob362b4c2015-10-20 15:15:46 +0200265 }
Michal Vaskoa5835e92015-10-20 15:07:39 +0200266
Radek Krejci225376f2016-02-16 17:36:22 +0100267 rc = resolve_superior_type(name, type->module_name, module, parent, &type->der);
Michal Vasko88c29542015-11-27 14:57:53 +0100268 lydict_remove(module->ctx, value);
Michal Vaskof7eee892015-08-24 15:03:11 +0200269 if (rc == -1) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100270 LOGVAL(LYE_INMOD, LOGLINE(yin), LY_VLOG_NONE, NULL, type->module_name);
Michal Vaskof7eee892015-08-24 15:03:11 +0200271 goto error;
Michal Vasko88c29542015-11-27 14:57:53 +0100272
273 /* the type could not be resolved or it was resolved to an unresolved typedef */
Michal Vaskof7eee892015-08-24 15:03:11 +0200274 } else if (rc == EXIT_FAILURE) {
Radek Krejcidc008d72016-02-17 13:12:14 +0100275 ret = EXIT_FAILURE;
276 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200277 }
278 type->base = type->der->type.base;
Radek Krejci25d782a2015-05-22 15:03:23 +0200279
Radek Krejcicf509982015-12-15 09:22:44 +0100280 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +0100281 if (lyp_check_status(type->parent->flags, type->parent->module, type->parent->name,
Radek Krejciadb57612016-02-16 13:34:34 +0100282 type->der->flags, type->der->module, type->der->name, LOGLINE(yin), parent)) {
Radek Krejcicf509982015-12-15 09:22:44 +0100283 return -1;
284 }
285
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200286 switch (type->base) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200287 case LY_TYPE_BITS:
Radek Krejci994b6f62015-06-18 16:47:27 +0200288 /* RFC 6020 9.7.4 - bit */
289
290 /* get bit specifications, at least one must be present */
291 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200292 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
293 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100294 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200295 continue;
296 }
297
Radek Krejci994b6f62015-06-18 16:47:27 +0200298 if (!strcmp(node->name, "bit")) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200299 type->info.bits.count++;
Radek Krejci41726f92015-06-19 13:11:05 +0200300 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100301 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200302 goto error;
Radek Krejci994b6f62015-06-18 16:47:27 +0200303 }
304 }
Radek Krejciac781922015-07-09 15:35:14 +0200305 if (!type->der->type.der && !type->info.bits.count) {
306 /* type is derived directly from buit-in bits type and bit statement is required */
Michal Vaskoe5e06292016-02-26 09:56:41 +0100307 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "bit", "type");
Radek Krejci994b6f62015-06-18 16:47:27 +0200308 goto error;
309 }
Radek Krejciac781922015-07-09 15:35:14 +0200310 if (type->der->type.der && type->info.bits.count) {
311 /* type is not directly derived from buit-in bits type and bit statement is prohibited */
Michal Vaskoe5e06292016-02-26 09:56:41 +0100312 LOGVAL(LYE_INSTMT, LOGLINE(yin), LY_VLOG_NONE, NULL, "bit");
Radek Krejciac781922015-07-09 15:35:14 +0200313 goto error;
314 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200315
316 type->info.bits.bit = calloc(type->info.bits.count, sizeof *type->info.bits.bit);
Michal Vasko253035f2015-12-17 16:58:13 +0100317 if (!type->info.bits.bit) {
318 LOGMEM;
319 goto error;
320 }
Radek Krejci73adb602015-07-02 18:07:40 +0200321 p = 0;
322 i = -1;
323 LY_TREE_FOR(yin->child, next) {
324 i++;
325
326 GETVAL(value, next, "name");
Radek Krejcic6556022016-01-27 15:16:45 +0100327 if (lyp_check_identifier(value, LY_IDENT_SIMPLE, LOGLINE(next), NULL, NULL)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100328 LOGVAL(LYE_PATH, 0, LY_VLOG_NONE, NULL);
Michal Vasko2d26a022015-12-07 09:27:21 +0100329 goto error;
330 }
331
Radek Krejci994b6f62015-06-18 16:47:27 +0200332 type->info.bits.bit[i].name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci76512572015-08-04 09:47:08 +0200333 if (read_yin_common(module, NULL, (struct lys_node *)&type->info.bits.bit[i], next, 0)) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200334 type->info.bits.count = i + 1;
335 goto error;
336 }
337
338 /* check the name uniqueness */
339 for (j = 0; j < i; j++) {
340 if (!strcmp(type->info.bits.bit[j].name, type->info.bits.bit[i].name)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100341 LOGVAL(LYE_BITS_DUPNAME, LOGLINE(next), LY_VLOG_NONE, NULL, type->info.bits.bit[i].name);
Radek Krejci994b6f62015-06-18 16:47:27 +0200342 type->info.bits.count = i + 1;
343 goto error;
344 }
345 }
346
Radek Krejci0d70c372015-07-02 16:23:10 +0200347 p_ = -1;
Radek Krejci73adb602015-07-02 18:07:40 +0200348 LY_TREE_FOR(next->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200349 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
350 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200351 continue;
Radek Krejci994b6f62015-06-18 16:47:27 +0200352 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200353
Radek Krejci0d70c372015-07-02 16:23:10 +0200354 if (!strcmp(node->name, "position")) {
355 GETVAL(value, node, "value");
Radek Krejci7511f402015-07-10 09:56:30 +0200356 p_ = strtoll(value, NULL, 10);
Radek Krejci0d70c372015-07-02 16:23:10 +0200357
358 /* range check */
Radek Krejcib8ca1082015-07-10 11:24:11 +0200359 if (p_ < 0 || p_ > UINT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100360 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, value, "bit/position");
Radek Krejci0d70c372015-07-02 16:23:10 +0200361 type->info.bits.count = i + 1;
362 goto error;
363 }
364 type->info.bits.bit[i].pos = (uint32_t)p_;
365
366 /* keep the highest enum value for automatic increment */
Michal Vasko9ab05942015-07-07 15:38:26 +0200367 if (type->info.bits.bit[i].pos >= p) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200368 p = type->info.bits.bit[i].pos;
369 p++;
370 } else {
371 /* check that the value is unique */
372 for (j = 0; j < i; j++) {
373 if (type->info.bits.bit[j].pos == type->info.bits.bit[i].pos) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100374 LOGVAL(LYE_BITS_DUPVAL, LOGLINE(node), LY_VLOG_NONE, NULL,
Radek Krejciadb57612016-02-16 13:34:34 +0100375 type->info.bits.bit[i].pos, type->info.bits.bit[i].name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200376 type->info.bits.count = i + 1;
377 goto error;
378 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200379 }
380 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200381 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100382 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200383 goto error;
Radek Krejci994b6f62015-06-18 16:47:27 +0200384 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200385 }
386 if (p_ == -1) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200387 /* assign value automatically */
388 if (p > UINT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100389 LOGVAL(LYE_INARG, LOGLINE(next), LY_VLOG_NONE, NULL, "4294967295", "bit/position");
Radek Krejci994b6f62015-06-18 16:47:27 +0200390 type->info.bits.count = i + 1;
391 goto error;
392 }
393 type->info.bits.bit[i].pos = (uint32_t)p;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100394 type->info.bits.bit[i].flags |= LYS_AUTOASSIGNED;
Radek Krejci994b6f62015-06-18 16:47:27 +0200395 p++;
396 }
Radek Krejci9a1b95a2015-07-09 15:32:21 +0200397
398 /* keep them ordered by position */
399 j = i;
400 while (j && type->info.bits.bit[j - 1].pos > type->info.bits.bit[j].pos) {
401 /* switch them */
402 memcpy(&bit, &type->info.bits.bit[j], sizeof bit);
403 memcpy(&type->info.bits.bit[j], &type->info.bits.bit[j - 1], sizeof bit);
404 memcpy(&type->info.bits.bit[j - 1], &bit, sizeof bit);
405 j--;
406 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200407 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200408 break;
Radek Krejci25d782a2015-05-22 15:03:23 +0200409
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200410 case LY_TYPE_DEC64:
Radek Krejcif9401c32015-06-26 16:47:36 +0200411 /* RFC 6020 9.2.4 - range and 9.3.4 - fraction-digits */
Radek Krejci73adb602015-07-02 18:07:40 +0200412 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200413 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
414 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200415 continue;
416 }
417
Radek Krejcif9401c32015-06-26 16:47:36 +0200418 if (!strcmp(node->name, "range")) {
419 if (type->info.dec64.range) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100420 LOGVAL(LYE_TOOMANY, LOGLINE(node), LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200421 goto error;
422 }
423
424 GETVAL(value, node, "value");
Radek Krejci6dc53a22015-08-17 13:27:59 +0200425 if (lyp_check_length_range(value, type)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100426 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, value, "range");
Radek Krejcif9401c32015-06-26 16:47:36 +0200427 goto error;
428 }
429 type->info.dec64.range = calloc(1, sizeof *type->info.dec64.range);
Michal Vasko253035f2015-12-17 16:58:13 +0100430 if (!type->info.dec64.range) {
431 LOGMEM;
432 goto error;
433 }
Radek Krejcif9401c32015-06-26 16:47:36 +0200434 type->info.dec64.range->expr = lydict_insert(module->ctx, value, 0);
435
436 /* get possible substatements */
437 if (read_restr_substmt(module->ctx, type->info.dec64.range, node)) {
438 goto error;
439 }
440 } else if (!strcmp(node->name, "fraction-digits")) {
441 if (type->info.dec64.dig) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100442 LOGVAL(LYE_TOOMANY, LOGLINE(node), LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200443 goto error;
444 }
445 GETVAL(value, node, "value");
446 v = strtol(value, NULL, 10);
447
448 /* range check */
449 if (v < 1 || v > 18) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100450 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, value, node->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200451 goto error;
452 }
453 type->info.dec64.dig = (uint8_t)v;
454 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100455 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200456 goto error;
457 }
Radek Krejcif9401c32015-06-26 16:47:36 +0200458 }
459
460 /* mandatory sub-statement(s) check */
461 if (!type->info.dec64.dig && !type->der->type.der) {
462 /* decimal64 type directly derived from built-in type requires fraction-digits */
Michal Vaskoe5e06292016-02-26 09:56:41 +0100463 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "fraction-digits", "type");
Radek Krejcif9401c32015-06-26 16:47:36 +0200464 goto error;
465 }
Radek Krejci7511f402015-07-10 09:56:30 +0200466 if (type->info.dec64.dig && type->der->type.der) {
467 /* type is not directly derived from buit-in type and fraction-digits statement is prohibited */
Michal Vaskoe5e06292016-02-26 09:56:41 +0100468 LOGVAL(LYE_INSTMT, LOGLINE(yin), LY_VLOG_NONE, NULL, "fraction-digits");
Radek Krejci7511f402015-07-10 09:56:30 +0200469 goto error;
470 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200471 break;
Radek Krejci25d782a2015-05-22 15:03:23 +0200472
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200473 case LY_TYPE_ENUM:
Radek Krejci994b6f62015-06-18 16:47:27 +0200474 /* RFC 6020 9.6 - enum */
Radek Krejci25d782a2015-05-22 15:03:23 +0200475
Radek Krejci994b6f62015-06-18 16:47:27 +0200476 /* get enum specifications, at least one must be present */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200477 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200478 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
479 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100480 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200481 continue;
482 }
483
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200484 if (!strcmp(node->name, "enum")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200485 type->info.enums.count++;
Radek Krejci5fbc9162015-06-19 14:11:11 +0200486 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100487 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200488 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200489 }
490 }
Radek Krejcib54bcb12015-07-10 13:16:40 +0200491 if (!type->der->type.der && !type->info.enums.count) {
492 /* type is derived directly from buit-in enumeartion type and enum statement is required */
Michal Vaskoe5e06292016-02-26 09:56:41 +0100493 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "enum", "type");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200494 goto error;
495 }
Radek Krejcib54bcb12015-07-10 13:16:40 +0200496 if (type->der->type.der && type->info.enums.count) {
497 /* type is not directly derived from buit-in enumeration type and enum statement is prohibited */
Michal Vaskoe5e06292016-02-26 09:56:41 +0100498 LOGVAL(LYE_INSTMT, LOGLINE(yin), LY_VLOG_NONE, NULL, "enum");
Radek Krejcib54bcb12015-07-10 13:16:40 +0200499 goto error;
500 }
Radek Krejci25d782a2015-05-22 15:03:23 +0200501
Radek Krejci1574a8d2015-08-03 14:16:52 +0200502 type->info.enums.enm = calloc(type->info.enums.count, sizeof *type->info.enums.enm);
Michal Vasko253035f2015-12-17 16:58:13 +0100503 if (!type->info.enums.enm) {
504 LOGMEM;
505 goto error;
506 }
Radek Krejci73adb602015-07-02 18:07:40 +0200507 v = 0;
508 i = -1;
509 LY_TREE_FOR(yin->child, next) {
510 i++;
511
512 GETVAL(value, next, "name");
Michal Vasko0fb58e92015-12-07 09:27:44 +0100513 if (!value[0]) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100514 LOGVAL(LYE_SPEC, LOGLINE(next), LY_VLOG_NONE, NULL, "Enum name must not be empty.");
Michal Vasko0fb58e92015-12-07 09:27:44 +0100515 goto error;
516 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200517 type->info.enums.enm[i].name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci76512572015-08-04 09:47:08 +0200518 if (read_yin_common(module, NULL, (struct lys_node *)&type->info.enums.enm[i], next, 0)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200519 type->info.enums.count = i + 1;
520 goto error;
521 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200522
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200523 /* the assigned name MUST NOT have any leading or trailing whitespace characters */
Radek Krejci1574a8d2015-08-03 14:16:52 +0200524 value = type->info.enums.enm[i].name;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200525 if (isspace(value[0]) || isspace(value[strlen(value) - 1])) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100526 LOGVAL(LYE_ENUM_WS, LOGLINE(next), LY_VLOG_NONE, NULL, value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200527 type->info.enums.count = i + 1;
528 goto error;
529 }
Radek Krejci25d782a2015-05-22 15:03:23 +0200530
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200531 /* check the name uniqueness */
532 for (j = 0; j < i; j++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +0200533 if (!strcmp(type->info.enums.enm[j].name, type->info.enums.enm[i].name)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100534 LOGVAL(LYE_ENUM_DUPNAME, LOGLINE(next), LY_VLOG_NONE, NULL, type->info.enums.enm[i].name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200535 type->info.enums.count = i + 1;
536 goto error;
537 }
538 }
Radek Krejci04581c62015-05-22 21:24:00 +0200539
Radek Krejci0d70c372015-07-02 16:23:10 +0200540 v_ = -1;
Radek Krejci73adb602015-07-02 18:07:40 +0200541 LY_TREE_FOR(next->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200542 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
543 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200544 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200545 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200546
Radek Krejci0d70c372015-07-02 16:23:10 +0200547 if (!strcmp(node->name, "value")) {
548 GETVAL(value, node, "value");
Radek Krejci7511f402015-07-10 09:56:30 +0200549 v_ = strtoll(value, NULL, 10);
Radek Krejci0d70c372015-07-02 16:23:10 +0200550
551 /* range check */
552 if (v_ < INT32_MIN || v_ > INT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100553 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, value, "enum/value");
Radek Krejci0d70c372015-07-02 16:23:10 +0200554 type->info.enums.count = i + 1;
555 goto error;
556 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200557 type->info.enums.enm[i].value = v_;
Radek Krejci0d70c372015-07-02 16:23:10 +0200558
559 /* keep the highest enum value for automatic increment */
Radek Krejci1574a8d2015-08-03 14:16:52 +0200560 if (type->info.enums.enm[i].value > v) {
561 v = type->info.enums.enm[i].value;
Radek Krejci0d70c372015-07-02 16:23:10 +0200562 v++;
563 } else {
564 /* check that the value is unique */
565 for (j = 0; j < i; j++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +0200566 if (type->info.enums.enm[j].value == type->info.enums.enm[i].value) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100567 LOGVAL(LYE_ENUM_DUPVAL, LOGLINE(node), LY_VLOG_NONE, NULL,
Radek Krejciadb57612016-02-16 13:34:34 +0100568 type->info.enums.enm[i].value, type->info.enums.enm[i].name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200569 type->info.enums.count = i + 1;
570 goto error;
571 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200572 }
573 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200574 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100575 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200576 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200577 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200578 }
579 if (v_ == -1) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200580 /* assign value automatically */
581 if (v > INT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100582 LOGVAL(LYE_INARG, LOGLINE(next), LY_VLOG_NONE, NULL, "2147483648", "enum/value");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200583 type->info.enums.count = i + 1;
584 goto error;
585 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200586 type->info.enums.enm[i].value = v;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100587 type->info.enums.enm[i].flags |= LYS_AUTOASSIGNED;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200588 v++;
589 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200590 }
591 break;
592
593 case LY_TYPE_IDENT:
Radek Krejci994b6f62015-06-18 16:47:27 +0200594 /* RFC 6020 9.10 - base */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200595
596 /* get base specification, exactly one must be present */
Radek Krejci0d70c372015-07-02 16:23:10 +0200597 LY_TREE_FOR_SAFE(yin->child, next, node) {
598 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
599 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100600 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200601 continue;
602 }
603
Michal Vaskoe29c6622015-11-27 15:02:31 +0100604 if (strcmp(node->name, "base")) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100605 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200606 goto error;
607 }
608 }
609
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200610 if (!yin->child) {
Radek Krejci65c889c2015-06-22 10:17:22 +0200611 if (type->der->type.der) {
612 /* this is just a derived type with no base specified/required */
613 break;
614 }
Michal Vaskoe5e06292016-02-26 09:56:41 +0100615 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "base", "type");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200616 goto error;
617 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200618 if (yin->child->next) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100619 LOGVAL(LYE_TOOMANY, LOGLINE(yin->child->next), LY_VLOG_NONE, NULL,
Radek Krejciadb57612016-02-16 13:34:34 +0100620 yin->child->next->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200621 goto error;
622 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200623 GETVAL(value, yin->child, "name");
Michal Vaskobdfb1e02016-02-05 13:15:11 +0100624 /* store in the JSON format */
625 value = transform_schema2json(module, value, LOGLINE(yin->child));
626 if (!value) {
627 goto error;
628 }
Radek Krejcic5989d42016-02-17 11:16:38 +0100629 rc = unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value, LOGLINE(yin->child));
630 lydict_remove(module->ctx, value);
631
632 if (rc == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +0200633 goto error;
634 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200635 break;
636
637 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200638 /* RFC 6020 9.13.2 - require-instance */
Radek Krejci73adb602015-07-02 18:07:40 +0200639 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200640 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
641 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200642 continue;
643 }
644
Radek Krejciaf351422015-06-19 14:49:38 +0200645 if (!strcmp(node->name, "require-instance")) {
646 if (type->info.inst.req) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100647 LOGVAL(LYE_TOOMANY, LOGLINE(node), LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejciaf351422015-06-19 14:49:38 +0200648 goto error;
649 }
650 GETVAL(value, node, "value");
Michal Vasko25fa5ac2015-07-17 10:53:38 +0200651 if (!strcmp(value, "true")) {
Radek Krejciaf351422015-06-19 14:49:38 +0200652 type->info.inst.req = 1;
Michal Vasko25fa5ac2015-07-17 10:53:38 +0200653 } else if (!strcmp(value, "false")) {
Radek Krejciaf351422015-06-19 14:49:38 +0200654 type->info.inst.req = -1;
655 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100656 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, value, node->name);
Radek Krejciaf351422015-06-19 14:49:38 +0200657 goto error;
658 }
659 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100660 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejciaf351422015-06-19 14:49:38 +0200661 goto error;
662 }
Radek Krejciaf351422015-06-19 14:49:38 +0200663 }
Michal Vasko8548cf92015-07-20 15:17:53 +0200664
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200665 break;
666
Radek Krejcif2860132015-06-20 12:37:20 +0200667 case LY_TYPE_BINARY:
668 /* RFC 6020 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200669 case LY_TYPE_INT8:
670 case LY_TYPE_INT16:
671 case LY_TYPE_INT32:
672 case LY_TYPE_INT64:
673 case LY_TYPE_UINT8:
674 case LY_TYPE_UINT16:
675 case LY_TYPE_UINT32:
676 case LY_TYPE_UINT64:
Radek Krejcif2860132015-06-20 12:37:20 +0200677 /* RFC 6020 9.2.4 - range */
678
679 /* length and range are actually the same restriction, so process
680 * them by this common code, we just need to differ the name and
681 * structure where the information will be stored
682 */
683 if (type->base == LY_TYPE_BINARY) {
684 restr = &type->info.binary.length;
685 name = "length";
686 } else {
687 restr = &type->info.num.range;
688 name = "range";
689 }
690
Radek Krejci73adb602015-07-02 18:07:40 +0200691 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200692 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
693 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200694 continue;
695 }
696
Radek Krejcif2860132015-06-20 12:37:20 +0200697 if (!strcmp(node->name, name)) {
698 if (*restr) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100699 LOGVAL(LYE_TOOMANY, LOGLINE(node), LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcif2860132015-06-20 12:37:20 +0200700 goto error;
701 }
702
703 GETVAL(value, node, "value");
Radek Krejci6dc53a22015-08-17 13:27:59 +0200704 if (lyp_check_length_range(value, type)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100705 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, value, name);
Radek Krejcif2860132015-06-20 12:37:20 +0200706 goto error;
707 }
708 *restr = calloc(1, sizeof **restr);
Michal Vasko253035f2015-12-17 16:58:13 +0100709 if (!(*restr)) {
710 LOGMEM;
711 goto error;
712 }
Radek Krejcif2860132015-06-20 12:37:20 +0200713 (*restr)->expr = lydict_insert(module->ctx, value, 0);
714
715 /* get possible substatements */
716 if (read_restr_substmt(module->ctx, *restr, node)) {
717 goto error;
718 }
719 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100720 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejcif2860132015-06-20 12:37:20 +0200721 goto error;
722 }
Radek Krejcif2860132015-06-20 12:37:20 +0200723 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200724 break;
725
726 case LY_TYPE_LEAFREF:
Radek Krejcidc4c1412015-06-19 15:39:54 +0200727 /* RFC 6020 9.9.2 - path */
Radek Krejci73adb602015-07-02 18:07:40 +0200728 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200729 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
730 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200731 continue;
732 }
733
Michal Vasko88c29542015-11-27 14:57:53 +0100734 if (!strcmp(node->name, "path") && !type->der->type.der) {
Radek Krejcidc4c1412015-06-19 15:39:54 +0200735 if (type->info.lref.path) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100736 LOGVAL(LYE_TOOMANY, LOGLINE(node), LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejcidc4c1412015-06-19 15:39:54 +0200737 goto error;
738 }
739
740 GETVAL(value, node, "value");
Michal Vasko1dca6882015-10-22 14:29:42 +0200741 /* store in the JSON format */
Michal Vaskofba15262015-10-21 12:10:28 +0200742 type->info.lref.path = transform_schema2json(module, value, LOGLINE(node));
Michal Vasko1dca6882015-10-22 14:29:42 +0200743 if (!type->info.lref.path) {
744 goto error;
745 }
Michal Vasko0bd29d12015-08-19 11:45:49 +0200746 if (unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent, LOGLINE(yin)) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +0200747 goto error;
748 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200749
Radek Krejcidc4c1412015-06-19 15:39:54 +0200750 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100751 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejcidc4c1412015-06-19 15:39:54 +0200752 goto error;
753 }
Radek Krejci73adb602015-07-02 18:07:40 +0200754 }
755
Michal Vasko88c29542015-11-27 14:57:53 +0100756 if (!type->info.lref.path && !type->der->type.der) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100757 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "path", "type");
Radek Krejci73adb602015-07-02 18:07:40 +0200758 goto error;
Radek Krejcidc4c1412015-06-19 15:39:54 +0200759 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200760 break;
761
762 case LY_TYPE_STRING:
Radek Krejci3733a802015-06-19 13:43:21 +0200763 /* RFC 6020 9.4.4 - length */
764 /* RFC 6020 9.4.6 - pattern */
Radek Krejci73adb602015-07-02 18:07:40 +0200765 i = 0;
Radek Krejci3733a802015-06-19 13:43:21 +0200766 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200767 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
768 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100769 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200770 continue;
771 }
772
Radek Krejci3733a802015-06-19 13:43:21 +0200773 if (!strcmp(node->name, "length")) {
774 if (type->info.str.length) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100775 LOGVAL(LYE_TOOMANY, LOGLINE(node), LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci3733a802015-06-19 13:43:21 +0200776 goto error;
777 }
778
779 GETVAL(value, node, "value");
Radek Krejci6dc53a22015-08-17 13:27:59 +0200780 if (lyp_check_length_range(value, type)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100781 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, value, "length");
Radek Krejci3733a802015-06-19 13:43:21 +0200782 goto error;
783 }
784 type->info.str.length = calloc(1, sizeof *type->info.str.length);
Michal Vasko253035f2015-12-17 16:58:13 +0100785 if (!type->info.str.length) {
786 LOGMEM;
787 goto error;
788 }
Radek Krejci3733a802015-06-19 13:43:21 +0200789 type->info.str.length->expr = lydict_insert(module->ctx, value, 0);
790
Radek Krejci5fbc9162015-06-19 14:11:11 +0200791 /* get possible sub-statements */
792 if (read_restr_substmt(module->ctx, type->info.str.length, node)) {
Radek Krejci3733a802015-06-19 13:43:21 +0200793 goto error;
794 }
Michal Vasko345da0a2015-12-02 10:35:55 +0100795 lyxml_free(module->ctx, node);
Radek Krejci3733a802015-06-19 13:43:21 +0200796 } else if (!strcmp(node->name, "pattern")) {
Radek Krejci73adb602015-07-02 18:07:40 +0200797 i++;
Radek Krejci3733a802015-06-19 13:43:21 +0200798 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100799 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejci3733a802015-06-19 13:43:21 +0200800 goto error;
801 }
802 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200803 /* store patterns in array */
Radek Krejci73adb602015-07-02 18:07:40 +0200804 if (i) {
805 type->info.str.patterns = calloc(i, sizeof *type->info.str.patterns);
Michal Vasko253035f2015-12-17 16:58:13 +0100806 if (!type->info.str.patterns) {
807 LOGMEM;
808 goto error;
809 }
Radek Krejci73adb602015-07-02 18:07:40 +0200810 LY_TREE_FOR(yin->child, node) {
Michal Vasko5b64da22015-11-23 15:22:30 +0100811 GETVAL(value, node, "value");
Michal Vasko69068852015-07-13 14:34:31 +0200812
813 /* check that the regex is valid */
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200814 precomp = pcre_compile(value, PCRE_NO_AUTO_CAPTURE, &err_ptr, &err_offset, NULL);
815 if (!precomp) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100816 LOGVAL(LYE_INREGEX, LOGLINE(node), LY_VLOG_NONE, NULL, value, err_ptr);
Michal Vasko69068852015-07-13 14:34:31 +0200817 free(type->info.str.patterns);
818 goto error;
819 }
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200820 free(precomp);
Michal Vasko69068852015-07-13 14:34:31 +0200821
Radek Krejci73adb602015-07-02 18:07:40 +0200822 type->info.str.patterns[type->info.str.pat_count].expr = lydict_insert(module->ctx, value, 0);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200823
824 /* get possible sub-statements */
Michal Vasko5b64da22015-11-23 15:22:30 +0100825 if (read_restr_substmt(module->ctx, &type->info.str.patterns[type->info.str.pat_count], node)) {
Michal Vasko69068852015-07-13 14:34:31 +0200826 free(type->info.str.patterns);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200827 goto error;
828 }
Radek Krejci73adb602015-07-02 18:07:40 +0200829 type->info.str.pat_count++;
Radek Krejci5fbc9162015-06-19 14:11:11 +0200830 }
831 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200832 break;
833
834 case LY_TYPE_UNION:
Radek Krejcie4c366b2015-07-02 10:11:31 +0200835 /* RFC 6020 7.4 - type */
836 /* count number of types in union */
837 i = 0;
Radek Krejci0d70c372015-07-02 16:23:10 +0200838 LY_TREE_FOR_SAFE(yin->child, next, node) {
839 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
840 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100841 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200842 continue;
843 }
844
Radek Krejcie4c366b2015-07-02 10:11:31 +0200845 if (!strcmp(node->name, "type")) {
846 i++;
847 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100848 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejcie4c366b2015-07-02 10:11:31 +0200849 goto error;
850 }
851 }
852
853 if (!i) {
854 if (type->der->type.der) {
Michal Vasko88c29542015-11-27 14:57:53 +0100855 /* this is just a derived type with no additional type specified/required */
Radek Krejcie4c366b2015-07-02 10:11:31 +0200856 break;
857 }
Michal Vaskoe5e06292016-02-26 09:56:41 +0100858 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "type", "(union) type");
Radek Krejcie4c366b2015-07-02 10:11:31 +0200859 goto error;
860 }
861
862 /* allocate array for union's types ... */
Radek Krejci1574a8d2015-08-03 14:16:52 +0200863 type->info.uni.types = calloc(i, sizeof *type->info.uni.types);
Michal Vasko253035f2015-12-17 16:58:13 +0100864 if (!type->info.uni.types) {
865 LOGMEM;
866 goto error;
867 }
Radek Krejcie4c366b2015-07-02 10:11:31 +0200868 /* ... and fill the structures */
Radek Krejci73adb602015-07-02 18:07:40 +0200869 LY_TREE_FOR(yin->child, node) {
Radek Krejcicf509982015-12-15 09:22:44 +0100870 type->info.uni.types[type->info.uni.count].parent = type->parent;
Michal Vasko88c29542015-11-27 14:57:53 +0100871 rc = fill_yin_type(module, parent, node, &type->info.uni.types[type->info.uni.count], unres);
872 if (!rc) {
873 type->info.uni.count++;
874
875 /* union's type cannot be empty or leafref */
876 if (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_EMPTY) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100877 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, "empty", node->name);
Michal Vasko88c29542015-11-27 14:57:53 +0100878 rc = -1;
879 } else if (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_LEAFREF) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100880 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, "leafref", node->name);
Michal Vasko88c29542015-11-27 14:57:53 +0100881 rc = -1;
882 }
883 }
884 if (rc) {
885 /* even if we got EXIT_FAILURE, throw it all away, too much trouble doing something else */
886 for (i = 0; i < type->info.uni.count; ++i) {
887 lys_type_free(module->ctx, &type->info.uni.types[i]);
888 }
889 free(type->info.uni.types);
890 type->info.uni.types = NULL;
891 type->info.uni.count = 0;
892
893 if (rc == EXIT_FAILURE) {
Radek Krejcidc008d72016-02-17 13:12:14 +0100894 ret = EXIT_FAILURE;
895 goto error;
Michal Vasko88c29542015-11-27 14:57:53 +0100896 }
Radek Krejcie4c366b2015-07-02 10:11:31 +0200897 goto error;
898 }
Michal Vasko88c29542015-11-27 14:57:53 +0100899 }
900 break;
Radek Krejcie4c366b2015-07-02 10:11:31 +0200901
Michal Vasko88c29542015-11-27 14:57:53 +0100902 case LY_TYPE_BOOL:
903 case LY_TYPE_EMPTY:
904 /* no sub-statement allowed */
905 LY_TREE_FOR(yin->child, node) {
906 if (node->ns && !strcmp(node->ns->value, LY_NSYIN)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100907 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejcie4c366b2015-07-02 10:11:31 +0200908 goto error;
909 }
Radek Krejcie4c366b2015-07-02 10:11:31 +0200910 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200911 break;
912
913 default:
Michal Vasko88c29542015-11-27 14:57:53 +0100914 LOGINT;
915 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200916 }
917
918 return EXIT_SUCCESS;
Radek Krejci25d782a2015-05-22 15:03:23 +0200919
920error:
Radek Krejcidc008d72016-02-17 13:12:14 +0100921 if (type->module_name) {
922 lydict_remove(module->ctx, type->module_name);
923 type->module_name = NULL;
924 }
925 return ret;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200926}
927
Michal Vasko0d343d12015-08-24 14:57:36 +0200928/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200929static int
Michal Vaskof02e3742015-08-05 16:27:02 +0200930fill_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 +0200931{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200932 const char *value;
Michal Vasko88c29542015-11-27 14:57:53 +0100933 struct lyxml_elem *node, *next;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200934 int has_type = 0, dflt_line;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200935
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200936 GETVAL(value, yin, "name");
Radek Krejcic6556022016-01-27 15:16:45 +0100937 if (lyp_check_identifier(value, LY_IDENT_TYPE, LOGLINE(yin), module, parent)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200938 goto error;
939 }
940 tpdf->name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejcida04f4a2015-05-21 12:54:09 +0200941
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200942 /* generic part - status, description, reference */
Radek Krejci225376f2016-02-16 17:36:22 +0100943 if (read_yin_common(module, NULL, (struct lys_node *)tpdf, yin, OPT_MODULE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200944 goto error;
945 }
Radek Krejcieac35532015-05-31 19:09:15 +0200946
Michal Vasko88c29542015-11-27 14:57:53 +0100947 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200948 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
949 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200950 continue;
951 }
952
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200953 if (!strcmp(node->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +0100954 if (has_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100955 LOGVAL(LYE_TOOMANY, LOGLINE(node), LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200956 goto error;
957 }
Michal Vasko88c29542015-11-27 14:57:53 +0100958 /* HACK for unres */
959 tpdf->type.der = (struct lys_tpdf *)node;
Radek Krejcicf509982015-12-15 09:22:44 +0100960 tpdf->type.parent = tpdf;
Michal Vasko88c29542015-11-27 14:57:53 +0100961 if (unres_schema_add_node(module, unres, &tpdf->type, UNRES_TYPE_DER, parent, LOGLINE(node))) {
Radek Krejci73adb602015-07-02 18:07:40 +0200962 goto error;
963 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200964 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200965 } else if (!strcmp(node->name, "default")) {
966 if (tpdf->dflt) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100967 LOGVAL(LYE_TOOMANY, LOGLINE(node), LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200968 goto error;
969 }
970 GETVAL(value, node, "value");
971 tpdf->dflt = lydict_insert(module->ctx, value, strlen(value));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200972 dflt_line = LOGLINE(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200973 } else if (!strcmp(node->name, "units")) {
974 if (tpdf->units) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100975 LOGVAL(LYE_TOOMANY, LOGLINE(node), LY_VLOG_NONE, NULL, node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200976 goto error;
977 }
978 GETVAL(value, node, "name");
979 tpdf->units = lydict_insert(module->ctx, value, strlen(value));
980 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100981 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200982 goto error;
983 }
984 }
Radek Krejci25d782a2015-05-22 15:03:23 +0200985
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200986 /* check mandatory value */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200987 if (!has_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +0100988 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200989 goto error;
990 }
Radek Krejcieac35532015-05-31 19:09:15 +0200991
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200992 /* check default value */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200993 if (tpdf->dflt) {
Michal Vasko0bd29d12015-08-19 11:45:49 +0200994 if (unres_schema_add_str(module, unres, &tpdf->type, UNRES_TYPE_DFLT, tpdf->dflt, dflt_line) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +0200995 goto error;
996 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200997 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200998
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200999 return EXIT_SUCCESS;
Radek Krejcieac35532015-05-31 19:09:15 +02001000
1001error:
1002
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001003 return EXIT_FAILURE;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001004}
1005
Michal Vasko0d343d12015-08-24 14:57:36 +02001006/* logs directly */
Radek Krejci3cf9e222015-06-18 11:37:50 +02001007static int
Michal Vaskof02e3742015-08-05 16:27:02 +02001008fill_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 +02001009{
1010 const char *value;
1011 struct lyxml_elem *child, *next;
Michal Vasko2d851a92015-10-20 16:16:36 +02001012 int c = 0, ret;
Radek Krejci3cf9e222015-06-18 11:37:50 +02001013
Radek Krejcib05774c2015-06-18 13:52:59 +02001014 GETVAL(value, yin, "name");
Radek Krejcic6556022016-01-27 15:16:45 +01001015 if (lyp_check_identifier(value, LY_IDENT_FEATURE, LOGLINE(yin), module, NULL)) {
Radek Krejcib05774c2015-06-18 13:52:59 +02001016 goto error;
1017 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001018 f->name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci6a113852015-07-03 16:04:20 +02001019 f->module = module;
Radek Krejcib05774c2015-06-18 13:52:59 +02001020
Radek Krejci76512572015-08-04 09:47:08 +02001021 if (read_yin_common(module, NULL, (struct lys_node *)f, yin, 0)) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02001022 goto error;
1023 }
1024
1025 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001026 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1027 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01001028 lyxml_free(module->ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001029 continue;
1030 }
1031
Radek Krejci3cf9e222015-06-18 11:37:50 +02001032 if (!strcmp(child->name, "if-feature")) {
1033 c++;
1034 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001035 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001036 goto error;
1037 }
1038 }
1039
1040 if (c) {
1041 f->features = calloc(c, sizeof *f->features);
Michal Vasko253035f2015-12-17 16:58:13 +01001042 if (!f->features) {
1043 LOGMEM;
1044 goto error;
1045 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02001046 }
Radek Krejci73adb602015-07-02 18:07:40 +02001047 LY_TREE_FOR(yin->child, child) {
Michal Vasko1d337e12016-02-15 12:32:04 +01001048 ret = fill_yin_iffeature((struct lys_node *)f, child, &f->features[f->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01001049 f->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01001050 if (ret) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001051 goto error;
1052 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02001053 }
1054
Radek Krejci3cf9e222015-06-18 11:37:50 +02001055 return EXIT_SUCCESS;
1056
1057error:
1058
1059 return EXIT_FAILURE;
1060}
1061
Michal Vasko0d343d12015-08-24 14:57:36 +02001062/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001063static int
Radek Krejcib8048692015-08-05 13:36:34 +02001064fill_yin_must(struct lys_module *module, struct lyxml_elem *yin, struct lys_restr *must)
Radek Krejci800af702015-06-02 13:46:01 +02001065{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001066 const char *value;
Radek Krejci800af702015-06-02 13:46:01 +02001067
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001068 GETVAL(value, yin, "condition");
Michal Vaskofba15262015-10-21 12:10:28 +02001069 must->expr = transform_schema2json(module, value, LOGLINE(yin));
Michal Vaskof9893382015-10-09 14:03:04 +02001070 if (!must->expr) {
1071 goto error;
1072 }
Michal Vasko77dc5652016-02-15 12:32:42 +01001073 if (lyxp_syntax_check(must->expr, LOGLINE(yin))) {
1074 goto error;
1075 }
Radek Krejci800af702015-06-02 13:46:01 +02001076
Radek Krejci41726f92015-06-19 13:11:05 +02001077 return read_restr_substmt(module->ctx, must, yin);
Radek Krejci800af702015-06-02 13:46:01 +02001078
Michal Vasko77dc5652016-02-15 12:32:42 +01001079error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001080 return EXIT_FAILURE;
Radek Krejci800af702015-06-02 13:46:01 +02001081}
1082
Radek Krejci581ce772015-11-10 17:22:40 +01001083static int
Michal Vasko88c29542015-11-27 14:57:53 +01001084fill_yin_unique(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_unique *unique,
1085 struct unres_schema *unres)
Radek Krejci581ce772015-11-10 17:22:40 +01001086{
1087 int i, j;
1088 const char *value, *vaux;
1089
1090 /* get unique value (list of leafs supposed to be unique */
1091 GETVAL(value, yin, "tag");
1092
1093 /* count the number of unique leafs in the value */
1094 vaux = value;
1095 while ((vaux = strpbrk(vaux, " \t\n"))) {
1096 unique->expr_size++;
1097 while (isspace(*vaux)) {
1098 vaux++;
1099 }
1100 }
1101 unique->expr_size++;
1102 unique->expr = calloc(unique->expr_size, sizeof *unique->expr);
Michal Vasko253035f2015-12-17 16:58:13 +01001103 if (!unique->expr) {
1104 LOGMEM;
1105 goto error;
1106 }
Radek Krejci581ce772015-11-10 17:22:40 +01001107
1108 for (i = 0; i < unique->expr_size; i++) {
1109 vaux = strpbrk(value, " \t\n");
1110 if (!vaux) {
1111 /* the last token, lydict_insert() will count its size on its own */
1112 vaux = value;
1113 }
1114
1115 /* store token into unique structure */
1116 unique->expr[i] = lydict_insert(module->ctx, value, vaux - value);
1117
1118 /* check that the expression does not repeat */
1119 for (j = 0; j < i; j++) {
Radek Krejci749190d2016-02-18 16:26:25 +01001120 if (ly_strequal(unique->expr[j], unique->expr[i], 1)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001121 LOGVAL(LYE_INARG, LOGLINE(yin), LY_VLOG_NONE, NULL, unique->expr[i], "unique");
Radek Krejciadb57612016-02-16 13:34:34 +01001122 LOGVAL(LYE_SPEC, 0, 0, NULL, "The identifier is not unique");
Radek Krejci581ce772015-11-10 17:22:40 +01001123 goto error;
1124 }
1125 }
1126
1127 /* try to resolve leaf */
1128 if (unres) {
1129 unres_schema_add_str(module, unres, parent, UNRES_LIST_UNIQ, unique->expr[i], LOGLINE(yin));
1130 } else {
1131 if (resolve_unique(parent, value, 0, LOGLINE(yin))) {
1132 goto error;
1133 }
1134 }
1135
1136 /* move to next token */
1137 value = vaux;
1138 while(isspace(*value)) {
1139 value++;
1140 }
1141 }
1142
1143 return EXIT_SUCCESS;
1144
1145error:
1146 return EXIT_FAILURE;
1147}
1148
Michal Vasko0d343d12015-08-24 14:57:36 +02001149/* logs directly
1150 *
Radek Krejcieb00f512015-07-01 16:44:58 +02001151 * type: 0 - min, 1 - max
1152 */
1153static int
Radek Krejcia52656e2015-08-05 13:41:50 +02001154deviate_minmax(struct lys_node *target, struct lyxml_elem *node, struct lys_deviate *d, int type)
Radek Krejcieb00f512015-07-01 16:44:58 +02001155{
1156 const char *value;
1157 char *endptr;
1158 unsigned long val;
1159 uint32_t *ui32val;
1160
1161 /* check target node type */
Radek Krejci76512572015-08-04 09:47:08 +02001162 if (target->nodetype == LYS_LEAFLIST) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001163 if (type) {
Radek Krejcib8048692015-08-05 13:36:34 +02001164 ui32val = &((struct lys_node_leaflist *)target)->max;
Radek Krejcieb00f512015-07-01 16:44:58 +02001165 } else {
Radek Krejcib8048692015-08-05 13:36:34 +02001166 ui32val = &((struct lys_node_leaflist *)target)->min;
Radek Krejcieb00f512015-07-01 16:44:58 +02001167 }
Radek Krejci76512572015-08-04 09:47:08 +02001168 } else if (target->nodetype == LYS_LIST) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001169 if (type) {
Radek Krejcib8048692015-08-05 13:36:34 +02001170 ui32val = &((struct lys_node_list *)target)->max;
Radek Krejcieb00f512015-07-01 16:44:58 +02001171 } else {
Radek Krejcib8048692015-08-05 13:36:34 +02001172 ui32val = &((struct lys_node_list *)target)->min;
Radek Krejcieb00f512015-07-01 16:44:58 +02001173 }
1174 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001175 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001176 LOGVAL(LYE_SPEC, 0, 0, NULL, "Target node does not allow \"%s\" property.", node->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001177 goto error;
1178 }
1179
1180 GETVAL(value, node, "value");
1181 while (isspace(value[0])) {
1182 value++;
1183 }
1184
Radek Krejci0d7b2472016-02-12 11:11:03 +01001185 if (type && !strcmp(value, "unbounded")) {
1186 d->max = val = 0;
Radek Krejcieb00f512015-07-01 16:44:58 +02001187 } else {
Radek Krejci0d7b2472016-02-12 11:11:03 +01001188 /* convert it to uint32_t */
1189 errno = 0;
1190 endptr = NULL;
1191 val = strtoul(value, &endptr, 10);
1192 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001193 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, value, node->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01001194 goto error;
1195 }
1196 if (type) {
1197 d->max = (uint32_t)val;
1198 } else {
1199 d->min = (uint32_t)val;
1200 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001201 }
1202
1203 if (d->mod == LY_DEVIATE_ADD) {
1204 /* check that there is no current value */
1205 if (*ui32val) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001206 LOGVAL(LYE_INSTMT, LOGLINE(node), LY_VLOG_NONE, NULL, node->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001207 LOGVAL(LYE_SPEC, 0, 0, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001208 goto error;
1209 }
1210 }
1211
1212 if (d->mod == LY_DEVIATE_DEL) {
1213 /* check values */
1214 if ((uint32_t)val != *ui32val) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001215 LOGVAL(LYE_INARG, LOGLINE(node), LY_VLOG_NONE, NULL, value, node->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001216 LOGVAL(LYE_SPEC, 0, 0, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001217 goto error;
1218 }
1219 /* remove current min-elements value of the target */
1220 *ui32val = 0;
1221 } else { /* add (already checked) and replace */
1222 /* set new value specified in deviation */
1223 *ui32val = (uint32_t)val;
1224 }
1225
1226 return EXIT_SUCCESS;
1227
1228error:
1229
1230 return EXIT_FAILURE;
1231}
1232
Michal Vasko0d343d12015-08-24 14:57:36 +02001233/* logs directly */
Radek Krejcieb00f512015-07-01 16:44:58 +02001234static int
Michal Vasko88c29542015-11-27 14:57:53 +01001235fill_yin_deviation(struct lys_module *module, struct lyxml_elem *yin, struct lys_deviation *dev,
1236 struct unres_schema *unres)
Radek Krejcieb00f512015-07-01 16:44:58 +02001237{
1238 const char *value, **stritem;
1239 struct lyxml_elem *next, *child, *develem;
1240 int c_dev = 0, c_must, c_uniq;
Radek Krejci0d7b2472016-02-12 11:11:03 +01001241 int f_min = 0, f_max = 0; /* flags */
Michal Vaskob40b4512016-02-11 11:35:37 +01001242 int i, j, rc;
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001243 struct ly_ctx *ctx;
Radek Krejcia52656e2015-08-05 13:41:50 +02001244 struct lys_deviate *d = NULL;
Michal Vasko60f4b452016-02-12 11:02:55 +01001245 struct lys_node *node = NULL, *dev_target = NULL;
Radek Krejcib8048692015-08-05 13:36:34 +02001246 struct lys_node_choice *choice = NULL;
1247 struct lys_node_leaf *leaf = NULL;
1248 struct lys_node_list *list = NULL;
Radek Krejci1574a8d2015-08-03 14:16:52 +02001249 struct lys_type *t = NULL;
Radek Krejcie4c366b2015-07-02 10:11:31 +02001250 uint8_t *trg_must_size = NULL;
Radek Krejci1574a8d2015-08-03 14:16:52 +02001251 struct lys_restr **trg_must = NULL;
Michal Vaskoff006c12016-02-17 11:15:19 +01001252 struct unres_schema tmp_unres;
Radek Krejcieb00f512015-07-01 16:44:58 +02001253
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001254 ctx = module->ctx;
1255
Radek Krejcieb00f512015-07-01 16:44:58 +02001256 GETVAL(value, yin, "target-node");
Michal Vaskofba15262015-10-21 12:10:28 +02001257 dev->target_name = transform_schema2json(module, value, LOGLINE(yin));
Michal Vaskoa8b25952015-10-20 15:30:25 +02001258 if (!dev->target_name) {
1259 goto error;
1260 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001261
1262 /* resolve target node */
Michal Vasko60f4b452016-02-12 11:02:55 +01001263 rc = resolve_augment_schema_nodeid(dev->target_name, NULL, module, (const struct lys_node **)&dev_target);
1264 if (rc || !dev_target) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001265 LOGVAL(LYE_INARG, LOGLINE(yin), LY_VLOG_NONE, NULL, dev->target_name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001266 goto error;
1267 }
Michal Vaskob7b068c2016-02-16 13:51:50 +01001268 if (dev_target->module == lys_module(module)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001269 LOGVAL(LYE_SPEC, LOGLINE(yin), LY_VLOG_NONE, NULL, "Deviating own module is not allowed.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001270 goto error;
1271 }
1272 /* mark the target module as deviated */
Michal Vaskoff006c12016-02-17 11:15:19 +01001273 dev_target->module->deviated = 1;
1274
1275 /* copy our imports to the deviated module (deviations may need them to work) */
1276 for (i = 0; i < module->imp_size; ++i) {
1277 for (j = 0; j < dev_target->module->imp_size; ++j) {
1278 if (module->imp[i].module == dev_target->module->imp[j].module) {
1279 break;
1280 }
1281 }
1282
1283 if (j < dev_target->module->imp_size) {
1284 /* import is already there */
1285 continue;
1286 }
1287
1288 /* copy the import, mark it as external */
1289 ++dev_target->module->imp_size;
1290 dev_target->module->imp = ly_realloc(dev_target->module->imp, dev_target->module->imp_size * sizeof *dev_target->module->imp);
1291 if (!dev_target->module->imp) {
1292 LOGMEM;
1293 goto error;
1294 }
1295 dev_target->module->imp[dev_target->module->imp_size - 1].module = module->imp[i].module;
1296 dev_target->module->imp[dev_target->module->imp_size - 1].prefix = lydict_insert(module->ctx, module->imp[i].prefix, 0);
1297 memcpy(dev_target->module->imp[dev_target->module->imp_size - 1].rev, module->imp[i].rev, LY_REV_SIZE);
1298 dev_target->module->imp[dev_target->module->imp_size - 1].external = 1;
1299 }
1300
1301 /* copy ourselves to the deviated module as a special import (if we haven't yet, there could be more deviations of the same module) */
1302 for (i = 0; i < dev_target->module->imp_size; ++i) {
1303 if (dev_target->module->imp[i].module == module) {
1304 break;
1305 }
1306 }
1307
1308 if (i == dev_target->module->imp_size) {
1309 ++dev_target->module->imp_size;
1310 dev_target->module->imp = ly_realloc(dev_target->module->imp, dev_target->module->imp_size * sizeof *dev_target->module->imp);
1311 if (!dev_target->module->imp) {
1312 LOGMEM;
1313 goto error;
1314 }
1315 dev_target->module->imp[dev_target->module->imp_size - 1].module = module;
1316 dev_target->module->imp[dev_target->module->imp_size - 1].prefix = lydict_insert(module->ctx, module->prefix, 0);
1317 if (module->rev_size) {
1318 memcpy(dev_target->module->imp[dev_target->module->imp_size - 1].rev, module->rev[0].date, LY_REV_SIZE);
1319 } else {
1320 memset(dev_target->module->imp[dev_target->module->imp_size - 1].rev, 0, LY_REV_SIZE);
1321 }
1322 dev_target->module->imp[dev_target->module->imp_size - 1].external = 2;
1323 } else {
1324 /* it could have been added by another deviating module that imported this deviating module */
1325 dev_target->module->imp[i].external = 2;
1326 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001327
1328 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001329 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1330 /* garbage */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001331 lyxml_free(ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001332 continue;
1333 }
1334
Radek Krejcieb00f512015-07-01 16:44:58 +02001335 if (!strcmp(child->name, "description")) {
1336 if (dev->dsc) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001337 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001338 goto error;
1339 }
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001340 dev->dsc = read_yin_subnode(ctx, child, "text");
Radek Krejcieb00f512015-07-01 16:44:58 +02001341 if (!dev->dsc) {
1342 goto error;
1343 }
1344 } else if (!strcmp(child->name, "reference")) {
1345 if (dev->ref) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001346 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001347 goto error;
1348 }
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001349 dev->ref = read_yin_subnode(ctx, child, "text");
Radek Krejcieb00f512015-07-01 16:44:58 +02001350 if (!dev->ref) {
1351 goto error;
1352 }
1353 } else if (!strcmp(child->name, "deviate")) {
1354 c_dev++;
1355
Michal Vasko345da0a2015-12-02 10:35:55 +01001356 /* skip lyxml_free() at the end of the loop, node will be
Radek Krejcieb00f512015-07-01 16:44:58 +02001357 * further processed later
1358 */
1359 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02001360
Radek Krejcieb00f512015-07-01 16:44:58 +02001361 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001362 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001363 goto error;
1364 }
1365
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001366 lyxml_free(ctx, child);
Radek Krejcieb00f512015-07-01 16:44:58 +02001367 }
1368
1369 if (c_dev) {
1370 dev->deviate = calloc(c_dev, sizeof *dev->deviate);
Michal Vasko253035f2015-12-17 16:58:13 +01001371 if (!dev->deviate) {
1372 LOGMEM;
1373 goto error;
1374 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001375 }
1376
1377 LY_TREE_FOR(yin->child, develem) {
1378 /* init */
1379 f_min = 0;
Radek Krejci0d7b2472016-02-12 11:11:03 +01001380 f_max = 0;
Radek Krejcieb00f512015-07-01 16:44:58 +02001381 c_must = 0;
1382 c_uniq = 0;
1383
1384 /* get deviation type */
1385 GETVAL(value, develem, "value");
1386 if (!strcmp(value, "not-supported")) {
1387 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_NO;
1388 /* no property expected in this case */
1389 if (develem->child) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001390 LOGVAL(LYE_INSTMT, LOGLINE(develem->child), LY_VLOG_NONE, NULL, develem->child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001391 goto error;
1392 }
1393
Radek Krejci5b917642015-07-02 09:03:13 +02001394 /* and neither any other deviate statement is expected,
1395 * not-supported deviation must be the only deviation of the target
1396 */
1397 if (dev->deviate_size || develem->next) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001398 LOGVAL(LYE_INARG, LOGLINE(develem), LY_VLOG_NONE, NULL, value, develem->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001399 LOGVAL(LYE_SPEC, 0, 0, NULL, "\"not-supported\" deviation cannot be combined with any other deviation.");
Radek Krejci5b917642015-07-02 09:03:13 +02001400 goto error;
1401 }
1402
Michal Vaskoad1f7b72016-02-17 11:13:58 +01001403 /* you cannot remove a key leaf */
1404 if ((dev_target->nodetype == LYS_LEAF) && dev_target->parent && (dev_target->parent->nodetype == LYS_LIST)) {
1405 for (i = 0; i < ((struct lys_node_list *)dev_target->parent)->keys_size; ++i) {
1406 if (((struct lys_node_list *)dev_target->parent)->keys[i] == (struct lys_node_leaf *)dev_target) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001407 LOGVAL(LYE_INARG, LOGLINE(develem), LY_VLOG_NONE, NULL, value, develem->name);
Michal Vaskoad1f7b72016-02-17 11:13:58 +01001408 LOGVAL(LYE_SPEC, 0, 0, NULL, "\"not-supported\" deviation cannot remove a list key.");
1409 goto error;
1410 }
1411 }
1412 }
Radek Krejci5b917642015-07-02 09:03:13 +02001413
Michal Vaskoff006c12016-02-17 11:15:19 +01001414 /* unlink and store the original node */
1415 lys_node_unlink(dev_target);
1416 dev->orig_node = dev_target;
Radek Krejcieb00f512015-07-01 16:44:58 +02001417
Radek Krejci5b917642015-07-02 09:03:13 +02001418 dev->deviate_size = 1;
1419 return EXIT_SUCCESS;
Radek Krejcieb00f512015-07-01 16:44:58 +02001420 } else if (!strcmp(value, "add")) {
1421 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_ADD;
1422 } else if (!strcmp(value, "replace")) {
1423 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_RPL;
1424 } else if (!strcmp(value, "delete")) {
1425 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_DEL;
1426 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001427 LOGVAL(LYE_INARG, LOGLINE(develem), LY_VLOG_NONE, NULL, value, develem->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001428 goto error;
1429 }
1430 d = &dev->deviate[dev->deviate_size];
1431
Michal Vaskoff006c12016-02-17 11:15:19 +01001432 /* store a shallow copy of the original node */
1433 if (!dev->orig_node) {
1434 memset(&tmp_unres, 0, sizeof tmp_unres);
1435 dev->orig_node = lys_node_dup(dev_target->module, NULL, dev_target, 0, 0, &tmp_unres, 1);
1436 /* just to be safe */
1437 if (tmp_unres.count) {
1438 LOGINT;
1439 goto error;
1440 }
1441 }
1442
Radek Krejcieb00f512015-07-01 16:44:58 +02001443 /* process deviation properties */
1444 LY_TREE_FOR_SAFE(develem->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001445 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1446 /* garbage */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001447 lyxml_free(ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001448 continue;
1449 }
1450
Radek Krejcieb00f512015-07-01 16:44:58 +02001451 if (!strcmp(child->name, "config")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001452 if (d->flags & LYS_CONFIG_MASK) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001453 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001454 goto error;
1455 }
1456
1457 /* for we deviate from RFC 6020 and allow config property even it is/is not
1458 * specified in the target explicitly since config property inherits. So we expect
1459 * that config is specified in every node. But for delete, we check that the value
1460 * is the same as here in deviation
1461 */
1462 GETVAL(value, child, "value");
1463 if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001464 d->flags |= LYS_CONFIG_R;
Radek Krejcieb00f512015-07-01 16:44:58 +02001465 } else if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001466 d->flags |= LYS_CONFIG_W;
Radek Krejcieb00f512015-07-01 16:44:58 +02001467 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001468 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001469 goto error;
1470 }
1471
1472 if (d->mod == LY_DEVIATE_DEL) {
1473 /* check values */
Michal Vasko60f4b452016-02-12 11:02:55 +01001474 if ((d->flags & LYS_CONFIG_MASK) != (dev_target->flags & LYS_CONFIG_MASK)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001475 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001476 LOGVAL(LYE_SPEC, 0, 0, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001477 goto error;
1478 }
1479 /* remove current config value of the target ... */
Michal Vasko60f4b452016-02-12 11:02:55 +01001480 dev_target->flags &= ~LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001481
1482 /* ... and inherit config value from the target's parent */
Michal Vasko60f4b452016-02-12 11:02:55 +01001483 if (dev_target->parent) {
1484 dev_target->flags |= dev_target->parent->flags & LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001485 } else {
1486 /* default config is true */
Michal Vasko60f4b452016-02-12 11:02:55 +01001487 dev_target->flags |= LYS_CONFIG_W;
Radek Krejcieb00f512015-07-01 16:44:58 +02001488 }
1489 } else { /* add and replace are the same in this case */
1490 /* remove current config value of the target ... */
Michal Vasko60f4b452016-02-12 11:02:55 +01001491 dev_target->flags &= ~LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001492
1493 /* ... and replace it with the value specified in deviation */
Michal Vasko60f4b452016-02-12 11:02:55 +01001494 dev_target->flags |= d->flags & LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001495 }
1496 } else if (!strcmp(child->name, "default")) {
1497 if (d->dflt) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001498 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001499 goto error;
1500 }
1501 GETVAL(value, child, "value");
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001502 d->dflt = lydict_insert(ctx, value, 0);
Radek Krejcieb00f512015-07-01 16:44:58 +02001503
Michal Vasko60f4b452016-02-12 11:02:55 +01001504 if (dev_target->nodetype == LYS_CHOICE) {
1505 choice = (struct lys_node_choice *)dev_target;
Radek Krejcieb00f512015-07-01 16:44:58 +02001506
1507 if (d->mod == LY_DEVIATE_ADD) {
1508 /* check that there is no current value */
1509 if (choice->dflt) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001510 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001511 LOGVAL(LYE_SPEC, 0, 0, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001512 goto error;
1513 }
1514 }
1515
Michal Vasko3edeaf72016-02-11 13:17:43 +01001516 rc = resolve_choice_default_schema_nodeid(d->dflt, choice->child, (const struct lys_node **)&node);
Michal Vasko9bb061b2016-02-12 11:00:19 +01001517 if (rc || !node) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001518 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001519 goto error;
1520 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001521 if (d->mod == LY_DEVIATE_DEL) {
Michal Vasko60f4b452016-02-12 11:02:55 +01001522 if (!choice->dflt || (choice->dflt != node)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001523 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001524 LOGVAL(LYE_SPEC, 0, 0, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001525 goto error;
1526 }
1527 } else { /* add (already checked) and replace */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001528 choice->dflt = node;
Radek Krejcieb00f512015-07-01 16:44:58 +02001529 if (!choice->dflt) {
1530 /* default branch not found */
Michal Vaskoe5e06292016-02-26 09:56:41 +01001531 LOGVAL(LYE_INARG, LOGLINE(yin), LY_VLOG_NONE, NULL, value, "default");
Radek Krejcieb00f512015-07-01 16:44:58 +02001532 goto error;
1533 }
1534 }
Michal Vasko60f4b452016-02-12 11:02:55 +01001535 } else if (dev_target->nodetype == LYS_LEAF) {
1536 leaf = (struct lys_node_leaf *)dev_target;
Radek Krejcieb00f512015-07-01 16:44:58 +02001537
1538 if (d->mod == LY_DEVIATE_ADD) {
1539 /* check that there is no current value */
1540 if (leaf->dflt) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001541 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001542 LOGVAL(LYE_SPEC, 0, 0, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001543 goto error;
1544 }
1545 }
1546
1547 if (d->mod == LY_DEVIATE_DEL) {
Michal Vasko60f4b452016-02-12 11:02:55 +01001548 if (!leaf->dflt || (leaf->dflt != d->dflt)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001549 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001550 LOGVAL(LYE_SPEC, 0, 0, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001551 goto error;
1552 }
1553 /* remove value */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001554 lydict_remove(ctx, leaf->dflt);
Radek Krejcieb00f512015-07-01 16:44:58 +02001555 leaf->dflt = NULL;
1556 } else { /* add (already checked) and replace */
1557 /* remove value */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001558 lydict_remove(ctx, leaf->dflt);
Radek Krejcieb00f512015-07-01 16:44:58 +02001559
1560 /* set new value */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001561 leaf->dflt = lydict_insert(ctx, d->dflt, 0);
Radek Krejcieb00f512015-07-01 16:44:58 +02001562 }
1563 } else {
1564 /* invalid target for default value */
Michal Vaskoe5e06292016-02-26 09:56:41 +01001565 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001566 LOGVAL(LYE_SPEC, 0, 0, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001567 goto error;
1568 }
1569 } else if (!strcmp(child->name, "mandatory")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001570 if (d->flags & LYS_MAND_MASK) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001571 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001572 goto error;
1573 }
1574
1575 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01001576 if (!(dev_target->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYXML))) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001577 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001578 LOGVAL(LYE_SPEC, 0, 0, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001579 goto error;
1580 }
1581
1582 GETVAL(value, child, "value");
1583 if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001584 d->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02001585 } else if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001586 d->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02001587 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001588 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001589 goto error;
1590 }
1591
1592 if (d->mod == LY_DEVIATE_ADD) {
1593 /* check that there is no current value */
Michal Vasko60f4b452016-02-12 11:02:55 +01001594 if (dev_target->flags & LYS_MAND_MASK) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001595 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001596 LOGVAL(LYE_SPEC, 0, 0, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001597 goto error;
1598 }
1599 }
1600
1601 if (d->mod == LY_DEVIATE_DEL) {
1602 /* check values */
Michal Vasko60f4b452016-02-12 11:02:55 +01001603 if ((d->flags & LYS_MAND_MASK) != (dev_target->flags & LYS_MAND_MASK)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001604 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001605 LOGVAL(LYE_SPEC, 0, 0, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001606 goto error;
1607 }
1608 /* remove current mandatory value of the target */
Michal Vasko60f4b452016-02-12 11:02:55 +01001609 dev_target->flags &= ~LYS_MAND_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001610 } else { /* add (already checked) and replace */
1611 /* remove current mandatory value of the target ... */
Michal Vasko60f4b452016-02-12 11:02:55 +01001612 dev_target->flags &= ~LYS_MAND_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001613
1614 /* ... and replace it with the value specified in deviation */
Michal Vasko60f4b452016-02-12 11:02:55 +01001615 dev_target->flags |= d->flags & LYS_MAND_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001616 }
1617 } else if (!strcmp(child->name, "min-elements")) {
1618 if (f_min) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001619 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001620 goto error;
1621 }
1622 f_min = 1;
1623
Michal Vasko60f4b452016-02-12 11:02:55 +01001624 if (deviate_minmax(dev_target, child, d, 0)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001625 goto error;
1626 }
1627 } else if (!strcmp(child->name, "max-elements")) {
Radek Krejci0d7b2472016-02-12 11:11:03 +01001628 if (f_max) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001629 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001630 goto error;
1631 }
Radek Krejci0d7b2472016-02-12 11:11:03 +01001632 f_max = 1;
Radek Krejcieb00f512015-07-01 16:44:58 +02001633
Michal Vasko60f4b452016-02-12 11:02:55 +01001634 if (deviate_minmax(dev_target, child, d, 1)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001635 goto error;
1636 }
1637 } else if (!strcmp(child->name, "must")) {
1638 c_must++;
Michal Vasko345da0a2015-12-02 10:35:55 +01001639 /* skip lyxml_free() at the end of the loop, this node will be processed later */
Radek Krejcieb00f512015-07-01 16:44:58 +02001640 continue;
1641 } else if (!strcmp(child->name, "type")) {
1642 if (d->type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001643 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001644 goto error;
1645 }
1646
1647 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01001648 if (dev_target->nodetype == LYS_LEAF) {
1649 t = &((struct lys_node_leaf *)dev_target)->type;
1650 } else if (dev_target->nodetype == LYS_LEAFLIST) {
1651 t = &((struct lys_node_leaflist *)dev_target)->type;
Radek Krejcieb00f512015-07-01 16:44:58 +02001652 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001653 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001654 LOGVAL(LYE_SPEC, 0, 0, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001655 goto error;
1656 }
1657
1658 if (d->mod == LY_DEVIATE_ADD) {
1659 /* not allowed, type is always present at the target */
Michal Vaskoe5e06292016-02-26 09:56:41 +01001660 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001661 LOGVAL(LYE_SPEC, 0, 0, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001662 goto error;
1663 } else if (d->mod == LY_DEVIATE_DEL) {
1664 /* not allowed, type cannot be deleted from the target */
Michal Vaskoe5e06292016-02-26 09:56:41 +01001665 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001666 LOGVAL(LYE_SPEC, 0, 0, NULL, "Deleteing type from the target is not allowed.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001667 goto error;
1668 }
1669
1670 /* replace */
1671 /* remove current units value of the target ... */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001672 lys_type_free(ctx, t);
Radek Krejcieb00f512015-07-01 16:44:58 +02001673
1674 /* ... and replace it with the value specified in deviation */
Michal Vasko88c29542015-11-27 14:57:53 +01001675 /* HACK for unres */
1676 t->der = (struct lys_tpdf *)child;
Michal Vasko60f4b452016-02-12 11:02:55 +01001677 if (unres_schema_add_node(module, unres, t, UNRES_TYPE_DER, dev_target, LOGLINE(child))) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001678 goto error;
1679 }
1680 d->type = t;
1681 } else if (!strcmp(child->name, "unique")) {
1682 c_uniq++;
Michal Vasko345da0a2015-12-02 10:35:55 +01001683 /* skip lyxml_free() at the end of the loop, this node will be processed later */
Radek Krejcieb00f512015-07-01 16:44:58 +02001684 continue;
1685 } else if (!strcmp(child->name, "units")) {
1686 if (d->units) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001687 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001688 goto error;
1689 }
1690
1691 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01001692 if (dev_target->nodetype == LYS_LEAFLIST) {
1693 stritem = &((struct lys_node_leaflist *)dev_target)->units;
1694 } else if (dev_target->nodetype == LYS_LEAF) {
1695 stritem = &((struct lys_node_leaf *)dev_target)->units;
Radek Krejcieb00f512015-07-01 16:44:58 +02001696 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001697 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001698 LOGVAL(LYE_SPEC, 0, 0, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001699 goto error;
1700 }
1701
1702 /* get units value */
1703 GETVAL(value, child, "name");
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001704 d->units = lydict_insert(ctx, value, 0);
Radek Krejcieb00f512015-07-01 16:44:58 +02001705
1706 /* apply to target */
1707 if (d->mod == LY_DEVIATE_ADD) {
1708 /* check that there is no current value */
1709 if (*stritem) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001710 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001711 LOGVAL(LYE_SPEC, 0, 0, NULL, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001712 goto error;
1713 }
1714 }
1715
1716 if (d->mod == LY_DEVIATE_DEL) {
1717 /* check values */
1718 if (*stritem != d->units) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001719 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001720 LOGVAL(LYE_SPEC, 0, 0, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001721 goto error;
1722 }
1723 /* remove current units value of the target */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001724 lydict_remove(ctx, *stritem);
Radek Krejcieb00f512015-07-01 16:44:58 +02001725 } else { /* add (already checked) and replace */
1726 /* remove current units value of the target ... */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001727 lydict_remove(ctx, *stritem);
Radek Krejcieb00f512015-07-01 16:44:58 +02001728
1729 /* ... and replace it with the value specified in deviation */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001730 *stritem = lydict_insert(ctx, value, 0);
Radek Krejcieb00f512015-07-01 16:44:58 +02001731 }
1732 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001733 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001734 goto error;
1735 }
1736
Michal Vasko88c29542015-11-27 14:57:53 +01001737 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejcieb00f512015-07-01 16:44:58 +02001738 }
1739
1740 if (c_must) {
1741 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01001742 switch (dev_target->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02001743 case LYS_LEAF:
Michal Vasko60f4b452016-02-12 11:02:55 +01001744 trg_must = &((struct lys_node_leaf *)dev_target)->must;
1745 trg_must_size = &((struct lys_node_leaf *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02001746 break;
Radek Krejci76512572015-08-04 09:47:08 +02001747 case LYS_CONTAINER:
Michal Vasko60f4b452016-02-12 11:02:55 +01001748 trg_must = &((struct lys_node_container *)dev_target)->must;
1749 trg_must_size = &((struct lys_node_container *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02001750 break;
Radek Krejci76512572015-08-04 09:47:08 +02001751 case LYS_LEAFLIST:
Michal Vasko60f4b452016-02-12 11:02:55 +01001752 trg_must = &((struct lys_node_leaflist *)dev_target)->must;
1753 trg_must_size = &((struct lys_node_leaflist *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02001754 break;
Radek Krejci76512572015-08-04 09:47:08 +02001755 case LYS_LIST:
Michal Vasko60f4b452016-02-12 11:02:55 +01001756 trg_must = &((struct lys_node_list *)dev_target)->must;
1757 trg_must_size = &((struct lys_node_list *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02001758 break;
Radek Krejci76512572015-08-04 09:47:08 +02001759 case LYS_ANYXML:
Michal Vasko60f4b452016-02-12 11:02:55 +01001760 trg_must = &((struct lys_node_anyxml *)dev_target)->must;
1761 trg_must_size = &((struct lys_node_anyxml *)dev_target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02001762 break;
1763 default:
Michal Vaskoe5e06292016-02-26 09:56:41 +01001764 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001765 LOGVAL(LYE_SPEC, 0, 0, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001766 goto error;
1767 }
1768
1769 if (d->mod == LY_DEVIATE_RPL) {
1770 /* remove target's musts and allocate new array for it */
1771 if (!*trg_must) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001772 LOGVAL(LYE_INARG, LOGLINE(develem), LY_VLOG_NONE, NULL, "replace", "deviate");
Radek Krejciadb57612016-02-16 13:34:34 +01001773 LOGVAL(LYE_SPEC, 0, 0, NULL, "Property \"must\" to replace does not exists in target.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001774 goto error;
1775 }
1776
1777 for (i = 0; i < list->must_size; i++) {
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001778 lys_restr_free(ctx, &(*trg_must[i]));
Radek Krejcieb00f512015-07-01 16:44:58 +02001779 }
1780 free(*trg_must);
1781 *trg_must = d->must = calloc(c_must, sizeof *d->must);
1782 d->must_size = c_must;
1783 *trg_must_size = 0;
1784 } else if (d->mod == LY_DEVIATE_ADD) {
1785 /* reallocate the must array of the target */
Michal Vasko253035f2015-12-17 16:58:13 +01001786 d->must = ly_realloc(*trg_must, (c_must + *trg_must_size) * sizeof *d->must);
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001787 if (!d->must) {
1788 LOGMEM;
1789 goto error;
1790 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001791 *trg_must = d->must;
Michal Vasko979ad5b2015-10-23 10:12:55 +02001792 d->must = &((*trg_must)[*trg_must_size]);
Radek Krejcieb00f512015-07-01 16:44:58 +02001793 d->must_size = c_must;
1794 } else { /* LY_DEVIATE_DEL */
1795 d->must = calloc(c_must, sizeof *d->must);
1796 }
Michal Vasko253035f2015-12-17 16:58:13 +01001797 if (!d->must) {
1798 LOGMEM;
1799 goto error;
1800 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001801 }
1802 if (c_uniq) {
1803 /* check target node type */
Michal Vasko60f4b452016-02-12 11:02:55 +01001804 if (dev_target->nodetype != LYS_LIST) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001805 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001806 LOGVAL(LYE_SPEC, 0, 0, NULL, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001807 goto error;
1808 }
1809
Michal Vasko60f4b452016-02-12 11:02:55 +01001810 list = (struct lys_node_list *)dev_target;
Radek Krejcieb00f512015-07-01 16:44:58 +02001811 if (d->mod == LY_DEVIATE_RPL) {
1812 /* remove target's unique and allocate new array for it */
1813 if (!list->unique) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01001814 LOGVAL(LYE_INARG, LOGLINE(develem), LY_VLOG_NONE, NULL, "replace", "deviate");
Radek Krejciadb57612016-02-16 13:34:34 +01001815 LOGVAL(LYE_SPEC, 0, 0, NULL, "Property \"unique\" to replace does not exists in target.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001816 goto error;
1817 }
1818
1819 for (i = 0; i < list->unique_size; i++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001820 for (j = 0; j < list->unique[i].expr_size; j++) {
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001821 lydict_remove(ctx, list->unique[i].expr[j]);
Radek Krejci581ce772015-11-10 17:22:40 +01001822 }
1823 free(list->unique[i].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001824 }
1825 free(list->unique);
1826 list->unique = d->unique = calloc(c_uniq, sizeof *d->unique);
1827 d->unique_size = c_uniq;
1828 list->unique_size = 0;
1829 } else if (d->mod == LY_DEVIATE_ADD) {
1830 /* reallocate the unique array of the target */
Michal Vasko253035f2015-12-17 16:58:13 +01001831 d->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *d->unique);
Radek Krejcieb00f512015-07-01 16:44:58 +02001832 list->unique = d->unique;
1833 d->unique = &list->unique[list->unique_size];
1834 d->unique_size = c_uniq;
1835 } else { /* LY_DEVIATE_DEL */
1836 d->unique = calloc(c_uniq, sizeof *d->unique);
1837 }
Michal Vasko253035f2015-12-17 16:58:13 +01001838 if (!d->unique) {
1839 LOGMEM;
1840 goto error;
1841 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001842 }
1843
1844 /* process deviation properties with 0..n cardinality */
Radek Krejci73adb602015-07-02 18:07:40 +02001845 LY_TREE_FOR(develem->child, child) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001846 if (!strcmp(child->name, "must")) {
1847 if (d->mod == LY_DEVIATE_DEL) {
1848 if (fill_yin_must(module, child, &d->must[d->must_size])) {
1849 goto error;
1850 }
1851
1852 /* find must to delete, we are ok with just matching conditions */
1853 for (i = 0; i < *trg_must_size; i++) {
Radek Krejci749190d2016-02-18 16:26:25 +01001854 if (ly_strequal(d->must[d->must_size].expr, (*trg_must)[i].expr, 1)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001855 /* we have a match, free the must structure ... */
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001856 lys_restr_free(ctx, &((*trg_must)[i]));
Radek Krejcieb00f512015-07-01 16:44:58 +02001857 /* ... and maintain the array */
1858 (*trg_must_size)--;
1859 if (i != *trg_must_size) {
1860 (*trg_must)[i].expr = (*trg_must)[*trg_must_size].expr;
1861 (*trg_must)[i].dsc = (*trg_must)[*trg_must_size].dsc;
1862 (*trg_must)[i].ref = (*trg_must)[*trg_must_size].ref;
1863 (*trg_must)[i].eapptag = (*trg_must)[*trg_must_size].eapptag;
1864 (*trg_must)[i].emsg = (*trg_must)[*trg_must_size].emsg;
1865 }
1866 if (!(*trg_must_size)) {
1867 free(*trg_must);
1868 *trg_must = NULL;
1869 } else {
1870 (*trg_must)[*trg_must_size].expr = NULL;
1871 (*trg_must)[*trg_must_size].dsc = NULL;
1872 (*trg_must)[*trg_must_size].ref = NULL;
1873 (*trg_must)[*trg_must_size].eapptag = NULL;
1874 (*trg_must)[*trg_must_size].emsg = NULL;
1875 }
1876
1877 i = -1; /* set match flag */
1878 break;
1879 }
1880 }
1881 d->must_size++;
1882 if (i != -1) {
1883 /* no match found */
Michal Vaskoe5e06292016-02-26 09:56:41 +01001884 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL,
Radek Krejciadb57612016-02-16 13:34:34 +01001885 d->must[d->must_size - 1].expr, child->name);
1886 LOGVAL(LYE_SPEC, 0, 0, NULL, "Value does not match any must from the target.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001887 goto error;
1888 }
1889 } else { /* replace or add */
Michal Vaskof92a7282016-02-11 12:35:57 +01001890 memset(&((*trg_must)[*trg_must_size]), 0, sizeof **trg_must);
1891 if (fill_yin_must(module, child, &((*trg_must)[*trg_must_size]))) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001892 goto error;
1893 }
1894 (*trg_must_size)++;
1895 }
1896 } else if (!strcmp(child->name, "unique")) {
1897 if (d->mod == LY_DEVIATE_DEL) {
Michal Vasko60f4b452016-02-12 11:02:55 +01001898 if (fill_yin_unique(module, dev_target, child, &d->unique[d->unique_size], NULL)) {
Radek Krejci581ce772015-11-10 17:22:40 +01001899 d->unique_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02001900 goto error;
1901 }
1902
1903 /* find unique structures to delete */
1904 for (i = 0; i < list->unique_size; i++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001905 if (list->unique[i].expr_size != d->unique[d->unique_size].expr_size) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001906 continue;
1907 }
1908
Radek Krejci581ce772015-11-10 17:22:40 +01001909 for (j = 0; j < d->unique[d->unique_size].expr_size; j++) {
Radek Krejci749190d2016-02-18 16:26:25 +01001910 if (!ly_strequal(list->unique[i].expr[j], d->unique[d->unique_size].expr[j], 1)) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001911 break;
1912 }
1913 }
1914
Radek Krejci581ce772015-11-10 17:22:40 +01001915 if (j == d->unique[d->unique_size].expr_size) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001916 /* we have a match, free the unique structure ... */
Radek Krejci581ce772015-11-10 17:22:40 +01001917 for (j = 0; j < list->unique[i].expr_size; j++) {
Michal Vaskodc48e7f2016-02-11 12:35:27 +01001918 lydict_remove(ctx, list->unique[i].expr[j]);
Radek Krejci581ce772015-11-10 17:22:40 +01001919 }
1920 free(list->unique[i].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001921 /* ... and maintain the array */
1922 list->unique_size--;
1923 if (i != list->unique_size) {
Radek Krejci581ce772015-11-10 17:22:40 +01001924 list->unique[i].expr_size = list->unique[list->unique_size].expr_size;
1925 list->unique[i].expr = list->unique[list->unique_size].expr;
Radek Krejcieb00f512015-07-01 16:44:58 +02001926 }
1927
1928 if (!list->unique_size) {
1929 free(list->unique);
1930 list->unique = NULL;
1931 } else {
Radek Krejci581ce772015-11-10 17:22:40 +01001932 list->unique[list->unique_size].expr_size = 0;
1933 list->unique[list->unique_size].expr = NULL;
Radek Krejcieb00f512015-07-01 16:44:58 +02001934 }
1935
1936 i = -1; /* set match flag */
1937 break;
1938 }
1939 }
1940
1941 d->unique_size++;
1942 if (i != -1) {
1943 /* no match found */
Michal Vaskoe5e06292016-02-26 09:56:41 +01001944 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, lyxml_get_attr(child, "tag", NULL), child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01001945 LOGVAL(LYE_SPEC, 0, 0, NULL, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001946 goto error;
1947 }
1948 } else { /* replace or add */
Michal Vasko60f4b452016-02-12 11:02:55 +01001949 i = fill_yin_unique(module, dev_target, child, &list->unique[list->unique_size], NULL);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01001950 list->unique_size++;
1951 if (i) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001952 goto error;
1953 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001954 }
1955 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001956 }
Radek Krejci5b917642015-07-02 09:03:13 +02001957
1958 dev->deviate_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02001959 }
1960
Radek Krejcieb00f512015-07-01 16:44:58 +02001961 return EXIT_SUCCESS;
1962
1963error:
1964
Radek Krejcieb00f512015-07-01 16:44:58 +02001965 return EXIT_FAILURE;
1966}
1967
Michal Vasko0d343d12015-08-24 14:57:36 +02001968/* logs directly */
Radek Krejcieb00f512015-07-01 16:44:58 +02001969static int
Radek Krejcib8048692015-08-05 13:36:34 +02001970fill_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 +02001971 struct unres_schema *unres)
Radek Krejci106efc02015-06-10 14:36:27 +02001972{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001973 const char *value;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001974 struct lyxml_elem *child, *next;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001975 struct lys_node *node;
Michal Vasko2d851a92015-10-20 16:16:36 +02001976 int c = 0, ret;
Radek Krejci106efc02015-06-10 14:36:27 +02001977
Michal Vasko591e0b22015-08-13 13:53:43 +02001978 aug->nodetype = LYS_AUGMENT;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001979 GETVAL(value, yin, "target-node");
Michal Vaskofba15262015-10-21 12:10:28 +02001980 aug->target_name = transform_schema2json(module, value, LOGLINE(yin));
Michal Vasko488c19e2015-10-20 15:21:00 +02001981 if (!aug->target_name) {
1982 goto error;
1983 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001984 aug->parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001985
Michal Vasko1d87a922015-08-21 12:57:16 +02001986 if (read_yin_common(module, NULL, (struct lys_node *)aug, yin, OPT_MODULE | OPT_NACMEXT)) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02001987 goto error;
1988 }
1989
1990 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001991 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1992 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01001993 lyxml_free(module->ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001994 continue;
1995 }
1996
Radek Krejci3cf9e222015-06-18 11:37:50 +02001997 if (!strcmp(child->name, "if-feature")) {
1998 c++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001999 continue;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002000 } else if (!strcmp(child->name, "when")) {
2001 if (aug->when) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002002 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002003 goto error;
2004 }
2005
2006 aug->when = read_yin_when(module, child);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002007 if (!aug->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01002008 lyxml_free(module->ctx, child);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002009 goto error;
2010 }
Michal Vasko345da0a2015-12-02 10:35:55 +01002011 lyxml_free(module->ctx, child);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002012 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02002013
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002014 /* check allowed data sub-statements */
2015 } else if (!strcmp(child->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002016 node = read_yin_container(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002017 } else if (!strcmp(child->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002018 node = read_yin_leaflist(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002019 } else if (!strcmp(child->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002020 node = read_yin_leaf(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002021 } else if (!strcmp(child->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002022 node = read_yin_list(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002023 } else if (!strcmp(child->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002024 node = read_yin_uses(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002025 } else if (!strcmp(child->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002026 node = read_yin_case(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002027 } else if (!strcmp(child->name, "case")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002028 node = read_yin_case(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002029 } else if (!strcmp(child->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002030 node = read_yin_anyxml(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002031 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002032 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002033 goto error;
2034 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002035
Radek Krejci1d82ef62015-08-07 14:44:40 +02002036 if (!node) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002037 goto error;
2038 }
2039
2040 /* check for mandatory nodes - if the target node is in another module
2041 * the added nodes cannot be mandatory
2042 */
Radek Krejcic6556022016-01-27 15:16:45 +01002043 if ((!parent || (parent->nodetype != LYS_USES)) && lyp_check_mandatory(node)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002044 LOGVAL(LYE_SPEC, LOGLINE(child), LY_VLOG_NONE, NULL,
Radek Krejciadb57612016-02-16 13:34:34 +01002045 "When augmenting data in another module, mandatory statement is not allowed.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002046 goto error;
2047 }
2048
Radek Krejci1d82ef62015-08-07 14:44:40 +02002049 node = NULL;
Michal Vasko345da0a2015-12-02 10:35:55 +01002050 lyxml_free(module->ctx, child);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002051 }
2052
2053 if (c) {
2054 aug->features = calloc(c, sizeof *aug->features);
Michal Vasko253035f2015-12-17 16:58:13 +01002055 if (!aug->features) {
2056 LOGMEM;
2057 goto error;
2058 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002059 }
2060
2061 LY_TREE_FOR_SAFE(yin->child, next, child) {
2062 if (!strcmp(child->name, "if-feature")) {
Michal Vasko1d337e12016-02-15 12:32:04 +01002063 ret = fill_yin_iffeature((struct lys_node *)aug, child, &aug->features[aug->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002064 aug->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01002065 if (ret) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002066 goto error;
2067 }
Michal Vasko345da0a2015-12-02 10:35:55 +01002068 lyxml_free(module->ctx, child);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002069 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002070 }
2071
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002072 /* aug->child points to the parsed nodes, they must now be
Michal Vasko49291b32015-08-06 09:49:41 +02002073 * connected to the tree and adjusted (if possible right now).
2074 * However, if this is augment in a uses, it gets resolved
2075 * when the uses does and cannot be resolved now for sure
2076 * (the grouping was not yet copied into uses).
2077 */
2078 if (!parent || (parent->nodetype != LYS_USES)) {
Michal Vasko7178e692016-02-12 15:58:05 +01002079 if (unres_schema_add_node(module, unres, aug, UNRES_AUGMENT, NULL, LOGLINE(yin)) == -1) {
Michal Vasko4adc10f2015-08-11 15:26:17 +02002080 goto error;
2081 }
Michal Vasko49291b32015-08-06 09:49:41 +02002082 }
Radek Krejci106efc02015-06-10 14:36:27 +02002083
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002084 return EXIT_SUCCESS;
Radek Krejci106efc02015-06-10 14:36:27 +02002085
2086error:
2087
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002088 return EXIT_FAILURE;
Radek Krejci106efc02015-06-10 14:36:27 +02002089}
2090
Michal Vasko0d343d12015-08-24 14:57:36 +02002091/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002092static int
Michal Vasko0d204592015-10-07 09:50:04 +02002093fill_yin_refine(struct lys_module *module, struct lyxml_elem *yin, struct lys_refine *rfn)
Radek Krejci3bde87f2015-06-05 16:51:58 +02002094{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002095 struct lyxml_elem *sub, *next;
2096 const char *value;
2097 char *endptr;
2098 int f_mand = 0, f_min = 0, f_max = 0;
2099 int c_must = 0;
2100 int r;
2101 unsigned long int val;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002102
Radek Krejci76512572015-08-04 09:47:08 +02002103 if (read_yin_common(module, NULL, (struct lys_node *)rfn, yin, OPT_CONFIG)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002104 goto error;
2105 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002106
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002107 GETVAL(value, yin, "target-node");
Michal Vaskofba15262015-10-21 12:10:28 +02002108 rfn->target_name = transform_schema2json(module, value, LOGLINE(yin));
Michal Vaskoa8b25952015-10-20 15:30:25 +02002109 if (!rfn->target_name) {
2110 goto error;
2111 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002112
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002113 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002114 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
2115 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002116 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02002117 continue;
2118 }
2119
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002120 /* limited applicability */
2121 if (!strcmp(sub->name, "default")) {
2122 /* leaf or choice */
2123 if (rfn->mod.dflt) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002124 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002125 goto error;
2126 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002127
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002128 /* check possibility of statements combination */
2129 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002130 rfn->target_type &= (LYS_LEAF | LYS_CHOICE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002131 if (!rfn->target_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002132 LOGVAL(LYE_SPEC, LOGLINE(sub), LY_VLOG_NONE, NULL, "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002133 goto error;
2134 }
2135 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002136 rfn->target_type = LYS_LEAF | LYS_CHOICE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002137 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002138
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002139 GETVAL(value, sub, "value");
2140 rfn->mod.dflt = lydict_insert(module->ctx, value, strlen(value));
2141 } else if (!strcmp(sub->name, "mandatory")) {
2142 /* leaf, choice or anyxml */
2143 if (f_mand) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002144 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002145 goto error;
2146 }
2147 /* just checking the flags in leaf is not sufficient, we would allow
2148 * multiple mandatory statements with the "false" value
2149 */
2150 f_mand = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002151
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002152 /* check possibility of statements combination */
2153 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002154 rfn->target_type &= (LYS_LEAF | LYS_CHOICE | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002155 if (!rfn->target_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002156 LOGVAL(LYE_SPEC, LOGLINE(sub), LY_VLOG_NONE, NULL, "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002157 goto error;
2158 }
2159 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002160 rfn->target_type = LYS_LEAF | LYS_CHOICE | LYS_ANYXML;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002161 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002162
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002163 GETVAL(value, sub, "value");
2164 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002165 rfn->flags |= LYS_MAND_TRUE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002166 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002167 rfn->flags |= LYS_MAND_FALSE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002168 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002169 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002170 goto error;
2171 }
2172 } else if (!strcmp(sub->name, "min-elements")) {
2173 /* list or leaf-list */
2174 if (f_min) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002175 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002176 goto error;
2177 }
2178 f_min = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002179
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002180 /* check possibility of statements combination */
2181 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002182 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002183 if (!rfn->target_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002184 LOGVAL(LYE_SPEC, LOGLINE(sub), LY_VLOG_NONE, NULL, "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002185 goto error;
2186 }
2187 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002188 rfn->target_type = LYS_LIST | LYS_LEAFLIST;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002189 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002190
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002191 GETVAL(value, sub, "value");
2192 while (isspace(value[0])) {
2193 value++;
2194 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002195
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002196 /* convert it to uint32_t */
2197 errno = 0;
2198 endptr = NULL;
2199 val = strtoul(value, &endptr, 10);
2200 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002201 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002202 goto error;
2203 }
2204 rfn->mod.list.min = (uint32_t) val;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002205
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002206 /* magic - bit 3 in flags means min set */
2207 rfn->flags |= 0x04;
2208 } else if (!strcmp(sub->name, "max-elements")) {
2209 /* list or leaf-list */
2210 if (f_max) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002211 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002212 goto error;
2213 }
2214 f_max = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002215
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002216 /* check possibility of statements combination */
2217 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002218 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002219 if (!rfn->target_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002220 LOGVAL(LYE_SPEC, LOGLINE(sub), LY_VLOG_NONE, NULL, "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002221 goto error;
2222 }
2223 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002224 rfn->target_type = LYS_LIST | LYS_LEAFLIST;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002225 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002226
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002227 GETVAL(value, sub, "value");
2228 while (isspace(value[0])) {
2229 value++;
2230 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002231
Radek Krejci0d7b2472016-02-12 11:11:03 +01002232 if (!strcmp(value, "unbounded")) {
2233 rfn->mod.list.max = 0;
2234 } else {
2235 /* convert it to uint32_t */
2236 errno = 0;
2237 endptr = NULL;
2238 val = strtoul(value, &endptr, 10);
2239 if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002240 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01002241 goto error;
2242 }
2243 rfn->mod.list.max = (uint32_t) val;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002244 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002245
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002246 /* magic - bit 4 in flags means min set */
2247 rfn->flags |= 0x08;
2248 } else if (!strcmp(sub->name, "presence")) {
2249 /* container */
2250 if (rfn->mod.presence) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002251 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002252 goto error;
2253 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002254
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002255 /* check possibility of statements combination */
2256 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002257 rfn->target_type &= LYS_CONTAINER;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002258 if (!rfn->target_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002259 LOGVAL(LYE_SPEC, LOGLINE(sub), LY_VLOG_NONE, NULL, "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002260 goto error;
2261 }
2262 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002263 rfn->target_type = LYS_CONTAINER;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002264 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002265
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002266 GETVAL(value, sub, "value");
2267 rfn->mod.presence = lydict_insert(module->ctx, value, strlen(value));
2268 } else if (!strcmp(sub->name, "must")) {
2269 /* leaf-list, list, container or anyxml */
2270 /* check possibility of statements combination */
2271 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002272 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002273 if (!rfn->target_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002274 LOGVAL(LYE_SPEC, LOGLINE(sub), LY_VLOG_NONE, NULL, "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002275 goto error;
2276 }
2277 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002278 rfn->target_type = LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYXML;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002279 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002280
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002281 c_must++;
Radek Krejci41882de2015-07-02 16:34:58 +02002282 continue;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002283
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002284 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002285 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002286 goto error;
2287 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002288
Michal Vasko345da0a2015-12-02 10:35:55 +01002289 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002290 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002291
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002292 /* process nodes with cardinality of 0..n */
2293 if (c_must) {
2294 rfn->must = calloc(c_must, sizeof *rfn->must);
Michal Vasko253035f2015-12-17 16:58:13 +01002295 if (!rfn->must) {
2296 LOGMEM;
2297 goto error;
2298 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002299 }
Radek Krejci73adb602015-07-02 18:07:40 +02002300 LY_TREE_FOR(yin->child, sub) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002301 r = fill_yin_must(module, sub, &rfn->must[rfn->must_size]);
2302 rfn->must_size++;
Radek Krejci73adb602015-07-02 18:07:40 +02002303 if (r) {
2304 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002305 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002306 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002307
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002308 return EXIT_SUCCESS;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002309
2310error:
2311
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002312 return EXIT_FAILURE;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002313}
2314
Michal Vasko0d343d12015-08-24 14:57:36 +02002315/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002316static int
Radek Krejcib8048692015-08-05 13:36:34 +02002317fill_yin_import(struct lys_module *module, struct lyxml_elem *yin, struct lys_import *imp)
Radek Krejciefaeba32015-05-27 14:30:57 +02002318{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002319 struct lyxml_elem *child;
2320 const char *value;
Michal Vasko1b882eb2015-10-22 11:43:14 +02002321 int count;
Radek Krejciefaeba32015-05-27 14:30:57 +02002322
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002323 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002324 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
2325 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +02002326 continue;
2327 }
2328
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002329 if (!strcmp(child->name, "prefix")) {
2330 GETVAL(value, child, "value");
Radek Krejcic6556022016-01-27 15:16:45 +01002331 if (lyp_check_identifier(value, LY_IDENT_PREFIX, LOGLINE(child), module, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002332 goto error;
2333 }
2334 imp->prefix = lydict_insert(module->ctx, value, strlen(value));
2335 } else if (!strcmp(child->name, "revision-date")) {
2336 if (imp->rev[0]) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002337 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, "revision-date", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002338 goto error;
2339 }
2340 GETVAL(value, child, "date");
Radek Krejcic6556022016-01-27 15:16:45 +01002341 if (lyp_check_date(value, LOGLINE(child))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002342 goto error;
2343 }
2344 memcpy(imp->rev, value, LY_REV_SIZE - 1);
2345 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002346 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002347 goto error;
2348 }
2349 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002350
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002351 /* check mandatory information */
2352 if (!imp->prefix) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002353 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "prefix", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002354 goto error;
2355 }
Radek Krejcice7fb782015-05-29 16:52:34 +02002356
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002357 GETVAL(value, yin, "module");
Michal Vasko1b882eb2015-10-22 11:43:14 +02002358
2359 /* check for circular import, store it if passed */
2360 if (!module->ctx->models.parsing) {
2361 count = 0;
2362 } else {
2363 for (count = 0; module->ctx->models.parsing[count]; ++count) {
Radek Krejci749190d2016-02-18 16:26:25 +01002364 if (ly_strequal(value, module->ctx->models.parsing[count], 1)) {
Michal Vasko1b882eb2015-10-22 11:43:14 +02002365 LOGERR(LY_EVALID, "Circular import dependency on the module \"%s\".", value);
2366 goto error;
2367 }
2368 }
2369 }
2370 ++count;
2371 module->ctx->models.parsing =
Michal Vasko253035f2015-12-17 16:58:13 +01002372 ly_realloc(module->ctx->models.parsing, (count + 1) * sizeof *module->ctx->models.parsing);
2373 if (!module->ctx->models.parsing) {
2374 LOGMEM;
2375 goto error;
2376 }
Michal Vasko1b882eb2015-10-22 11:43:14 +02002377 module->ctx->models.parsing[count - 1] = value;
2378 module->ctx->models.parsing[count] = NULL;
2379
2380 /* try to load the module */
Michal Vasko1e62a092015-12-01 12:27:20 +01002381 imp->module = (struct lys_module *)ly_ctx_get_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002382 if (!imp->module) {
Michal Vasko99b0aad2015-12-01 12:28:51 +01002383 /* whether to use a user callback is decided in the function */
2384 imp->module = (struct lys_module *)ly_ctx_load_module(module->ctx, value, imp->rev[0] ? imp->rev : NULL);
Michal Vasko1b882eb2015-10-22 11:43:14 +02002385 }
2386
2387 /* remove the new module name now that its parsing is finished (even if failed) */
Radek Krejci749190d2016-02-18 16:26:25 +01002388 if (module->ctx->models.parsing[count] || !ly_strequal(module->ctx->models.parsing[count - 1], value, 1)) {
Michal Vasko1b882eb2015-10-22 11:43:14 +02002389 LOGINT;
2390 }
2391 --count;
2392 if (count) {
2393 module->ctx->models.parsing[count] = NULL;
2394 } else {
2395 free(module->ctx->models.parsing);
2396 module->ctx->models.parsing = NULL;
2397 }
2398
2399 /* check the result */
2400 if (!imp->module) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002401 LOGVAL(LYE_INARG, LOGLINE(yin), LY_VLOG_NONE, NULL, value, yin->name);
Michal Vasko1b882eb2015-10-22 11:43:14 +02002402 LOGERR(LY_EVALID, "Importing \"%s\" module into \"%s\" failed.", value, module->name);
2403 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002404 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002405
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002406 return EXIT_SUCCESS;
Radek Krejcice7fb782015-05-29 16:52:34 +02002407
2408error:
2409
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002410 return EXIT_FAILURE;
Radek Krejciefaeba32015-05-27 14:30:57 +02002411}
2412
Michal Vasko0d343d12015-08-24 14:57:36 +02002413/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002414static int
Michal Vasko5ff78822016-02-12 09:33:31 +01002415fill_yin_include(struct lys_module *module, struct lys_submodule *submodule, struct lyxml_elem *yin,
2416 struct lys_include *inc, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02002417{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002418 struct lyxml_elem *child;
2419 const char *value;
Michal Vasko99b0aad2015-12-01 12:28:51 +01002420 char *module_data;
2421 void (*module_data_free)(char *module_data) = NULL;
2422 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko5a721fd2016-02-16 12:16:48 +01002423 int count, i;
Radek Krejciefaeba32015-05-27 14:30:57 +02002424
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002425 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002426 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
2427 /* garbage */
2428 continue;
2429 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002430 if (!strcmp(child->name, "revision-date")) {
2431 if (inc->rev[0]) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002432 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, "revision-date", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002433 goto error;
2434 }
2435 GETVAL(value, child, "date");
Radek Krejcic6556022016-01-27 15:16:45 +01002436 if (lyp_check_date(value, LOGLINE(child))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002437 goto error;
2438 }
2439 memcpy(inc->rev, value, LY_REV_SIZE - 1);
2440 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002441 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002442 goto error;
2443 }
2444 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002445
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002446 GETVAL(value, yin, "module");
Michal Vasko1b882eb2015-10-22 11:43:14 +02002447
Michal Vasko9c4c99d2016-02-11 15:47:08 +01002448 /* check that the submodule was not included yet (previous submodule could have included it) */
2449 for (i = 0; i < module->inc_size; ++i) {
Radek Krejci749190d2016-02-18 16:26:25 +01002450 if (module->inc[i].submodule && (ly_strequal(module->inc[i].submodule->name, value, 1))) {
Michal Vasko5ff78822016-02-12 09:33:31 +01002451 /* copy the duplicate into the result */
Michal Vasko9c4c99d2016-02-11 15:47:08 +01002452 memcpy(inc, &module->inc[i], sizeof *inc);
Michal Vasko9c4c99d2016-02-11 15:47:08 +01002453
Michal Vasko5ff78822016-02-12 09:33:31 +01002454 if (submodule) {
2455 /* we don't care if it was external or not */
2456 inc->external = 0;
2457 } else if (inc->external) {
2458 /* remove the duplicate */
2459 --module->inc_size;
2460 memmove(&module->inc[i], &module->inc[i + 1], (module->inc_size - i) * sizeof *inc);
2461 module->inc = ly_realloc(module->inc, module->inc_size * sizeof *module->inc);
2462
2463 /* it is no longer external */
2464 inc->external = 0;
2465 }
2466 /* if !submodule && !inc->external, we just create a duplicate so it is detected and ended with error */
Michal Vasko9c4c99d2016-02-11 15:47:08 +01002467
2468 return EXIT_SUCCESS;
2469 }
2470 }
2471
Michal Vasko1b882eb2015-10-22 11:43:14 +02002472 /* check for circular include, store it if passed */
2473 if (!module->ctx->models.parsing) {
2474 count = 0;
2475 } else {
2476 for (count = 0; module->ctx->models.parsing[count]; ++count) {
Radek Krejci749190d2016-02-18 16:26:25 +01002477 if (ly_strequal(value, module->ctx->models.parsing[count], 1)) {
Michal Vasko1b882eb2015-10-22 11:43:14 +02002478 LOGERR(LY_EVALID, "Circular include dependency on the submodule \"%s\".", value);
2479 goto error;
2480 }
2481 }
2482 }
2483 ++count;
2484 module->ctx->models.parsing =
Michal Vasko253035f2015-12-17 16:58:13 +01002485 ly_realloc(module->ctx->models.parsing, (count + 1) * sizeof *module->ctx->models.parsing);
2486 if (!module->ctx->models.parsing) {
2487 LOGMEM;
2488 goto error;
2489 }
Michal Vasko1b882eb2015-10-22 11:43:14 +02002490 module->ctx->models.parsing[count - 1] = value;
2491 module->ctx->models.parsing[count] = NULL;
2492
2493 /* try to load the submodule */
Michal Vasko1e62a092015-12-01 12:27:20 +01002494 inc->submodule = (struct lys_submodule *)ly_ctx_get_submodule(module, value, inc->rev[0] ? inc->rev : NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002495 if (!inc->submodule) {
Michal Vasko99b0aad2015-12-01 12:28:51 +01002496 if (module->ctx->module_clb) {
2497 module_data = module->ctx->module_clb(value, inc->rev[0] ? inc->rev : NULL, module->ctx->module_clb_data,
2498 &format, &module_data_free);
2499 if (module_data) {
Michal Vasko5a721fd2016-02-16 12:16:48 +01002500 inc->submodule = lys_submodule_parse(module, module_data, format, unres);
Michal Vasko99b0aad2015-12-01 12:28:51 +01002501 if (module_data_free) {
2502 module_data_free(module_data);
2503 } else {
2504 free(module_data);
2505 }
2506 } else {
2507 LOGERR(LY_EVALID, "User module retrieval callback failed!");
2508 }
2509 } else {
Michal Vasko5a721fd2016-02-16 12:16:48 +01002510 inc->submodule = (struct lys_submodule *)lyp_search_file(module->ctx, module, value,
2511 inc->rev[0] ? inc->rev : NULL, unres);
Michal Vasko99b0aad2015-12-01 12:28:51 +01002512 }
Michal Vasko1b882eb2015-10-22 11:43:14 +02002513 }
2514
2515 /* remove the new submodule name now that its parsing is finished (even if failed) */
Radek Krejci749190d2016-02-18 16:26:25 +01002516 if (module->ctx->models.parsing[count] || !ly_strequal(module->ctx->models.parsing[count - 1], value, 1)) {
Michal Vasko1b882eb2015-10-22 11:43:14 +02002517 LOGINT;
2518 }
2519 --count;
2520 if (count) {
2521 module->ctx->models.parsing[count] = NULL;
2522 } else {
2523 free(module->ctx->models.parsing);
2524 module->ctx->models.parsing = NULL;
2525 }
2526
2527 /* check the result */
Michal Vasko5a721fd2016-02-16 12:16:48 +01002528 if (!inc->submodule) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002529 LOGVAL(LYE_INARG, LOGLINE(yin), LY_VLOG_NONE, NULL, value, yin->name);
Michal Vasko1b882eb2015-10-22 11:43:14 +02002530 LOGERR(LY_EVALID, "Including \"%s\" module into \"%s\" failed.", value, module->name);
2531 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002532 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002533
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002534 return EXIT_SUCCESS;
Radek Krejcice7fb782015-05-29 16:52:34 +02002535
2536error:
2537
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002538 return EXIT_FAILURE;
Radek Krejciefaeba32015-05-27 14:30:57 +02002539}
2540
Michal Vasko0d343d12015-08-24 14:57:36 +02002541/* logs directly
2542 *
Radek Krejcida04f4a2015-05-21 12:54:09 +02002543 * Covers:
Radek Krejci25d782a2015-05-22 15:03:23 +02002544 * description, reference, status, optionaly config
Radek Krejcib388c152015-06-04 17:03:03 +02002545 *
Radek Krejcida04f4a2015-05-21 12:54:09 +02002546 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002547static int
Radek Krejcib8048692015-08-05 13:36:34 +02002548read_yin_common(struct lys_module *module, struct lys_node *parent,
Radek Krejci1d82ef62015-08-07 14:44:40 +02002549 struct lys_node *node, struct lyxml_elem *xmlnode, int opt)
Radek Krejcida04f4a2015-05-21 12:54:09 +02002550{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002551 const char *value;
2552 struct lyxml_elem *sub, *next;
2553 struct ly_ctx *const ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002554
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002555 if (opt & OPT_MODULE) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002556 node->module = module;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002557 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002558
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002559 if (opt & OPT_IDENT) {
2560 GETVAL(value, xmlnode, "name");
Radek Krejcic6556022016-01-27 15:16:45 +01002561 if (lyp_check_identifier(value, LY_IDENT_NAME, LOGLINE(xmlnode), NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002562 goto error;
2563 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02002564 node->name = lydict_insert(ctx, value, strlen(value));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002565 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002566
Radek Krejci6764bb32015-07-03 15:16:04 +02002567 /* inherit NACM flags */
Radek Krejci6a113852015-07-03 16:04:20 +02002568 if ((opt & OPT_NACMEXT) && parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002569 node->nacm = parent->nacm;
Radek Krejci6764bb32015-07-03 15:16:04 +02002570 }
2571
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002572 /* process local parameters */
2573 LY_TREE_FOR_SAFE(xmlnode->child, next, sub) {
Radek Krejci6764bb32015-07-03 15:16:04 +02002574 if (!sub->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002575 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002576 lyxml_free(ctx, sub);
Radek Krejci6764bb32015-07-03 15:16:04 +02002577 continue;
2578 }
2579 if (strcmp(sub->ns->value, LY_NSYIN)) {
2580 /* NACM extensions */
Radek Krejci6a113852015-07-03 16:04:20 +02002581 if ((opt & OPT_NACMEXT) && !strcmp(sub->ns->value, LY_NSNACM)) {
Radek Krejci6764bb32015-07-03 15:16:04 +02002582 if (!strcmp(sub->name, "default-deny-write")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002583 node->nacm |= LYS_NACM_DENYW;
Radek Krejci6764bb32015-07-03 15:16:04 +02002584 } else if (!strcmp(sub->name, "default-deny-all")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002585 node->nacm |= LYS_NACM_DENYA;
Radek Krejci6764bb32015-07-03 15:16:04 +02002586 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002587 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6764bb32015-07-03 15:16:04 +02002588 goto error;
2589 }
2590 }
2591
2592 /* else garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002593 lyxml_free(ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02002594 continue;
2595 }
2596
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002597 if (!strcmp(sub->name, "description")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002598 if (node->dsc) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002599 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002600 goto error;
2601 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02002602 node->dsc = read_yin_subnode(ctx, sub, "text");
2603 if (!node->dsc) {
Radek Krejci73adb602015-07-02 18:07:40 +02002604 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002605 }
2606 } else if (!strcmp(sub->name, "reference")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002607 if (node->ref) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002608 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002609 goto error;
2610 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02002611 node->ref = read_yin_subnode(ctx, sub, "text");
2612 if (!node->ref) {
Radek Krejci73adb602015-07-02 18:07:40 +02002613 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002614 }
2615 } else if (!strcmp(sub->name, "status")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002616 if (node->flags & LYS_STATUS_MASK) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002617 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002618 goto error;
2619 }
2620 GETVAL(value, sub, "value");
2621 if (!strcmp(value, "current")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002622 node->flags |= LYS_STATUS_CURR;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002623 } else if (!strcmp(value, "deprecated")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002624 node->flags |= LYS_STATUS_DEPRC;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002625 } else if (!strcmp(value, "obsolete")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002626 node->flags |= LYS_STATUS_OBSLT;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002627 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002628 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci73adb602015-07-02 18:07:40 +02002629 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002630 }
2631 } else if ((opt & OPT_CONFIG) && !strcmp(sub->name, "config")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002632 if (node->flags & LYS_CONFIG_MASK) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002633 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002634 goto error;
2635 }
2636 GETVAL(value, sub, "value");
2637 if (!strcmp(value, "false")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002638 node->flags |= LYS_CONFIG_R;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002639 } else if (!strcmp(value, "true")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002640 node->flags |= LYS_CONFIG_W;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002641 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002642 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci73adb602015-07-02 18:07:40 +02002643 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002644 }
2645 } else {
Michal Vasko345da0a2015-12-02 10:35:55 +01002646 /* skip the lyxml_free */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002647 continue;
2648 }
Michal Vasko345da0a2015-12-02 10:35:55 +01002649 lyxml_free(ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002650 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002651
Radek Krejci1d82ef62015-08-07 14:44:40 +02002652 if ((opt & OPT_INHERIT) && !(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002653 /* get config flag from parent */
2654 if (parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002655 node->flags |= parent->flags & LYS_CONFIG_MASK;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002656 } else {
2657 /* default config is true */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002658 node->flags |= LYS_CONFIG_W;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002659 }
2660 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002661
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002662 return EXIT_SUCCESS;
Radek Krejcieac35532015-05-31 19:09:15 +02002663
2664error:
2665
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002666 return EXIT_FAILURE;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002667}
2668
Michal Vasko0d343d12015-08-24 14:57:36 +02002669/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02002670static struct lys_when *
Radek Krejcib8048692015-08-05 13:36:34 +02002671read_yin_when(struct lys_module *module, struct lyxml_elem *yin)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002672{
Radek Krejci76512572015-08-04 09:47:08 +02002673 struct lys_when *retval = NULL;
Radek Krejci73adb602015-07-02 18:07:40 +02002674 struct lyxml_elem *child;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002675 const char *value;
2676
2677 retval = calloc(1, sizeof *retval);
Michal Vasko253035f2015-12-17 16:58:13 +01002678 if (!retval) {
2679 LOGMEM;
2680 return NULL;
2681 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002682
2683 GETVAL(value, yin, "condition");
Michal Vaskofba15262015-10-21 12:10:28 +02002684 retval->cond = transform_schema2json(module, value, LOGLINE(yin));
Michal Vaskof9893382015-10-09 14:03:04 +02002685 if (!retval->cond) {
2686 goto error;
2687 }
Michal Vasko77dc5652016-02-15 12:32:42 +01002688 if (lyxp_syntax_check(retval->cond, LOGLINE(yin))) {
2689 goto error;
2690 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002691
Radek Krejci73adb602015-07-02 18:07:40 +02002692 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002693 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
2694 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +02002695 continue;
2696 }
2697
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002698 if (!strcmp(child->name, "description")) {
2699 if (retval->dsc) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002700 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002701 goto error;
2702 }
2703 retval->dsc = read_yin_subnode(module->ctx, child, "text");
2704 if (!retval->dsc) {
2705 goto error;
2706 }
2707 } else if (!strcmp(child->name, "reference")) {
2708 if (retval->ref) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002709 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002710 goto error;
2711 }
2712 retval->ref = read_yin_subnode(module->ctx, child, "text");
2713 if (!retval->ref) {
2714 goto error;
2715 }
2716 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002717 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002718 goto error;
2719 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002720 }
2721
2722 return retval;
2723
2724error:
2725
Michal Vasko0308dd62015-10-07 09:14:40 +02002726 lys_when_free(module->ctx, retval);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002727 return NULL;
2728}
2729
Michal Vasko0d343d12015-08-24 14:57:36 +02002730/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02002731static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02002732read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
2733 struct unres_schema *unres)
Radek Krejcib4cf2022015-06-03 14:40:05 +02002734{
Michal Vasko29fc0182015-08-24 15:02:39 +02002735 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002736 struct lys_node_case *cs;
2737 struct lys_node *retval, *node = NULL;
Michal Vasko2d851a92015-10-20 16:16:36 +02002738 int c_ftrs = 0, ret;
Radek Krejcib4cf2022015-06-03 14:40:05 +02002739
Radek Krejcie867c852015-08-27 09:52:34 +02002740 /* init */
2741 memset(&root, 0, sizeof root);
2742
Radek Krejci1d82ef62015-08-07 14:44:40 +02002743 cs = calloc(1, sizeof *cs);
Michal Vasko253035f2015-12-17 16:58:13 +01002744 if (!cs) {
2745 LOGMEM;
2746 return NULL;
2747 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02002748 cs->nodetype = LYS_CASE;
2749 cs->prev = (struct lys_node *)cs;
2750 retval = (struct lys_node *)cs;
Radek Krejcib4cf2022015-06-03 14:40:05 +02002751
Radek Krejci6a113852015-07-03 16:04:20 +02002752 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_INHERIT | OPT_NACMEXT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002753 goto error;
2754 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02002755
Radek Krejcia9544502015-08-14 08:24:29 +02002756 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
2757
Michal Vasko3a0043f2015-08-12 12:11:30 +02002758 /* insert the node into the schema tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +01002759 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02002760 goto error;
2761 }
2762
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002763 /* process choice's specific children */
2764 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002765 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
2766 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002767 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02002768 continue;
2769 }
2770
Michal Vasko29fc0182015-08-24 15:02:39 +02002771 if (!strcmp(sub->name, "container") ||
2772 !strcmp(sub->name, "leaf-list") ||
2773 !strcmp(sub->name, "leaf") ||
2774 !strcmp(sub->name, "list") ||
2775 !strcmp(sub->name, "uses") ||
2776 !strcmp(sub->name, "choice") ||
2777 !strcmp(sub->name, "anyxml")) {
2778
Michal Vaskof3930de2015-10-22 12:03:59 +02002779 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vasko29fc0182015-08-24 15:02:39 +02002780 lyxml_add_child(module->ctx, &root, sub);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002781 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko29fc0182015-08-24 15:02:39 +02002782 c_ftrs++;
Michal Vasko345da0a2015-12-02 10:35:55 +01002783 /* skip lyxml_free() at the end of the loop, sub is processed later */
Michal Vasko29fc0182015-08-24 15:02:39 +02002784 continue;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002785 } else if (!strcmp(sub->name, "when")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002786 if (cs->when) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002787 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002788 goto error;
2789 }
2790
Radek Krejci1d82ef62015-08-07 14:44:40 +02002791 cs->when = read_yin_when(module, sub);
2792 if (!cs->when) {
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002793 goto error;
2794 }
Michal Vasko29fc0182015-08-24 15:02:39 +02002795
Michal Vasko345da0a2015-12-02 10:35:55 +01002796 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002797 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002798 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002799 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002800 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002801 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02002802
Radek Krejci3cf9e222015-06-18 11:37:50 +02002803 if (c_ftrs) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002804 cs->features = calloc(c_ftrs, sizeof *cs->features);
Michal Vasko253035f2015-12-17 16:58:13 +01002805 if (!cs->features) {
2806 LOGMEM;
2807 goto error;
2808 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002809 }
Radek Krejci73adb602015-07-02 18:07:40 +02002810 LY_TREE_FOR(yin->child, sub) {
Michal Vasko1d337e12016-02-15 12:32:04 +01002811 ret = fill_yin_iffeature(retval, sub, &cs->features[cs->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002812 cs->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01002813 if (ret) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002814 goto error;
2815 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002816 }
Radek Krejcib388c152015-06-04 17:03:03 +02002817
Michal Vasko29fc0182015-08-24 15:02:39 +02002818 /* last part - process data nodes */
2819 LY_TREE_FOR_SAFE(root.child, next, sub) {
2820 if (!strcmp(sub->name, "container")) {
2821 node = read_yin_container(module, retval, sub, resolve, unres);
2822 } else if (!strcmp(sub->name, "leaf-list")) {
2823 node = read_yin_leaflist(module, retval, sub, resolve, unres);
2824 } else if (!strcmp(sub->name, "leaf")) {
2825 node = read_yin_leaf(module, retval, sub, resolve, unres);
2826 } else if (!strcmp(sub->name, "list")) {
2827 node = read_yin_list(module, retval, sub, resolve, unres);
2828 } else if (!strcmp(sub->name, "choice")) {
2829 node = read_yin_choice(module, retval, sub, resolve, unres);
2830 } else if (!strcmp(sub->name, "uses")) {
2831 node = read_yin_uses(module, retval, sub, resolve, unres);
2832 } else if (!strcmp(sub->name, "anyxml")) {
2833 node = read_yin_anyxml(module, retval, sub, resolve, unres);
2834 }
2835 if (!node) {
2836 goto error;
2837 }
2838
Michal Vasko345da0a2015-12-02 10:35:55 +01002839 lyxml_free(module->ctx, sub);
Michal Vasko29fc0182015-08-24 15:02:39 +02002840 }
2841
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002842 return retval;
Radek Krejcib4cf2022015-06-03 14:40:05 +02002843
2844error:
2845
Michal Vasko29fc0182015-08-24 15:02:39 +02002846 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01002847 lyxml_free(module->ctx, root.child);
Michal Vasko29fc0182015-08-24 15:02:39 +02002848 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002849 lys_node_free(retval, NULL, 0);
Radek Krejcib4cf2022015-06-03 14:40:05 +02002850
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002851 return NULL;
Radek Krejcib4cf2022015-06-03 14:40:05 +02002852}
2853
Michal Vasko0d343d12015-08-24 14:57:36 +02002854/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02002855static struct lys_node *
Radek Krejci10c760e2015-08-14 14:45:43 +02002856read_yin_choice(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve, struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02002857{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002858 struct lyxml_elem *sub, *next;
2859 struct ly_ctx *const ctx = module->ctx;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002860 struct lys_node *retval, *node = NULL;
Radek Krejcib8048692015-08-05 13:36:34 +02002861 struct lys_node_choice *choice;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002862 const char *value, *dflt_str = NULL;
Michal Vasko2d851a92015-10-20 16:16:36 +02002863 int f_mand = 0, c_ftrs = 0, ret;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002864
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002865 choice = calloc(1, sizeof *choice);
Michal Vasko253035f2015-12-17 16:58:13 +01002866 if (!choice) {
2867 LOGMEM;
2868 return NULL;
2869 }
Radek Krejci76512572015-08-04 09:47:08 +02002870 choice->nodetype = LYS_CHOICE;
2871 choice->prev = (struct lys_node *)choice;
2872 retval = (struct lys_node *)choice;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002873
Michal Vaskoe0c59842015-09-24 13:52:20 +02002874 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
2875 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002876 goto error;
2877 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002878
Radek Krejcia9544502015-08-14 08:24:29 +02002879 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
2880
Michal Vasko3a0043f2015-08-12 12:11:30 +02002881 /* insert the node into the schema tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +01002882 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02002883 goto error;
2884 }
2885
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002886 /* process choice's specific children */
2887 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002888 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
2889 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002890 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02002891 continue;
2892 }
2893
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002894 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002895 if (!(node = read_yin_container(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002896 goto error;
2897 }
2898 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002899 if (!(node = read_yin_leaflist(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002900 goto error;
2901 }
2902 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002903 if (!(node = read_yin_leaf(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002904 goto error;
2905 }
2906 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002907 if (!(node = read_yin_list(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002908 goto error;
2909 }
2910 } else if (!strcmp(sub->name, "case")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002911 if (!(node = read_yin_case(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002912 goto error;
2913 }
2914 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002915 if (!(node = read_yin_anyxml(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002916 goto error;
2917 }
2918 } else if (!strcmp(sub->name, "default")) {
2919 if (dflt_str) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002920 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002921 goto error;
2922 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002923 GETVAL(dflt_str, sub, "value");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002924 } else if (!strcmp(sub->name, "mandatory")) {
2925 if (f_mand) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002926 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002927 goto error;
2928 }
2929 /* just checking the flags in leaf is not sufficient, we would allow
2930 * multiple mandatory statements with the "false" value
2931 */
2932 f_mand = 1;
Radek Krejcib4cf2022015-06-03 14:40:05 +02002933
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002934 GETVAL(value, sub, "value");
2935 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002936 choice->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02002937 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002938 choice->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02002939 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002940 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002941 goto error;
2942 } /* else false is the default value, so we can ignore it */
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002943 } else if (!strcmp(sub->name, "when")) {
2944 if (choice->when) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002945 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002946 goto error;
2947 }
2948
2949 choice->when = read_yin_when(module, sub);
2950 if (!choice->when) {
2951 goto error;
2952 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002953 } else if (!strcmp(sub->name, "if-feature")) {
2954 c_ftrs++;
2955
Michal Vasko345da0a2015-12-02 10:35:55 +01002956 /* skip lyxml_free() at the end of the loop, the sub node is processed later */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002957 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002958 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002959 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002960 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002961 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02002962
Radek Krejci1d82ef62015-08-07 14:44:40 +02002963 node = NULL;
Michal Vasko345da0a2015-12-02 10:35:55 +01002964 lyxml_free(ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002965 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02002966
Radek Krejci3cf9e222015-06-18 11:37:50 +02002967 if (c_ftrs) {
2968 choice->features = calloc(c_ftrs, sizeof *choice->features);
Michal Vasko253035f2015-12-17 16:58:13 +01002969 if (!choice->features) {
2970 LOGMEM;
2971 goto error;
2972 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002973 }
2974
Radek Krejci73adb602015-07-02 18:07:40 +02002975 LY_TREE_FOR(yin->child, sub) {
Michal Vasko1d337e12016-02-15 12:32:04 +01002976 ret = fill_yin_iffeature(retval, sub, &choice->features[choice->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002977 choice->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01002978 if (ret) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002979 goto error;
2980 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002981 }
2982
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002983 /* check - default is prohibited in combination with mandatory */
Radek Krejci1574a8d2015-08-03 14:16:52 +02002984 if (dflt_str && (choice->flags & LYS_MAND_TRUE)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01002985 LOGVAL(LYE_SPEC, LOGLINE(yin), LY_VLOG_NONE, NULL,
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002986 "The \"default\" statement MUST NOT be present on choices where \"mandatory\" is true.");
2987 goto error;
2988 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02002989
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002990 /* link default with the case */
2991 if (dflt_str) {
Michal Vasko0bd29d12015-08-19 11:45:49 +02002992 if (unres_schema_add_str(module, unres, choice, UNRES_CHOICE_DFLT, dflt_str, LOGLINE(yin)) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002993 goto error;
2994 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002995 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002996
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002997 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002998
2999error:
3000
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003001 lys_node_free(retval, NULL, 0);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003002
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003003 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003004}
3005
Michal Vasko0d343d12015-08-24 14:57:36 +02003006/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003007static struct lys_node *
Radek Krejcib8048692015-08-05 13:36:34 +02003008read_yin_anyxml(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
Michal Vaskof02e3742015-08-05 16:27:02 +02003009 struct unres_schema *unres)
Radek Krejci863c2852015-06-03 15:47:11 +02003010{
Radek Krejci76512572015-08-04 09:47:08 +02003011 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003012 struct lys_node_leaf *anyxml;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003013 struct lyxml_elem *sub, *next;
3014 const char *value;
3015 int r;
3016 int f_mand = 0;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003017 int c_must = 0, c_ftrs = 0;
Radek Krejci863c2852015-06-03 15:47:11 +02003018
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003019 anyxml = calloc(1, sizeof *anyxml);
Michal Vasko253035f2015-12-17 16:58:13 +01003020 if (!anyxml) {
3021 LOGMEM;
3022 return NULL;
3023 }
Radek Krejci76512572015-08-04 09:47:08 +02003024 anyxml->nodetype = LYS_ANYXML;
3025 anyxml->prev = (struct lys_node *)anyxml;
3026 retval = (struct lys_node *)anyxml;
Radek Krejci863c2852015-06-03 15:47:11 +02003027
Michal Vaskoe0c59842015-09-24 13:52:20 +02003028 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
3029 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003030 goto error;
3031 }
Radek Krejci863c2852015-06-03 15:47:11 +02003032
Radek Krejcia9544502015-08-14 08:24:29 +02003033 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02003034
Michal Vasko4f0dad02016-02-15 14:08:23 +01003035 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003036 goto error;
3037 }
3038
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003039 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003040 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3041 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003042 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003043 continue;
3044 }
3045
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003046 if (!strcmp(sub->name, "mandatory")) {
3047 if (f_mand) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003048 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003049 goto error;
3050 }
3051 /* just checking the flags in leaf is not sufficient, we would allow
3052 * multiple mandatory statements with the "false" value
3053 */
3054 f_mand = 1;
Radek Krejci863c2852015-06-03 15:47:11 +02003055
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003056 GETVAL(value, sub, "value");
3057 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003058 anyxml->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003059 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003060 anyxml->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003061 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003062 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003063 goto error;
3064 }
3065 /* else false is the default value, so we can ignore it */
Michal Vasko345da0a2015-12-02 10:35:55 +01003066 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003067 } else if (!strcmp(sub->name, "when")) {
3068 if (anyxml->when) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003069 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003070 goto error;
3071 }
3072
3073 anyxml->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003074 if (!anyxml->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003075 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003076 goto error;
3077 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003078 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003079 } else if (!strcmp(sub->name, "must")) {
3080 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003081 } else if (!strcmp(sub->name, "if-feature")) {
3082 c_ftrs++;
Radek Krejci863c2852015-06-03 15:47:11 +02003083
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003084 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003085 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003086 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003087 }
3088 }
Radek Krejci863c2852015-06-03 15:47:11 +02003089
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003090 /* middle part - process nodes with cardinality of 0..n */
3091 if (c_must) {
3092 anyxml->must = calloc(c_must, sizeof *anyxml->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003093 if (!anyxml->must) {
3094 LOGMEM;
3095 goto error;
3096 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003097 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003098 if (c_ftrs) {
3099 anyxml->features = calloc(c_ftrs, sizeof *anyxml->features);
Michal Vasko253035f2015-12-17 16:58:13 +01003100 if (!anyxml->features) {
3101 LOGMEM;
3102 goto error;
3103 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003104 }
Radek Krejci863c2852015-06-03 15:47:11 +02003105
Radek Krejci73adb602015-07-02 18:07:40 +02003106 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003107 if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003108 r = fill_yin_must(module, sub, &anyxml->must[anyxml->must_size]);
3109 anyxml->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003110 if (r) {
3111 goto error;
3112 }
Radek Krejci0b24d752015-07-02 15:02:27 +02003113 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko1d337e12016-02-15 12:32:04 +01003114 r = fill_yin_iffeature(retval, sub, &anyxml->features[anyxml->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003115 anyxml->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01003116 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003117 goto error;
3118 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003119 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003120 }
Radek Krejci863c2852015-06-03 15:47:11 +02003121
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003122 return retval;
Radek Krejci863c2852015-06-03 15:47:11 +02003123
3124error:
3125
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003126 lys_node_free(retval, NULL, 0);
Radek Krejci863c2852015-06-03 15:47:11 +02003127
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003128 return NULL;
Radek Krejci863c2852015-06-03 15:47:11 +02003129}
3130
Michal Vasko0d343d12015-08-24 14:57:36 +02003131/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003132static struct lys_node *
Radek Krejcib8048692015-08-05 13:36:34 +02003133read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
Michal Vaskof02e3742015-08-05 16:27:02 +02003134 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003135{
Radek Krejci76512572015-08-04 09:47:08 +02003136 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003137 struct lys_node_leaf *leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003138 struct lyxml_elem *sub, *next;
3139 const char *value;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003140 int r, has_type = 0, dflt_line;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003141 int c_must = 0, c_ftrs = 0, f_mand = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003142
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003143 leaf = calloc(1, sizeof *leaf);
Michal Vasko253035f2015-12-17 16:58:13 +01003144 if (!leaf) {
3145 LOGMEM;
3146 return NULL;
3147 }
Radek Krejci76512572015-08-04 09:47:08 +02003148 leaf->nodetype = LYS_LEAF;
3149 leaf->prev = (struct lys_node *)leaf;
3150 retval = (struct lys_node *)leaf;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003151
Michal Vaskoe0c59842015-09-24 13:52:20 +02003152 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
3153 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003154 goto error;
3155 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003156
Radek Krejcia9544502015-08-14 08:24:29 +02003157 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02003158
Michal Vasko4f0dad02016-02-15 14:08:23 +01003159 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003160 goto error;
3161 }
3162
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003163 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003164 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3165 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003166 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003167 continue;
3168 }
3169
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003170 if (!strcmp(sub->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +01003171 if (has_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003172 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003173 goto error;
3174 }
Michal Vasko88c29542015-11-27 14:57:53 +01003175 /* HACK for unres */
3176 leaf->type.der = (struct lys_tpdf *)sub;
Radek Krejcicf509982015-12-15 09:22:44 +01003177 leaf->type.parent = (struct lys_tpdf *)leaf;
Michal Vasko88c29542015-11-27 14:57:53 +01003178 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DER, parent, LOGLINE(sub))) {
3179 leaf->type.der = NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003180 goto error;
3181 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003182 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003183 } else if (!strcmp(sub->name, "default")) {
3184 if (leaf->dflt) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003185 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003186 goto error;
3187 }
3188 GETVAL(value, sub, "value");
3189 leaf->dflt = lydict_insert(module->ctx, value, strlen(value));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003190 dflt_line = LOGLINE(sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003191 } else if (!strcmp(sub->name, "units")) {
3192 if (leaf->units) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003193 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003194 goto error;
3195 }
3196 GETVAL(value, sub, "name");
3197 leaf->units = lydict_insert(module->ctx, value, strlen(value));
3198 } else if (!strcmp(sub->name, "mandatory")) {
3199 if (f_mand) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003200 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003201 goto error;
3202 }
3203 /* just checking the flags in leaf is not sufficient, we would allow
3204 * multiple mandatory statements with the "false" value
3205 */
3206 f_mand = 1;
Radek Krejci4c31f122015-06-02 14:51:22 +02003207
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003208 GETVAL(value, sub, "value");
3209 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003210 leaf->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003211 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003212 leaf->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003213 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003214 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003215 goto error;
3216 } /* else false is the default value, so we can ignore it */
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003217 } else if (!strcmp(sub->name, "when")) {
3218 if (leaf->when) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003219 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003220 goto error;
3221 }
3222
3223 leaf->when = read_yin_when(module, sub);
3224 if (!leaf->when) {
3225 goto error;
3226 }
3227
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003228 } else if (!strcmp(sub->name, "must")) {
Radek Krejci41882de2015-07-02 16:34:58 +02003229 c_must++;
3230 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003231 } else if (!strcmp(sub->name, "if-feature")) {
3232 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003233 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02003234
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003235 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003236 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003237 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003238 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003239
Michal Vasko88c29542015-11-27 14:57:53 +01003240 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003241 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003242
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003243 /* check mandatory parameters */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003244 if (!has_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003245 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003246 goto error;
3247 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003248 if (leaf->dflt) {
Michal Vasko0bd29d12015-08-19 11:45:49 +02003249 if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt, dflt_line) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003250 goto error;
3251 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003252 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003253
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003254 /* middle part - process nodes with cardinality of 0..n */
3255 if (c_must) {
3256 leaf->must = calloc(c_must, sizeof *leaf->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003257 if (!leaf->must) {
3258 LOGMEM;
3259 goto error;
3260 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003261 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003262 if (c_ftrs) {
3263 leaf->features = calloc(c_ftrs, sizeof *leaf->features);
Michal Vasko253035f2015-12-17 16:58:13 +01003264 if (!leaf->features) {
3265 LOGMEM;
3266 goto error;
3267 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003268 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003269
Radek Krejci73adb602015-07-02 18:07:40 +02003270 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003271 if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003272 r = fill_yin_must(module, sub, &leaf->must[leaf->must_size]);
3273 leaf->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003274 if (r) {
3275 goto error;
3276 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003277 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko1d337e12016-02-15 12:32:04 +01003278 r = fill_yin_iffeature(retval, sub, &leaf->features[leaf->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003279 leaf->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01003280 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003281 goto error;
3282 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003283 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003284 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003285
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003286 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003287
3288error:
3289
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003290 lys_node_free(retval, NULL, 0);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003291
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003292 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003293}
3294
Michal Vasko0d343d12015-08-24 14:57:36 +02003295/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003296static struct lys_node *
Radek Krejcib8048692015-08-05 13:36:34 +02003297read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
Michal Vaskof02e3742015-08-05 16:27:02 +02003298 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003299{
Radek Krejci76512572015-08-04 09:47:08 +02003300 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003301 struct lys_node_leaflist *llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003302 struct lyxml_elem *sub, *next;
3303 const char *value;
3304 char *endptr;
3305 unsigned long val;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003306 int r, has_type = 0;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003307 int c_must = 0, c_ftrs = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003308 int f_ordr = 0, f_min = 0, f_max = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003309
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003310 llist = calloc(1, sizeof *llist);
Michal Vasko253035f2015-12-17 16:58:13 +01003311 if (!llist) {
3312 LOGMEM;
3313 return NULL;
3314 }
Radek Krejci76512572015-08-04 09:47:08 +02003315 llist->nodetype = LYS_LEAFLIST;
3316 llist->prev = (struct lys_node *)llist;
3317 retval = (struct lys_node *)llist;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003318
Michal Vaskoe0c59842015-09-24 13:52:20 +02003319 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
3320 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003321 goto error;
3322 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003323
Radek Krejcia9544502015-08-14 08:24:29 +02003324 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02003325
Michal Vasko4f0dad02016-02-15 14:08:23 +01003326 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003327 goto error;
3328 }
3329
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003330 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003331 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3332 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003333 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003334 continue;
3335 }
3336
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003337 if (!strcmp(sub->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +01003338 if (has_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003339 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003340 goto error;
3341 }
Michal Vasko88c29542015-11-27 14:57:53 +01003342 /* HACK for unres */
3343 llist->type.der = (struct lys_tpdf *)sub;
Radek Krejcicf509982015-12-15 09:22:44 +01003344 llist->type.parent = (struct lys_tpdf *)llist;
Michal Vasko88c29542015-11-27 14:57:53 +01003345 if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DER, parent, LOGLINE(sub))) {
3346 llist->type.der = NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003347 goto error;
3348 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003349 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003350 } else if (!strcmp(sub->name, "units")) {
3351 if (llist->units) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003352 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003353 goto error;
3354 }
3355 GETVAL(value, sub, "name");
3356 llist->units = lydict_insert(module->ctx, value, strlen(value));
3357 } else if (!strcmp(sub->name, "ordered-by")) {
3358 if (f_ordr) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003359 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003360 goto error;
3361 }
3362 /* just checking the flags in llist is not sufficient, we would
3363 * allow multiple ordered-by statements with the "system" value
3364 */
3365 f_ordr = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003366
Radek Krejci1574a8d2015-08-03 14:16:52 +02003367 if (llist->flags & LYS_CONFIG_R) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003368 /* RFC 6020, 7.7.5 - ignore ordering when the list represents
3369 * state data
3370 */
Michal Vasko345da0a2015-12-02 10:35:55 +01003371 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003372 continue;
3373 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003374
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003375 GETVAL(value, sub, "value");
3376 if (!strcmp(value, "user")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003377 llist->flags |= LYS_USERORDERED;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003378 } else if (strcmp(value, "system")) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003379 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003380 goto error;
Radek Krejci41882de2015-07-02 16:34:58 +02003381 } /* else system is the default value, so we can ignore it */
3382
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003383 } else if (!strcmp(sub->name, "must")) {
3384 c_must++;
Radek Krejci41882de2015-07-02 16:34:58 +02003385 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003386 } else if (!strcmp(sub->name, "if-feature")) {
3387 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003388 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02003389
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003390 } else if (!strcmp(sub->name, "min-elements")) {
3391 if (f_min) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003392 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003393 goto error;
3394 }
3395 f_min = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003396
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003397 GETVAL(value, sub, "value");
3398 while (isspace(value[0])) {
3399 value++;
3400 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003401
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003402 /* convert it to uint32_t */
3403 errno = 0;
3404 endptr = NULL;
3405 val = strtoul(value, &endptr, 10);
3406 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003407 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003408 goto error;
3409 }
3410 llist->min = (uint32_t) val;
3411 } else if (!strcmp(sub->name, "max-elements")) {
3412 if (f_max) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003413 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003414 goto error;
3415 }
3416 f_max = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003417
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003418 GETVAL(value, sub, "value");
3419 while (isspace(value[0])) {
3420 value++;
3421 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003422
Radek Krejci0d7b2472016-02-12 11:11:03 +01003423 if (!strcmp(value, "unbounded")) {
3424 llist->max = 0;
3425 } else {
3426 /* convert it to uint32_t */
3427 errno = 0;
3428 endptr = NULL;
3429 val = strtoul(value, &endptr, 10);
3430 if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003431 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01003432 goto error;
3433 }
3434 llist->max = (uint32_t) val;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003435 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003436 } else if (!strcmp(sub->name, "when")) {
3437 if (llist->when) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003438 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003439 goto error;
3440 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003441
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003442 llist->when = read_yin_when(module, sub);
3443 if (!llist->when) {
3444 goto error;
3445 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003446 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003447 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003448 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003449 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003450
Michal Vasko88c29542015-11-27 14:57:53 +01003451 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003452 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003453
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003454 /* check constraints */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003455 if (!has_type) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003456 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003457 goto error;
3458 }
3459 if (llist->max && llist->min > llist->max) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003460 LOGVAL(LYE_SPEC, LOGLINE(yin), LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003461 goto error;
3462 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003463
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003464 /* middle part - process nodes with cardinality of 0..n */
3465 if (c_must) {
3466 llist->must = calloc(c_must, sizeof *llist->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003467 if (!llist->must) {
3468 LOGMEM;
3469 goto error;
3470 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003471 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003472 if (c_ftrs) {
3473 llist->features = calloc(c_ftrs, sizeof *llist->features);
Michal Vasko253035f2015-12-17 16:58:13 +01003474 if (!llist->features) {
3475 LOGMEM;
3476 goto error;
3477 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003478 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003479
Radek Krejci73adb602015-07-02 18:07:40 +02003480 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003481 if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003482 r = fill_yin_must(module, sub, &llist->must[llist->must_size]);
3483 llist->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003484 if (r) {
3485 goto error;
3486 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003487 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko1d337e12016-02-15 12:32:04 +01003488 r = fill_yin_iffeature(retval, sub, &llist->features[llist->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003489 llist->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01003490 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003491 goto error;
3492 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003493 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003494 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003495
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003496 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003497
3498error:
3499
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003500 lys_node_free(retval, NULL, 0);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003501
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003502 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003503}
3504
Michal Vasko0d343d12015-08-24 14:57:36 +02003505/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003506static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02003507read_yin_list(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
3508 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003509{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003510 struct lys_node *retval, *node;
Radek Krejcib8048692015-08-05 13:36:34 +02003511 struct lys_node_list *list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003512 struct lyxml_elem *sub, *next, root, uniq;
Michal Vaskoe38af6e2015-08-03 14:39:27 +02003513 int r, key_line;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003514 int c_tpdf = 0, c_must = 0, c_uniq = 0, c_ftrs = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003515 int f_ordr = 0, f_max = 0, f_min = 0;
Radek Krejcieb00f512015-07-01 16:44:58 +02003516 const char *key_str = NULL, *value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003517 char *auxs;
3518 unsigned long val;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003519
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003520 /* init */
3521 memset(&root, 0, sizeof root);
3522 memset(&uniq, 0, sizeof uniq);
Radek Krejcie0674f82015-06-15 13:58:51 +02003523
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003524 list = calloc(1, sizeof *list);
Michal Vasko253035f2015-12-17 16:58:13 +01003525 if (!list) {
3526 LOGMEM;
3527 return NULL;
3528 }
Radek Krejci76512572015-08-04 09:47:08 +02003529 list->nodetype = LYS_LIST;
3530 list->prev = (struct lys_node *)list;
3531 retval = (struct lys_node *)list;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003532
Michal Vaskoe0c59842015-09-24 13:52:20 +02003533 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
3534 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003535 goto error;
3536 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003537
Radek Krejcia9544502015-08-14 08:24:29 +02003538 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
3539
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003540 /* process list's specific children */
3541 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003542 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3543 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003544 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003545 continue;
3546 }
3547
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003548 /* data statements */
3549 if (!strcmp(sub->name, "container") ||
3550 !strcmp(sub->name, "leaf-list") ||
3551 !strcmp(sub->name, "leaf") ||
3552 !strcmp(sub->name, "list") ||
3553 !strcmp(sub->name, "choice") ||
3554 !strcmp(sub->name, "uses") ||
3555 !strcmp(sub->name, "grouping") ||
3556 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02003557 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02003558 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003559
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003560 /* array counters */
3561 } else if (!strcmp(sub->name, "key")) {
3562 /* check cardinality 0..1 */
3563 if (list->keys_size) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003564 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, list->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003565 goto error;
3566 }
Radek Krejcid7f0d012015-05-25 15:04:52 +02003567
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003568 /* count the number of keys */
3569 GETVAL(value, sub, "value");
3570 key_str = value;
Michal Vaskoe38af6e2015-08-03 14:39:27 +02003571 key_line = LOGLINE(sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003572 while ((value = strpbrk(value, " \t\n"))) {
3573 list->keys_size++;
3574 while (isspace(*value)) {
3575 value++;
3576 }
3577 }
3578 list->keys_size++;
3579 list->keys = calloc(list->keys_size, sizeof *list->keys);
Michal Vasko253035f2015-12-17 16:58:13 +01003580 if (!list->keys) {
3581 LOGMEM;
3582 goto error;
3583 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003584 } else if (!strcmp(sub->name, "unique")) {
3585 c_uniq++;
Michal Vaskof3930de2015-10-22 12:03:59 +02003586 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02003587 lyxml_add_child(module->ctx, &uniq, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003588 } else if (!strcmp(sub->name, "typedef")) {
3589 c_tpdf++;
3590 } else if (!strcmp(sub->name, "must")) {
3591 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003592 } else if (!strcmp(sub->name, "if-feature")) {
3593 c_ftrs++;
Radek Krejci345ad742015-06-03 11:04:18 +02003594
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003595 /* optional stetments */
3596 } else if (!strcmp(sub->name, "ordered-by")) {
3597 if (f_ordr) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003598 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003599 goto error;
3600 }
3601 /* just checking the flags in llist is not sufficient, we would
3602 * allow multiple ordered-by statements with the "system" value
3603 */
3604 f_ordr = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02003605
Radek Krejci1574a8d2015-08-03 14:16:52 +02003606 if (list->flags & LYS_CONFIG_R) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003607 /* RFC 6020, 7.7.5 - ignore ordering when the list represents
3608 * state data
3609 */
Michal Vasko345da0a2015-12-02 10:35:55 +01003610 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003611 continue;
3612 }
Radek Krejci345ad742015-06-03 11:04:18 +02003613
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003614 GETVAL(value, sub, "value");
3615 if (!strcmp(value, "user")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003616 list->flags |= LYS_USERORDERED;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003617 } else if (strcmp(value, "system")) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003618 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003619 goto error;
3620 }
3621 /* else system is the default value, so we can ignore it */
Michal Vasko345da0a2015-12-02 10:35:55 +01003622 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003623 } else if (!strcmp(sub->name, "min-elements")) {
3624 if (f_min) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003625 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003626 goto error;
3627 }
3628 f_min = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02003629
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003630 GETVAL(value, sub, "value");
3631 while (isspace(value[0])) {
3632 value++;
3633 }
Radek Krejci345ad742015-06-03 11:04:18 +02003634
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003635 /* convert it to uint32_t */
3636 errno = 0;
3637 auxs = NULL;
3638 val = strtoul(value, &auxs, 10);
3639 if (*auxs || value[0] == '-' || errno || val > UINT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003640 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003641 goto error;
3642 }
3643 list->min = (uint32_t) val;
Michal Vasko345da0a2015-12-02 10:35:55 +01003644 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003645 } else if (!strcmp(sub->name, "max-elements")) {
3646 if (f_max) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003647 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003648 goto error;
3649 }
3650 f_max = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02003651
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003652 GETVAL(value, sub, "value");
3653 while (isspace(value[0])) {
3654 value++;
3655 }
Radek Krejci345ad742015-06-03 11:04:18 +02003656
Radek Krejci0d7b2472016-02-12 11:11:03 +01003657 if (!strcmp(value, "unbounded")) {
3658 list->max = 0;;
3659 } else {
3660 /* convert it to uint32_t */
3661 errno = 0;
3662 auxs = NULL;
3663 val = strtoul(value, &auxs, 10);
3664 if (*auxs || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003665 LOGVAL(LYE_INARG, LOGLINE(sub), LY_VLOG_NONE, NULL, value, sub->name);
Radek Krejci0d7b2472016-02-12 11:11:03 +01003666 goto error;
3667 }
3668 list->max = (uint32_t) val;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003669 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003670 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003671 } else if (!strcmp(sub->name, "when")) {
3672 if (list->when) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003673 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003674 goto error;
3675 }
3676
3677 list->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003678 if (!list->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003679 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003680 goto error;
3681 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003682 lyxml_free(module->ctx, sub);
Radek Krejci3cf9e222015-06-18 11:37:50 +02003683 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003684 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02003685 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003686 }
3687 }
Radek Krejci345ad742015-06-03 11:04:18 +02003688
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003689 /* check - if list is configuration, key statement is mandatory */
Radek Krejci1574a8d2015-08-03 14:16:52 +02003690 if ((list->flags & LYS_CONFIG_W) && !key_str) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003691 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "key", "list");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003692 goto error;
3693 }
3694 if (list->max && list->min > list->max) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003695 LOGVAL(LYE_SPEC, LOGLINE(yin), LY_VLOG_NONE, NULL, "\"min-elements\" is bigger than \"max-elements\".");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003696 goto error;
3697 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003698
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003699 /* middle part - process nodes with cardinality of 0..n except the data nodes */
3700 if (c_tpdf) {
3701 list->tpdf = calloc(c_tpdf, sizeof *list->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01003702 if (!list->tpdf) {
3703 LOGMEM;
3704 goto error;
3705 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003706 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003707 if (c_must) {
3708 list->must = calloc(c_must, sizeof *list->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003709 if (!list->must) {
3710 LOGMEM;
3711 goto error;
3712 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003713 }
3714 if (c_ftrs) {
3715 list->features = calloc(c_ftrs, sizeof *list->features);
Michal Vasko253035f2015-12-17 16:58:13 +01003716 if (!list->features) {
3717 LOGMEM;
3718 goto error;
3719 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003720 }
Radek Krejci73adb602015-07-02 18:07:40 +02003721 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003722 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003723 r = fill_yin_typedef(module, retval, sub, &list->tpdf[list->tpdf_size], unres);
3724 list->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003725 if (r) {
3726 goto error;
3727 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003728 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko1d337e12016-02-15 12:32:04 +01003729 r = fill_yin_iffeature(retval, sub, &list->features[list->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003730 list->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01003731 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003732 goto error;
3733 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003734 } else if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003735 r = fill_yin_must(module, sub, &list->must[list->must_size]);
3736 list->must_size++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003737 if (r) {
3738 goto error;
3739 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003740 }
3741 }
Radek Krejci25d782a2015-05-22 15:03:23 +02003742
Michal Vasko4f0dad02016-02-15 14:08:23 +01003743 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003744 goto error;
3745 }
3746
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003747 /* last part - process data nodes */
3748 LY_TREE_FOR_SAFE(root.child, next, sub) {
3749 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003750 node = read_yin_container(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003751 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003752 node = read_yin_leaflist(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003753 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003754 node = read_yin_leaf(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003755 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003756 node = read_yin_list(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003757 } else if (!strcmp(sub->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003758 node = read_yin_choice(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003759 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003760 node = read_yin_uses(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003761 } else if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003762 node = read_yin_grouping(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003763 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003764 node = read_yin_anyxml(module, retval, sub, resolve, unres);
Michal Vaskoc07187d2015-08-13 15:20:57 +02003765 } else {
3766 LOGINT;
3767 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003768 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003769 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003770 goto error;
3771 }
Radek Krejci73adb602015-07-02 18:07:40 +02003772
Michal Vasko345da0a2015-12-02 10:35:55 +01003773 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003774 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003775
Radek Krejci461efb92016-02-12 15:52:18 +01003776 if (key_str) {
3777 if (unres_schema_add_str(module, unres, list, UNRES_LIST_KEYS, key_str, key_line) == -1) {
3778 goto error;
3779 }
3780 } /* 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 +02003781
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003782 /* process unique statements */
3783 if (c_uniq) {
3784 list->unique = calloc(c_uniq, sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01003785 if (!list->unique) {
3786 LOGMEM;
3787 goto error;
3788 }
Radek Krejci1e9b9992015-06-04 17:57:04 +02003789
Radek Krejci461efb92016-02-12 15:52:18 +01003790 LY_TREE_FOR_SAFE(uniq.child, next, sub) {
3791 r = fill_yin_unique(module, retval, sub, &list->unique[list->unique_size], unres);
3792 list->unique_size++;
3793 if (r) {
3794 goto error;
3795 }
3796
3797 lyxml_free(module->ctx, sub);
3798 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003799 }
Radek Krejci1e9b9992015-06-04 17:57:04 +02003800
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003801 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003802
3803error:
3804
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003805 lys_node_free(retval, NULL, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003806 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003807 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003808 }
3809 while (uniq.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003810 lyxml_free(module->ctx, uniq.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003811 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003812
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003813 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003814}
3815
Michal Vasko0d343d12015-08-24 14:57:36 +02003816/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003817static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02003818read_yin_container(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
3819 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003820{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003821 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02003822 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02003823 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003824 struct lys_node_container *cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003825 const char *value;
3826 int r;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003827 int c_tpdf = 0, c_must = 0, c_ftrs = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003828
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003829 /* init */
3830 memset(&root, 0, sizeof root);
Radek Krejcie0674f82015-06-15 13:58:51 +02003831
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003832 cont = calloc(1, sizeof *cont);
Michal Vasko253035f2015-12-17 16:58:13 +01003833 if (!cont) {
3834 LOGMEM;
3835 return NULL;
3836 }
Radek Krejci76512572015-08-04 09:47:08 +02003837 cont->nodetype = LYS_CONTAINER;
3838 cont->prev = (struct lys_node *)cont;
3839 retval = (struct lys_node *)cont;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003840
Michal Vaskoe0c59842015-09-24 13:52:20 +02003841 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
3842 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003843 goto error;
3844 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003845
Radek Krejcia9544502015-08-14 08:24:29 +02003846 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
3847
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003848 /* process container's specific children */
3849 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci6764bb32015-07-03 15:16:04 +02003850 if (!sub->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003851 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003852 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003853 continue;
3854 }
3855
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003856 if (!strcmp(sub->name, "presence")) {
3857 if (cont->presence) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003858 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003859 goto error;
3860 }
3861 GETVAL(value, sub, "value");
3862 cont->presence = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci800af702015-06-02 13:46:01 +02003863
Michal Vasko345da0a2015-12-02 10:35:55 +01003864 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003865 } else if (!strcmp(sub->name, "when")) {
3866 if (cont->when) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003867 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003868 goto error;
3869 }
3870
3871 cont->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003872 if (!cont->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003873 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003874 goto error;
3875 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003876 lyxml_free(module->ctx, sub);
Radek Krejci4c31f122015-06-02 14:51:22 +02003877
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003878 /* data statements */
3879 } else if (!strcmp(sub->name, "container") ||
3880 !strcmp(sub->name, "leaf-list") ||
3881 !strcmp(sub->name, "leaf") ||
3882 !strcmp(sub->name, "list") ||
3883 !strcmp(sub->name, "choice") ||
3884 !strcmp(sub->name, "uses") ||
3885 !strcmp(sub->name, "grouping") ||
3886 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02003887 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02003888 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003889
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003890 /* array counters */
3891 } else if (!strcmp(sub->name, "typedef")) {
3892 c_tpdf++;
3893 } else if (!strcmp(sub->name, "must")) {
3894 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003895 } else if (!strcmp(sub->name, "if-feature")) {
3896 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003897 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01003898 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003899 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003900 }
3901 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003902
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003903 /* middle part - process nodes with cardinality of 0..n except the data nodes */
3904 if (c_tpdf) {
3905 cont->tpdf = calloc(c_tpdf, sizeof *cont->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01003906 if (!cont->tpdf) {
3907 LOGMEM;
3908 goto error;
3909 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003910 }
3911 if (c_must) {
3912 cont->must = calloc(c_must, sizeof *cont->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003913 if (!cont->must) {
3914 LOGMEM;
3915 goto error;
3916 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003917 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003918 if (c_ftrs) {
3919 cont->features = calloc(c_ftrs, sizeof *cont->features);
Michal Vasko253035f2015-12-17 16:58:13 +01003920 if (!cont->features) {
3921 LOGMEM;
3922 goto error;
3923 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003924 }
Radek Krejci800af702015-06-02 13:46:01 +02003925
Radek Krejci73adb602015-07-02 18:07:40 +02003926 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003927 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003928 r = fill_yin_typedef(module, retval, sub, &cont->tpdf[cont->tpdf_size], unres);
3929 cont->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003930 if (r) {
3931 goto error;
3932 }
3933 } else if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003934 r = fill_yin_must(module, sub, &cont->must[cont->must_size]);
3935 cont->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003936 if (r) {
3937 goto error;
3938 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003939 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko1d337e12016-02-15 12:32:04 +01003940 r = fill_yin_iffeature(retval, sub, &cont->features[cont->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003941 cont->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01003942 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003943 goto error;
3944 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003945 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003946 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003947
Michal Vasko4f0dad02016-02-15 14:08:23 +01003948 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003949 goto error;
3950 }
3951
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003952 /* last part - process data nodes */
3953 LY_TREE_FOR_SAFE(root.child, next, sub) {
3954 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003955 node = read_yin_container(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003956 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003957 node = read_yin_leaflist(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003958 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003959 node = read_yin_leaf(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003960 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003961 node = read_yin_list(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003962 } else if (!strcmp(sub->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003963 node = read_yin_choice(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003964 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003965 node = read_yin_uses(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003966 } else if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003967 node = read_yin_grouping(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003968 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003969 node = read_yin_anyxml(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003970 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003971 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003972 goto error;
3973 }
Radek Krejci73adb602015-07-02 18:07:40 +02003974
Michal Vasko345da0a2015-12-02 10:35:55 +01003975 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003976 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003977
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003978 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003979
3980error:
3981
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003982 lys_node_free(retval, NULL, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003983 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003984 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003985 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003986
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003987 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003988}
3989
Michal Vasko0d343d12015-08-24 14:57:36 +02003990/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003991static struct lys_node *
Radek Krejci1d82ef62015-08-07 14:44:40 +02003992read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
Michal Vaskof02e3742015-08-05 16:27:02 +02003993 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003994{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003995 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02003996 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02003997 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003998 struct lys_node_grp *grp;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003999 int r;
4000 int c_tpdf = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004001
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004002 /* init */
4003 memset(&root, 0, sizeof root);
Radek Krejcie0674f82015-06-15 13:58:51 +02004004
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004005 grp = calloc(1, sizeof *grp);
Michal Vasko253035f2015-12-17 16:58:13 +01004006 if (!grp) {
4007 LOGMEM;
4008 return NULL;
4009 }
Radek Krejci76512572015-08-04 09:47:08 +02004010 grp->nodetype = LYS_GROUPING;
4011 grp->prev = (struct lys_node *)grp;
4012 retval = (struct lys_node *)grp;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004013
Michal Vasko71e1aa82015-08-12 12:17:51 +02004014 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004015 goto error;
4016 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004017
Radek Krejcia9544502015-08-14 08:24:29 +02004018 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4019
Radek Krejci1d82ef62015-08-07 14:44:40 +02004020 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004021 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4022 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004023 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004024 continue;
4025 }
4026
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004027 /* data statements */
4028 if (!strcmp(sub->name, "container") ||
4029 !strcmp(sub->name, "leaf-list") ||
4030 !strcmp(sub->name, "leaf") ||
4031 !strcmp(sub->name, "list") ||
4032 !strcmp(sub->name, "choice") ||
4033 !strcmp(sub->name, "uses") ||
4034 !strcmp(sub->name, "grouping") ||
4035 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004036 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004037 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02004038
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004039 /* array counters */
4040 } else if (!strcmp(sub->name, "typedef")) {
4041 c_tpdf++;
4042 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004043 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004044 goto error;
4045 }
4046 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004047
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004048 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4049 if (c_tpdf) {
4050 grp->tpdf = calloc(c_tpdf, sizeof *grp->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004051 if (!grp->tpdf) {
4052 LOGMEM;
4053 goto error;
4054 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004055 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004056 LY_TREE_FOR(yin->child, sub) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004057 r = fill_yin_typedef(module, retval, sub, &grp->tpdf[grp->tpdf_size], unres);
4058 grp->tpdf_size++;
Radek Krejci73adb602015-07-02 18:07:40 +02004059 if (r) {
4060 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004061 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004062 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004063
Michal Vasko4f0dad02016-02-15 14:08:23 +01004064 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004065 goto error;
4066 }
4067
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004068 /* last part - process data nodes */
4069 LY_TREE_FOR_SAFE(root.child, next, sub) {
4070 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004071 node = read_yin_container(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004072 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004073 node = read_yin_leaflist(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004074 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004075 node = read_yin_leaf(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004076 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004077 node = read_yin_list(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004078 } else if (!strcmp(sub->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004079 node = read_yin_choice(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004080 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004081 node = read_yin_uses(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004082 } else if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004083 node = read_yin_grouping(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004084 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004085 node = read_yin_anyxml(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004086 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004087 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004088 goto error;
4089 }
Radek Krejci73adb602015-07-02 18:07:40 +02004090
Michal Vasko345da0a2015-12-02 10:35:55 +01004091 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004092 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004093
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004094 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004095
4096error:
4097
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004098 lys_node_free(retval, NULL, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004099 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004100 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004101 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004102
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004103 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004104}
4105
Michal Vasko0d343d12015-08-24 14:57:36 +02004106/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004107static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02004108read_yin_input_output(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
4109 struct unres_schema *unres)
Michal Vasko38d01f72015-06-15 09:41:06 +02004110{
Radek Krejcie0674f82015-06-15 13:58:51 +02004111 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004112 struct lys_node *node = NULL;
Radek Krejci31fc30d2015-08-13 08:27:38 +02004113 struct lys_node *retval = NULL;
Radek Krejci4608ada2015-08-05 16:04:37 +02004114 struct lys_node_rpc_inout *inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02004115 int r;
4116 int c_tpdf = 0;
4117
Radek Krejcie0674f82015-06-15 13:58:51 +02004118 /* init */
4119 memset(&root, 0, sizeof root);
4120
Michal Vasko38d01f72015-06-15 09:41:06 +02004121 inout = calloc(1, sizeof *inout);
Michal Vasko253035f2015-12-17 16:58:13 +01004122 if (!inout) {
4123 LOGMEM;
4124 return NULL;
4125 }
Radek Krejci6acc8012015-08-13 09:07:04 +02004126 inout->prev = (struct lys_node *)inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02004127
4128 if (!strcmp(yin->name, "input")) {
Radek Krejci76512572015-08-04 09:47:08 +02004129 inout->nodetype = LYS_INPUT;
Michal Vasko38d01f72015-06-15 09:41:06 +02004130 } else if (!strcmp(yin->name, "output")) {
Radek Krejci76512572015-08-04 09:47:08 +02004131 inout->nodetype = LYS_OUTPUT;
Michal Vasko38d01f72015-06-15 09:41:06 +02004132 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02004133 LOGINT;
Radek Krejci6acc8012015-08-13 09:07:04 +02004134 free(inout);
Michal Vasko0c888fd2015-08-11 15:54:08 +02004135 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02004136 }
4137
Radek Krejci76512572015-08-04 09:47:08 +02004138 retval = (struct lys_node *)inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02004139
Radek Krejci6a113852015-07-03 16:04:20 +02004140 if (read_yin_common(module, parent, retval, yin, OPT_MODULE | OPT_NACMEXT)) {
Michal Vaskoebeac942015-06-15 12:11:50 +02004141 goto error;
4142 }
4143
Radek Krejcia9544502015-08-14 08:24:29 +02004144 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4145
Michal Vasko38d01f72015-06-15 09:41:06 +02004146 /* data statements */
4147 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004148 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4149 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004150 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004151 continue;
4152 }
4153
Michal Vasko38d01f72015-06-15 09:41:06 +02004154 if (!strcmp(sub->name, "container") ||
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004155 !strcmp(sub->name, "leaf-list") ||
4156 !strcmp(sub->name, "leaf") ||
4157 !strcmp(sub->name, "list") ||
4158 !strcmp(sub->name, "choice") ||
4159 !strcmp(sub->name, "uses") ||
4160 !strcmp(sub->name, "grouping") ||
4161 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004162 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004163 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004164
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004165 /* array counters */
Michal Vasko38d01f72015-06-15 09:41:06 +02004166 } else if (!strcmp(sub->name, "typedef")) {
4167 c_tpdf++;
Radek Krejcid2bfa792015-07-02 16:45:29 +02004168
Michal Vasko38d01f72015-06-15 09:41:06 +02004169 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004170 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02004171 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02004172 }
4173 }
4174
4175 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4176 if (c_tpdf) {
4177 inout->tpdf = calloc(c_tpdf, sizeof *inout->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004178 if (!inout->tpdf) {
4179 LOGMEM;
4180 goto error;
4181 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004182 }
4183
Radek Krejci73adb602015-07-02 18:07:40 +02004184 LY_TREE_FOR(yin->child, sub) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004185 r = fill_yin_typedef(module, retval, sub, &inout->tpdf[inout->tpdf_size], unres);
4186 inout->tpdf_size++;
Radek Krejci73adb602015-07-02 18:07:40 +02004187 if (r) {
4188 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02004189 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004190 }
4191
Michal Vasko4f0dad02016-02-15 14:08:23 +01004192 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004193 goto error;
4194 }
4195
Michal Vasko38d01f72015-06-15 09:41:06 +02004196 /* last part - process data nodes */
4197 LY_TREE_FOR_SAFE(root.child, next, sub) {
4198 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004199 node = read_yin_container(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004200 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004201 node = read_yin_leaflist(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004202 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004203 node = read_yin_leaf(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004204 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004205 node = read_yin_list(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004206 } else if (!strcmp(sub->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004207 node = read_yin_choice(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004208 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004209 node = read_yin_uses(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004210 } else if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004211 node = read_yin_grouping(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004212 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004213 node = read_yin_anyxml(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004214 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004215 if (!node) {
Michal Vasko38d01f72015-06-15 09:41:06 +02004216 goto error;
4217 }
Radek Krejci73adb602015-07-02 18:07:40 +02004218
Michal Vasko345da0a2015-12-02 10:35:55 +01004219 lyxml_free(module->ctx, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004220 }
4221
Michal Vasko38d01f72015-06-15 09:41:06 +02004222 return retval;
4223
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004224error:
Michal Vasko38d01f72015-06-15 09:41:06 +02004225
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004226 lys_node_free(retval, NULL, 0);
Michal Vasko3a9abf82015-06-17 10:18:26 +02004227 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004228 lyxml_free(module->ctx, root.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02004229 }
4230
4231 return NULL;
4232}
4233
Michal Vasko0d343d12015-08-24 14:57:36 +02004234/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004235static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02004236read_yin_notif(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
4237 struct unres_schema *unres)
Michal Vasko0ea41032015-06-16 08:53:55 +02004238{
Michal Vaskoc6551b32015-06-16 10:51:43 +02004239 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004240 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02004241 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004242 struct lys_node_notif *notif;
Michal Vasko0ea41032015-06-16 08:53:55 +02004243 int r;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004244 int c_tpdf = 0, c_ftrs = 0;
Michal Vasko0ea41032015-06-16 08:53:55 +02004245
Michal Vaskoc6551b32015-06-16 10:51:43 +02004246 memset(&root, 0, sizeof root);
4247
Michal Vasko0ea41032015-06-16 08:53:55 +02004248 notif = calloc(1, sizeof *notif);
Michal Vasko253035f2015-12-17 16:58:13 +01004249 if (!notif) {
4250 LOGMEM;
4251 return NULL;
4252 }
Radek Krejci76512572015-08-04 09:47:08 +02004253 notif->nodetype = LYS_NOTIF;
4254 notif->prev = (struct lys_node *)notif;
4255 retval = (struct lys_node *)notif;
Michal Vasko0ea41032015-06-16 08:53:55 +02004256
Radek Krejci6a113852015-07-03 16:04:20 +02004257 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_NACMEXT)) {
Michal Vasko0ea41032015-06-16 08:53:55 +02004258 goto error;
4259 }
4260
Radek Krejcia9544502015-08-14 08:24:29 +02004261 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4262
Michal Vasko0ea41032015-06-16 08:53:55 +02004263 /* process rpc's specific children */
4264 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004265 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4266 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004267 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004268 continue;
4269 }
4270
Michal Vasko0ea41032015-06-16 08:53:55 +02004271 /* data statements */
4272 if (!strcmp(sub->name, "container") ||
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004273 !strcmp(sub->name, "leaf-list") ||
4274 !strcmp(sub->name, "leaf") ||
4275 !strcmp(sub->name, "list") ||
4276 !strcmp(sub->name, "choice") ||
4277 !strcmp(sub->name, "uses") ||
4278 !strcmp(sub->name, "grouping") ||
4279 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004280 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004281 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko0ea41032015-06-16 08:53:55 +02004282
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004283 /* array counters */
Michal Vasko0ea41032015-06-16 08:53:55 +02004284 } else if (!strcmp(sub->name, "typedef")) {
4285 c_tpdf++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004286 } else if (!strcmp(sub->name, "if-feature")) {
4287 c_ftrs++;
Michal Vasko0ea41032015-06-16 08:53:55 +02004288 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004289 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Michal Vasko0ea41032015-06-16 08:53:55 +02004290 goto error;
Michal Vasko0ea41032015-06-16 08:53:55 +02004291 }
4292 }
4293
4294 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4295 if (c_tpdf) {
4296 notif->tpdf = calloc(c_tpdf, sizeof *notif->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004297 if (!notif->tpdf) {
4298 LOGMEM;
4299 goto error;
4300 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004301 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004302 if (c_ftrs) {
4303 notif->features = calloc(c_ftrs, sizeof *notif->features);
Michal Vasko253035f2015-12-17 16:58:13 +01004304 if (!notif->features) {
4305 LOGMEM;
4306 goto error;
4307 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004308 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004309
Radek Krejci73adb602015-07-02 18:07:40 +02004310 LY_TREE_FOR(yin->child, sub) {
Michal Vasko0ea41032015-06-16 08:53:55 +02004311 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004312 r = fill_yin_typedef(module, retval, sub, &notif->tpdf[notif->tpdf_size], unres);
4313 notif->tpdf_size++;
Michal Vasko0ea41032015-06-16 08:53:55 +02004314 if (r) {
4315 goto error;
4316 }
Radek Krejci0b24d752015-07-02 15:02:27 +02004317 } else if (!strcmp(sub->name, "if-features")) {
Michal Vasko1d337e12016-02-15 12:32:04 +01004318 r = fill_yin_iffeature(retval, sub, &notif->features[notif->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004319 notif->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01004320 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004321 goto error;
4322 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004323 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004324 }
4325
Michal Vasko4f0dad02016-02-15 14:08:23 +01004326 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004327 goto error;
4328 }
4329
Michal Vasko0ea41032015-06-16 08:53:55 +02004330 /* last part - process data nodes */
4331 LY_TREE_FOR_SAFE(root.child, next, sub) {
4332 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004333 node = read_yin_container(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004334 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004335 node = read_yin_leaflist(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004336 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004337 node = read_yin_leaf(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004338 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004339 node = read_yin_list(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004340 } else if (!strcmp(sub->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004341 node = read_yin_choice(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004342 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004343 node = read_yin_uses(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004344 } else if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004345 node = read_yin_grouping(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004346 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004347 node = read_yin_anyxml(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004348 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004349 if (!node) {
Michal Vasko0ea41032015-06-16 08:53:55 +02004350 goto error;
4351 }
Radek Krejci73adb602015-07-02 18:07:40 +02004352
Michal Vasko345da0a2015-12-02 10:35:55 +01004353 lyxml_free(module->ctx, sub);
Michal Vasko0ea41032015-06-16 08:53:55 +02004354 }
4355
Michal Vasko0ea41032015-06-16 08:53:55 +02004356 return retval;
4357
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004358error:
Michal Vasko0ea41032015-06-16 08:53:55 +02004359
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004360 lys_node_free(retval, NULL, 0);
Michal Vasko0ea41032015-06-16 08:53:55 +02004361 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004362 lyxml_free(module->ctx, root.child);
Michal Vasko0ea41032015-06-16 08:53:55 +02004363 }
4364
4365 return NULL;
4366}
4367
Michal Vasko0d343d12015-08-24 14:57:36 +02004368/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004369static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02004370read_yin_rpc(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
4371 struct unres_schema *unres)
Michal Vasko38d01f72015-06-15 09:41:06 +02004372{
Radek Krejcie0674f82015-06-15 13:58:51 +02004373 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004374 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02004375 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004376 struct lys_node_rpc *rpc;
Michal Vasko38d01f72015-06-15 09:41:06 +02004377 int r;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004378 int c_tpdf = 0, c_ftrs = 0;
Michal Vasko38d01f72015-06-15 09:41:06 +02004379
Radek Krejcie0674f82015-06-15 13:58:51 +02004380 /* init */
4381 memset(&root, 0, sizeof root);
4382
Michal Vasko38d01f72015-06-15 09:41:06 +02004383 rpc = calloc(1, sizeof *rpc);
Michal Vasko253035f2015-12-17 16:58:13 +01004384 if (!rpc) {
4385 LOGMEM;
4386 return NULL;
4387 }
Radek Krejci76512572015-08-04 09:47:08 +02004388 rpc->nodetype = LYS_RPC;
4389 rpc->prev = (struct lys_node *)rpc;
4390 retval = (struct lys_node *)rpc;
Michal Vasko38d01f72015-06-15 09:41:06 +02004391
Radek Krejci6a113852015-07-03 16:04:20 +02004392 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_NACMEXT)) {
Michal Vasko38d01f72015-06-15 09:41:06 +02004393 goto error;
4394 }
4395
Radek Krejcia9544502015-08-14 08:24:29 +02004396 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4397
Michal Vasko38d01f72015-06-15 09:41:06 +02004398 /* process rpc's specific children */
4399 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004400 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4401 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004402 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004403 continue;
4404 }
4405
Michal Vasko38d01f72015-06-15 09:41:06 +02004406 if (!strcmp(sub->name, "input")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004407 if (rpc->child
Radek Krejci76512572015-08-04 09:47:08 +02004408 && (rpc->child->nodetype == LYS_INPUT
4409 || (rpc->child->next && rpc->child->next->nodetype == LYS_INPUT))) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004410 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02004411 goto error;
4412 }
Michal Vaskof3930de2015-10-22 12:03:59 +02004413 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004414 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004415 } else if (!strcmp(sub->name, "output")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004416 if (rpc->child
Radek Krejci76512572015-08-04 09:47:08 +02004417 && (rpc->child->nodetype == LYS_INPUT
4418 || (rpc->child->next && rpc->child->next->nodetype == LYS_INPUT))) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004419 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02004420 goto error;
4421 }
Michal Vaskof3930de2015-10-22 12:03:59 +02004422 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004423 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004424
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004425 /* data statements */
Michal Vasko38d01f72015-06-15 09:41:06 +02004426 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004427 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004428 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004429
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004430 /* array counters */
Michal Vasko38d01f72015-06-15 09:41:06 +02004431 } else if (!strcmp(sub->name, "typedef")) {
4432 c_tpdf++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004433 } else if (!strcmp(sub->name, "if-feature")) {
4434 c_ftrs++;
Michal Vasko38d01f72015-06-15 09:41:06 +02004435 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004436 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02004437 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02004438 }
4439 }
4440
4441 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4442 if (c_tpdf) {
4443 rpc->tpdf = calloc(c_tpdf, sizeof *rpc->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004444 if (!rpc->tpdf) {
4445 LOGMEM;
4446 goto error;
4447 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004448 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004449 if (c_ftrs) {
4450 rpc->features = calloc(c_ftrs, sizeof *rpc->features);
Michal Vasko253035f2015-12-17 16:58:13 +01004451 if (!rpc->features) {
4452 LOGMEM;
4453 goto error;
4454 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004455 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004456
Radek Krejci73adb602015-07-02 18:07:40 +02004457 LY_TREE_FOR(yin->child, sub) {
Michal Vasko38d01f72015-06-15 09:41:06 +02004458 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004459 r = fill_yin_typedef(module, retval, sub, &rpc->tpdf[rpc->tpdf_size], unres);
4460 rpc->tpdf_size++;
Michal Vasko38d01f72015-06-15 09:41:06 +02004461 if (r) {
4462 goto error;
4463 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004464 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko1d337e12016-02-15 12:32:04 +01004465 r = fill_yin_iffeature(retval, sub, &rpc->features[rpc->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004466 rpc->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01004467 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004468 goto error;
4469 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004470 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004471 }
4472
Michal Vasko4f0dad02016-02-15 14:08:23 +01004473 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004474 goto error;
4475 }
4476
Michal Vasko38d01f72015-06-15 09:41:06 +02004477 /* last part - process data nodes */
4478 LY_TREE_FOR_SAFE(root.child, next, sub) {
4479 if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004480 node = read_yin_grouping(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004481 } else if (!strcmp(sub->name, "input")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004482 node = read_yin_input_output(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004483 } else if (!strcmp(sub->name, "output")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004484 node = read_yin_input_output(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004485 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004486 if (!node) {
Michal Vasko38d01f72015-06-15 09:41:06 +02004487 goto error;
4488 }
Radek Krejci73adb602015-07-02 18:07:40 +02004489
Michal Vasko345da0a2015-12-02 10:35:55 +01004490 lyxml_free(module->ctx, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004491 }
4492
Michal Vasko38d01f72015-06-15 09:41:06 +02004493 return retval;
4494
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004495error:
Michal Vasko38d01f72015-06-15 09:41:06 +02004496
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004497 lys_node_free(retval, NULL, 0);
Michal Vasko3a9abf82015-06-17 10:18:26 +02004498 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004499 lyxml_free(module->ctx, root.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02004500 }
4501
4502 return NULL;
4503}
4504
Michal Vasko0d343d12015-08-24 14:57:36 +02004505/* logs directly
4506 *
Radek Krejci74705112015-06-05 10:25:44 +02004507 * resolve - referenced grouping should be bounded to the namespace (resolved)
4508 * only when uses does not appear in grouping. In a case of grouping's uses,
4509 * we just get information but we do not apply augment or refine to it.
4510 */
Radek Krejci76512572015-08-04 09:47:08 +02004511static struct lys_node *
Radek Krejcia9544502015-08-14 08:24:29 +02004512read_yin_uses(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
Michal Vaskof02e3742015-08-05 16:27:02 +02004513 struct unres_schema *unres)
Radek Krejci74705112015-06-05 10:25:44 +02004514{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004515 struct lyxml_elem *sub, *next;
Radek Krejci76512572015-08-04 09:47:08 +02004516 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004517 struct lys_node_uses *uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004518 const char *value;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004519 int c_ref = 0, c_aug = 0, c_ftrs = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004520 int r;
Radek Krejci74705112015-06-05 10:25:44 +02004521
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004522 uses = calloc(1, sizeof *uses);
Michal Vasko253035f2015-12-17 16:58:13 +01004523 if (!uses) {
4524 LOGMEM;
4525 return NULL;
4526 }
Radek Krejci76512572015-08-04 09:47:08 +02004527 uses->nodetype = LYS_USES;
4528 uses->prev = (struct lys_node *)uses;
4529 retval = (struct lys_node *)uses;
Radek Krejci74705112015-06-05 10:25:44 +02004530
Radek Krejcia9544502015-08-14 08:24:29 +02004531 GETVAL(value, yin, "name");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004532 uses->name = lydict_insert(module->ctx, value, 0);
Radek Krejci106efc02015-06-10 14:36:27 +02004533
Michal Vaskoe0c59842015-09-24 13:52:20 +02004534 if (read_yin_common(module, parent, retval, yin, OPT_MODULE
4535 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004536 goto error;
4537 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02004538
Radek Krejcia9544502015-08-14 08:24:29 +02004539 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4540
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004541 /* get other properties of uses */
Radek Krejcia9544502015-08-14 08:24:29 +02004542 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004543 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4544 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004545 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004546 continue;
4547 }
4548
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004549 if (!strcmp(sub->name, "refine")) {
4550 c_ref++;
4551 } else if (!strcmp(sub->name, "augment")) {
4552 c_aug++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004553 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci56e89772015-06-19 10:00:54 +02004554 c_ftrs++;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004555 } else if (!strcmp(sub->name, "when")) {
4556 if (uses->when) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004557 LOGVAL(LYE_TOOMANY, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004558 goto error;
4559 }
4560
4561 uses->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004562 if (!uses->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004563 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004564 goto error;
4565 }
Michal Vasko345da0a2015-12-02 10:35:55 +01004566 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004567 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004568 LOGVAL(LYE_INSTMT, LOGLINE(sub), LY_VLOG_NONE, NULL, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004569 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004570 }
4571 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02004572
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004573 /* process properties with cardinality 0..n */
4574 if (c_ref) {
4575 uses->refine = calloc(c_ref, sizeof *uses->refine);
Michal Vasko253035f2015-12-17 16:58:13 +01004576 if (!uses->refine) {
4577 LOGMEM;
4578 goto error;
4579 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004580 }
4581 if (c_aug) {
4582 uses->augment = calloc(c_aug, sizeof *uses->augment);
Michal Vasko253035f2015-12-17 16:58:13 +01004583 if (!uses->augment) {
4584 LOGMEM;
4585 goto error;
4586 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004587 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004588 if (c_ftrs) {
4589 uses->features = calloc(c_ftrs, sizeof *uses->features);
Michal Vasko253035f2015-12-17 16:58:13 +01004590 if (!uses->features) {
4591 LOGMEM;
4592 goto error;
4593 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004594 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02004595
Michal Vasko4f0dad02016-02-15 14:08:23 +01004596 if (lys_node_addchild(parent, lys_module(module), retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004597 goto error;
4598 }
4599
Radek Krejcia9544502015-08-14 08:24:29 +02004600 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004601 if (!strcmp(sub->name, "refine")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004602 r = fill_yin_refine(module, sub, &uses->refine[uses->refine_size]);
4603 uses->refine_size++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004604 if (r) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02004605 goto error;
4606 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004607 } else if (!strcmp(sub->name, "augment")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004608 r = fill_yin_augment(module, retval, sub, &uses->augment[uses->augment_size], unres);
4609 uses->augment_size++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004610 if (r) {
4611 goto error;
4612 }
4613 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko1d337e12016-02-15 12:32:04 +01004614 r = fill_yin_iffeature(retval, sub, &uses->features[uses->features_size], unres);
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004615 uses->features_size++;
Michal Vasko1d337e12016-02-15 12:32:04 +01004616 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004617 goto error;
4618 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004619 }
4620 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02004621
Michal Vasko0bd29d12015-08-19 11:45:49 +02004622 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL, LOGLINE(yin)) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004623 goto error;
4624 }
Radek Krejci74705112015-06-05 10:25:44 +02004625
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004626 if (resolve) {
4627 /* inherit config flag */
4628 if (parent) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02004629 retval->flags |= parent->flags & LYS_CONFIG_MASK;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004630 } else {
4631 /* default config is true */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004632 retval->flags |= LYS_CONFIG_W;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004633 }
4634 }
Radek Krejcib388c152015-06-04 17:03:03 +02004635
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004636 return retval;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02004637
4638error:
4639
Michal Vaskod51d6ad2016-02-16 13:24:31 +01004640 lys_node_free(retval, NULL, 0);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02004641
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004642 return NULL;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02004643}
4644
Michal Vasko0d343d12015-08-24 14:57:36 +02004645/* logs directly
4646 *
4647 * common code for yin_read_module() and yin_read_submodule()
4648 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004649static int
Radek Krejcic071c542016-01-27 14:57:51 +01004650read_sub_module(struct lys_module *module, struct lys_submodule *submodule, struct lyxml_elem *yin,
4651 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02004652{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004653 struct ly_ctx *ctx = module->ctx;
Michal Vasko2f7925f2015-10-21 15:06:56 +02004654 struct lyxml_elem *next, *child, *child2, root, grps, augs;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004655 struct lys_node *node = NULL;
Radek Krejcic071c542016-01-27 14:57:51 +01004656 struct lys_module *trg;
4657 struct lys_import *aux_imp;
4658 struct lys_include *aux_inc, inc;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004659 const char *value;
Radek Krejcic071c542016-01-27 14:57:51 +01004660 int i, j, r;
4661 int inc_size_aux = 0;
4662 int version_flag = 0;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004663 /* counters */
Radek Krejcieb00f512015-07-01 16:44:58 +02004664 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 +02004665
Radek Krejcic071c542016-01-27 14:57:51 +01004666 /* to simplify code, store the module/submodule being processed as trg */
4667 trg = submodule ? (struct lys_module*)submodule : module;
4668
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004669 /* init */
4670 memset(&root, 0, sizeof root);
4671 memset(&grps, 0, sizeof grps);
Michal Vasko2f7925f2015-10-21 15:06:56 +02004672 memset(&augs, 0, sizeof augs);
Radek Krejcie0674f82015-06-15 13:58:51 +02004673
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004674 /*
4675 * in the first run, we process elements with cardinality of 1 or 0..1 and
4676 * count elements with cardinality 0..n. Data elements (choices, containers,
4677 * leafs, lists, leaf-lists) are moved aside to be processed last, since we
4678 * need have all top-level and groupings already prepared at that time. In
4679 * the middle loop, we process other elements with carinality of 0..n since
4680 * we need to allocate arrays to store them.
4681 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004682 LY_TREE_FOR_SAFE(yin->child, next, child) {
4683 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004684 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004685 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004686 continue;
4687 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004688
Radek Krejcic071c542016-01-27 14:57:51 +01004689 if (!submodule && !strcmp(child->name, "namespace")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004690 if (module->ns) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004691 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004692 goto error;
4693 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004694 GETVAL(value, child, "uri");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004695 module->ns = lydict_insert(ctx, value, strlen(value));
Michal Vasko345da0a2015-12-02 10:35:55 +01004696 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004697 } else if (!submodule && !strcmp(child->name, "prefix")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004698 if (module->prefix) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004699 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004700 goto error;
4701 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004702 GETVAL(value, child, "value");
Radek Krejcic6556022016-01-27 15:16:45 +01004703 if (lyp_check_identifier(value, LY_IDENT_PREFIX, LOGLINE(child), module, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004704 goto error;
4705 }
4706 module->prefix = lydict_insert(ctx, value, strlen(value));
Michal Vasko345da0a2015-12-02 10:35:55 +01004707 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004708 } else if (submodule && !strcmp(child->name, "belongs-to")) {
4709 if (submodule->prefix) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004710 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004711 goto error;
4712 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004713 GETVAL(value, child, "module");
Radek Krejci749190d2016-02-18 16:26:25 +01004714 if (!ly_strequal(value, submodule->belongsto->name, 1)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004715 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004716 goto error;
4717 }
Radek Krejcif3886932015-06-04 17:36:06 +02004718
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004719 /* get the prefix substatement, start with checks */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004720 if (!child->child) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004721 LOGVAL(LYE_MISSSTMT2, LOGLINE(child), LY_VLOG_NONE, NULL, "prefix", child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004722 goto error;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004723 } else if (strcmp(child->child->name, "prefix")) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004724 LOGVAL(LYE_INSTMT, LOGLINE(child->child), LY_VLOG_NONE, NULL, child->child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004725 goto error;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004726 } else if (child->child->next) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004727 LOGVAL(LYE_INSTMT, LOGLINE(child->child->next), LY_VLOG_NONE, NULL,
Radek Krejciadb57612016-02-16 13:34:34 +01004728 child->child->next->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004729 goto error;
4730 }
4731 /* and now finally get the value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004732 GETVAL(value, child->child, "value");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004733 /* check here differs from a generic prefix check, since this prefix
4734 * don't have to be unique
Michal Vasko38d01f72015-06-15 09:41:06 +02004735 */
Radek Krejcic6556022016-01-27 15:16:45 +01004736 if (lyp_check_identifier(value, LY_IDENT_NAME, LOGLINE(child->child), NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004737 goto error;
4738 }
Radek Krejcic071c542016-01-27 14:57:51 +01004739 submodule->prefix = lydict_insert(ctx, value, strlen(value));
Radek Krejci0af13872015-05-30 11:50:52 +02004740
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004741 /* we are done with belongs-to */
Michal Vasko345da0a2015-12-02 10:35:55 +01004742 lyxml_free(ctx, child);
Radek Krejcieb00f512015-07-01 16:44:58 +02004743
4744 /* counters (statements with n..1 cardinality) */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004745 } else if (!strcmp(child->name, "import")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004746 c_imp++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004747 } else if (!strcmp(child->name, "revision")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004748 c_rev++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004749 } else if (!strcmp(child->name, "typedef")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004750 c_tpdf++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004751 } else if (!strcmp(child->name, "identity")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004752 c_ident++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004753 } else if (!strcmp(child->name, "include")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004754 c_inc++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004755 } else if (!strcmp(child->name, "augment")) {
Radek Krejcif5be10f2015-06-16 13:29:36 +02004756 c_aug++;
Michal Vasko2f7925f2015-10-21 15:06:56 +02004757 /* keep augments separated, processed last */
Radek Krejcic071c542016-01-27 14:57:51 +01004758 lyxml_unlink_elem(ctx, child, 2);
4759 lyxml_add_child(ctx, &augs, child);
Michal Vasko2f7925f2015-10-21 15:06:56 +02004760
Radek Krejci1d82ef62015-08-07 14:44:40 +02004761 } else if (!strcmp(child->name, "feature")) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02004762 c_ftrs++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004763 } else if (!strcmp(child->name, "deviation")) {
Radek Krejcieb00f512015-07-01 16:44:58 +02004764 c_dev++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004765
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004766 /* data statements */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004767 } else if (!strcmp(child->name, "container") ||
4768 !strcmp(child->name, "leaf-list") ||
4769 !strcmp(child->name, "leaf") ||
4770 !strcmp(child->name, "list") ||
4771 !strcmp(child->name, "choice") ||
4772 !strcmp(child->name, "uses") ||
Michal Vasko7ffc3052015-10-21 15:05:56 +02004773 !strcmp(child->name, "anyxml") ||
4774 !strcmp(child->name, "rpc") ||
4775 !strcmp(child->name, "notification")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004776 lyxml_unlink_elem(ctx, child, 2);
4777 lyxml_add_child(ctx, &root, child);
Michal Vasko7ffc3052015-10-21 15:05:56 +02004778
Radek Krejci1d82ef62015-08-07 14:44:40 +02004779 } else if (!strcmp(child->name, "grouping")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004780 /* keep groupings separated and process them before other data statements */
Radek Krejcic071c542016-01-27 14:57:51 +01004781 lyxml_unlink_elem(ctx, child, 2);
4782 lyxml_add_child(ctx, &grps, child);
Radek Krejcida04f4a2015-05-21 12:54:09 +02004783
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004784 /* optional statements */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004785 } else if (!strcmp(child->name, "description")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004786 if (trg->dsc) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004787 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004788 goto error;
4789 }
Radek Krejcic071c542016-01-27 14:57:51 +01004790 trg->dsc = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01004791 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004792 if (!trg->dsc) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004793 goto error;
4794 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004795 } else if (!strcmp(child->name, "reference")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004796 if (trg->ref) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004797 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004798 goto error;
4799 }
Radek Krejcic071c542016-01-27 14:57:51 +01004800 trg->ref = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01004801 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004802 if (!trg->ref) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004803 goto error;
4804 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004805 } else if (!strcmp(child->name, "organization")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004806 if (trg->org) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004807 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004808 goto error;
4809 }
Radek Krejcic071c542016-01-27 14:57:51 +01004810 trg->org = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01004811 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004812 if (!trg->org) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004813 goto error;
4814 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004815 } else if (!strcmp(child->name, "contact")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004816 if (trg->contact) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004817 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004818 goto error;
4819 }
Radek Krejcic071c542016-01-27 14:57:51 +01004820 trg->contact = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01004821 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004822 if (!trg->contact) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004823 goto error;
4824 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004825 } else if (!strcmp(child->name, "yang-version")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004826 /* TODO: support YANG 1.1 ? */
Radek Krejcic071c542016-01-27 14:57:51 +01004827 if (version_flag) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004828 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004829 goto error;
4830 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004831 GETVAL(value, child, "value");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004832 if (strcmp(value, "1")) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004833 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, "yang-version");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004834 goto error;
4835 }
Radek Krejcic071c542016-01-27 14:57:51 +01004836 version_flag = 1;
4837 if (!submodule) {
4838 module->version = 1;
4839 } /* TODO else check for the submodule's same version as in main module, waits for YANG 1.1 support */
Michal Vasko345da0a2015-12-02 10:35:55 +01004840 lyxml_free(ctx, child);
Michal Vasko38d01f72015-06-15 09:41:06 +02004841
Radek Krejci1d82ef62015-08-07 14:44:40 +02004842 } else if (!strcmp(child->name, "extension")) {
4843 GETVAL(value, child, "name");
Radek Krejci5166a892015-07-02 16:44:24 +02004844
Radek Krejci3d468122015-10-02 13:36:12 +02004845 /* we have the following supported (hardcoded) extensions: */
4846 /* ietf-netconf's get-filter-element-attributes */
4847 if (!strcmp(module->ns, LY_NSNC) &&
4848 !strcmp(value, "get-filter-element-attributes")) {
4849 LOGDBG("NETCONF filter extension found");
4850 /* NACM's default-deny-write and default-deny-all */
4851 } else if (!strcmp(module->ns, LY_NSNACM) &&
4852 (!strcmp(value, "default-deny-write") || !strcmp(value, "default-deny-all"))) {
4853 LOGDBG("NACM extension found");
4854 /* other extensions are not supported, so inform about such an extension */
4855 } else {
Radek Krejci6764bb32015-07-03 15:16:04 +02004856 LOGWRN("Not supported \"%s\" extension statement found, ignoring.", value);
Michal Vasko345da0a2015-12-02 10:35:55 +01004857 lyxml_free(ctx, child);
Radek Krejci6764bb32015-07-03 15:16:04 +02004858 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004859 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004860 LOGVAL(LYE_INSTMT, LOGLINE(child), LY_VLOG_NONE, NULL, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004861 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004862 }
4863 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004864
Radek Krejcic071c542016-01-27 14:57:51 +01004865 /* check for mandatory statements */
4866 if (submodule && !submodule->prefix) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004867 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "belongs-to", "submodule");
Michal Vaskobdf51ef2015-12-10 12:11:21 +01004868 goto error;
Radek Krejcic071c542016-01-27 14:57:51 +01004869 } else if (!submodule) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004870 if (!module->ns) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004871 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "namespace", "module");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004872 goto error;
4873 }
4874 if (!module->prefix) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004875 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), LY_VLOG_NONE, NULL, "prefix", "module");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004876 goto error;
4877 }
4878 }
Radek Krejcibb3257d2015-05-21 23:03:51 +02004879
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004880 /* allocate arrays for elements with cardinality of 0..n */
4881 if (c_imp) {
Radek Krejcic071c542016-01-27 14:57:51 +01004882 trg->imp = calloc(c_imp, sizeof *trg->imp);
4883 if (!trg->imp) {
Michal Vasko253035f2015-12-17 16:58:13 +01004884 LOGMEM;
4885 goto error;
4886 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004887 }
4888 if (c_rev) {
Radek Krejcic071c542016-01-27 14:57:51 +01004889 trg->rev = calloc(c_rev, sizeof *trg->rev);
4890 if (!trg->rev) {
Michal Vasko253035f2015-12-17 16:58:13 +01004891 LOGMEM;
4892 goto error;
4893 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004894 }
4895 if (c_tpdf) {
Radek Krejcic071c542016-01-27 14:57:51 +01004896 trg->tpdf = calloc(c_tpdf, sizeof *trg->tpdf);
4897 if (!trg->tpdf) {
Michal Vasko253035f2015-12-17 16:58:13 +01004898 LOGMEM;
4899 goto error;
4900 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004901 }
4902 if (c_ident) {
Radek Krejcic071c542016-01-27 14:57:51 +01004903 trg->ident = calloc(c_ident, sizeof *trg->ident);
4904 if (!trg->ident) {
Michal Vasko253035f2015-12-17 16:58:13 +01004905 LOGMEM;
4906 goto error;
4907 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004908 }
4909 if (c_inc) {
Radek Krejcic071c542016-01-27 14:57:51 +01004910 trg->inc = calloc(c_inc, sizeof *trg->inc);
4911 if (!trg->inc) {
Michal Vasko253035f2015-12-17 16:58:13 +01004912 LOGMEM;
4913 goto error;
4914 }
Radek Krejcic071c542016-01-27 14:57:51 +01004915 trg->inc_size = c_inc;
4916 /* trg->inc_size can be updated by the included submodules,
4917 * so we will use inc_size_aux here, trg->inc_size stores the
4918 * target size of the array
4919 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004920 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02004921 if (c_aug) {
Radek Krejcic071c542016-01-27 14:57:51 +01004922 trg->augment = calloc(c_aug, sizeof *trg->augment);
4923 if (!trg->augment) {
Michal Vasko253035f2015-12-17 16:58:13 +01004924 LOGMEM;
4925 goto error;
4926 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02004927 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004928 if (c_ftrs) {
Radek Krejcic071c542016-01-27 14:57:51 +01004929 trg->features = calloc(c_ftrs, sizeof *trg->features);
4930 if (!trg->features) {
Michal Vasko253035f2015-12-17 16:58:13 +01004931 LOGMEM;
4932 goto error;
4933 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004934 }
Radek Krejcieb00f512015-07-01 16:44:58 +02004935 if (c_dev) {
Radek Krejcic071c542016-01-27 14:57:51 +01004936 trg->deviation = calloc(c_dev, sizeof *trg->deviation);
4937 if (!trg->deviation) {
Michal Vasko253035f2015-12-17 16:58:13 +01004938 LOGMEM;
4939 goto error;
4940 }
Radek Krejcieb00f512015-07-01 16:44:58 +02004941 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004942
Michal Vasko2f7925f2015-10-21 15:06:56 +02004943 /* middle part - process nodes with cardinality of 0..n except the data nodes and augments */
4944 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004945 if (!strcmp(child->name, "import")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004946 r = fill_yin_import(trg, child, &trg->imp[trg->imp_size]);
4947 trg->imp_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004948 if (r) {
4949 goto error;
4950 }
Radek Krejcice7fb782015-05-29 16:52:34 +02004951
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004952 /* check duplicities in imported modules */
Radek Krejcic071c542016-01-27 14:57:51 +01004953 for (i = 0; i < trg->imp_size - 1; i++) {
4954 if (!strcmp(trg->imp[i].module->name, trg->imp[trg->imp_size - 1].module->name)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004955 LOGVAL(LYE_SPEC, LOGLINE(child), LY_VLOG_NONE, NULL, "Importing module \"%s\" repeatedly.", trg->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004956 goto error;
4957 }
4958 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02004959
Radek Krejci1d82ef62015-08-07 14:44:40 +02004960 } else if (!strcmp(child->name, "include")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004961 memset(&inc, 0, sizeof inc);
4962 /* 1) pass module, not trg, since we want to pass the main module
4963 * 2) we cannot pass directly the structure in the array since
4964 * submodule parser can realloc our array of includes */
Michal Vasko5ff78822016-02-12 09:33:31 +01004965 r = fill_yin_include(module, submodule, child, &inc, unres);
Michal Vasko9c4c99d2016-02-11 15:47:08 +01004966 memcpy(&trg->inc[inc_size_aux], &inc, sizeof inc);
4967 inc_size_aux++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004968 if (r) {
4969 goto error;
4970 }
Radek Krejcice7fb782015-05-29 16:52:34 +02004971
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004972 /* check duplications in include submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01004973 for (i = 0; i < inc_size_aux - 1; i++) {
Michal Vasko27ab8222016-02-12 09:33:52 +01004974 if (trg->inc[i].submodule && !strcmp(trg->inc[i].submodule->name, trg->inc[inc_size_aux - 1].submodule->name)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004975 LOGVAL(LYE_SPEC, LOGLINE(child), LY_VLOG_NONE, NULL, "Including submodule \"%s\" repeatedly.",
Michal Vasko27ab8222016-02-12 09:33:52 +01004976 trg->inc[i].submodule->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004977 goto error;
4978 }
4979 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02004980
Radek Krejci1d82ef62015-08-07 14:44:40 +02004981 } else if (!strcmp(child->name, "revision")) {
4982 GETVAL(value, child, "date");
Radek Krejcic6556022016-01-27 15:16:45 +01004983 if (lyp_check_date(value, LOGLINE(child))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004984 goto error;
4985 }
Radek Krejcic071c542016-01-27 14:57:51 +01004986 memcpy(trg->rev[trg->rev_size].date, value, LY_REV_SIZE - 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004987 /* check uniqueness of the revision date - not required by RFC */
Radek Krejcic071c542016-01-27 14:57:51 +01004988 for (i = 0; i < trg->rev_size; i++) {
4989 if (!strcmp(value, trg->rev[i].date)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004990 LOGVAL(LYE_INARG, LOGLINE(child), LY_VLOG_NONE, NULL, value, child->name);
Radek Krejciadb57612016-02-16 13:34:34 +01004991 LOGVAL(LYE_SPEC, 0, 0, NULL, "Revision is not unique.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004992 }
4993 }
Radek Krejcice7fb782015-05-29 16:52:34 +02004994
Radek Krejci1d82ef62015-08-07 14:44:40 +02004995 LY_TREE_FOR(child->child, child2) {
4996 if (!strcmp(child2->name, "description")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004997 if (trg->rev[trg->rev_size].dsc) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01004998 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child2->name, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004999 goto error;
5000 }
Radek Krejcic071c542016-01-27 14:57:51 +01005001 trg->rev[trg->rev_size].dsc = read_yin_subnode(ctx, child2, "text");
5002 if (!trg->rev[trg->rev_size].dsc) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005003 goto error;
5004 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005005 } else if (!strcmp(child2->name, "reference")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005006 if (trg->rev[trg->rev_size].ref) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01005007 LOGVAL(LYE_TOOMANY, LOGLINE(child), LY_VLOG_NONE, NULL, child2->name, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005008 goto error;
5009 }
Radek Krejcic071c542016-01-27 14:57:51 +01005010 trg->rev[trg->rev_size].ref = read_yin_subnode(ctx, child2, "text");
5011 if (!trg->rev[trg->rev_size].ref) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005012 goto error;
5013 }
5014 } else {
Michal Vaskoe5e06292016-02-26 09:56:41 +01005015 LOGVAL(LYE_INSTMT, LOGLINE(child2), LY_VLOG_NONE, NULL, child2->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005016 goto error;
5017 }
5018 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005019
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005020 /* keep the latest revision at position 0 */
Radek Krejcic071c542016-01-27 14:57:51 +01005021 if (trg->rev_size && strcmp(trg->rev[trg->rev_size].date, trg->rev[0].date) > 0) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005022 /* switch their position */
Radek Krejcic071c542016-01-27 14:57:51 +01005023 value = strdup(trg->rev[0].date);
Michal Vasko253035f2015-12-17 16:58:13 +01005024 if (!value) {
5025 LOGMEM;
5026 goto error;
5027 }
Radek Krejcic071c542016-01-27 14:57:51 +01005028 memcpy(trg->rev[0].date, trg->rev[trg->rev_size].date, LY_REV_SIZE - 1);
5029 memcpy(trg->rev[trg->rev_size].date, value, LY_REV_SIZE - 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005030 free((char *)value);
Radek Krejcice7fb782015-05-29 16:52:34 +02005031
Radek Krejci749190d2016-02-18 16:26:25 +01005032 if (!ly_strequal(trg->rev[0].dsc, trg->rev[trg->rev_size].dsc, 1)) {
Radek Krejcic071c542016-01-27 14:57:51 +01005033 value = trg->rev[0].dsc;
5034 trg->rev[0].dsc = trg->rev[trg->rev_size].dsc;
5035 trg->rev[trg->rev_size].dsc = value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005036 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005037
Radek Krejci749190d2016-02-18 16:26:25 +01005038 if (!ly_strequal(trg->rev[0].ref, trg->rev[trg->rev_size].ref, 1)) {
Radek Krejcic071c542016-01-27 14:57:51 +01005039 value = trg->rev[0].ref;
5040 trg->rev[0].ref = trg->rev[trg->rev_size].ref;
5041 trg->rev[trg->rev_size].ref = value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005042 }
5043 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005044
Radek Krejcic071c542016-01-27 14:57:51 +01005045 trg->rev_size++;
Michal Vasko2f7925f2015-10-21 15:06:56 +02005046
Radek Krejci1d82ef62015-08-07 14:44:40 +02005047 } else if (!strcmp(child->name, "typedef")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005048 r = fill_yin_typedef(trg, NULL, child, &trg->tpdf[trg->tpdf_size], unres);
5049 trg->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005050 if (r) {
5051 goto error;
5052 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02005053
Radek Krejci1d82ef62015-08-07 14:44:40 +02005054 } else if (!strcmp(child->name, "identity")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005055 r = fill_yin_identity(trg, child, &trg->ident[trg->ident_size], unres);
5056 trg->ident_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005057 if (r) {
5058 goto error;
5059 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02005060
Radek Krejci1d82ef62015-08-07 14:44:40 +02005061 } else if (!strcmp(child->name, "feature")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005062 r = fill_yin_feature(trg, child, &trg->features[trg->features_size], unres);
5063 trg->features_size++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005064 if (r) {
5065 goto error;
5066 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02005067
Radek Krejci1d82ef62015-08-07 14:44:40 +02005068 } else if (!strcmp(child->name, "deviation")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005069 r = fill_yin_deviation(trg, child, &trg->deviation[trg->deviation_size], unres);
5070 trg->deviation_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02005071 if (r) {
5072 goto error;
5073 }
Michal Vasko53a42af2016-02-12 11:05:02 +01005074 /* module with deviation - must be implemented (description of /ietf-yang-library:modules-state/module/deviation) */
5075 module->implemented = 1;
Michal Vasko2f7925f2015-10-21 15:06:56 +02005076
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005077 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005078 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005079
Radek Krejcic071c542016-01-27 14:57:51 +01005080 if (submodule) {
5081 /* propagate imports into the main module */
5082 for (i = r = 0; i < submodule->imp_size; i++) {
5083 for (j = 0; j < module->imp_size; j++) {
5084 if (submodule->imp[i].module == module->imp[j].module &&
5085 !strcmp(submodule->imp[i].rev, module->imp[j].rev)) {
5086 /* check prefix match */
Radek Krejci749190d2016-02-18 16:26:25 +01005087 if (!ly_strequal(submodule->imp[i].prefix, module->imp[j].prefix, 1)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01005088 LOGVAL(LYE_INID, LOGLINE(yin), LY_VLOG_NONE, NULL, submodule->imp[i].prefix,
Radek Krejcic071c542016-01-27 14:57:51 +01005089 "non-matching prefixes of imported module in main module and submodule");
5090 goto error;
5091 }
5092 break;
5093 }
5094 }
5095 if (j == module->imp_size) {
5096 /* new import */
5097 r++;
5098 }
5099 }
5100 if (r) {
5101 aux_imp = realloc(module->imp, (module->imp_size + r) * sizeof *module->imp);
5102 if (!aux_imp) {
5103 LOGMEM;
5104 goto error;
5105 }
5106 module->imp = aux_imp;
5107 for (i = r = 0; i < submodule->imp_size; i++) {
5108 for (j = 0; j < module->imp_size; j++) {
5109 if (submodule->imp[i].module == module->imp[j].module) {
5110 break;
5111 }
5112 }
5113 if (j == module->imp_size) {
5114 /* new import */
5115 /* check prefix uniqueness */
5116 if (dup_prefix_check(submodule->imp[i].prefix, module)) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01005117 LOGVAL(LYE_DUPID, LOGLINE(yin), LY_VLOG_NONE, NULL, "prefix", submodule->imp[i].prefix);
Radek Krejcic071c542016-01-27 14:57:51 +01005118 goto error;
5119 }
5120 memcpy(&module->imp[module->imp_size + r], &submodule->imp[i], sizeof *submodule->imp);
5121 module->imp[module->imp_size + r].external = 1;
5122 r++;
5123 }
5124 }
5125 module->imp_size += r;
5126 }
5127
Michal Vaskoe2905632016-02-11 15:42:24 +01005128 /* propagate includes into the main module */
Radek Krejcic071c542016-01-27 14:57:51 +01005129 for (i = r = 0; i < submodule->inc_size; i++) {
5130 for (j = 0; j < module->inc_size; j++) {
5131 if (submodule->inc[i].submodule == module->inc[j].submodule) {
5132 break;
5133 }
5134 }
5135 if (j == module->inc_size) {
5136 /* new include */
5137 r++;
5138 }
5139 }
5140
5141 if (r) {
5142 aux_inc = realloc(module->inc, (module->inc_size + r) * sizeof *module->inc);
5143 if (!aux_inc) {
5144 LOGMEM;
5145 goto error;
5146 }
5147 module->inc = aux_inc;
5148 for (i = r = 0; i < submodule->inc_size; i++) {
5149 for (j = 0; j < module->inc_size; j++) {
5150 if (submodule->inc[i].submodule == module->inc[j].submodule) {
5151 break;
5152 }
5153 }
5154 if (j == module->inc_size) {
5155 /* new include */
5156 memcpy(&module->inc[module->inc_size + r], &submodule->inc[i], sizeof *submodule->inc);
5157 module->inc[module->inc_size + r].external = 1;
5158 r++;
5159 }
5160 }
5161 module->inc_size += r;
5162 }
5163 }
5164
Radek Krejcif5be10f2015-06-16 13:29:36 +02005165 /* process data nodes. Start with groupings to allow uses
Radek Krejcic071c542016-01-27 14:57:51 +01005166 * refer to them. Submodule's data nodes are stored in the
5167 * main module data tree.
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005168 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005169 LY_TREE_FOR_SAFE(grps.child, next, child) {
Radek Krejcic071c542016-01-27 14:57:51 +01005170 node = read_yin_grouping(trg, NULL, child, 0, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005171 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005172 goto error;
5173 }
Radek Krejci74705112015-06-05 10:25:44 +02005174
Michal Vasko345da0a2015-12-02 10:35:55 +01005175 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005176 }
Radek Krejci74705112015-06-05 10:25:44 +02005177
Radek Krejcif5be10f2015-06-16 13:29:36 +02005178 /* parse data nodes, ... */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005179 LY_TREE_FOR_SAFE(root.child, next, child) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02005180
Radek Krejci1d82ef62015-08-07 14:44:40 +02005181 if (!strcmp(child->name, "container")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005182 node = read_yin_container(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005183 } else if (!strcmp(child->name, "leaf-list")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005184 node = read_yin_leaflist(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005185 } else if (!strcmp(child->name, "leaf")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005186 node = read_yin_leaf(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005187 } else if (!strcmp(child->name, "list")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005188 node = read_yin_list(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005189 } else if (!strcmp(child->name, "choice")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005190 node = read_yin_choice(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005191 } else if (!strcmp(child->name, "uses")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005192 node = read_yin_uses(trg, NULL, child, 1, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005193 } else if (!strcmp(child->name, "anyxml")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005194 node = read_yin_anyxml(trg, NULL, child, 1, unres);
Michal Vasko7ffc3052015-10-21 15:05:56 +02005195 } else if (!strcmp(child->name, "rpc")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005196 node = read_yin_rpc(trg, NULL, child, 0, unres);
Michal Vasko7ffc3052015-10-21 15:05:56 +02005197 } else if (!strcmp(child->name, "notification")) {
Michal Vaskocd074222016-02-15 11:07:03 +01005198 node = read_yin_notif(trg, NULL, child, 0, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005199 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005200 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005201 goto error;
5202 }
Radek Krejci25d782a2015-05-22 15:03:23 +02005203
Michal Vasko345da0a2015-12-02 10:35:55 +01005204 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005205 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02005206
Michal Vasko2f7925f2015-10-21 15:06:56 +02005207 /* ... and finally augments (last, so we can augment our data, for instance) */
5208 LY_TREE_FOR_SAFE(augs.child, next, child) {
Radek Krejcic071c542016-01-27 14:57:51 +01005209 r = fill_yin_augment(trg, NULL, child, &trg->augment[trg->augment_size], unres);
5210 trg->augment_size++;
Radek Krejcif5be10f2015-06-16 13:29:36 +02005211
Michal Vasko2f7925f2015-10-21 15:06:56 +02005212 if (r) {
Radek Krejcif5be10f2015-06-16 13:29:36 +02005213 goto error;
5214 }
Michal Vasko345da0a2015-12-02 10:35:55 +01005215 lyxml_free(ctx, child);
Radek Krejcif5be10f2015-06-16 13:29:36 +02005216 }
5217
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005218 return EXIT_SUCCESS;
Radek Krejciefaeba32015-05-27 14:30:57 +02005219
5220error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005221 /* cleanup */
5222 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005223 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005224 }
5225 while (grps.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005226 lyxml_free(module->ctx, grps.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005227 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02005228 while (augs.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005229 lyxml_free(module->ctx, augs.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02005230 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005231
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005232 return EXIT_FAILURE;
Radek Krejciefaeba32015-05-27 14:30:57 +02005233}
5234
Michal Vasko0d343d12015-08-24 14:57:36 +02005235/* logs directly */
Michal Vasko5a721fd2016-02-16 12:16:48 +01005236struct lys_submodule *
5237yin_read_submodule(struct lys_module *module, const char *data, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02005238{
Michal Vasko9f258e42016-02-11 11:36:27 +01005239 struct lys_node *next, *elem;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005240 struct lyxml_elem *yin;
Michal Vasko5a721fd2016-02-16 12:16:48 +01005241 struct lys_submodule *submodule = NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005242 const char *value;
Michal Vasko9f258e42016-02-11 11:36:27 +01005243 uint8_t i;
Radek Krejciefaeba32015-05-27 14:30:57 +02005244
Michal Vasko5a721fd2016-02-16 12:16:48 +01005245 assert(module->ctx);
Radek Krejciefaeba32015-05-27 14:30:57 +02005246
Radek Krejci722b0072016-02-01 17:09:45 +01005247 yin = lyxml_parse_mem(module->ctx, data, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005248 if (!yin) {
Michal Vasko5a721fd2016-02-16 12:16:48 +01005249 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005250 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005251
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005252 /* check root element */
5253 if (!yin->name || strcmp(yin->name, "submodule")) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01005254 LOGVAL(LYE_INSTMT, LOGLINE(yin), LY_VLOG_NONE, NULL, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005255 goto error;
5256 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005257
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005258 GETVAL(value, yin, "name");
Radek Krejcic6556022016-01-27 15:16:45 +01005259 if (lyp_check_identifier(value, LY_IDENT_NAME, LOGLINE(yin), NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005260 goto error;
5261 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005262
Michal Vasko5a721fd2016-02-16 12:16:48 +01005263 submodule = calloc(1, sizeof *submodule);
5264 if (!submodule) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005265 LOGMEM;
5266 goto error;
5267 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005268
Michal Vasko5a721fd2016-02-16 12:16:48 +01005269 submodule->ctx = module->ctx;
5270 submodule->name = lydict_insert(submodule->ctx, value, strlen(value));
5271 submodule->type = 1;
5272 submodule->belongsto = module;
Radek Krejciefaeba32015-05-27 14:30:57 +02005273
Michal Vasko5a721fd2016-02-16 12:16:48 +01005274 LOGVRB("Reading submodule \"%s\".", submodule->name);
5275 if (read_sub_module(module, submodule, yin, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005276 goto error;
5277 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005278
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005279 /* cleanup */
Michal Vasko345da0a2015-12-02 10:35:55 +01005280 lyxml_free(module->ctx, yin);
Radek Krejciefaeba32015-05-27 14:30:57 +02005281
Michal Vasko5a721fd2016-02-16 12:16:48 +01005282 LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name);
Radek Krejciefaeba32015-05-27 14:30:57 +02005283
Michal Vasko5a721fd2016-02-16 12:16:48 +01005284 return submodule;
Radek Krejciefaeba32015-05-27 14:30:57 +02005285
5286error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005287 /* cleanup */
Michal Vasko5a721fd2016-02-16 12:16:48 +01005288 unres_schema_free((struct lys_module *)submodule, &unres);
Michal Vasko345da0a2015-12-02 10:35:55 +01005289 lyxml_free(module->ctx, yin);
Michal Vasko9f258e42016-02-11 11:36:27 +01005290
Michal Vasko5a721fd2016-02-16 12:16:48 +01005291 if (!submodule) {
Radek Krejcidaea8212016-02-15 08:28:20 +01005292 LOGERR(ly_errno, "Submodule parsing failed.");
Michal Vasko5a721fd2016-02-16 12:16:48 +01005293 return NULL;
Radek Krejcidaea8212016-02-15 08:28:20 +01005294 }
5295
Michal Vasko5a721fd2016-02-16 12:16:48 +01005296 LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name);
Radek Krejcidaea8212016-02-15 08:28:20 +01005297
Michal Vaskoff006c12016-02-17 11:15:19 +01005298 /* remove parsed data */
5299 LY_TREE_FOR_SAFE(module->data, next, elem) {
5300 if (elem->module == (struct lys_module *)submodule) {
5301 lys_node_free(elem, NULL, 0);
5302 }
5303 }
5304
5305 /* remove applied deviations */
Michal Vasko5a721fd2016-02-16 12:16:48 +01005306 for (i = 0; i < submodule->deviation_size; ++i) {
Michal Vaskoff006c12016-02-17 11:15:19 +01005307 if (submodule->deviation[i].orig_node) {
5308 resolve_augment_schema_nodeid(submodule->deviation[i].target_name, NULL, module, (const struct lys_node **)&elem);
5309 lys_node_switch(elem, submodule->deviation[i].orig_node);
Michal Vasko9f258e42016-02-11 11:36:27 +01005310 }
5311 }
5312
5313 /* remove applied augments */
Michal Vasko5a721fd2016-02-16 12:16:48 +01005314 for (i = 0; i < submodule->augment_size; ++i) {
5315 if (submodule->augment[i].target) {
5316 LY_TREE_FOR_SAFE(submodule->augment[i].target->child, next, elem) {
5317 if (elem->parent == (struct lys_node *)&submodule->augment[i]) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01005318 lys_node_free(elem, NULL, 0);
Michal Vasko9f258e42016-02-11 11:36:27 +01005319 }
5320 }
5321 }
5322 }
5323
Michal Vasko5a721fd2016-02-16 12:16:48 +01005324 lys_submodule_free(submodule, NULL);
5325
5326 return NULL;
Radek Krejciefaeba32015-05-27 14:30:57 +02005327}
5328
Michal Vasko0d343d12015-08-24 14:57:36 +02005329/* logs directly */
Radek Krejcib8048692015-08-05 13:36:34 +02005330struct lys_module *
Radek Krejcic071c542016-01-27 14:57:51 +01005331yin_read_module(struct ly_ctx *ctx, const char *data, int implement)
Radek Krejciefaeba32015-05-27 14:30:57 +02005332{
Michal Vasko9f258e42016-02-11 11:36:27 +01005333 struct lys_node *next, *elem;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005334 struct lyxml_elem *yin;
Radek Krejcib8048692015-08-05 13:36:34 +02005335 struct lys_module *module = NULL, **newlist = NULL;
Radek Krejcic071c542016-01-27 14:57:51 +01005336 struct unres_schema *unres;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005337 const char *value;
5338 int i;
Radek Krejciefaeba32015-05-27 14:30:57 +02005339
Radek Krejcic071c542016-01-27 14:57:51 +01005340 unres = calloc(1, sizeof *unres);
5341 if (!unres) {
5342 LOGMEM;
5343 return NULL;
5344 }
5345
Radek Krejci722b0072016-02-01 17:09:45 +01005346 yin = lyxml_parse_mem(ctx, data, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005347 if (!yin) {
Radek Krejcic071c542016-01-27 14:57:51 +01005348 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005349 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005350
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005351 /* check root element */
5352 if (!yin->name || strcmp(yin->name, "module")) {
Michal Vaskoe5e06292016-02-26 09:56:41 +01005353 LOGVAL(LYE_INSTMT, LOGLINE(yin), LY_VLOG_NONE, NULL, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005354 goto error;
5355 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005356
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005357 GETVAL(value, yin, "name");
Radek Krejcic6556022016-01-27 15:16:45 +01005358 if (lyp_check_identifier(value, LY_IDENT_NAME, LOGLINE(yin), NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005359 goto error;
5360 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005361
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005362 module = calloc(1, sizeof *module);
5363 if (!module) {
5364 LOGMEM;
5365 goto error;
5366 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005367
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005368 module->ctx = ctx;
5369 module->name = lydict_insert(ctx, value, strlen(value));
5370 module->type = 0;
Michal Vaskod8aa32d2015-07-24 11:50:01 +02005371 module->implemented = (implement ? 1 : 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02005372
Michal Vasko9f258e42016-02-11 11:36:27 +01005373 LOGVRB("Reading module \"%s\".", module->name);
Radek Krejcic071c542016-01-27 14:57:51 +01005374 if (read_sub_module(module, NULL, yin, unres)) {
5375 goto error;
5376 }
5377
5378 /* resolve rest of unres items */
5379 if (unres->count && resolve_unres_schema(module, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005380 goto error;
5381 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005382
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005383 /* add to the context's list of modules */
5384 if (ctx->models.used == ctx->models.size) {
Michal Vaskof2e1a992015-11-09 09:54:47 +01005385 newlist = realloc(ctx->models.list, (2 * ctx->models.size) * sizeof *newlist);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005386 if (!newlist) {
5387 LOGMEM;
5388 goto error;
5389 }
5390 for (i = ctx->models.size; i < ctx->models.size * 2; i++) {
5391 newlist[i] = NULL;
5392 }
5393 ctx->models.size *= 2;
5394 ctx->models.list = newlist;
5395 }
5396 for (i = 0; ctx->models.list[i]; i++) {
5397 /* check name (name/revision) and namespace uniqueness */
5398 if (!strcmp(ctx->models.list[i]->name, module->name)) {
Radek Krejci63a91a92015-07-29 13:31:04 +02005399 if (ctx->models.list[i]->rev_size == module->rev_size) {
5400 /* both have the same number of revisions */
5401 if (!module->rev_size || !strcmp(ctx->models.list[i]->rev[0].date, module->rev[0].date)) {
5402 /* both have the same revision -> we already have the same module */
5403 /* so free the new one and update the old one's implement flag if needed */
Michal Vasko9f258e42016-02-11 11:36:27 +01005404 LOGVRB("Module \"%s\" already in context.", ctx->models.list[i]->name);
Radek Krejci63a91a92015-07-29 13:31:04 +02005405
Michal Vasko9f258e42016-02-11 11:36:27 +01005406 lys_free(module, NULL, 1);
Radek Krejcic071c542016-01-27 14:57:51 +01005407 module = ctx->models.list[i];
5408 if (implement && !module->implemented) {
5409 lyp_set_implemented(module);
Radek Krejci63a91a92015-07-29 13:31:04 +02005410 }
Radek Krejcic071c542016-01-27 14:57:51 +01005411
5412 goto success;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005413 }
5414 }
Radek Krejcif647e612015-07-30 11:36:07 +02005415 /* else (both elses) keep searching, for now the caller is just adding
5416 * another revision of an already present schema
5417 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005418 } else if (!strcmp(ctx->models.list[i]->ns, module->ns)) {
Michal Vasko9f258e42016-02-11 11:36:27 +01005419 LOGERR(LY_EINVAL, "Two different modules (\"%s\" and \"%s\") have the same namespace \"%s\".",
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005420 ctx->models.list[i]->name, module->name, module->ns);
5421 goto error;
5422 }
5423 }
5424 ctx->models.list[i] = module;
5425 ctx->models.used++;
Michal Vasko68cffd72015-08-03 12:50:11 +02005426 ctx->models.module_set_id++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005427
Radek Krejcic071c542016-01-27 14:57:51 +01005428success:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005429 /* cleanup */
Michal Vasko345da0a2015-12-02 10:35:55 +01005430 lyxml_free(ctx, yin);
Radek Krejcic071c542016-01-27 14:57:51 +01005431 unres_schema_free(NULL, &unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02005432
Michal Vasko9f258e42016-02-11 11:36:27 +01005433 LOGVRB("Module \"%s\" successfully parsed.", module->name);
Radek Krejcida04f4a2015-05-21 12:54:09 +02005434
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005435 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005436
5437error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005438 /* cleanup */
Radek Krejcic071c542016-01-27 14:57:51 +01005439 lyxml_free(ctx, yin);
Radek Krejcib8c07b82016-02-12 11:11:55 +01005440 unres_schema_free(module, &unres);
5441
5442 if (!module) {
Radek Krejcidaea8212016-02-15 08:28:20 +01005443 LOGERR(ly_errno, "Module parsing failed.");
Radek Krejcib8c07b82016-02-12 11:11:55 +01005444 return NULL;
5445 }
5446
5447 LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name);
Radek Krejcida04f4a2015-05-21 12:54:09 +02005448
Michal Vaskoff006c12016-02-17 11:15:19 +01005449 /* remove applied deviations */
Michal Vasko9f258e42016-02-11 11:36:27 +01005450 for (i = 0; i < module->deviation_size; ++i) {
Michal Vaskoff006c12016-02-17 11:15:19 +01005451 if (module->deviation[i].orig_node) {
5452 resolve_augment_schema_nodeid(module->deviation[i].target_name, NULL, module, (const struct lys_node **)&elem);
5453 lys_node_switch(elem, module->deviation[i].orig_node);
Michal Vasko9f258e42016-02-11 11:36:27 +01005454 }
5455 }
5456
5457 /* remove applied augments */
5458 for (i = 0; i < module->augment_size; ++i) {
5459 if (module->augment[i].target) {
5460 LY_TREE_FOR_SAFE(module->augment[i].target->child, next, elem) {
5461 if (elem->parent == (struct lys_node *)&module->augment[i]) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01005462 lys_node_free(elem, NULL, 0);
Michal Vasko9f258e42016-02-11 11:36:27 +01005463 }
5464 }
5465 }
5466 }
5467
5468 lys_free(module, NULL, 1);
5469
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005470 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005471}