blob: 1f5be287cfd8e5bde4f4f4a66340608407c9edd2 [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 */
21
Radek Krejci812b10a2015-05-28 16:48:25 +020022#include <assert.h>
Radek Krejci25d782a2015-05-22 15:03:23 +020023#include <ctype.h>
Radek Krejci8b4f23c2015-06-02 16:09:25 +020024#include <errno.h>
25#include <limits.h>
Radek Krejci25d782a2015-05-22 15:03:23 +020026#include <stdint.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020027#include <stdlib.h>
28#include <string.h>
Radek Krejci25d782a2015-05-22 15:03:23 +020029#include <stddef.h>
Michal Vasko69068852015-07-13 14:34:31 +020030#include <sys/types.h>
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020031#include <pcre.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020032
Radek Krejci998a0b82015-08-17 13:14:36 +020033#include "libyang.h"
34#include "common.h"
35#include "context.h"
Radek Krejci41912fe2015-10-22 10:22:12 +020036#include "dict_private.h"
Michal Vaskofcdac172015-10-07 09:35:05 +020037#include "xpath.h"
Radek Krejci998a0b82015-08-17 13:14:36 +020038#include "parser.h"
Radek Krejci998a0b82015-08-17 13:14:36 +020039#include "resolve.h"
40#include "tree_internal.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020041#include "xml_internal.h"
Radek Krejciefdd0ce2015-05-26 16:48:29 +020042
Radek Krejcice7fb782015-05-29 16:52:34 +020043#define GETVAL(value, node, arg) \
Michal Vasko2d710f32016-02-05 12:29:21 +010044 value = lyxml_get_attr(node, arg, NULL); \
45 if (!value) { \
46 LOGVAL(LYE_MISSARG, LOGLINE(node), arg, node->name); \
47 goto error; \
48 }
Radek Krejcice7fb782015-05-29 16:52:34 +020049
Radek Krejcic6556022016-01-27 15:16:45 +010050/* parser.c */
51int dup_prefix_check(const char *prefix, struct lys_module *module);
52
Radek Krejcib388c152015-06-04 17:03:03 +020053#define OPT_IDENT 0x01
54#define OPT_CONFIG 0x02
55#define OPT_MODULE 0x04
56#define OPT_INHERIT 0x08
Radek Krejci6764bb32015-07-03 15:16:04 +020057#define OPT_NACMEXT 0x10
Radek Krejcib8048692015-08-05 13:36:34 +020058static int read_yin_common(struct lys_module *, struct lys_node *, struct lys_node *, struct lyxml_elem *, int);
Radek Krejcib388c152015-06-04 17:03:03 +020059
Radek Krejcib8048692015-08-05 13:36:34 +020060static 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 +020061 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020062static 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 +020063 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020064static 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 +020065 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020066static 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 +020067 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020068static 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 +020069 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020070static 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 +020071 int resolve, struct unres_schema *unres);
Radek Krejcib8048692015-08-05 13:36:34 +020072static 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 +020073 int resolve, struct unres_schema *unres);
Radek Krejcia9544502015-08-14 08:24:29 +020074static 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 +020075 int resolve, struct unres_schema *unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +020076static 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 +020077 int resolve, struct unres_schema *unres);
78static struct lys_when *read_yin_when(struct lys_module *module, struct lyxml_elem *yin);
Radek Krejci74705112015-06-05 10:25:44 +020079
Michal Vasko0d343d12015-08-24 14:57:36 +020080/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020081static const char *
82read_yin_subnode(struct ly_ctx *ctx, struct lyxml_elem *node, const char *name)
Radek Krejcice7fb782015-05-29 16:52:34 +020083{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020084 int len;
Radek Krejcida04f4a2015-05-21 12:54:09 +020085
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020086 /* there should be <text> child */
87 if (!node->child || !node->child->name || strcmp(node->child->name, name)) {
Radek Krejci218436d2016-02-10 12:54:06 +010088 LOGERR(LY_EVALID, "Expected \"%s\" element in \"%s\" element.", name, node->name);
89 LOGVAL(LYE_INARG, LOGLINE(node), name, node->name);
90 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020091 } else if (node->child->content) {
92 len = strlen(node->child->content);
93 return lydict_insert(ctx, node->child->content, len);
Radek Krejci218436d2016-02-10 12:54:06 +010094 } else {
95 return lydict_insert(ctx, "", 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020096 }
Radek Krejcida04f4a2015-05-21 12:54:09 +020097}
98
Michal Vasko0d343d12015-08-24 14:57:36 +020099/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200100static int
Michal Vaskof02e3742015-08-05 16:27:02 +0200101fill_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 +0200102{
Radek Krejci73adb602015-07-02 18:07:40 +0200103 struct lyxml_elem *node;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200104 const char *value;
Radek Krejciad73b6f2016-02-09 15:42:55 +0100105 int base_flag = 0;
Radek Krejci04581c62015-05-22 21:24:00 +0200106
Michal Vasko4cfcd252015-08-03 14:31:10 +0200107 GETVAL(value, yin, "name");
Michal Vaskoc94283a2015-10-29 09:07:20 +0100108 ident->name = value;
Michal Vasko4cfcd252015-08-03 14:31:10 +0200109
Radek Krejci76512572015-08-04 09:47:08 +0200110 if (read_yin_common(module, NULL, (struct lys_node *)ident, yin, OPT_IDENT | OPT_MODULE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200111 return EXIT_FAILURE;
112 }
Radek Krejci04581c62015-05-22 21:24:00 +0200113
Radek Krejci73adb602015-07-02 18:07:40 +0200114 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200115 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
116 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200117 continue;
118 }
119
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200120 if (!strcmp(node->name, "base")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +0100121 if (base_flag) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200122 LOGVAL(LYE_TOOMANY, LOGLINE(node), "base", "identity");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200123 return EXIT_FAILURE;
124 }
Radek Krejciad73b6f2016-02-09 15:42:55 +0100125 base_flag = 1;
126
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200127 GETVAL(value, node, "name");
Michal Vaskoc94283a2015-10-29 09:07:20 +0100128 value = transform_schema2json(module, value, LOGLINE(node));
129 if (!value) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +0200130 return EXIT_FAILURE;
131 }
Michal Vaskoc94283a2015-10-29 09:07:20 +0100132
133 if (unres_schema_add_str(module, unres, ident, UNRES_IDENT, value, LOGLINE(node)) == -1) {
134 lydict_remove(module->ctx, value);
135 return EXIT_FAILURE;
136 }
137 lydict_remove(module->ctx, value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200138 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200139 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name, "identity");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200140 return EXIT_FAILURE;
141 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200142 }
Radek Krejci04581c62015-05-22 21:24:00 +0200143
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200144 return EXIT_SUCCESS;
Michal Vasko2d851a92015-10-20 16:16:36 +0200145
146error:
147 return EXIT_FAILURE;
Radek Krejci04581c62015-05-22 21:24:00 +0200148}
149
Michal Vasko0d343d12015-08-24 14:57:36 +0200150/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200151static int
Radek Krejci1574a8d2015-08-03 14:16:52 +0200152read_restr_substmt(struct ly_ctx *ctx, struct lys_restr *restr, struct lyxml_elem *yin)
Radek Krejci41726f92015-06-19 13:11:05 +0200153{
Radek Krejci73adb602015-07-02 18:07:40 +0200154 struct lyxml_elem *child;
Radek Krejci461d1622015-06-30 14:06:28 +0200155 const char *value;
Radek Krejci41726f92015-06-19 13:11:05 +0200156
Radek Krejci73adb602015-07-02 18:07:40 +0200157 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200158 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
159 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200160 continue;
161 }
162
Radek Krejci41726f92015-06-19 13:11:05 +0200163 if (!strcmp(child->name, "description")) {
164 if (restr->dsc) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200165 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200166 return EXIT_FAILURE;
167 }
168 restr->dsc = read_yin_subnode(ctx, child, "text");
169 if (!restr->dsc) {
170 return EXIT_FAILURE;
171 }
172 } else if (!strcmp(child->name, "reference")) {
173 if (restr->ref) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200174 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200175 return EXIT_FAILURE;
176 }
177 restr->ref = read_yin_subnode(ctx, child, "text");
178 if (!restr->ref) {
179 return EXIT_FAILURE;
180 }
181 } else if (!strcmp(child->name, "error-app-tag")) {
182 if (restr->eapptag) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200183 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200184 return EXIT_FAILURE;
185 }
Michal Vasko54e426f2015-07-07 15:38:02 +0200186 GETVAL(value, child, "value");
Radek Krejci461d1622015-06-30 14:06:28 +0200187 restr->eapptag = lydict_insert(ctx, value, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200188 } else if (!strcmp(child->name, "error-message")) {
189 if (restr->emsg) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200190 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200191 return EXIT_FAILURE;
192 }
193 restr->emsg = read_yin_subnode(ctx, child, "value");
194 if (!restr->emsg) {
195 return EXIT_FAILURE;
196 }
197 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200198 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200199 return EXIT_FAILURE;
200 }
Radek Krejci41726f92015-06-19 13:11:05 +0200201 }
202
203 return EXIT_SUCCESS;
Michal Vaskoc8ef47f2015-06-29 14:56:19 +0200204
205error:
206 return EXIT_FAILURE;
Radek Krejci41726f92015-06-19 13:11:05 +0200207}
208
Michal Vasko88c29542015-11-27 14:57:53 +0100209/* logs directly, returns EXIT_SUCCESS, EXIT_FAILURE, -1 */
210int
Radek Krejcib8048692015-08-05 13:36:34 +0200211fill_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 +0200212 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200213{
Michal Vasko1dca6882015-10-22 14:29:42 +0200214 const char *value, *name, *err_ptr;
Radek Krejci5fbc9162015-06-19 14:11:11 +0200215 struct lyxml_elem *next, *node;
Radek Krejci1574a8d2015-08-03 14:16:52 +0200216 struct lys_restr **restr;
217 struct lys_type_bit bit;
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200218 pcre *precomp;
219 int i, j, rc, err_offset;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200220 int64_t v, v_;
Radek Krejci994b6f62015-06-18 16:47:27 +0200221 int64_t p, p_;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200222
Radek Krejci8de7b0f2015-07-02 11:43:42 +0200223 GETVAL(value, yin, "name");
Michal Vaskofba15262015-10-21 12:10:28 +0200224 value = transform_schema2json(module, value, LOGLINE(yin));
Michal Vasko1dca6882015-10-22 14:29:42 +0200225 if (!value) {
226 goto error;
Michal Vaskoa5835e92015-10-20 15:07:39 +0200227 }
Michal Vaskob362b4c2015-10-20 15:15:46 +0200228
229 i = parse_identifier(value);
230 if (i < 1) {
Michal Vasko0160ffe2015-10-21 13:25:26 +0200231 LOGVAL(LYE_INCHAR, LOGLINE(yin), value[-i], &value[-i]);
Michal Vasko88c29542015-11-27 14:57:53 +0100232 lydict_remove(module->ctx, value);
Michal Vaskob362b4c2015-10-20 15:15:46 +0200233 goto error;
234 }
235 /* module name */
236 if (value[i]) {
237 type->module_name = lydict_insert(module->ctx, value, i);
238 value += i;
Michal Vasko534f8442015-10-21 13:25:49 +0200239 if ((value[0] != ':') || (parse_identifier(value + 1) < 1)) {
Michal Vasko0160ffe2015-10-21 13:25:26 +0200240 LOGVAL(LYE_INCHAR, LOGLINE(yin), value[0], value);
Michal Vasko88c29542015-11-27 14:57:53 +0100241 lydict_remove(module->ctx, value);
Michal Vaskob362b4c2015-10-20 15:15:46 +0200242 goto error;
243 }
Michal Vasko534f8442015-10-21 13:25:49 +0200244 ++value;
Michal Vaskob362b4c2015-10-20 15:15:46 +0200245 }
Michal Vaskoa5835e92015-10-20 15:07:39 +0200246
Michal Vasko1dca6882015-10-22 14:29:42 +0200247 rc = resolve_superior_type(value, type->module_name, module, parent, &type->der);
Michal Vasko88c29542015-11-27 14:57:53 +0100248 lydict_remove(module->ctx, value);
Michal Vaskof7eee892015-08-24 15:03:11 +0200249 if (rc == -1) {
Michal Vasko1dca6882015-10-22 14:29:42 +0200250 LOGVAL(LYE_INMOD, LOGLINE(yin), type->module_name);
Michal Vaskof7eee892015-08-24 15:03:11 +0200251 goto error;
Michal Vasko88c29542015-11-27 14:57:53 +0100252
253 /* the type could not be resolved or it was resolved to an unresolved typedef */
Michal Vaskof7eee892015-08-24 15:03:11 +0200254 } else if (rc == EXIT_FAILURE) {
Michal Vasko88c29542015-11-27 14:57:53 +0100255 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200256 }
257 type->base = type->der->type.base;
Radek Krejci25d782a2015-05-22 15:03:23 +0200258
Radek Krejcicf509982015-12-15 09:22:44 +0100259 /* check status */
Radek Krejcic6556022016-01-27 15:16:45 +0100260 if (lyp_check_status(type->parent->flags, type->parent->module, type->parent->name,
Radek Krejcicf509982015-12-15 09:22:44 +0100261 type->der->flags, type->der->module, type->der->name, LOGLINE(yin))) {
262 return -1;
263 }
264
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200265 switch (type->base) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200266 case LY_TYPE_BITS:
Radek Krejci994b6f62015-06-18 16:47:27 +0200267 /* RFC 6020 9.7.4 - bit */
268
269 /* get bit specifications, at least one must be present */
270 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200271 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
272 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100273 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200274 continue;
275 }
276
Radek Krejci994b6f62015-06-18 16:47:27 +0200277 if (!strcmp(node->name, "bit")) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200278 type->info.bits.count++;
Radek Krejci41726f92015-06-19 13:11:05 +0200279 } else {
Michal Vaskoe29c6622015-11-27 15:02:31 +0100280 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejci41726f92015-06-19 13:11:05 +0200281 goto error;
Radek Krejci994b6f62015-06-18 16:47:27 +0200282 }
283 }
Radek Krejciac781922015-07-09 15:35:14 +0200284 if (!type->der->type.der && !type->info.bits.count) {
285 /* type is derived directly from buit-in bits type and bit statement is required */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200286 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "bit", "type");
Radek Krejci994b6f62015-06-18 16:47:27 +0200287 goto error;
288 }
Radek Krejciac781922015-07-09 15:35:14 +0200289 if (type->der->type.der && type->info.bits.count) {
290 /* type is not directly derived from buit-in bits type and bit statement is prohibited */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200291 LOGVAL(LYE_INSTMT, LOGLINE(yin), "bit");
Radek Krejciac781922015-07-09 15:35:14 +0200292 goto error;
293 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200294
295 type->info.bits.bit = calloc(type->info.bits.count, sizeof *type->info.bits.bit);
Michal Vasko253035f2015-12-17 16:58:13 +0100296 if (!type->info.bits.bit) {
297 LOGMEM;
298 goto error;
299 }
Radek Krejci73adb602015-07-02 18:07:40 +0200300 p = 0;
301 i = -1;
302 LY_TREE_FOR(yin->child, next) {
303 i++;
304
305 GETVAL(value, next, "name");
Radek Krejcic6556022016-01-27 15:16:45 +0100306 if (lyp_check_identifier(value, LY_IDENT_SIMPLE, LOGLINE(next), NULL, NULL)) {
Michal Vasko2d26a022015-12-07 09:27:21 +0100307 goto error;
308 }
309
Radek Krejci994b6f62015-06-18 16:47:27 +0200310 type->info.bits.bit[i].name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci76512572015-08-04 09:47:08 +0200311 if (read_yin_common(module, NULL, (struct lys_node *)&type->info.bits.bit[i], next, 0)) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200312 type->info.bits.count = i + 1;
313 goto error;
314 }
315
316 /* check the name uniqueness */
317 for (j = 0; j < i; j++) {
318 if (!strcmp(type->info.bits.bit[j].name, type->info.bits.bit[i].name)) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200319 LOGVAL(LYE_BITS_DUPNAME, LOGLINE(next), type->info.bits.bit[i].name);
Radek Krejci994b6f62015-06-18 16:47:27 +0200320 type->info.bits.count = i + 1;
321 goto error;
322 }
323 }
324
Radek Krejci0d70c372015-07-02 16:23:10 +0200325 p_ = -1;
Radek Krejci73adb602015-07-02 18:07:40 +0200326 LY_TREE_FOR(next->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200327 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
328 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200329 continue;
Radek Krejci994b6f62015-06-18 16:47:27 +0200330 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200331
Radek Krejci0d70c372015-07-02 16:23:10 +0200332 if (!strcmp(node->name, "position")) {
333 GETVAL(value, node, "value");
Radek Krejci7511f402015-07-10 09:56:30 +0200334 p_ = strtoll(value, NULL, 10);
Radek Krejci0d70c372015-07-02 16:23:10 +0200335
336 /* range check */
Radek Krejcib8ca1082015-07-10 11:24:11 +0200337 if (p_ < 0 || p_ > UINT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200338 LOGVAL(LYE_INARG, LOGLINE(node), value, "bit/position");
Radek Krejci0d70c372015-07-02 16:23:10 +0200339 type->info.bits.count = i + 1;
340 goto error;
341 }
342 type->info.bits.bit[i].pos = (uint32_t)p_;
343
344 /* keep the highest enum value for automatic increment */
Michal Vasko9ab05942015-07-07 15:38:26 +0200345 if (type->info.bits.bit[i].pos >= p) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200346 p = type->info.bits.bit[i].pos;
347 p++;
348 } else {
349 /* check that the value is unique */
350 for (j = 0; j < i; j++) {
351 if (type->info.bits.bit[j].pos == type->info.bits.bit[i].pos) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200352 LOGVAL(LYE_BITS_DUPVAL, LOGLINE(node), type->info.bits.bit[i].pos, type->info.bits.bit[i].name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200353 type->info.bits.count = i + 1;
354 goto error;
355 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200356 }
357 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200358 } else {
Michal Vaskoe29c6622015-11-27 15:02:31 +0100359 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200360 goto error;
Radek Krejci994b6f62015-06-18 16:47:27 +0200361 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200362 }
363 if (p_ == -1) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200364 /* assign value automatically */
365 if (p > UINT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200366 LOGVAL(LYE_INARG, LOGLINE(next), "4294967295", "bit/position");
Radek Krejci994b6f62015-06-18 16:47:27 +0200367 type->info.bits.count = i + 1;
368 goto error;
369 }
370 type->info.bits.bit[i].pos = (uint32_t)p;
371 p++;
372 }
Radek Krejci9a1b95a2015-07-09 15:32:21 +0200373
374 /* keep them ordered by position */
375 j = i;
376 while (j && type->info.bits.bit[j - 1].pos > type->info.bits.bit[j].pos) {
377 /* switch them */
378 memcpy(&bit, &type->info.bits.bit[j], sizeof bit);
379 memcpy(&type->info.bits.bit[j], &type->info.bits.bit[j - 1], sizeof bit);
380 memcpy(&type->info.bits.bit[j - 1], &bit, sizeof bit);
381 j--;
382 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200383 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200384 break;
Radek Krejci25d782a2015-05-22 15:03:23 +0200385
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200386 case LY_TYPE_DEC64:
Radek Krejcif9401c32015-06-26 16:47:36 +0200387 /* RFC 6020 9.2.4 - range and 9.3.4 - fraction-digits */
Radek Krejci73adb602015-07-02 18:07:40 +0200388 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200389 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
390 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200391 continue;
392 }
393
Radek Krejcif9401c32015-06-26 16:47:36 +0200394 if (!strcmp(node->name, "range")) {
395 if (type->info.dec64.range) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200396 LOGVAL(LYE_TOOMANY, LOGLINE(node), node->name, yin->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200397 goto error;
398 }
399
400 GETVAL(value, node, "value");
Radek Krejci6dc53a22015-08-17 13:27:59 +0200401 if (lyp_check_length_range(value, type)) {
Michal Vasko9c1bc642015-08-05 16:25:53 +0200402 LOGVAL(LYE_INARG, LOGLINE(node), value, "range");
Radek Krejcif9401c32015-06-26 16:47:36 +0200403 goto error;
404 }
405 type->info.dec64.range = calloc(1, sizeof *type->info.dec64.range);
Michal Vasko253035f2015-12-17 16:58:13 +0100406 if (!type->info.dec64.range) {
407 LOGMEM;
408 goto error;
409 }
Radek Krejcif9401c32015-06-26 16:47:36 +0200410 type->info.dec64.range->expr = lydict_insert(module->ctx, value, 0);
411
412 /* get possible substatements */
413 if (read_restr_substmt(module->ctx, type->info.dec64.range, node)) {
414 goto error;
415 }
416 } else if (!strcmp(node->name, "fraction-digits")) {
417 if (type->info.dec64.dig) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200418 LOGVAL(LYE_TOOMANY, LOGLINE(node), node->name, yin->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200419 goto error;
420 }
421 GETVAL(value, node, "value");
422 v = strtol(value, NULL, 10);
423
424 /* range check */
425 if (v < 1 || v > 18) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200426 LOGVAL(LYE_INARG, LOGLINE(node), value, node->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200427 goto error;
428 }
429 type->info.dec64.dig = (uint8_t)v;
430 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200431 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejcif9401c32015-06-26 16:47:36 +0200432 goto error;
433 }
Radek Krejcif9401c32015-06-26 16:47:36 +0200434 }
435
436 /* mandatory sub-statement(s) check */
437 if (!type->info.dec64.dig && !type->der->type.der) {
438 /* decimal64 type directly derived from built-in type requires fraction-digits */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200439 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "fraction-digits", "type");
Radek Krejcif9401c32015-06-26 16:47:36 +0200440 goto error;
441 }
Radek Krejci7511f402015-07-10 09:56:30 +0200442 if (type->info.dec64.dig && type->der->type.der) {
443 /* type is not directly derived from buit-in type and fraction-digits statement is prohibited */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200444 LOGVAL(LYE_INSTMT, LOGLINE(yin), "fraction-digits");
Radek Krejci7511f402015-07-10 09:56:30 +0200445 goto error;
446 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200447 break;
Radek Krejci25d782a2015-05-22 15:03:23 +0200448
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200449 case LY_TYPE_ENUM:
Radek Krejci994b6f62015-06-18 16:47:27 +0200450 /* RFC 6020 9.6 - enum */
Radek Krejci25d782a2015-05-22 15:03:23 +0200451
Radek Krejci994b6f62015-06-18 16:47:27 +0200452 /* get enum specifications, at least one must be present */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200453 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200454 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
455 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100456 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200457 continue;
458 }
459
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200460 if (!strcmp(node->name, "enum")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200461 type->info.enums.count++;
Radek Krejci5fbc9162015-06-19 14:11:11 +0200462 } else {
Michal Vaskoe29c6622015-11-27 15:02:31 +0100463 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200464 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200465 }
466 }
Radek Krejcib54bcb12015-07-10 13:16:40 +0200467 if (!type->der->type.der && !type->info.enums.count) {
468 /* type is derived directly from buit-in enumeartion type and enum statement is required */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200469 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "enum", "type");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200470 goto error;
471 }
Radek Krejcib54bcb12015-07-10 13:16:40 +0200472 if (type->der->type.der && type->info.enums.count) {
473 /* type is not directly derived from buit-in enumeration type and enum statement is prohibited */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200474 LOGVAL(LYE_INSTMT, LOGLINE(yin), "enum");
Radek Krejcib54bcb12015-07-10 13:16:40 +0200475 goto error;
476 }
Radek Krejci25d782a2015-05-22 15:03:23 +0200477
Radek Krejci1574a8d2015-08-03 14:16:52 +0200478 type->info.enums.enm = calloc(type->info.enums.count, sizeof *type->info.enums.enm);
Michal Vasko253035f2015-12-17 16:58:13 +0100479 if (!type->info.enums.enm) {
480 LOGMEM;
481 goto error;
482 }
Radek Krejci73adb602015-07-02 18:07:40 +0200483 v = 0;
484 i = -1;
485 LY_TREE_FOR(yin->child, next) {
486 i++;
487
488 GETVAL(value, next, "name");
Michal Vasko0fb58e92015-12-07 09:27:44 +0100489 if (!value[0]) {
490 LOGVAL(LYE_SPEC, LOGLINE(next), "Enum name must not be empty.");
491 goto error;
492 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200493 type->info.enums.enm[i].name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci76512572015-08-04 09:47:08 +0200494 if (read_yin_common(module, NULL, (struct lys_node *)&type->info.enums.enm[i], next, 0)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200495 type->info.enums.count = i + 1;
496 goto error;
497 }
Radek Krejci994b6f62015-06-18 16:47:27 +0200498
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200499 /* the assigned name MUST NOT have any leading or trailing whitespace characters */
Radek Krejci1574a8d2015-08-03 14:16:52 +0200500 value = type->info.enums.enm[i].name;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200501 if (isspace(value[0]) || isspace(value[strlen(value) - 1])) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200502 LOGVAL(LYE_ENUM_WS, LOGLINE(next), value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200503 type->info.enums.count = i + 1;
504 goto error;
505 }
Radek Krejci25d782a2015-05-22 15:03:23 +0200506
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200507 /* check the name uniqueness */
508 for (j = 0; j < i; j++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +0200509 if (!strcmp(type->info.enums.enm[j].name, type->info.enums.enm[i].name)) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200510 LOGVAL(LYE_ENUM_DUPNAME, LOGLINE(next), type->info.enums.enm[i].name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200511 type->info.enums.count = i + 1;
512 goto error;
513 }
514 }
Radek Krejci04581c62015-05-22 21:24:00 +0200515
Radek Krejci0d70c372015-07-02 16:23:10 +0200516 v_ = -1;
Radek Krejci73adb602015-07-02 18:07:40 +0200517 LY_TREE_FOR(next->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200518 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
519 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200520 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200521 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200522
Radek Krejci0d70c372015-07-02 16:23:10 +0200523 if (!strcmp(node->name, "value")) {
524 GETVAL(value, node, "value");
Radek Krejci7511f402015-07-10 09:56:30 +0200525 v_ = strtoll(value, NULL, 10);
Radek Krejci0d70c372015-07-02 16:23:10 +0200526
527 /* range check */
528 if (v_ < INT32_MIN || v_ > INT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200529 LOGVAL(LYE_INARG, LOGLINE(node), value, "enum/value");
Radek Krejci0d70c372015-07-02 16:23:10 +0200530 type->info.enums.count = i + 1;
531 goto error;
532 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200533 type->info.enums.enm[i].value = v_;
Radek Krejci0d70c372015-07-02 16:23:10 +0200534
535 /* keep the highest enum value for automatic increment */
Radek Krejci1574a8d2015-08-03 14:16:52 +0200536 if (type->info.enums.enm[i].value > v) {
537 v = type->info.enums.enm[i].value;
Radek Krejci0d70c372015-07-02 16:23:10 +0200538 v++;
539 } else {
540 /* check that the value is unique */
541 for (j = 0; j < i; j++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +0200542 if (type->info.enums.enm[j].value == type->info.enums.enm[i].value) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200543 LOGVAL(LYE_ENUM_DUPVAL, LOGLINE(node), type->info.enums.enm[i].value,
Radek Krejci1574a8d2015-08-03 14:16:52 +0200544 type->info.enums.enm[i].name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200545 type->info.enums.count = i + 1;
546 goto error;
547 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200548 }
549 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200550 } else {
Michal Vaskoe29c6622015-11-27 15:02:31 +0100551 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200552 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200553 }
Radek Krejci0d70c372015-07-02 16:23:10 +0200554 }
555 if (v_ == -1) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200556 /* assign value automatically */
557 if (v > INT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200558 LOGVAL(LYE_INARG, LOGLINE(next), "2147483648", "enum/value");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200559 type->info.enums.count = i + 1;
560 goto error;
561 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200562 type->info.enums.enm[i].value = v;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200563 v++;
564 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200565 }
566 break;
567
568 case LY_TYPE_IDENT:
Radek Krejci994b6f62015-06-18 16:47:27 +0200569 /* RFC 6020 9.10 - base */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200570
571 /* get base specification, exactly one must be present */
Radek Krejci0d70c372015-07-02 16:23:10 +0200572 LY_TREE_FOR_SAFE(yin->child, next, node) {
573 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
574 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100575 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200576 continue;
577 }
578
Michal Vaskoe29c6622015-11-27 15:02:31 +0100579 if (strcmp(node->name, "base")) {
580 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejci0d70c372015-07-02 16:23:10 +0200581 goto error;
582 }
583 }
584
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200585 if (!yin->child) {
Radek Krejci65c889c2015-06-22 10:17:22 +0200586 if (type->der->type.der) {
587 /* this is just a derived type with no base specified/required */
588 break;
589 }
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200590 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "base", "type");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200591 goto error;
592 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200593 if (yin->child->next) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200594 LOGVAL(LYE_TOOMANY, LOGLINE(yin->child->next), yin->child->next->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200595 goto error;
596 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200597 GETVAL(value, yin->child, "name");
Michal Vaskobdfb1e02016-02-05 13:15:11 +0100598 /* store in the JSON format */
599 value = transform_schema2json(module, value, LOGLINE(yin->child));
600 if (!value) {
601 goto error;
602 }
Michal Vasko0bd29d12015-08-19 11:45:49 +0200603 if (unres_schema_add_str(module, unres, type, UNRES_TYPE_IDENTREF, value, LOGLINE(yin->child)) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +0200604 goto error;
605 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200606 break;
607
608 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200609 /* RFC 6020 9.13.2 - require-instance */
Radek Krejci73adb602015-07-02 18:07:40 +0200610 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200611 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
612 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200613 continue;
614 }
615
Radek Krejciaf351422015-06-19 14:49:38 +0200616 if (!strcmp(node->name, "require-instance")) {
617 if (type->info.inst.req) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200618 LOGVAL(LYE_TOOMANY, LOGLINE(node), node->name, yin->name);
Radek Krejciaf351422015-06-19 14:49:38 +0200619 goto error;
620 }
621 GETVAL(value, node, "value");
Michal Vasko25fa5ac2015-07-17 10:53:38 +0200622 if (!strcmp(value, "true")) {
Radek Krejciaf351422015-06-19 14:49:38 +0200623 type->info.inst.req = 1;
Michal Vasko25fa5ac2015-07-17 10:53:38 +0200624 } else if (!strcmp(value, "false")) {
Radek Krejciaf351422015-06-19 14:49:38 +0200625 type->info.inst.req = -1;
626 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200627 LOGVAL(LYE_INARG, LOGLINE(node), value, node->name);
Radek Krejciaf351422015-06-19 14:49:38 +0200628 goto error;
629 }
630 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200631 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejciaf351422015-06-19 14:49:38 +0200632 goto error;
633 }
Radek Krejciaf351422015-06-19 14:49:38 +0200634 }
Michal Vasko8548cf92015-07-20 15:17:53 +0200635
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200636 break;
637
Radek Krejcif2860132015-06-20 12:37:20 +0200638 case LY_TYPE_BINARY:
639 /* RFC 6020 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200640 case LY_TYPE_INT8:
641 case LY_TYPE_INT16:
642 case LY_TYPE_INT32:
643 case LY_TYPE_INT64:
644 case LY_TYPE_UINT8:
645 case LY_TYPE_UINT16:
646 case LY_TYPE_UINT32:
647 case LY_TYPE_UINT64:
Radek Krejcif2860132015-06-20 12:37:20 +0200648 /* RFC 6020 9.2.4 - range */
649
650 /* length and range are actually the same restriction, so process
651 * them by this common code, we just need to differ the name and
652 * structure where the information will be stored
653 */
654 if (type->base == LY_TYPE_BINARY) {
655 restr = &type->info.binary.length;
656 name = "length";
657 } else {
658 restr = &type->info.num.range;
659 name = "range";
660 }
661
Radek Krejci73adb602015-07-02 18:07:40 +0200662 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200663 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
664 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200665 continue;
666 }
667
Radek Krejcif2860132015-06-20 12:37:20 +0200668 if (!strcmp(node->name, name)) {
669 if (*restr) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200670 LOGVAL(LYE_TOOMANY, LOGLINE(node), node->name, yin->name);
Radek Krejcif2860132015-06-20 12:37:20 +0200671 goto error;
672 }
673
674 GETVAL(value, node, "value");
Radek Krejci6dc53a22015-08-17 13:27:59 +0200675 if (lyp_check_length_range(value, type)) {
Michal Vasko9c1bc642015-08-05 16:25:53 +0200676 LOGVAL(LYE_INARG, LOGLINE(node), value, name);
Radek Krejcif2860132015-06-20 12:37:20 +0200677 goto error;
678 }
679 *restr = calloc(1, sizeof **restr);
Michal Vasko253035f2015-12-17 16:58:13 +0100680 if (!(*restr)) {
681 LOGMEM;
682 goto error;
683 }
Radek Krejcif2860132015-06-20 12:37:20 +0200684 (*restr)->expr = lydict_insert(module->ctx, value, 0);
685
686 /* get possible substatements */
687 if (read_restr_substmt(module->ctx, *restr, node)) {
688 goto error;
689 }
690 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200691 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejcif2860132015-06-20 12:37:20 +0200692 goto error;
693 }
Radek Krejcif2860132015-06-20 12:37:20 +0200694 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200695 break;
696
697 case LY_TYPE_LEAFREF:
Radek Krejcidc4c1412015-06-19 15:39:54 +0200698 /* RFC 6020 9.9.2 - path */
Radek Krejci73adb602015-07-02 18:07:40 +0200699 LY_TREE_FOR(yin->child, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200700 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
701 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200702 continue;
703 }
704
Michal Vasko88c29542015-11-27 14:57:53 +0100705 if (!strcmp(node->name, "path") && !type->der->type.der) {
Radek Krejcidc4c1412015-06-19 15:39:54 +0200706 if (type->info.lref.path) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200707 LOGVAL(LYE_TOOMANY, LOGLINE(node), node->name, yin->name);
Radek Krejcidc4c1412015-06-19 15:39:54 +0200708 goto error;
709 }
710
711 GETVAL(value, node, "value");
Michal Vasko1dca6882015-10-22 14:29:42 +0200712 /* store in the JSON format */
Michal Vaskofba15262015-10-21 12:10:28 +0200713 type->info.lref.path = transform_schema2json(module, value, LOGLINE(node));
Michal Vasko1dca6882015-10-22 14:29:42 +0200714 if (!type->info.lref.path) {
715 goto error;
716 }
Michal Vasko0bd29d12015-08-19 11:45:49 +0200717 if (unres_schema_add_node(module, unres, type, UNRES_TYPE_LEAFREF, parent, LOGLINE(yin)) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +0200718 goto error;
719 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200720
Radek Krejcidc4c1412015-06-19 15:39:54 +0200721 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200722 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejcidc4c1412015-06-19 15:39:54 +0200723 goto error;
724 }
Radek Krejci73adb602015-07-02 18:07:40 +0200725 }
726
Michal Vasko88c29542015-11-27 14:57:53 +0100727 if (!type->info.lref.path && !type->der->type.der) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200728 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "path", "type");
Radek Krejci73adb602015-07-02 18:07:40 +0200729 goto error;
Radek Krejcidc4c1412015-06-19 15:39:54 +0200730 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200731 break;
732
733 case LY_TYPE_STRING:
Radek Krejci3733a802015-06-19 13:43:21 +0200734 /* RFC 6020 9.4.4 - length */
735 /* RFC 6020 9.4.6 - pattern */
Radek Krejci73adb602015-07-02 18:07:40 +0200736 i = 0;
Radek Krejci3733a802015-06-19 13:43:21 +0200737 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200738 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
739 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100740 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200741 continue;
742 }
743
Radek Krejci3733a802015-06-19 13:43:21 +0200744 if (!strcmp(node->name, "length")) {
745 if (type->info.str.length) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200746 LOGVAL(LYE_TOOMANY, LOGLINE(node), node->name, yin->name);
Radek Krejci3733a802015-06-19 13:43:21 +0200747 goto error;
748 }
749
750 GETVAL(value, node, "value");
Radek Krejci6dc53a22015-08-17 13:27:59 +0200751 if (lyp_check_length_range(value, type)) {
Michal Vasko9c1bc642015-08-05 16:25:53 +0200752 LOGVAL(LYE_INARG, LOGLINE(node), value, "length");
Radek Krejci3733a802015-06-19 13:43:21 +0200753 goto error;
754 }
755 type->info.str.length = calloc(1, sizeof *type->info.str.length);
Michal Vasko253035f2015-12-17 16:58:13 +0100756 if (!type->info.str.length) {
757 LOGMEM;
758 goto error;
759 }
Radek Krejci3733a802015-06-19 13:43:21 +0200760 type->info.str.length->expr = lydict_insert(module->ctx, value, 0);
761
Radek Krejci5fbc9162015-06-19 14:11:11 +0200762 /* get possible sub-statements */
763 if (read_restr_substmt(module->ctx, type->info.str.length, node)) {
Radek Krejci3733a802015-06-19 13:43:21 +0200764 goto error;
765 }
Michal Vasko345da0a2015-12-02 10:35:55 +0100766 lyxml_free(module->ctx, node);
Radek Krejci3733a802015-06-19 13:43:21 +0200767 } else if (!strcmp(node->name, "pattern")) {
Radek Krejci73adb602015-07-02 18:07:40 +0200768 i++;
Radek Krejci3733a802015-06-19 13:43:21 +0200769 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200770 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejci3733a802015-06-19 13:43:21 +0200771 goto error;
772 }
773 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200774 /* store patterns in array */
Radek Krejci73adb602015-07-02 18:07:40 +0200775 if (i) {
776 type->info.str.patterns = calloc(i, sizeof *type->info.str.patterns);
Michal Vasko253035f2015-12-17 16:58:13 +0100777 if (!type->info.str.patterns) {
778 LOGMEM;
779 goto error;
780 }
Radek Krejci73adb602015-07-02 18:07:40 +0200781 LY_TREE_FOR(yin->child, node) {
Michal Vasko5b64da22015-11-23 15:22:30 +0100782 GETVAL(value, node, "value");
Michal Vasko69068852015-07-13 14:34:31 +0200783
784 /* check that the regex is valid */
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200785 precomp = pcre_compile(value, PCRE_NO_AUTO_CAPTURE, &err_ptr, &err_offset, NULL);
786 if (!precomp) {
787 LOGVAL(LYE_INREGEX, LOGLINE(node), value, err_ptr);
Michal Vasko69068852015-07-13 14:34:31 +0200788 free(type->info.str.patterns);
789 goto error;
790 }
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200791 free(precomp);
Michal Vasko69068852015-07-13 14:34:31 +0200792
Radek Krejci73adb602015-07-02 18:07:40 +0200793 type->info.str.patterns[type->info.str.pat_count].expr = lydict_insert(module->ctx, value, 0);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200794
795 /* get possible sub-statements */
Michal Vasko5b64da22015-11-23 15:22:30 +0100796 if (read_restr_substmt(module->ctx, &type->info.str.patterns[type->info.str.pat_count], node)) {
Michal Vasko69068852015-07-13 14:34:31 +0200797 free(type->info.str.patterns);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200798 goto error;
799 }
Radek Krejci73adb602015-07-02 18:07:40 +0200800 type->info.str.pat_count++;
Radek Krejci5fbc9162015-06-19 14:11:11 +0200801 }
802 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200803 break;
804
805 case LY_TYPE_UNION:
Radek Krejcie4c366b2015-07-02 10:11:31 +0200806 /* RFC 6020 7.4 - type */
807 /* count number of types in union */
808 i = 0;
Radek Krejci0d70c372015-07-02 16:23:10 +0200809 LY_TREE_FOR_SAFE(yin->child, next, node) {
810 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
811 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100812 lyxml_free(module->ctx, node);
Radek Krejci0d70c372015-07-02 16:23:10 +0200813 continue;
814 }
815
Radek Krejcie4c366b2015-07-02 10:11:31 +0200816 if (!strcmp(node->name, "type")) {
817 i++;
818 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200819 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejcie4c366b2015-07-02 10:11:31 +0200820 goto error;
821 }
822 }
823
824 if (!i) {
825 if (type->der->type.der) {
Michal Vasko88c29542015-11-27 14:57:53 +0100826 /* this is just a derived type with no additional type specified/required */
Radek Krejcie4c366b2015-07-02 10:11:31 +0200827 break;
828 }
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200829 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "type", "(union) type");
Radek Krejcie4c366b2015-07-02 10:11:31 +0200830 goto error;
831 }
832
833 /* allocate array for union's types ... */
Radek Krejci1574a8d2015-08-03 14:16:52 +0200834 type->info.uni.types = calloc(i, sizeof *type->info.uni.types);
Michal Vasko253035f2015-12-17 16:58:13 +0100835 if (!type->info.uni.types) {
836 LOGMEM;
837 goto error;
838 }
Radek Krejcie4c366b2015-07-02 10:11:31 +0200839 /* ... and fill the structures */
Radek Krejci73adb602015-07-02 18:07:40 +0200840 LY_TREE_FOR(yin->child, node) {
Radek Krejcicf509982015-12-15 09:22:44 +0100841 type->info.uni.types[type->info.uni.count].parent = type->parent;
Michal Vasko88c29542015-11-27 14:57:53 +0100842 rc = fill_yin_type(module, parent, node, &type->info.uni.types[type->info.uni.count], unres);
843 if (!rc) {
844 type->info.uni.count++;
845
846 /* union's type cannot be empty or leafref */
847 if (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_EMPTY) {
848 LOGVAL(LYE_INARG, LOGLINE(node), "empty", node->name);
849 rc = -1;
850 } else if (type->info.uni.types[type->info.uni.count - 1].base == LY_TYPE_LEAFREF) {
851 LOGVAL(LYE_INARG, LOGLINE(node), "leafref", node->name);
852 rc = -1;
853 }
854 }
855 if (rc) {
856 /* even if we got EXIT_FAILURE, throw it all away, too much trouble doing something else */
857 for (i = 0; i < type->info.uni.count; ++i) {
858 lys_type_free(module->ctx, &type->info.uni.types[i]);
859 }
860 free(type->info.uni.types);
861 type->info.uni.types = NULL;
862 type->info.uni.count = 0;
863
864 if (rc == EXIT_FAILURE) {
865 return EXIT_FAILURE;
866 }
Radek Krejcie4c366b2015-07-02 10:11:31 +0200867 goto error;
868 }
Michal Vasko88c29542015-11-27 14:57:53 +0100869 }
870 break;
Radek Krejcie4c366b2015-07-02 10:11:31 +0200871
Michal Vasko88c29542015-11-27 14:57:53 +0100872 case LY_TYPE_BOOL:
873 case LY_TYPE_EMPTY:
874 /* no sub-statement allowed */
875 LY_TREE_FOR(yin->child, node) {
876 if (node->ns && !strcmp(node->ns->value, LY_NSYIN)) {
877 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
Radek Krejcie4c366b2015-07-02 10:11:31 +0200878 goto error;
879 }
Radek Krejcie4c366b2015-07-02 10:11:31 +0200880 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200881 break;
882
883 default:
Michal Vasko88c29542015-11-27 14:57:53 +0100884 LOGINT;
885 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200886 }
887
888 return EXIT_SUCCESS;
Radek Krejci25d782a2015-05-22 15:03:23 +0200889
890error:
Michal Vasko88c29542015-11-27 14:57:53 +0100891 return -1;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200892}
893
Michal Vasko0d343d12015-08-24 14:57:36 +0200894/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200895static int
Michal Vaskof02e3742015-08-05 16:27:02 +0200896fill_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 +0200897{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200898 const char *value;
Michal Vasko88c29542015-11-27 14:57:53 +0100899 struct lyxml_elem *node, *next;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200900 int has_type = 0, dflt_line;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200901
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200902 GETVAL(value, yin, "name");
Radek Krejcic6556022016-01-27 15:16:45 +0100903 if (lyp_check_identifier(value, LY_IDENT_TYPE, LOGLINE(yin), module, parent)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200904 goto error;
905 }
906 tpdf->name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejcida04f4a2015-05-21 12:54:09 +0200907
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200908 /* generic part - status, description, reference */
Radek Krejci76512572015-08-04 09:47:08 +0200909 if (read_yin_common(module, NULL, (struct lys_node *)tpdf, yin, OPT_IDENT | OPT_MODULE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200910 goto error;
911 }
Radek Krejcieac35532015-05-31 19:09:15 +0200912
Michal Vasko88c29542015-11-27 14:57:53 +0100913 LY_TREE_FOR_SAFE(yin->child, next, node) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200914 if (!node->ns || strcmp(node->ns->value, LY_NSYIN)) {
915 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +0200916 continue;
917 }
918
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200919 if (!strcmp(node->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +0100920 if (has_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200921 LOGVAL(LYE_TOOMANY, LOGLINE(node), node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200922 goto error;
923 }
Michal Vasko88c29542015-11-27 14:57:53 +0100924 /* HACK for unres */
925 tpdf->type.der = (struct lys_tpdf *)node;
Radek Krejcicf509982015-12-15 09:22:44 +0100926 tpdf->type.parent = tpdf;
Michal Vasko88c29542015-11-27 14:57:53 +0100927 if (unres_schema_add_node(module, unres, &tpdf->type, UNRES_TYPE_DER, parent, LOGLINE(node))) {
Radek Krejci73adb602015-07-02 18:07:40 +0200928 goto error;
929 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200930 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200931 } else if (!strcmp(node->name, "default")) {
932 if (tpdf->dflt) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200933 LOGVAL(LYE_TOOMANY, LOGLINE(node), node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200934 goto error;
935 }
936 GETVAL(value, node, "value");
937 tpdf->dflt = lydict_insert(module->ctx, value, strlen(value));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200938 dflt_line = LOGLINE(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200939 } else if (!strcmp(node->name, "units")) {
940 if (tpdf->units) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200941 LOGVAL(LYE_TOOMANY, LOGLINE(node), node->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200942 goto error;
943 }
944 GETVAL(value, node, "name");
945 tpdf->units = lydict_insert(module->ctx, value, strlen(value));
946 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200947 LOGVAL(LYE_INSTMT, LOGLINE(node), value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200948 goto error;
949 }
950 }
Radek Krejci25d782a2015-05-22 15:03:23 +0200951
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200952 /* check mandatory value */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200953 if (!has_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200954 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200955 goto error;
956 }
Radek Krejcieac35532015-05-31 19:09:15 +0200957
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200958 /* check default value */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200959 if (tpdf->dflt) {
Michal Vasko0bd29d12015-08-19 11:45:49 +0200960 if (unres_schema_add_str(module, unres, &tpdf->type, UNRES_TYPE_DFLT, tpdf->dflt, dflt_line) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +0200961 goto error;
962 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200963 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200964
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200965 return EXIT_SUCCESS;
Radek Krejcieac35532015-05-31 19:09:15 +0200966
967error:
968
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200969 return EXIT_FAILURE;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200970}
971
Michal Vasko0d343d12015-08-24 14:57:36 +0200972/* logs directly */
Radek Krejci3cf9e222015-06-18 11:37:50 +0200973static int
Michal Vaskof02e3742015-08-05 16:27:02 +0200974fill_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 +0200975{
976 const char *value;
977 struct lyxml_elem *child, *next;
Michal Vasko2d851a92015-10-20 16:16:36 +0200978 int c = 0, ret;
Radek Krejci3cf9e222015-06-18 11:37:50 +0200979
Radek Krejcib05774c2015-06-18 13:52:59 +0200980 GETVAL(value, yin, "name");
Radek Krejcic6556022016-01-27 15:16:45 +0100981 if (lyp_check_identifier(value, LY_IDENT_FEATURE, LOGLINE(yin), module, NULL)) {
Radek Krejcib05774c2015-06-18 13:52:59 +0200982 goto error;
983 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +0200984 f->name = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci6a113852015-07-03 16:04:20 +0200985 f->module = module;
Radek Krejcib05774c2015-06-18 13:52:59 +0200986
Radek Krejci76512572015-08-04 09:47:08 +0200987 if (read_yin_common(module, NULL, (struct lys_node *)f, yin, 0)) {
Radek Krejci3cf9e222015-06-18 11:37:50 +0200988 goto error;
989 }
990
991 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +0200992 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
993 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +0100994 lyxml_free(module->ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +0200995 continue;
996 }
997
Radek Krejci3cf9e222015-06-18 11:37:50 +0200998 if (!strcmp(child->name, "if-feature")) {
999 c++;
1000 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001001 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001002 goto error;
1003 }
1004 }
1005
1006 if (c) {
1007 f->features = calloc(c, sizeof *f->features);
Michal Vasko253035f2015-12-17 16:58:13 +01001008 if (!f->features) {
1009 LOGMEM;
1010 goto error;
1011 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02001012 }
Radek Krejci73adb602015-07-02 18:07:40 +02001013 LY_TREE_FOR(yin->child, child) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02001014 GETVAL(value, child, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02001015 if (!(value = transform_schema2json(module, value, LOGLINE(child)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02001016 goto error;
1017 }
Radek Krejcicf509982015-12-15 09:22:44 +01001018 /* hack - store pointer to the parent node for later status check */
1019 f->features[f->features_size] = f;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01001020 ret = unres_schema_add_str(module, unres, &f->features[f->features_size], UNRES_IFFEAT, value,
Michal Vasko2d851a92015-10-20 16:16:36 +02001021 LOGLINE(child));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01001022 f->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02001023 lydict_remove(module->ctx, value);
1024 if (ret == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001025 goto error;
1026 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02001027 }
1028
Radek Krejci3cf9e222015-06-18 11:37:50 +02001029 return EXIT_SUCCESS;
1030
1031error:
1032
1033 return EXIT_FAILURE;
1034}
1035
Michal Vasko0d343d12015-08-24 14:57:36 +02001036/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001037static int
Radek Krejcib8048692015-08-05 13:36:34 +02001038fill_yin_must(struct lys_module *module, struct lyxml_elem *yin, struct lys_restr *must)
Radek Krejci800af702015-06-02 13:46:01 +02001039{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001040 const char *value;
Radek Krejci800af702015-06-02 13:46:01 +02001041
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001042 GETVAL(value, yin, "condition");
Michal Vaskofba15262015-10-21 12:10:28 +02001043 must->expr = transform_schema2json(module, value, LOGLINE(yin));
Michal Vaskof9893382015-10-09 14:03:04 +02001044 if (!must->expr) {
1045 goto error;
1046 }
Radek Krejci800af702015-06-02 13:46:01 +02001047
Radek Krejci41726f92015-06-19 13:11:05 +02001048 return read_restr_substmt(module->ctx, must, yin);
Radek Krejci800af702015-06-02 13:46:01 +02001049
Radek Krejci41726f92015-06-19 13:11:05 +02001050error: /* GETVAL requires this label */
Radek Krejci800af702015-06-02 13:46:01 +02001051
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001052 return EXIT_FAILURE;
Radek Krejci800af702015-06-02 13:46:01 +02001053}
1054
Radek Krejci581ce772015-11-10 17:22:40 +01001055static int
Michal Vasko88c29542015-11-27 14:57:53 +01001056fill_yin_unique(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_unique *unique,
1057 struct unres_schema *unres)
Radek Krejci581ce772015-11-10 17:22:40 +01001058{
1059 int i, j;
1060 const char *value, *vaux;
1061
1062 /* get unique value (list of leafs supposed to be unique */
1063 GETVAL(value, yin, "tag");
1064
1065 /* count the number of unique leafs in the value */
1066 vaux = value;
1067 while ((vaux = strpbrk(vaux, " \t\n"))) {
1068 unique->expr_size++;
1069 while (isspace(*vaux)) {
1070 vaux++;
1071 }
1072 }
1073 unique->expr_size++;
1074 unique->expr = calloc(unique->expr_size, sizeof *unique->expr);
Michal Vasko253035f2015-12-17 16:58:13 +01001075 if (!unique->expr) {
1076 LOGMEM;
1077 goto error;
1078 }
Radek Krejci581ce772015-11-10 17:22:40 +01001079
1080 for (i = 0; i < unique->expr_size; i++) {
1081 vaux = strpbrk(value, " \t\n");
1082 if (!vaux) {
1083 /* the last token, lydict_insert() will count its size on its own */
1084 vaux = value;
1085 }
1086
1087 /* store token into unique structure */
1088 unique->expr[i] = lydict_insert(module->ctx, value, vaux - value);
1089
1090 /* check that the expression does not repeat */
1091 for (j = 0; j < i; j++) {
1092 if (unique->expr[j] == unique->expr[i]) {
1093 LOGVAL(LYE_INARG, LOGLINE(yin), unique->expr[i], "unique");
1094 LOGVAL(LYE_SPEC, 0, "The identifier is not unique");
1095 goto error;
1096 }
1097 }
1098
1099 /* try to resolve leaf */
1100 if (unres) {
1101 unres_schema_add_str(module, unres, parent, UNRES_LIST_UNIQ, unique->expr[i], LOGLINE(yin));
1102 } else {
1103 if (resolve_unique(parent, value, 0, LOGLINE(yin))) {
1104 goto error;
1105 }
1106 }
1107
1108 /* move to next token */
1109 value = vaux;
1110 while(isspace(*value)) {
1111 value++;
1112 }
1113 }
1114
1115 return EXIT_SUCCESS;
1116
1117error:
1118 return EXIT_FAILURE;
1119}
1120
Michal Vasko0d343d12015-08-24 14:57:36 +02001121/* logs directly
1122 *
Radek Krejcieb00f512015-07-01 16:44:58 +02001123 * type: 0 - min, 1 - max
1124 */
1125static int
Radek Krejcia52656e2015-08-05 13:41:50 +02001126deviate_minmax(struct lys_node *target, struct lyxml_elem *node, struct lys_deviate *d, int type)
Radek Krejcieb00f512015-07-01 16:44:58 +02001127{
1128 const char *value;
1129 char *endptr;
1130 unsigned long val;
1131 uint32_t *ui32val;
1132
1133 /* check target node type */
Radek Krejci76512572015-08-04 09:47:08 +02001134 if (target->nodetype == LYS_LEAFLIST) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001135 if (type) {
Radek Krejcib8048692015-08-05 13:36:34 +02001136 ui32val = &((struct lys_node_leaflist *)target)->max;
Radek Krejcieb00f512015-07-01 16:44:58 +02001137 } else {
Radek Krejcib8048692015-08-05 13:36:34 +02001138 ui32val = &((struct lys_node_leaflist *)target)->min;
Radek Krejcieb00f512015-07-01 16:44:58 +02001139 }
Radek Krejci76512572015-08-04 09:47:08 +02001140 } else if (target->nodetype == LYS_LIST) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001141 if (type) {
Radek Krejcib8048692015-08-05 13:36:34 +02001142 ui32val = &((struct lys_node_list *)target)->max;
Radek Krejcieb00f512015-07-01 16:44:58 +02001143 } else {
Radek Krejcib8048692015-08-05 13:36:34 +02001144 ui32val = &((struct lys_node_list *)target)->min;
Radek Krejcieb00f512015-07-01 16:44:58 +02001145 }
1146 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001147 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
1148 LOGVAL(LYE_SPEC, 0, "Target node does not allow \"%s\" property.", node->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001149 goto error;
1150 }
1151
1152 GETVAL(value, node, "value");
1153 while (isspace(value[0])) {
1154 value++;
1155 }
1156
1157 /* convert it to uint32_t */
1158 errno = 0;
1159 endptr = NULL;
1160 val = strtoul(value, &endptr, 10);
1161 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001162 LOGVAL(LYE_INARG, LOGLINE(node), value, node->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001163 goto error;
1164 }
1165 if (type) {
1166 d->max = (uint32_t)val;
1167 } else {
1168 d->min = (uint32_t)val;
1169 }
1170
1171 if (d->mod == LY_DEVIATE_ADD) {
1172 /* check that there is no current value */
1173 if (*ui32val) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001174 LOGVAL(LYE_INSTMT, LOGLINE(node), node->name);
1175 LOGVAL(LYE_SPEC, 0, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001176 goto error;
1177 }
1178 }
1179
1180 if (d->mod == LY_DEVIATE_DEL) {
1181 /* check values */
1182 if ((uint32_t)val != *ui32val) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001183 LOGVAL(LYE_INARG, LOGLINE(node), value, node->name);
1184 LOGVAL(LYE_SPEC, 0, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001185 goto error;
1186 }
1187 /* remove current min-elements value of the target */
1188 *ui32val = 0;
1189 } else { /* add (already checked) and replace */
1190 /* set new value specified in deviation */
1191 *ui32val = (uint32_t)val;
1192 }
1193
1194 return EXIT_SUCCESS;
1195
1196error:
1197
1198 return EXIT_FAILURE;
1199}
1200
Michal Vasko0d343d12015-08-24 14:57:36 +02001201/* logs directly */
Radek Krejcieb00f512015-07-01 16:44:58 +02001202static int
Michal Vasko88c29542015-11-27 14:57:53 +01001203fill_yin_deviation(struct lys_module *module, struct lyxml_elem *yin, struct lys_deviation *dev,
1204 struct unres_schema *unres)
Radek Krejcieb00f512015-07-01 16:44:58 +02001205{
1206 const char *value, **stritem;
1207 struct lyxml_elem *next, *child, *develem;
1208 int c_dev = 0, c_must, c_uniq;
1209 int f_min = 0; /* flags */
Michal Vaskob40b4512016-02-11 11:35:37 +01001210 int i, j, rc;
Radek Krejcia52656e2015-08-05 13:41:50 +02001211 struct lys_deviate *d = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001212 struct lys_node *node = NULL;
Radek Krejcib8048692015-08-05 13:36:34 +02001213 struct lys_node_choice *choice = NULL;
1214 struct lys_node_leaf *leaf = NULL;
1215 struct lys_node_list *list = NULL;
Radek Krejci1574a8d2015-08-03 14:16:52 +02001216 struct lys_type *t = NULL;
Radek Krejcie4c366b2015-07-02 10:11:31 +02001217 uint8_t *trg_must_size = NULL;
Radek Krejci1574a8d2015-08-03 14:16:52 +02001218 struct lys_restr **trg_must = NULL;
Radek Krejcieb00f512015-07-01 16:44:58 +02001219
1220 GETVAL(value, yin, "target-node");
Michal Vaskofba15262015-10-21 12:10:28 +02001221 dev->target_name = transform_schema2json(module, value, LOGLINE(yin));
Michal Vaskoa8b25952015-10-20 15:30:25 +02001222 if (!dev->target_name) {
1223 goto error;
1224 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001225
1226 /* resolve target node */
Michal Vasko1e62a092015-12-01 12:27:20 +01001227 rc = resolve_schema_nodeid(dev->target_name, NULL, module, LYS_AUGMENT, (const struct lys_node **)&dev->target);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001228 if (rc) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001229 LOGVAL(LYE_INARG, LOGLINE(yin), dev->target_name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001230 goto error;
1231 }
1232 if (dev->target->module == module) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001233 LOGVAL(LYE_SPEC, LOGLINE(yin), "Deviating own module is not allowed.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001234 goto error;
1235 }
1236 /* mark the target module as deviated */
1237 dev->target->module->deviated = 1;
1238
1239 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001240 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1241 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01001242 lyxml_free(module->ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001243 continue;
1244 }
1245
Radek Krejcieb00f512015-07-01 16:44:58 +02001246 if (!strcmp(child->name, "description")) {
1247 if (dev->dsc) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001248 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001249 goto error;
1250 }
1251 dev->dsc = read_yin_subnode(module->ctx, child, "text");
1252 if (!dev->dsc) {
1253 goto error;
1254 }
1255 } else if (!strcmp(child->name, "reference")) {
1256 if (dev->ref) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001257 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001258 goto error;
1259 }
1260 dev->ref = read_yin_subnode(module->ctx, child, "text");
1261 if (!dev->ref) {
1262 goto error;
1263 }
1264 } else if (!strcmp(child->name, "deviate")) {
1265 c_dev++;
1266
Michal Vasko345da0a2015-12-02 10:35:55 +01001267 /* skip lyxml_free() at the end of the loop, node will be
Radek Krejcieb00f512015-07-01 16:44:58 +02001268 * further processed later
1269 */
1270 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02001271
Radek Krejcieb00f512015-07-01 16:44:58 +02001272 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001273 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001274 goto error;
1275 }
1276
Michal Vasko345da0a2015-12-02 10:35:55 +01001277 lyxml_free(module->ctx, child);
Radek Krejcieb00f512015-07-01 16:44:58 +02001278 }
1279
1280 if (c_dev) {
1281 dev->deviate = calloc(c_dev, sizeof *dev->deviate);
Michal Vasko253035f2015-12-17 16:58:13 +01001282 if (!dev->deviate) {
1283 LOGMEM;
1284 goto error;
1285 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001286 }
1287
1288 LY_TREE_FOR(yin->child, develem) {
1289 /* init */
1290 f_min = 0;
1291 c_must = 0;
1292 c_uniq = 0;
1293
1294 /* get deviation type */
1295 GETVAL(value, develem, "value");
1296 if (!strcmp(value, "not-supported")) {
1297 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_NO;
1298 /* no property expected in this case */
1299 if (develem->child) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001300 LOGVAL(LYE_INSTMT, LOGLINE(develem->child), develem->child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001301 goto error;
1302 }
1303
Radek Krejci5b917642015-07-02 09:03:13 +02001304 /* and neither any other deviate statement is expected,
1305 * not-supported deviation must be the only deviation of the target
1306 */
1307 if (dev->deviate_size || develem->next) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001308 LOGVAL(LYE_INARG, LOGLINE(develem), value, develem->name);
1309 LOGVAL(LYE_SPEC, 0, "\"not-supported\" deviation cannot be combined with any other deviation.");
Radek Krejci5b917642015-07-02 09:03:13 +02001310 goto error;
1311 }
1312
1313
Radek Krejcieb00f512015-07-01 16:44:58 +02001314 /* remove target node */
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001315 lys_node_free(dev->target, NULL);
Radek Krejci5b917642015-07-02 09:03:13 +02001316 dev->target = NULL;
Radek Krejcieb00f512015-07-01 16:44:58 +02001317
Radek Krejci5b917642015-07-02 09:03:13 +02001318 dev->deviate_size = 1;
1319 return EXIT_SUCCESS;
Radek Krejcieb00f512015-07-01 16:44:58 +02001320 } else if (!strcmp(value, "add")) {
1321 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_ADD;
1322 } else if (!strcmp(value, "replace")) {
1323 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_RPL;
1324 } else if (!strcmp(value, "delete")) {
1325 dev->deviate[dev->deviate_size].mod = LY_DEVIATE_DEL;
1326 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001327 LOGVAL(LYE_INARG, LOGLINE(develem), value, develem->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001328 goto error;
1329 }
1330 d = &dev->deviate[dev->deviate_size];
1331
1332 /* process deviation properties */
1333 LY_TREE_FOR_SAFE(develem->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001334 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1335 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01001336 lyxml_free(module->ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001337 continue;
1338 }
1339
Radek Krejcieb00f512015-07-01 16:44:58 +02001340 if (!strcmp(child->name, "config")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001341 if (d->flags & LYS_CONFIG_MASK) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001342 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001343 goto error;
1344 }
1345
1346 /* for we deviate from RFC 6020 and allow config property even it is/is not
1347 * specified in the target explicitly since config property inherits. So we expect
1348 * that config is specified in every node. But for delete, we check that the value
1349 * is the same as here in deviation
1350 */
1351 GETVAL(value, child, "value");
1352 if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001353 d->flags |= LYS_CONFIG_R;
Radek Krejcieb00f512015-07-01 16:44:58 +02001354 } else if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001355 d->flags |= LYS_CONFIG_W;
Radek Krejcieb00f512015-07-01 16:44:58 +02001356 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001357 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001358 goto error;
1359 }
1360
1361 if (d->mod == LY_DEVIATE_DEL) {
1362 /* check values */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001363 if ((d->flags & LYS_CONFIG_MASK) != (dev->target->flags & LYS_CONFIG_MASK)) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001364 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
1365 LOGVAL(LYE_SPEC, 0, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001366 goto error;
1367 }
1368 /* remove current config value of the target ... */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001369 dev->target->flags &= ~LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001370
1371 /* ... and inherit config value from the target's parent */
1372 if (dev->target->parent) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001373 dev->target->flags |= dev->target->parent->flags & LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001374 } else {
1375 /* default config is true */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001376 dev->target->flags |= LYS_CONFIG_W;
Radek Krejcieb00f512015-07-01 16:44:58 +02001377 }
1378 } else { /* add and replace are the same in this case */
1379 /* remove current config value of the target ... */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001380 dev->target->flags &= ~LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001381
1382 /* ... and replace it with the value specified in deviation */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001383 dev->target->flags |= d->flags & LYS_CONFIG_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001384 }
1385 } else if (!strcmp(child->name, "default")) {
1386 if (d->dflt) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001387 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001388 goto error;
1389 }
1390 GETVAL(value, child, "value");
1391 d->dflt = lydict_insert(module->ctx, value, 0);
1392
Radek Krejci76512572015-08-04 09:47:08 +02001393 if (dev->target->nodetype == LYS_CHOICE) {
Radek Krejcib8048692015-08-05 13:36:34 +02001394 choice = (struct lys_node_choice *)dev->target;
Radek Krejcieb00f512015-07-01 16:44:58 +02001395
1396 if (d->mod == LY_DEVIATE_ADD) {
1397 /* check that there is no current value */
1398 if (choice->dflt) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001399 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1400 LOGVAL(LYE_SPEC, 0, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001401 goto error;
1402 }
1403 }
1404
Michal Vasko1e62a092015-12-01 12:27:20 +01001405 rc = resolve_schema_nodeid(d->dflt, choice->child, choice->module, LYS_CHOICE,
1406 (const struct lys_node **)&node);
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001407 if (rc) {
1408 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
1409 goto error;
1410 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001411 if (d->mod == LY_DEVIATE_DEL) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001412 if (!choice->dflt || choice->dflt != node) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001413 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
1414 LOGVAL(LYE_SPEC, 0, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001415 goto error;
1416 }
1417 } else { /* add (already checked) and replace */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001418 choice->dflt = node;
Radek Krejcieb00f512015-07-01 16:44:58 +02001419 if (!choice->dflt) {
1420 /* default branch not found */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001421 LOGVAL(LYE_INARG, LOGLINE(yin), value, "default");
Radek Krejcieb00f512015-07-01 16:44:58 +02001422 goto error;
1423 }
1424 }
Radek Krejci76512572015-08-04 09:47:08 +02001425 } else if (dev->target->nodetype == LYS_LEAF) {
Radek Krejcib8048692015-08-05 13:36:34 +02001426 leaf = (struct lys_node_leaf *)dev->target;
Radek Krejcieb00f512015-07-01 16:44:58 +02001427
1428 if (d->mod == LY_DEVIATE_ADD) {
1429 /* check that there is no current value */
1430 if (leaf->dflt) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001431 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1432 LOGVAL(LYE_SPEC, 0, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001433 goto error;
1434 }
1435 }
1436
1437 if (d->mod == LY_DEVIATE_DEL) {
1438 if (!leaf->dflt || leaf->dflt != d->dflt) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001439 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
1440 LOGVAL(LYE_SPEC, 0, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001441 goto error;
1442 }
1443 /* remove value */
1444 lydict_remove(leaf->module->ctx, leaf->dflt);
1445 leaf->dflt = NULL;
1446 } else { /* add (already checked) and replace */
1447 /* remove value */
1448 lydict_remove(leaf->module->ctx, leaf->dflt);
1449
1450 /* set new value */
1451 leaf->dflt = lydict_insert(leaf->module->ctx, d->dflt, 0);
1452 }
1453 } else {
1454 /* invalid target for default value */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001455 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1456 LOGVAL(LYE_SPEC, 0, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001457 goto error;
1458 }
1459 } else if (!strcmp(child->name, "mandatory")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001460 if (d->flags & LYS_MAND_MASK) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001461 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001462 goto error;
1463 }
1464
1465 /* check target node type */
Radek Krejci76512572015-08-04 09:47:08 +02001466 if (!(dev->target->nodetype &= (LYS_LEAF | LYS_CHOICE | LYS_ANYXML))) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001467 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1468 LOGVAL(LYE_SPEC, 0, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001469 goto error;
1470 }
1471
1472 GETVAL(value, child, "value");
1473 if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001474 d->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02001475 } else if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001476 d->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02001477 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001478 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001479 goto error;
1480 }
1481
1482 if (d->mod == LY_DEVIATE_ADD) {
1483 /* check that there is no current value */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001484 if (dev->target->flags & LYS_MAND_MASK) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001485 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1486 LOGVAL(LYE_SPEC, 0, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001487 goto error;
1488 }
1489 }
1490
1491 if (d->mod == LY_DEVIATE_DEL) {
1492 /* check values */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001493 if ((d->flags & LYS_MAND_MASK) != (dev->target->flags & LYS_MAND_MASK)) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001494 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
1495 LOGVAL(LYE_SPEC, 0, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001496 goto error;
1497 }
1498 /* remove current mandatory value of the target */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001499 dev->target->flags &= ~LYS_MAND_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001500 } else { /* add (already checked) and replace */
1501 /* remove current mandatory value of the target ... */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001502 dev->target->flags &= ~LYS_MAND_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001503
1504 /* ... and replace it with the value specified in deviation */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001505 dev->target->flags |= d->flags & LYS_MAND_MASK;
Radek Krejcieb00f512015-07-01 16:44:58 +02001506 }
1507 } else if (!strcmp(child->name, "min-elements")) {
1508 if (f_min) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001509 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001510 goto error;
1511 }
1512 f_min = 1;
1513
1514 if (deviate_minmax(dev->target, child, d, 0)) {
1515 goto error;
1516 }
1517 } else if (!strcmp(child->name, "max-elements")) {
1518 if (d->max) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001519 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001520 goto error;
1521 }
1522
1523 if (deviate_minmax(dev->target, child, d, 1)) {
1524 goto error;
1525 }
1526 } else if (!strcmp(child->name, "must")) {
1527 c_must++;
Michal Vasko345da0a2015-12-02 10:35:55 +01001528 /* skip lyxml_free() at the end of the loop, this node will be processed later */
Radek Krejcieb00f512015-07-01 16:44:58 +02001529 continue;
1530 } else if (!strcmp(child->name, "type")) {
1531 if (d->type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001532 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001533 goto error;
1534 }
1535
1536 /* check target node type */
Radek Krejci76512572015-08-04 09:47:08 +02001537 if (dev->target->nodetype == LYS_LEAF) {
Radek Krejcib8048692015-08-05 13:36:34 +02001538 t = &((struct lys_node_leaf *)dev->target)->type;
Radek Krejci76512572015-08-04 09:47:08 +02001539 } else if (dev->target->nodetype == LYS_LEAFLIST) {
Radek Krejcib8048692015-08-05 13:36:34 +02001540 t = &((struct lys_node_leaflist *)dev->target)->type;
Radek Krejcieb00f512015-07-01 16:44:58 +02001541 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001542 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1543 LOGVAL(LYE_SPEC, 0, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001544 goto error;
1545 }
1546
1547 if (d->mod == LY_DEVIATE_ADD) {
1548 /* not allowed, type is always present at the target */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001549 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1550 LOGVAL(LYE_SPEC, 0, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001551 goto error;
1552 } else if (d->mod == LY_DEVIATE_DEL) {
1553 /* not allowed, type cannot be deleted from the target */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001554 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
1555 LOGVAL(LYE_SPEC, 0, "Deleteing type from the target is not allowed.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001556 goto error;
1557 }
1558
1559 /* replace */
1560 /* remove current units value of the target ... */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001561 lys_type_free(dev->target->module->ctx, t);
Radek Krejcieb00f512015-07-01 16:44:58 +02001562
1563 /* ... and replace it with the value specified in deviation */
Michal Vasko88c29542015-11-27 14:57:53 +01001564 /* HACK for unres */
1565 t->der = (struct lys_tpdf *)child;
1566 if (unres_schema_add_node(module, unres, t, UNRES_TYPE_DER, dev->target, LOGLINE(child))) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001567 goto error;
1568 }
1569 d->type = t;
1570 } else if (!strcmp(child->name, "unique")) {
1571 c_uniq++;
Michal Vasko345da0a2015-12-02 10:35:55 +01001572 /* skip lyxml_free() at the end of the loop, this node will be processed later */
Radek Krejcieb00f512015-07-01 16:44:58 +02001573 continue;
1574 } else if (!strcmp(child->name, "units")) {
1575 if (d->units) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001576 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001577 goto error;
1578 }
1579
1580 /* check target node type */
Radek Krejci76512572015-08-04 09:47:08 +02001581 if (dev->target->nodetype == LYS_LEAFLIST) {
Radek Krejcib8048692015-08-05 13:36:34 +02001582 stritem = &((struct lys_node_leaflist *)dev->target)->units;
Radek Krejci76512572015-08-04 09:47:08 +02001583 } else if (dev->target->nodetype == LYS_LEAF) {
Radek Krejcib8048692015-08-05 13:36:34 +02001584 stritem = &((struct lys_node_leaf *)dev->target)->units;
Radek Krejcieb00f512015-07-01 16:44:58 +02001585 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001586 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1587 LOGVAL(LYE_SPEC, 0, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001588 goto error;
1589 }
1590
1591 /* get units value */
1592 GETVAL(value, child, "name");
1593 d->units = lydict_insert(module->ctx, value, 0);
1594
1595 /* apply to target */
1596 if (d->mod == LY_DEVIATE_ADD) {
1597 /* check that there is no current value */
1598 if (*stritem) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001599 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1600 LOGVAL(LYE_SPEC, 0, "Adding property that already exists.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001601 goto error;
1602 }
1603 }
1604
1605 if (d->mod == LY_DEVIATE_DEL) {
1606 /* check values */
1607 if (*stritem != d->units) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001608 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
1609 LOGVAL(LYE_SPEC, 0, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001610 goto error;
1611 }
1612 /* remove current units value of the target */
1613 lydict_remove(dev->target->module->ctx, *stritem);
1614 } else { /* add (already checked) and replace */
1615 /* remove current units value of the target ... */
1616 lydict_remove(dev->target->module->ctx, *stritem);
1617
1618 /* ... and replace it with the value specified in deviation */
1619 *stritem = lydict_insert(module->ctx, value, 0);
1620 }
1621 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001622 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001623 goto error;
1624 }
1625
Michal Vasko88c29542015-11-27 14:57:53 +01001626 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejcieb00f512015-07-01 16:44:58 +02001627 }
1628
1629 if (c_must) {
1630 /* check target node type */
1631 switch (dev->target->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02001632 case LYS_LEAF:
Radek Krejcib8048692015-08-05 13:36:34 +02001633 trg_must = &((struct lys_node_leaf *)dev->target)->must;
1634 trg_must_size = &((struct lys_node_leaf *)dev->target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02001635 break;
Radek Krejci76512572015-08-04 09:47:08 +02001636 case LYS_CONTAINER:
Radek Krejcib8048692015-08-05 13:36:34 +02001637 trg_must = &((struct lys_node_container *)dev->target)->must;
1638 trg_must_size = &((struct lys_node_container *)dev->target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02001639 break;
Radek Krejci76512572015-08-04 09:47:08 +02001640 case LYS_LEAFLIST:
Radek Krejcib8048692015-08-05 13:36:34 +02001641 trg_must = &((struct lys_node_leaflist *)dev->target)->must;
1642 trg_must_size = &((struct lys_node_leaflist *)dev->target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02001643 break;
Radek Krejci76512572015-08-04 09:47:08 +02001644 case LYS_LIST:
Radek Krejcib8048692015-08-05 13:36:34 +02001645 trg_must = &((struct lys_node_list *)dev->target)->must;
1646 trg_must_size = &((struct lys_node_list *)dev->target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02001647 break;
Radek Krejci76512572015-08-04 09:47:08 +02001648 case LYS_ANYXML:
Radek Krejcib8048692015-08-05 13:36:34 +02001649 trg_must = &((struct lys_node_anyxml *)dev->target)->must;
1650 trg_must_size = &((struct lys_node_anyxml *)dev->target)->must_size;
Radek Krejcieb00f512015-07-01 16:44:58 +02001651 break;
1652 default:
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001653 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1654 LOGVAL(LYE_SPEC, 0, "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_RPL) {
1659 /* remove target's musts and allocate new array for it */
1660 if (!*trg_must) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001661 LOGVAL(LYE_INARG, LOGLINE(develem), "replace", "deviate");
1662 LOGVAL(LYE_SPEC, 0, "Property \"must\" to replace does not exists in target.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001663 goto error;
1664 }
1665
1666 for (i = 0; i < list->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001667 lys_restr_free(dev->target->module->ctx, &(*trg_must[i]));
Radek Krejcieb00f512015-07-01 16:44:58 +02001668 }
1669 free(*trg_must);
1670 *trg_must = d->must = calloc(c_must, sizeof *d->must);
1671 d->must_size = c_must;
1672 *trg_must_size = 0;
1673 } else if (d->mod == LY_DEVIATE_ADD) {
1674 /* reallocate the must array of the target */
Michal Vasko253035f2015-12-17 16:58:13 +01001675 d->must = ly_realloc(*trg_must, (c_must + *trg_must_size) * sizeof *d->must);
Radek Krejcieb00f512015-07-01 16:44:58 +02001676 *trg_must = d->must;
Michal Vasko979ad5b2015-10-23 10:12:55 +02001677 d->must = &((*trg_must)[*trg_must_size]);
Radek Krejcieb00f512015-07-01 16:44:58 +02001678 d->must_size = c_must;
1679 } else { /* LY_DEVIATE_DEL */
1680 d->must = calloc(c_must, sizeof *d->must);
1681 }
Michal Vasko253035f2015-12-17 16:58:13 +01001682 if (!d->must) {
1683 LOGMEM;
1684 goto error;
1685 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001686 }
1687 if (c_uniq) {
1688 /* check target node type */
Radek Krejci76512572015-08-04 09:47:08 +02001689 if (dev->target->nodetype != LYS_LIST) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001690 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
1691 LOGVAL(LYE_SPEC, 0, "Target node does not allow \"%s\" property.", child->name);
Radek Krejcieb00f512015-07-01 16:44:58 +02001692 goto error;
1693 }
1694
Radek Krejcib8048692015-08-05 13:36:34 +02001695 list = (struct lys_node_list *)dev->target;
Radek Krejcieb00f512015-07-01 16:44:58 +02001696 if (d->mod == LY_DEVIATE_RPL) {
1697 /* remove target's unique and allocate new array for it */
1698 if (!list->unique) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001699 LOGVAL(LYE_INARG, LOGLINE(develem), "replace", "deviate");
1700 LOGVAL(LYE_SPEC, 0, "Property \"unique\" to replace does not exists in target.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001701 goto error;
1702 }
1703
1704 for (i = 0; i < list->unique_size; i++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001705 for (j = 0; j < list->unique[i].expr_size; j++) {
1706 lydict_remove(list->module->ctx, list->unique[i].expr[j]);
1707 }
1708 free(list->unique[i].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001709 }
1710 free(list->unique);
1711 list->unique = d->unique = calloc(c_uniq, sizeof *d->unique);
1712 d->unique_size = c_uniq;
1713 list->unique_size = 0;
1714 } else if (d->mod == LY_DEVIATE_ADD) {
1715 /* reallocate the unique array of the target */
Michal Vasko253035f2015-12-17 16:58:13 +01001716 d->unique = ly_realloc(list->unique, (c_uniq + list->unique_size) * sizeof *d->unique);
Radek Krejcieb00f512015-07-01 16:44:58 +02001717 list->unique = d->unique;
1718 d->unique = &list->unique[list->unique_size];
1719 d->unique_size = c_uniq;
1720 } else { /* LY_DEVIATE_DEL */
1721 d->unique = calloc(c_uniq, sizeof *d->unique);
1722 }
Michal Vasko253035f2015-12-17 16:58:13 +01001723 if (!d->unique) {
1724 LOGMEM;
1725 goto error;
1726 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001727 }
1728
1729 /* process deviation properties with 0..n cardinality */
Radek Krejci73adb602015-07-02 18:07:40 +02001730 LY_TREE_FOR(develem->child, child) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001731 if (!strcmp(child->name, "must")) {
1732 if (d->mod == LY_DEVIATE_DEL) {
1733 if (fill_yin_must(module, child, &d->must[d->must_size])) {
1734 goto error;
1735 }
1736
1737 /* find must to delete, we are ok with just matching conditions */
1738 for (i = 0; i < *trg_must_size; i++) {
1739 if (d->must[d->must_size].expr == (*trg_must)[i].expr) {
1740 /* we have a match, free the must structure ... */
Michal Vasko5e7d9ca2015-10-23 10:37:28 +02001741 lys_restr_free(dev->target->module->ctx, &((*trg_must)[i]));
Radek Krejcieb00f512015-07-01 16:44:58 +02001742 /* ... and maintain the array */
1743 (*trg_must_size)--;
1744 if (i != *trg_must_size) {
1745 (*trg_must)[i].expr = (*trg_must)[*trg_must_size].expr;
1746 (*trg_must)[i].dsc = (*trg_must)[*trg_must_size].dsc;
1747 (*trg_must)[i].ref = (*trg_must)[*trg_must_size].ref;
1748 (*trg_must)[i].eapptag = (*trg_must)[*trg_must_size].eapptag;
1749 (*trg_must)[i].emsg = (*trg_must)[*trg_must_size].emsg;
1750 }
1751 if (!(*trg_must_size)) {
1752 free(*trg_must);
1753 *trg_must = NULL;
1754 } else {
1755 (*trg_must)[*trg_must_size].expr = NULL;
1756 (*trg_must)[*trg_must_size].dsc = NULL;
1757 (*trg_must)[*trg_must_size].ref = NULL;
1758 (*trg_must)[*trg_must_size].eapptag = NULL;
1759 (*trg_must)[*trg_must_size].emsg = NULL;
1760 }
1761
1762 i = -1; /* set match flag */
1763 break;
1764 }
1765 }
1766 d->must_size++;
1767 if (i != -1) {
1768 /* no match found */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001769 LOGVAL(LYE_INARG, LOGLINE(child), d->must[d->must_size - 1].expr, child->name);
1770 LOGVAL(LYE_SPEC, 0, "Value does not match any must from the target.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001771 goto error;
1772 }
1773 } else { /* replace or add */
1774 if (fill_yin_must(dev->target->module, child, &((*trg_must)[*trg_must_size]))) {
1775 goto error;
1776 }
1777 (*trg_must_size)++;
1778 }
1779 } else if (!strcmp(child->name, "unique")) {
1780 if (d->mod == LY_DEVIATE_DEL) {
Radek Krejci581ce772015-11-10 17:22:40 +01001781 if (fill_yin_unique(module, dev->target, child, &d->unique[d->unique_size], NULL)) {
1782 d->unique_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02001783 goto error;
1784 }
1785
1786 /* find unique structures to delete */
1787 for (i = 0; i < list->unique_size; i++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001788 if (list->unique[i].expr_size != d->unique[d->unique_size].expr_size) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001789 continue;
1790 }
1791
Radek Krejci581ce772015-11-10 17:22:40 +01001792 for (j = 0; j < d->unique[d->unique_size].expr_size; j++) {
1793 if (list->unique[i].expr[j] != d->unique[d->unique_size].expr[j]) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001794 break;
1795 }
1796 }
1797
Radek Krejci581ce772015-11-10 17:22:40 +01001798 if (j == d->unique[d->unique_size].expr_size) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001799 /* we have a match, free the unique structure ... */
Radek Krejci581ce772015-11-10 17:22:40 +01001800 for (j = 0; j < list->unique[i].expr_size; j++) {
1801 lydict_remove(list->module->ctx, list->unique[i].expr[j]);
1802 }
1803 free(list->unique[i].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001804 /* ... and maintain the array */
1805 list->unique_size--;
1806 if (i != list->unique_size) {
Radek Krejci581ce772015-11-10 17:22:40 +01001807 list->unique[i].expr_size = list->unique[list->unique_size].expr_size;
1808 list->unique[i].expr = list->unique[list->unique_size].expr;
Radek Krejcieb00f512015-07-01 16:44:58 +02001809 }
1810
1811 if (!list->unique_size) {
1812 free(list->unique);
1813 list->unique = NULL;
1814 } else {
Radek Krejci581ce772015-11-10 17:22:40 +01001815 list->unique[list->unique_size].expr_size = 0;
1816 list->unique[list->unique_size].expr = NULL;
Radek Krejcieb00f512015-07-01 16:44:58 +02001817 }
1818
1819 i = -1; /* set match flag */
1820 break;
1821 }
1822 }
1823
1824 d->unique_size++;
1825 if (i != -1) {
1826 /* no match found */
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001827 LOGVAL(LYE_INARG, LOGLINE(child), lyxml_get_attr(child, "tag", NULL), child->name);
1828 LOGVAL(LYE_SPEC, 0, "Value differs from the target being deleted.");
Radek Krejcieb00f512015-07-01 16:44:58 +02001829 goto error;
1830 }
1831 } else { /* replace or add */
Radek Krejci7d74ebc2015-12-10 16:05:02 +01001832 i = fill_yin_unique(module, dev->target, child, &list->unique[list->unique_size], NULL);
1833 list->unique_size++;
1834 if (i) {
Radek Krejcieb00f512015-07-01 16:44:58 +02001835 goto error;
1836 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001837 }
1838 }
Radek Krejcieb00f512015-07-01 16:44:58 +02001839 }
Radek Krejci5b917642015-07-02 09:03:13 +02001840
1841 dev->deviate_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02001842 }
1843
Radek Krejcieb00f512015-07-01 16:44:58 +02001844 return EXIT_SUCCESS;
1845
1846error:
1847
Radek Krejcieb00f512015-07-01 16:44:58 +02001848 return EXIT_FAILURE;
1849}
1850
Michal Vasko0d343d12015-08-24 14:57:36 +02001851/* logs directly */
Radek Krejcieb00f512015-07-01 16:44:58 +02001852static int
Radek Krejcib8048692015-08-05 13:36:34 +02001853fill_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 +02001854 struct unres_schema *unres)
Radek Krejci106efc02015-06-10 14:36:27 +02001855{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001856 const char *value;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001857 struct lyxml_elem *child, *next;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001858 struct lys_node *node;
Michal Vasko2d851a92015-10-20 16:16:36 +02001859 int c = 0, ret;
Radek Krejci106efc02015-06-10 14:36:27 +02001860
Michal Vasko591e0b22015-08-13 13:53:43 +02001861 aug->nodetype = LYS_AUGMENT;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001862 GETVAL(value, yin, "target-node");
Michal Vaskofba15262015-10-21 12:10:28 +02001863 aug->target_name = transform_schema2json(module, value, LOGLINE(yin));
Michal Vasko488c19e2015-10-20 15:21:00 +02001864 if (!aug->target_name) {
1865 goto error;
1866 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001867 aug->parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001868
Michal Vasko1d87a922015-08-21 12:57:16 +02001869 if (read_yin_common(module, NULL, (struct lys_node *)aug, yin, OPT_MODULE | OPT_NACMEXT)) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02001870 goto error;
1871 }
1872
1873 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02001874 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
1875 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01001876 lyxml_free(module->ctx, child);
Radek Krejci0d70c372015-07-02 16:23:10 +02001877 continue;
1878 }
1879
Radek Krejci3cf9e222015-06-18 11:37:50 +02001880 if (!strcmp(child->name, "if-feature")) {
1881 c++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001882 continue;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001883 } else if (!strcmp(child->name, "when")) {
1884 if (aug->when) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001885 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001886 goto error;
1887 }
1888
1889 aug->when = read_yin_when(module, child);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001890 if (!aug->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01001891 lyxml_free(module->ctx, child);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001892 goto error;
1893 }
Michal Vaskofcdac172015-10-07 09:35:05 +02001894 if (lyxp_syntax_check(aug->when->cond, LOGLINE(child))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001895 goto error;
1896 }
Michal Vasko345da0a2015-12-02 10:35:55 +01001897 lyxml_free(module->ctx, child);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001898 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02001899
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001900 /* check allowed data sub-statements */
1901 } else if (!strcmp(child->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001902 node = read_yin_container(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001903 } else if (!strcmp(child->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001904 node = read_yin_leaflist(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001905 } else if (!strcmp(child->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001906 node = read_yin_leaf(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001907 } else if (!strcmp(child->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001908 node = read_yin_list(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001909 } else if (!strcmp(child->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001910 node = read_yin_uses(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001911 } else if (!strcmp(child->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001912 node = read_yin_case(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001913 } else if (!strcmp(child->name, "case")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001914 node = read_yin_case(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001915 } else if (!strcmp(child->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001916 node = read_yin_anyxml(module, (struct lys_node *)aug, child, 0, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001917 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001918 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001919 goto error;
1920 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001921
Radek Krejci1d82ef62015-08-07 14:44:40 +02001922 if (!node) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001923 goto error;
1924 }
1925
1926 /* check for mandatory nodes - if the target node is in another module
1927 * the added nodes cannot be mandatory
1928 */
Radek Krejcic6556022016-01-27 15:16:45 +01001929 if ((!parent || (parent->nodetype != LYS_USES)) && lyp_check_mandatory(node)) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001930 LOGVAL(LYE_SPEC, LOGLINE(child), "When augmenting data in another module, mandatory statement is not allowed.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001931 goto error;
1932 }
1933
Radek Krejci1d82ef62015-08-07 14:44:40 +02001934 node = NULL;
Michal Vasko345da0a2015-12-02 10:35:55 +01001935 lyxml_free(module->ctx, child);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001936 }
1937
1938 if (c) {
1939 aug->features = calloc(c, sizeof *aug->features);
Michal Vasko253035f2015-12-17 16:58:13 +01001940 if (!aug->features) {
1941 LOGMEM;
1942 goto error;
1943 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02001944 }
1945
1946 LY_TREE_FOR_SAFE(yin->child, next, child) {
1947 if (!strcmp(child->name, "if-feature")) {
1948 GETVAL(value, child, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02001949 if (!(value = transform_schema2json(module, value, LOGLINE(child)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02001950 goto error;
1951 }
Radek Krejcicf509982015-12-15 09:22:44 +01001952 /* hack - store pointer to the parent node for later status check */
1953 aug->features[aug->features_size] = (struct lys_feature *)aug;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01001954 ret = unres_schema_add_str(module, unres, &aug->features[aug->features_size], UNRES_IFFEAT, value,
Michal Vasko2d851a92015-10-20 16:16:36 +02001955 LOGLINE(child));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01001956 aug->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02001957 lydict_remove(module->ctx, value);
1958 if (ret == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02001959 goto error;
1960 }
Michal Vasko345da0a2015-12-02 10:35:55 +01001961 lyxml_free(module->ctx, child);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001962 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02001963 }
1964
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001965 /* aug->child points to the parsed nodes, they must now be
Michal Vasko49291b32015-08-06 09:49:41 +02001966 * connected to the tree and adjusted (if possible right now).
1967 * However, if this is augment in a uses, it gets resolved
1968 * when the uses does and cannot be resolved now for sure
1969 * (the grouping was not yet copied into uses).
1970 */
1971 if (!parent || (parent->nodetype != LYS_USES)) {
Michal Vasko1d87a922015-08-21 12:57:16 +02001972 if (resolve_augment(aug, aug->child)) {
Michal Vasko4adc10f2015-08-11 15:26:17 +02001973 LOGVAL(LYE_INRESOLV, LOGLINE(yin), "augment", aug->target_name);
1974 goto error;
1975 }
Michal Vasko49291b32015-08-06 09:49:41 +02001976 }
Radek Krejci106efc02015-06-10 14:36:27 +02001977
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001978 return EXIT_SUCCESS;
Radek Krejci106efc02015-06-10 14:36:27 +02001979
1980error:
1981
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001982 return EXIT_FAILURE;
Radek Krejci106efc02015-06-10 14:36:27 +02001983}
1984
Michal Vasko0d343d12015-08-24 14:57:36 +02001985/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001986static int
Michal Vasko0d204592015-10-07 09:50:04 +02001987fill_yin_refine(struct lys_module *module, struct lyxml_elem *yin, struct lys_refine *rfn)
Radek Krejci3bde87f2015-06-05 16:51:58 +02001988{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001989 struct lyxml_elem *sub, *next;
1990 const char *value;
1991 char *endptr;
1992 int f_mand = 0, f_min = 0, f_max = 0;
1993 int c_must = 0;
1994 int r;
1995 unsigned long int val;
Radek Krejci3bde87f2015-06-05 16:51:58 +02001996
Radek Krejci76512572015-08-04 09:47:08 +02001997 if (read_yin_common(module, NULL, (struct lys_node *)rfn, yin, OPT_CONFIG)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001998 goto error;
1999 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002000
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002001 GETVAL(value, yin, "target-node");
Michal Vaskofba15262015-10-21 12:10:28 +02002002 rfn->target_name = transform_schema2json(module, value, LOGLINE(yin));
Michal Vaskoa8b25952015-10-20 15:30:25 +02002003 if (!rfn->target_name) {
2004 goto error;
2005 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002006
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002007 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002008 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
2009 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002010 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02002011 continue;
2012 }
2013
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002014 /* limited applicability */
2015 if (!strcmp(sub->name, "default")) {
2016 /* leaf or choice */
2017 if (rfn->mod.dflt) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002018 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002019 goto error;
2020 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002021
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002022 /* check possibility of statements combination */
2023 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002024 rfn->target_type &= (LYS_LEAF | LYS_CHOICE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002025 if (!rfn->target_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002026 LOGVAL(LYE_SPEC, LOGLINE(sub), "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002027 goto error;
2028 }
2029 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002030 rfn->target_type = LYS_LEAF | LYS_CHOICE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002031 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002032
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002033 GETVAL(value, sub, "value");
2034 rfn->mod.dflt = lydict_insert(module->ctx, value, strlen(value));
2035 } else if (!strcmp(sub->name, "mandatory")) {
2036 /* leaf, choice or anyxml */
2037 if (f_mand) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002038 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002039 goto error;
2040 }
2041 /* just checking the flags in leaf is not sufficient, we would allow
2042 * multiple mandatory statements with the "false" value
2043 */
2044 f_mand = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002045
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002046 /* check possibility of statements combination */
2047 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002048 rfn->target_type &= (LYS_LEAF | LYS_CHOICE | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002049 if (!rfn->target_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002050 LOGVAL(LYE_SPEC, LOGLINE(sub), "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002051 goto error;
2052 }
2053 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002054 rfn->target_type = LYS_LEAF | LYS_CHOICE | LYS_ANYXML;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002055 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002056
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002057 GETVAL(value, sub, "value");
2058 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002059 rfn->flags |= LYS_MAND_TRUE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002060 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002061 rfn->flags |= LYS_MAND_FALSE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002062 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002063 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002064 goto error;
2065 }
2066 } else if (!strcmp(sub->name, "min-elements")) {
2067 /* list or leaf-list */
2068 if (f_min) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002069 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002070 goto error;
2071 }
2072 f_min = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002073
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002074 /* check possibility of statements combination */
2075 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002076 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002077 if (!rfn->target_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002078 LOGVAL(LYE_SPEC, LOGLINE(sub), "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002079 goto error;
2080 }
2081 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002082 rfn->target_type = LYS_LIST | LYS_LEAFLIST;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002083 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002084
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002085 GETVAL(value, sub, "value");
2086 while (isspace(value[0])) {
2087 value++;
2088 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002089
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002090 /* convert it to uint32_t */
2091 errno = 0;
2092 endptr = NULL;
2093 val = strtoul(value, &endptr, 10);
2094 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002095 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002096 goto error;
2097 }
2098 rfn->mod.list.min = (uint32_t) val;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002099
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002100 /* magic - bit 3 in flags means min set */
2101 rfn->flags |= 0x04;
2102 } else if (!strcmp(sub->name, "max-elements")) {
2103 /* list or leaf-list */
2104 if (f_max) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002105 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002106 goto error;
2107 }
2108 f_max = 1;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002109
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002110 /* check possibility of statements combination */
2111 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002112 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002113 if (!rfn->target_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002114 LOGVAL(LYE_SPEC, LOGLINE(sub), "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002115 goto error;
2116 }
2117 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002118 rfn->target_type = LYS_LIST | LYS_LEAFLIST;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002119 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002120
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002121 GETVAL(value, sub, "value");
2122 while (isspace(value[0])) {
2123 value++;
2124 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002125
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002126 /* convert it to uint32_t */
2127 errno = 0;
2128 endptr = NULL;
2129 val = strtoul(value, &endptr, 10);
2130 if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002131 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002132 goto error;
2133 }
2134 rfn->mod.list.max = (uint32_t) val;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002135
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002136 /* magic - bit 4 in flags means min set */
2137 rfn->flags |= 0x08;
2138 } else if (!strcmp(sub->name, "presence")) {
2139 /* container */
2140 if (rfn->mod.presence) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002141 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002142 goto error;
2143 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002144
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002145 /* check possibility of statements combination */
2146 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002147 rfn->target_type &= LYS_CONTAINER;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002148 if (!rfn->target_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002149 LOGVAL(LYE_SPEC, LOGLINE(sub), "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002150 goto error;
2151 }
2152 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002153 rfn->target_type = LYS_CONTAINER;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002154 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002155
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002156 GETVAL(value, sub, "value");
2157 rfn->mod.presence = lydict_insert(module->ctx, value, strlen(value));
2158 } else if (!strcmp(sub->name, "must")) {
2159 /* leaf-list, list, container or anyxml */
2160 /* check possibility of statements combination */
2161 if (rfn->target_type) {
Radek Krejci76512572015-08-04 09:47:08 +02002162 rfn->target_type &= (LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002163 if (!rfn->target_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002164 LOGVAL(LYE_SPEC, LOGLINE(sub), "invalid combination of refine substatements");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002165 goto error;
2166 }
2167 } else {
Radek Krejci76512572015-08-04 09:47:08 +02002168 rfn->target_type = LYS_LIST | LYS_LEAFLIST | LYS_CONTAINER | LYS_ANYXML;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002169 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002170
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002171 c_must++;
Radek Krejci41882de2015-07-02 16:34:58 +02002172 continue;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002173
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002174 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002175 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002176 goto error;
2177 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002178
Michal Vasko345da0a2015-12-02 10:35:55 +01002179 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002180 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002181
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002182 /* process nodes with cardinality of 0..n */
2183 if (c_must) {
2184 rfn->must = calloc(c_must, sizeof *rfn->must);
Michal Vasko253035f2015-12-17 16:58:13 +01002185 if (!rfn->must) {
2186 LOGMEM;
2187 goto error;
2188 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002189 }
Radek Krejci73adb602015-07-02 18:07:40 +02002190 LY_TREE_FOR(yin->child, sub) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002191 r = fill_yin_must(module, sub, &rfn->must[rfn->must_size]);
2192 rfn->must_size++;
Radek Krejci73adb602015-07-02 18:07:40 +02002193 if (r) {
2194 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002195 }
Michal Vasko0d204592015-10-07 09:50:04 +02002196 if (lyxp_syntax_check(rfn->must[rfn->must_size-1].expr, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002197 goto error;
2198 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002199 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02002200
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002201 return EXIT_SUCCESS;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002202
2203error:
2204
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002205 return EXIT_FAILURE;
Radek Krejci3bde87f2015-06-05 16:51:58 +02002206}
2207
Michal Vasko0d343d12015-08-24 14:57:36 +02002208/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002209static int
Radek Krejcib8048692015-08-05 13:36:34 +02002210fill_yin_import(struct lys_module *module, struct lyxml_elem *yin, struct lys_import *imp)
Radek Krejciefaeba32015-05-27 14:30:57 +02002211{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002212 struct lyxml_elem *child;
2213 const char *value;
Michal Vasko1b882eb2015-10-22 11:43:14 +02002214 int count;
Radek Krejciefaeba32015-05-27 14:30:57 +02002215
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002216 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002217 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
2218 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +02002219 continue;
2220 }
2221
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002222 if (!strcmp(child->name, "prefix")) {
2223 GETVAL(value, child, "value");
Radek Krejcic6556022016-01-27 15:16:45 +01002224 if (lyp_check_identifier(value, LY_IDENT_PREFIX, LOGLINE(child), module, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002225 goto error;
2226 }
2227 imp->prefix = lydict_insert(module->ctx, value, strlen(value));
2228 } else if (!strcmp(child->name, "revision-date")) {
2229 if (imp->rev[0]) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002230 LOGVAL(LYE_TOOMANY, LOGLINE(child), "revision-date", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002231 goto error;
2232 }
2233 GETVAL(value, child, "date");
Radek Krejcic6556022016-01-27 15:16:45 +01002234 if (lyp_check_date(value, LOGLINE(child))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002235 goto error;
2236 }
2237 memcpy(imp->rev, value, LY_REV_SIZE - 1);
2238 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002239 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002240 goto error;
2241 }
2242 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002243
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002244 /* check mandatory information */
2245 if (!imp->prefix) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002246 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "prefix", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002247 goto error;
2248 }
Radek Krejcice7fb782015-05-29 16:52:34 +02002249
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002250 GETVAL(value, yin, "module");
Michal Vasko1b882eb2015-10-22 11:43:14 +02002251
2252 /* check for circular import, store it if passed */
2253 if (!module->ctx->models.parsing) {
2254 count = 0;
2255 } else {
2256 for (count = 0; module->ctx->models.parsing[count]; ++count) {
2257 if (value == module->ctx->models.parsing[count]) {
2258 LOGERR(LY_EVALID, "Circular import dependency on the module \"%s\".", value);
2259 goto error;
2260 }
2261 }
2262 }
2263 ++count;
2264 module->ctx->models.parsing =
Michal Vasko253035f2015-12-17 16:58:13 +01002265 ly_realloc(module->ctx->models.parsing, (count + 1) * sizeof *module->ctx->models.parsing);
2266 if (!module->ctx->models.parsing) {
2267 LOGMEM;
2268 goto error;
2269 }
Michal Vasko1b882eb2015-10-22 11:43:14 +02002270 module->ctx->models.parsing[count - 1] = value;
2271 module->ctx->models.parsing[count] = NULL;
2272
2273 /* try to load the module */
Michal Vasko1e62a092015-12-01 12:27:20 +01002274 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 +02002275 if (!imp->module) {
Michal Vasko99b0aad2015-12-01 12:28:51 +01002276 /* whether to use a user callback is decided in the function */
2277 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 +02002278 }
2279
2280 /* remove the new module name now that its parsing is finished (even if failed) */
2281 if (module->ctx->models.parsing[count] || (module->ctx->models.parsing[count - 1] != value)) {
2282 LOGINT;
2283 }
2284 --count;
2285 if (count) {
2286 module->ctx->models.parsing[count] = NULL;
2287 } else {
2288 free(module->ctx->models.parsing);
2289 module->ctx->models.parsing = NULL;
2290 }
2291
2292 /* check the result */
2293 if (!imp->module) {
2294 LOGVAL(LYE_INARG, LOGLINE(yin), value, yin->name);
2295 LOGERR(LY_EVALID, "Importing \"%s\" module into \"%s\" failed.", value, module->name);
2296 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002297 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002298
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002299 return EXIT_SUCCESS;
Radek Krejcice7fb782015-05-29 16:52:34 +02002300
2301error:
2302
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002303 return EXIT_FAILURE;
Radek Krejciefaeba32015-05-27 14:30:57 +02002304}
2305
Michal Vasko0d343d12015-08-24 14:57:36 +02002306/* logs directly */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002307static int
Radek Krejcic071c542016-01-27 14:57:51 +01002308fill_yin_include(struct lys_module *module, struct lyxml_elem *yin, struct lys_include *inc, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02002309{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002310 struct lyxml_elem *child;
2311 const char *value;
Michal Vasko99b0aad2015-12-01 12:28:51 +01002312 char *module_data;
2313 void (*module_data_free)(char *module_data) = NULL;
2314 LYS_INFORMAT format = LYS_IN_UNKNOWN;
Michal Vasko1b882eb2015-10-22 11:43:14 +02002315 int count;
Radek Krejciefaeba32015-05-27 14:30:57 +02002316
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002317 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002318 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
2319 /* garbage */
2320 continue;
2321 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002322 if (!strcmp(child->name, "revision-date")) {
2323 if (inc->rev[0]) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002324 LOGVAL(LYE_TOOMANY, LOGLINE(child), "revision-date", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002325 goto error;
2326 }
2327 GETVAL(value, child, "date");
Radek Krejcic6556022016-01-27 15:16:45 +01002328 if (lyp_check_date(value, LOGLINE(child))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002329 goto error;
2330 }
2331 memcpy(inc->rev, value, LY_REV_SIZE - 1);
2332 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002333 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002334 goto error;
2335 }
2336 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002337
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002338 GETVAL(value, yin, "module");
Michal Vasko1b882eb2015-10-22 11:43:14 +02002339
2340 /* check for circular include, store it if passed */
2341 if (!module->ctx->models.parsing) {
2342 count = 0;
2343 } else {
2344 for (count = 0; module->ctx->models.parsing[count]; ++count) {
2345 if (value == module->ctx->models.parsing[count]) {
2346 LOGERR(LY_EVALID, "Circular include dependency on the submodule \"%s\".", value);
2347 goto error;
2348 }
2349 }
2350 }
2351 ++count;
2352 module->ctx->models.parsing =
Michal Vasko253035f2015-12-17 16:58:13 +01002353 ly_realloc(module->ctx->models.parsing, (count + 1) * sizeof *module->ctx->models.parsing);
2354 if (!module->ctx->models.parsing) {
2355 LOGMEM;
2356 goto error;
2357 }
Michal Vasko1b882eb2015-10-22 11:43:14 +02002358 module->ctx->models.parsing[count - 1] = value;
2359 module->ctx->models.parsing[count] = NULL;
2360
2361 /* try to load the submodule */
Michal Vasko1e62a092015-12-01 12:27:20 +01002362 inc->submodule = (struct lys_submodule *)ly_ctx_get_submodule(module, value, inc->rev[0] ? inc->rev : NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002363 if (!inc->submodule) {
Michal Vasko99b0aad2015-12-01 12:28:51 +01002364 if (module->ctx->module_clb) {
2365 module_data = module->ctx->module_clb(value, inc->rev[0] ? inc->rev : NULL, module->ctx->module_clb_data,
2366 &format, &module_data_free);
2367 if (module_data) {
Radek Krejcic071c542016-01-27 14:57:51 +01002368 inc->submodule = lys_submodule_parse(module, module_data, format, unres);
Michal Vasko99b0aad2015-12-01 12:28:51 +01002369 if (module_data_free) {
2370 module_data_free(module_data);
2371 } else {
2372 free(module_data);
2373 }
2374 } else {
2375 LOGERR(LY_EVALID, "User module retrieval callback failed!");
2376 }
2377 } else {
2378 inc->submodule = (struct lys_submodule *)lyp_search_file(module->ctx, module, value,
Radek Krejcic071c542016-01-27 14:57:51 +01002379 inc->rev[0] ? inc->rev : NULL, unres);
Michal Vasko99b0aad2015-12-01 12:28:51 +01002380 }
Michal Vasko1b882eb2015-10-22 11:43:14 +02002381 }
2382
2383 /* remove the new submodule name now that its parsing is finished (even if failed) */
2384 if (module->ctx->models.parsing[count] || (module->ctx->models.parsing[count - 1] != value)) {
2385 LOGINT;
2386 }
2387 --count;
2388 if (count) {
2389 module->ctx->models.parsing[count] = NULL;
2390 } else {
2391 free(module->ctx->models.parsing);
2392 module->ctx->models.parsing = NULL;
2393 }
2394
2395 /* check the result */
2396 if (!inc->submodule) {
2397 LOGVAL(LYE_INARG, LOGLINE(yin), value, yin->name);
2398 LOGERR(LY_EVALID, "Including \"%s\" module into \"%s\" failed.", value, module->name);
2399 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002400 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002401
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002402 return EXIT_SUCCESS;
Radek Krejcice7fb782015-05-29 16:52:34 +02002403
2404error:
2405
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002406 return EXIT_FAILURE;
Radek Krejciefaeba32015-05-27 14:30:57 +02002407}
2408
Michal Vasko0d343d12015-08-24 14:57:36 +02002409/* logs directly
2410 *
Radek Krejcida04f4a2015-05-21 12:54:09 +02002411 * Covers:
Radek Krejci25d782a2015-05-22 15:03:23 +02002412 * description, reference, status, optionaly config
Radek Krejcib388c152015-06-04 17:03:03 +02002413 *
Radek Krejcida04f4a2015-05-21 12:54:09 +02002414 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002415static int
Radek Krejcib8048692015-08-05 13:36:34 +02002416read_yin_common(struct lys_module *module, struct lys_node *parent,
Radek Krejci1d82ef62015-08-07 14:44:40 +02002417 struct lys_node *node, struct lyxml_elem *xmlnode, int opt)
Radek Krejcida04f4a2015-05-21 12:54:09 +02002418{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002419 const char *value;
2420 struct lyxml_elem *sub, *next;
2421 struct ly_ctx *const ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002422
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002423 if (opt & OPT_MODULE) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002424 node->module = module;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002425 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002426
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002427 if (opt & OPT_IDENT) {
2428 GETVAL(value, xmlnode, "name");
Radek Krejcic6556022016-01-27 15:16:45 +01002429 if (lyp_check_identifier(value, LY_IDENT_NAME, LOGLINE(xmlnode), NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002430 goto error;
2431 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02002432 node->name = lydict_insert(ctx, value, strlen(value));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002433 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002434
Radek Krejci6764bb32015-07-03 15:16:04 +02002435 /* inherit NACM flags */
Radek Krejci6a113852015-07-03 16:04:20 +02002436 if ((opt & OPT_NACMEXT) && parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002437 node->nacm = parent->nacm;
Radek Krejci6764bb32015-07-03 15:16:04 +02002438 }
2439
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002440 /* process local parameters */
2441 LY_TREE_FOR_SAFE(xmlnode->child, next, sub) {
Radek Krejci6764bb32015-07-03 15:16:04 +02002442 if (!sub->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002443 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002444 lyxml_free(ctx, sub);
Radek Krejci6764bb32015-07-03 15:16:04 +02002445 continue;
2446 }
2447 if (strcmp(sub->ns->value, LY_NSYIN)) {
2448 /* NACM extensions */
Radek Krejci6a113852015-07-03 16:04:20 +02002449 if ((opt & OPT_NACMEXT) && !strcmp(sub->ns->value, LY_NSNACM)) {
Radek Krejci6764bb32015-07-03 15:16:04 +02002450 if (!strcmp(sub->name, "default-deny-write")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002451 node->nacm |= LYS_NACM_DENYW;
Radek Krejci6764bb32015-07-03 15:16:04 +02002452 } else if (!strcmp(sub->name, "default-deny-all")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002453 node->nacm |= LYS_NACM_DENYA;
Radek Krejci6764bb32015-07-03 15:16:04 +02002454 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002455 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci6764bb32015-07-03 15:16:04 +02002456 goto error;
2457 }
2458 }
2459
2460 /* else garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002461 lyxml_free(ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02002462 continue;
2463 }
2464
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002465 if (!strcmp(sub->name, "description")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002466 if (node->dsc) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002467 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002468 goto error;
2469 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02002470 node->dsc = read_yin_subnode(ctx, sub, "text");
2471 if (!node->dsc) {
Radek Krejci73adb602015-07-02 18:07:40 +02002472 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002473 }
2474 } else if (!strcmp(sub->name, "reference")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002475 if (node->ref) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002476 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002477 goto error;
2478 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02002479 node->ref = read_yin_subnode(ctx, sub, "text");
2480 if (!node->ref) {
Radek Krejci73adb602015-07-02 18:07:40 +02002481 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002482 }
2483 } else if (!strcmp(sub->name, "status")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002484 if (node->flags & LYS_STATUS_MASK) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002485 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002486 goto error;
2487 }
2488 GETVAL(value, sub, "value");
2489 if (!strcmp(value, "current")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002490 node->flags |= LYS_STATUS_CURR;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002491 } else if (!strcmp(value, "deprecated")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002492 node->flags |= LYS_STATUS_DEPRC;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002493 } else if (!strcmp(value, "obsolete")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002494 node->flags |= LYS_STATUS_OBSLT;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002495 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002496 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci73adb602015-07-02 18:07:40 +02002497 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002498 }
2499 } else if ((opt & OPT_CONFIG) && !strcmp(sub->name, "config")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002500 if (node->flags & LYS_CONFIG_MASK) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002501 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, xmlnode->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002502 goto error;
2503 }
2504 GETVAL(value, sub, "value");
2505 if (!strcmp(value, "false")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002506 node->flags |= LYS_CONFIG_R;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002507 } else if (!strcmp(value, "true")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002508 node->flags |= LYS_CONFIG_W;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002509 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002510 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci73adb602015-07-02 18:07:40 +02002511 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002512 }
2513 } else {
Michal Vasko345da0a2015-12-02 10:35:55 +01002514 /* skip the lyxml_free */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002515 continue;
2516 }
Michal Vasko345da0a2015-12-02 10:35:55 +01002517 lyxml_free(ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002518 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002519
Radek Krejci1d82ef62015-08-07 14:44:40 +02002520 if ((opt & OPT_INHERIT) && !(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002521 /* get config flag from parent */
2522 if (parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002523 node->flags |= parent->flags & LYS_CONFIG_MASK;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002524 } else {
2525 /* default config is true */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002526 node->flags |= LYS_CONFIG_W;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002527 }
2528 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002529
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002530 return EXIT_SUCCESS;
Radek Krejcieac35532015-05-31 19:09:15 +02002531
2532error:
2533
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002534 return EXIT_FAILURE;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002535}
2536
Michal Vasko0d343d12015-08-24 14:57:36 +02002537/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02002538static struct lys_when *
Radek Krejcib8048692015-08-05 13:36:34 +02002539read_yin_when(struct lys_module *module, struct lyxml_elem *yin)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002540{
Radek Krejci76512572015-08-04 09:47:08 +02002541 struct lys_when *retval = NULL;
Radek Krejci73adb602015-07-02 18:07:40 +02002542 struct lyxml_elem *child;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002543 const char *value;
2544
2545 retval = calloc(1, sizeof *retval);
Michal Vasko253035f2015-12-17 16:58:13 +01002546 if (!retval) {
2547 LOGMEM;
2548 return NULL;
2549 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002550
2551 GETVAL(value, yin, "condition");
Michal Vaskofba15262015-10-21 12:10:28 +02002552 retval->cond = transform_schema2json(module, value, LOGLINE(yin));
Michal Vaskof9893382015-10-09 14:03:04 +02002553 if (!retval->cond) {
2554 goto error;
2555 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002556
Radek Krejci73adb602015-07-02 18:07:40 +02002557 LY_TREE_FOR(yin->child, child) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002558 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
2559 /* garbage */
Radek Krejci0d70c372015-07-02 16:23:10 +02002560 continue;
2561 }
2562
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002563 if (!strcmp(child->name, "description")) {
2564 if (retval->dsc) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002565 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002566 goto error;
2567 }
2568 retval->dsc = read_yin_subnode(module->ctx, child, "text");
2569 if (!retval->dsc) {
2570 goto error;
2571 }
2572 } else if (!strcmp(child->name, "reference")) {
2573 if (retval->ref) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002574 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002575 goto error;
2576 }
2577 retval->ref = read_yin_subnode(module->ctx, child, "text");
2578 if (!retval->ref) {
2579 goto error;
2580 }
2581 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002582 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002583 goto error;
2584 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002585 }
2586
2587 return retval;
2588
2589error:
2590
Michal Vasko0308dd62015-10-07 09:14:40 +02002591 lys_when_free(module->ctx, retval);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002592 return NULL;
2593}
2594
Michal Vasko0d343d12015-08-24 14:57:36 +02002595/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02002596static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02002597read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
2598 struct unres_schema *unres)
Radek Krejcib4cf2022015-06-03 14:40:05 +02002599{
Michal Vasko29fc0182015-08-24 15:02:39 +02002600 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002601 struct lys_node_case *cs;
2602 struct lys_node *retval, *node = NULL;
Michal Vasko2d851a92015-10-20 16:16:36 +02002603 int c_ftrs = 0, ret;
Radek Krejci3cf9e222015-06-18 11:37:50 +02002604 const char *value;
Radek Krejcib4cf2022015-06-03 14:40:05 +02002605
Radek Krejcie867c852015-08-27 09:52:34 +02002606 /* init */
2607 memset(&root, 0, sizeof root);
2608
Radek Krejci1d82ef62015-08-07 14:44:40 +02002609 cs = calloc(1, sizeof *cs);
Michal Vasko253035f2015-12-17 16:58:13 +01002610 if (!cs) {
2611 LOGMEM;
2612 return NULL;
2613 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02002614 cs->nodetype = LYS_CASE;
2615 cs->prev = (struct lys_node *)cs;
2616 retval = (struct lys_node *)cs;
Radek Krejcib4cf2022015-06-03 14:40:05 +02002617
Radek Krejci6a113852015-07-03 16:04:20 +02002618 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_INHERIT | OPT_NACMEXT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002619 goto error;
2620 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02002621
Radek Krejcia9544502015-08-14 08:24:29 +02002622 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
2623
Michal Vasko3a0043f2015-08-12 12:11:30 +02002624 /* insert the node into the schema tree */
Radek Krejcic071c542016-01-27 14:57:51 +01002625 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02002626 goto error;
2627 }
2628
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002629 /* process choice's specific children */
2630 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002631 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
2632 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002633 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02002634 continue;
2635 }
2636
Michal Vasko29fc0182015-08-24 15:02:39 +02002637 if (!strcmp(sub->name, "container") ||
2638 !strcmp(sub->name, "leaf-list") ||
2639 !strcmp(sub->name, "leaf") ||
2640 !strcmp(sub->name, "list") ||
2641 !strcmp(sub->name, "uses") ||
2642 !strcmp(sub->name, "choice") ||
2643 !strcmp(sub->name, "anyxml")) {
2644
Michal Vaskof3930de2015-10-22 12:03:59 +02002645 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vasko29fc0182015-08-24 15:02:39 +02002646 lyxml_add_child(module->ctx, &root, sub);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002647 } else if (!strcmp(sub->name, "if-feature")) {
Michal Vasko29fc0182015-08-24 15:02:39 +02002648 c_ftrs++;
Michal Vasko345da0a2015-12-02 10:35:55 +01002649 /* skip lyxml_free() at the end of the loop, sub is processed later */
Michal Vasko29fc0182015-08-24 15:02:39 +02002650 continue;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002651 } else if (!strcmp(sub->name, "when")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002652 if (cs->when) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002653 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002654 goto error;
2655 }
2656
Radek Krejci1d82ef62015-08-07 14:44:40 +02002657 cs->when = read_yin_when(module, sub);
2658 if (!cs->when) {
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002659 goto error;
2660 }
Michal Vaskofcdac172015-10-07 09:35:05 +02002661 if (lyxp_syntax_check(cs->when->cond, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002662 goto error;
2663 }
Michal Vasko29fc0182015-08-24 15:02:39 +02002664
Michal Vasko345da0a2015-12-02 10:35:55 +01002665 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002666 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002667 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002668 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002669 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002670 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02002671
Radek Krejci3cf9e222015-06-18 11:37:50 +02002672 if (c_ftrs) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002673 cs->features = calloc(c_ftrs, sizeof *cs->features);
Michal Vasko253035f2015-12-17 16:58:13 +01002674 if (!cs->features) {
2675 LOGMEM;
2676 goto error;
2677 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002678 }
Radek Krejci73adb602015-07-02 18:07:40 +02002679 LY_TREE_FOR(yin->child, sub) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02002680 GETVAL(value, sub, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02002681 if (!(value = transform_schema2json(module, value, LOGLINE(sub)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02002682 goto error;
2683 }
Radek Krejcicf509982015-12-15 09:22:44 +01002684 /* hack - store pointer to the parent node for later status check */
2685 cs->features[cs->features_size] = (struct lys_feature *)cs;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002686 ret = unres_schema_add_str(module, unres, &cs->features[cs->features_size], UNRES_IFFEAT, value,
Michal Vasko2d851a92015-10-20 16:16:36 +02002687 LOGLINE(sub));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002688 cs->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02002689 lydict_remove(module->ctx, value);
2690 if (ret == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002691 goto error;
2692 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002693 }
Radek Krejcib388c152015-06-04 17:03:03 +02002694
Michal Vasko29fc0182015-08-24 15:02:39 +02002695 /* last part - process data nodes */
2696 LY_TREE_FOR_SAFE(root.child, next, sub) {
2697 if (!strcmp(sub->name, "container")) {
2698 node = read_yin_container(module, retval, sub, resolve, unres);
2699 } else if (!strcmp(sub->name, "leaf-list")) {
2700 node = read_yin_leaflist(module, retval, sub, resolve, unres);
2701 } else if (!strcmp(sub->name, "leaf")) {
2702 node = read_yin_leaf(module, retval, sub, resolve, unres);
2703 } else if (!strcmp(sub->name, "list")) {
2704 node = read_yin_list(module, retval, sub, resolve, unres);
2705 } else if (!strcmp(sub->name, "choice")) {
2706 node = read_yin_choice(module, retval, sub, resolve, unres);
2707 } else if (!strcmp(sub->name, "uses")) {
2708 node = read_yin_uses(module, retval, sub, resolve, unres);
2709 } else if (!strcmp(sub->name, "anyxml")) {
2710 node = read_yin_anyxml(module, retval, sub, resolve, unres);
2711 }
2712 if (!node) {
2713 goto error;
2714 }
2715
Michal Vasko345da0a2015-12-02 10:35:55 +01002716 lyxml_free(module->ctx, sub);
Michal Vasko29fc0182015-08-24 15:02:39 +02002717 }
2718
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002719 return retval;
Radek Krejcib4cf2022015-06-03 14:40:05 +02002720
2721error:
2722
Michal Vasko29fc0182015-08-24 15:02:39 +02002723 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01002724 lyxml_free(module->ctx, root.child);
Michal Vasko29fc0182015-08-24 15:02:39 +02002725 }
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002726 lys_node_free(retval, NULL);
Radek Krejcib4cf2022015-06-03 14:40:05 +02002727
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002728 return NULL;
Radek Krejcib4cf2022015-06-03 14:40:05 +02002729}
2730
Michal Vasko0d343d12015-08-24 14:57:36 +02002731/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02002732static struct lys_node *
Radek Krejci10c760e2015-08-14 14:45:43 +02002733read_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 +02002734{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002735 struct lyxml_elem *sub, *next;
2736 struct ly_ctx *const ctx = module->ctx;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002737 struct lys_node *retval, *node = NULL;
Radek Krejcib8048692015-08-05 13:36:34 +02002738 struct lys_node_choice *choice;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002739 const char *value, *dflt_str = NULL;
Michal Vasko2d851a92015-10-20 16:16:36 +02002740 int f_mand = 0, c_ftrs = 0, ret;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002741
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002742 choice = calloc(1, sizeof *choice);
Michal Vasko253035f2015-12-17 16:58:13 +01002743 if (!choice) {
2744 LOGMEM;
2745 return NULL;
2746 }
Radek Krejci76512572015-08-04 09:47:08 +02002747 choice->nodetype = LYS_CHOICE;
2748 choice->prev = (struct lys_node *)choice;
2749 retval = (struct lys_node *)choice;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002750
Michal Vaskoe0c59842015-09-24 13:52:20 +02002751 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
2752 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002753 goto error;
2754 }
Radek Krejcida04f4a2015-05-21 12:54:09 +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 */
Radek Krejcic071c542016-01-27 14:57:51 +01002759 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: 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
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002771 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002772 if (!(node = read_yin_container(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002773 goto error;
2774 }
2775 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002776 if (!(node = read_yin_leaflist(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002777 goto error;
2778 }
2779 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002780 if (!(node = read_yin_leaf(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002781 goto error;
2782 }
2783 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002784 if (!(node = read_yin_list(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002785 goto error;
2786 }
2787 } else if (!strcmp(sub->name, "case")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002788 if (!(node = read_yin_case(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002789 goto error;
2790 }
2791 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002792 if (!(node = read_yin_anyxml(module, retval, sub, resolve, unres))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002793 goto error;
2794 }
2795 } else if (!strcmp(sub->name, "default")) {
2796 if (dflt_str) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002797 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002798 goto error;
2799 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002800 GETVAL(dflt_str, sub, "value");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002801 } else if (!strcmp(sub->name, "mandatory")) {
2802 if (f_mand) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002803 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002804 goto error;
2805 }
2806 /* just checking the flags in leaf is not sufficient, we would allow
2807 * multiple mandatory statements with the "false" value
2808 */
2809 f_mand = 1;
Radek Krejcib4cf2022015-06-03 14:40:05 +02002810
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002811 GETVAL(value, sub, "value");
2812 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002813 choice->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02002814 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002815 choice->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02002816 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002817 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002818 goto error;
2819 } /* else false is the default value, so we can ignore it */
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002820 } else if (!strcmp(sub->name, "when")) {
2821 if (choice->when) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002822 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002823 goto error;
2824 }
2825
2826 choice->when = read_yin_when(module, sub);
2827 if (!choice->when) {
2828 goto error;
2829 }
Michal Vaskofcdac172015-10-07 09:35:05 +02002830 if (lyxp_syntax_check(choice->when->cond, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002831 goto error;
2832 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002833 } else if (!strcmp(sub->name, "if-feature")) {
2834 c_ftrs++;
2835
Michal Vasko345da0a2015-12-02 10:35:55 +01002836 /* skip lyxml_free() at the end of the loop, the sub node is processed later */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002837 continue;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002838 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002839 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002840 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002841 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02002842
Radek Krejci1d82ef62015-08-07 14:44:40 +02002843 node = NULL;
Michal Vasko345da0a2015-12-02 10:35:55 +01002844 lyxml_free(ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002845 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02002846
Radek Krejci3cf9e222015-06-18 11:37:50 +02002847 if (c_ftrs) {
2848 choice->features = calloc(c_ftrs, sizeof *choice->features);
Michal Vasko253035f2015-12-17 16:58:13 +01002849 if (!choice->features) {
2850 LOGMEM;
2851 goto error;
2852 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002853 }
2854
Radek Krejci73adb602015-07-02 18:07:40 +02002855 LY_TREE_FOR(yin->child, sub) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02002856 GETVAL(value, sub, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02002857 if (!(value = transform_schema2json(module, value, LOGLINE(sub)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02002858 goto error;
2859 }
Radek Krejcicf509982015-12-15 09:22:44 +01002860 /* hack - store pointer to the parent node for later status check */
2861 choice->features[choice->features_size] = (struct lys_feature *)choice;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002862 ret = unres_schema_add_str(module, unres, &choice->features[choice->features_size], UNRES_IFFEAT, value,
Michal Vasko2d851a92015-10-20 16:16:36 +02002863 LOGLINE(sub));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002864 choice->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02002865 lydict_remove(module->ctx, value);
2866 if (ret == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002867 goto error;
2868 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002869 }
2870
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002871 /* check - default is prohibited in combination with mandatory */
Radek Krejci1574a8d2015-08-03 14:16:52 +02002872 if (dflt_str && (choice->flags & LYS_MAND_TRUE)) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002873 LOGVAL(LYE_SPEC, LOGLINE(yin),
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002874 "The \"default\" statement MUST NOT be present on choices where \"mandatory\" is true.");
2875 goto error;
2876 }
Radek Krejcib4cf2022015-06-03 14:40:05 +02002877
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002878 /* link default with the case */
2879 if (dflt_str) {
Michal Vasko0bd29d12015-08-19 11:45:49 +02002880 if (unres_schema_add_str(module, unres, choice, UNRES_CHOICE_DFLT, dflt_str, LOGLINE(yin)) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002881 goto error;
2882 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002883 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002884
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002885 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002886
2887error:
2888
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002889 lys_node_free(retval, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002890
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002891 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002892}
2893
Michal Vasko0d343d12015-08-24 14:57:36 +02002894/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02002895static struct lys_node *
Radek Krejcib8048692015-08-05 13:36:34 +02002896read_yin_anyxml(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
Michal Vaskof02e3742015-08-05 16:27:02 +02002897 struct unres_schema *unres)
Radek Krejci863c2852015-06-03 15:47:11 +02002898{
Radek Krejci76512572015-08-04 09:47:08 +02002899 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02002900 struct lys_node_leaf *anyxml;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002901 struct lyxml_elem *sub, *next;
2902 const char *value;
2903 int r;
2904 int f_mand = 0;
Radek Krejci3cf9e222015-06-18 11:37:50 +02002905 int c_must = 0, c_ftrs = 0;
Radek Krejci863c2852015-06-03 15:47:11 +02002906
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002907 anyxml = calloc(1, sizeof *anyxml);
Michal Vasko253035f2015-12-17 16:58:13 +01002908 if (!anyxml) {
2909 LOGMEM;
2910 return NULL;
2911 }
Radek Krejci76512572015-08-04 09:47:08 +02002912 anyxml->nodetype = LYS_ANYXML;
2913 anyxml->prev = (struct lys_node *)anyxml;
2914 retval = (struct lys_node *)anyxml;
Radek Krejci863c2852015-06-03 15:47:11 +02002915
Michal Vaskoe0c59842015-09-24 13:52:20 +02002916 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
2917 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002918 goto error;
2919 }
Radek Krejci863c2852015-06-03 15:47:11 +02002920
Radek Krejcia9544502015-08-14 08:24:29 +02002921 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02002922
Radek Krejcic071c542016-01-27 14:57:51 +01002923 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02002924 goto error;
2925 }
2926
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002927 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02002928 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
2929 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01002930 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02002931 continue;
2932 }
2933
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002934 if (!strcmp(sub->name, "mandatory")) {
2935 if (f_mand) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002936 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002937 goto error;
2938 }
2939 /* just checking the flags in leaf is not sufficient, we would allow
2940 * multiple mandatory statements with the "false" value
2941 */
2942 f_mand = 1;
Radek Krejci863c2852015-06-03 15:47:11 +02002943
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002944 GETVAL(value, sub, "value");
2945 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002946 anyxml->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02002947 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002948 anyxml->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02002949 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002950 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002951 goto error;
2952 }
2953 /* else false is the default value, so we can ignore it */
Michal Vasko345da0a2015-12-02 10:35:55 +01002954 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002955 } else if (!strcmp(sub->name, "when")) {
2956 if (anyxml->when) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002957 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002958 goto error;
2959 }
2960
2961 anyxml->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002962 if (!anyxml->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01002963 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002964 goto error;
2965 }
Michal Vaskofcdac172015-10-07 09:35:05 +02002966 if (lyxp_syntax_check(anyxml->when->cond, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002967 goto error;
2968 }
Michal Vasko345da0a2015-12-02 10:35:55 +01002969 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002970 } else if (!strcmp(sub->name, "must")) {
2971 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02002972 } else if (!strcmp(sub->name, "if-feature")) {
2973 c_ftrs++;
Radek Krejci863c2852015-06-03 15:47:11 +02002974
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002975 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02002976 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002977 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002978 }
2979 }
Radek Krejci863c2852015-06-03 15:47:11 +02002980
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002981 /* middle part - process nodes with cardinality of 0..n */
2982 if (c_must) {
2983 anyxml->must = calloc(c_must, sizeof *anyxml->must);
Michal Vasko253035f2015-12-17 16:58:13 +01002984 if (!anyxml->must) {
2985 LOGMEM;
2986 goto error;
2987 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002988 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002989 if (c_ftrs) {
2990 anyxml->features = calloc(c_ftrs, sizeof *anyxml->features);
Michal Vasko253035f2015-12-17 16:58:13 +01002991 if (!anyxml->features) {
2992 LOGMEM;
2993 goto error;
2994 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02002995 }
Radek Krejci863c2852015-06-03 15:47:11 +02002996
Radek Krejci73adb602015-07-02 18:07:40 +02002997 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002998 if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01002999 r = fill_yin_must(module, sub, &anyxml->must[anyxml->must_size]);
3000 anyxml->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003001 if (r) {
3002 goto error;
3003 }
Michal Vasko0d204592015-10-07 09:50:04 +02003004 if (lyxp_syntax_check(anyxml->must[anyxml->must_size-1].expr, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003005 goto error;
3006 }
Radek Krejci0b24d752015-07-02 15:02:27 +02003007 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02003008 GETVAL(value, sub, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02003009 if (!(value = transform_schema2json(module, value, LOGLINE(sub)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02003010 goto error;
3011 }
Radek Krejcicf509982015-12-15 09:22:44 +01003012 /* hack - store pointer to the parent node for later status check */
3013 anyxml->features[anyxml->features_size] = (struct lys_feature *)anyxml;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003014 r = unres_schema_add_str(module, unres, &anyxml->features[anyxml->features_size], UNRES_IFFEAT, value,
Michal Vasko2d851a92015-10-20 16:16:36 +02003015 LOGLINE(sub));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003016 anyxml->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02003017 lydict_remove(module->ctx, value);
3018 if (r == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003019 goto error;
3020 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003021 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003022 }
Radek Krejci863c2852015-06-03 15:47:11 +02003023
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003024 return retval;
Radek Krejci863c2852015-06-03 15:47:11 +02003025
3026error:
3027
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003028 lys_node_free(retval, NULL);
Radek Krejci863c2852015-06-03 15:47:11 +02003029
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003030 return NULL;
Radek Krejci863c2852015-06-03 15:47:11 +02003031}
3032
Michal Vasko0d343d12015-08-24 14:57:36 +02003033/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003034static struct lys_node *
Radek Krejcib8048692015-08-05 13:36:34 +02003035read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
Michal Vaskof02e3742015-08-05 16:27:02 +02003036 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003037{
Radek Krejci76512572015-08-04 09:47:08 +02003038 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003039 struct lys_node_leaf *leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003040 struct lyxml_elem *sub, *next;
3041 const char *value;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003042 int r, has_type = 0, dflt_line;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003043 int c_must = 0, c_ftrs = 0, f_mand = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003044
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003045 leaf = calloc(1, sizeof *leaf);
Michal Vasko253035f2015-12-17 16:58:13 +01003046 if (!leaf) {
3047 LOGMEM;
3048 return NULL;
3049 }
Radek Krejci76512572015-08-04 09:47:08 +02003050 leaf->nodetype = LYS_LEAF;
3051 leaf->prev = (struct lys_node *)leaf;
3052 retval = (struct lys_node *)leaf;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003053
Michal Vaskoe0c59842015-09-24 13:52:20 +02003054 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
3055 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003056 goto error;
3057 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003058
Radek Krejcia9544502015-08-14 08:24:29 +02003059 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02003060
Radek Krejcic071c542016-01-27 14:57:51 +01003061 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003062 goto error;
3063 }
3064
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003065 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003066 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3067 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003068 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003069 continue;
3070 }
3071
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003072 if (!strcmp(sub->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +01003073 if (has_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003074 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003075 goto error;
3076 }
Michal Vasko88c29542015-11-27 14:57:53 +01003077 /* HACK for unres */
3078 leaf->type.der = (struct lys_tpdf *)sub;
Radek Krejcicf509982015-12-15 09:22:44 +01003079 leaf->type.parent = (struct lys_tpdf *)leaf;
Michal Vasko88c29542015-11-27 14:57:53 +01003080 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DER, parent, LOGLINE(sub))) {
3081 leaf->type.der = NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003082 goto error;
3083 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003084 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003085 } else if (!strcmp(sub->name, "default")) {
3086 if (leaf->dflt) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003087 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003088 goto error;
3089 }
3090 GETVAL(value, sub, "value");
3091 leaf->dflt = lydict_insert(module->ctx, value, strlen(value));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003092 dflt_line = LOGLINE(sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003093 } else if (!strcmp(sub->name, "units")) {
3094 if (leaf->units) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003095 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003096 goto error;
3097 }
3098 GETVAL(value, sub, "name");
3099 leaf->units = lydict_insert(module->ctx, value, strlen(value));
3100 } else if (!strcmp(sub->name, "mandatory")) {
3101 if (f_mand) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003102 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003103 goto error;
3104 }
3105 /* just checking the flags in leaf is not sufficient, we would allow
3106 * multiple mandatory statements with the "false" value
3107 */
3108 f_mand = 1;
Radek Krejci4c31f122015-06-02 14:51:22 +02003109
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003110 GETVAL(value, sub, "value");
3111 if (!strcmp(value, "true")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003112 leaf->flags |= LYS_MAND_TRUE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003113 } else if (!strcmp(value, "false")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003114 leaf->flags |= LYS_MAND_FALSE;
Radek Krejcieb00f512015-07-01 16:44:58 +02003115 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003116 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003117 goto error;
3118 } /* else false is the default value, so we can ignore it */
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003119 } else if (!strcmp(sub->name, "when")) {
3120 if (leaf->when) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003121 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003122 goto error;
3123 }
3124
3125 leaf->when = read_yin_when(module, sub);
3126 if (!leaf->when) {
3127 goto error;
3128 }
Michal Vaskofcdac172015-10-07 09:35:05 +02003129 if (lyxp_syntax_check(leaf->when->cond, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003130 goto error;
3131 }
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003132
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003133 } else if (!strcmp(sub->name, "must")) {
Radek Krejci41882de2015-07-02 16:34:58 +02003134 c_must++;
3135 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003136 } else if (!strcmp(sub->name, "if-feature")) {
3137 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003138 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02003139
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003140 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003141 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003142 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003143 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003144
Michal Vasko88c29542015-11-27 14:57:53 +01003145 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003146 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003147
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003148 /* check mandatory parameters */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003149 if (!has_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003150 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003151 goto error;
3152 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003153 if (leaf->dflt) {
Michal Vasko0bd29d12015-08-19 11:45:49 +02003154 if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt, dflt_line) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003155 goto error;
3156 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003157 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003158
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003159 /* middle part - process nodes with cardinality of 0..n */
3160 if (c_must) {
3161 leaf->must = calloc(c_must, sizeof *leaf->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003162 if (!leaf->must) {
3163 LOGMEM;
3164 goto error;
3165 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003166 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003167 if (c_ftrs) {
3168 leaf->features = calloc(c_ftrs, sizeof *leaf->features);
Michal Vasko253035f2015-12-17 16:58:13 +01003169 if (!leaf->features) {
3170 LOGMEM;
3171 goto error;
3172 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003173 }
Radek Krejci4c31f122015-06-02 14:51:22 +02003174
Radek Krejci73adb602015-07-02 18:07:40 +02003175 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003176 if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003177 r = fill_yin_must(module, sub, &leaf->must[leaf->must_size]);
3178 leaf->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003179 if (r) {
3180 goto error;
3181 }
Michal Vasko0d204592015-10-07 09:50:04 +02003182 if (lyxp_syntax_check(leaf->must[leaf->must_size-1].expr, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003183 goto error;
3184 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003185 } else if (!strcmp(sub->name, "if-feature")) {
3186 GETVAL(value, sub, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02003187 if (!(value = transform_schema2json(module, value, LOGLINE(sub)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02003188 goto error;
3189 }
Radek Krejcicf509982015-12-15 09:22:44 +01003190 /* hack - store pointer to the parent node for later status check */
3191 leaf->features[leaf->features_size] = (struct lys_feature *)leaf;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003192 r = unres_schema_add_str(module, unres, &leaf->features[leaf->features_size], UNRES_IFFEAT, value,
Michal Vasko2d851a92015-10-20 16:16:36 +02003193 LOGLINE(sub));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003194 leaf->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02003195 lydict_remove(module->ctx, value);
3196 if (r == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003197 goto error;
3198 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003199 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003200 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003201
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003202 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003203
3204error:
3205
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003206 lys_node_free(retval, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003207
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003208 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003209}
3210
Michal Vasko0d343d12015-08-24 14:57:36 +02003211/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003212static struct lys_node *
Radek Krejcib8048692015-08-05 13:36:34 +02003213read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
Michal Vaskof02e3742015-08-05 16:27:02 +02003214 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003215{
Radek Krejci76512572015-08-04 09:47:08 +02003216 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003217 struct lys_node_leaflist *llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003218 struct lyxml_elem *sub, *next;
3219 const char *value;
3220 char *endptr;
3221 unsigned long val;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003222 int r, has_type = 0;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003223 int c_must = 0, c_ftrs = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003224 int f_ordr = 0, f_min = 0, f_max = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003225
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003226 llist = calloc(1, sizeof *llist);
Michal Vasko253035f2015-12-17 16:58:13 +01003227 if (!llist) {
3228 LOGMEM;
3229 return NULL;
3230 }
Radek Krejci76512572015-08-04 09:47:08 +02003231 llist->nodetype = LYS_LEAFLIST;
3232 llist->prev = (struct lys_node *)llist;
3233 retval = (struct lys_node *)llist;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003234
Michal Vaskoe0c59842015-09-24 13:52:20 +02003235 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
3236 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003237 goto error;
3238 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003239
Radek Krejcia9544502015-08-14 08:24:29 +02003240 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02003241
Radek Krejcic071c542016-01-27 14:57:51 +01003242 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003243 goto error;
3244 }
3245
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003246 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003247 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3248 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003249 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003250 continue;
3251 }
3252
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003253 if (!strcmp(sub->name, "type")) {
Radek Krejciad73b6f2016-02-09 15:42:55 +01003254 if (has_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003255 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003256 goto error;
3257 }
Michal Vasko88c29542015-11-27 14:57:53 +01003258 /* HACK for unres */
3259 llist->type.der = (struct lys_tpdf *)sub;
Radek Krejcicf509982015-12-15 09:22:44 +01003260 llist->type.parent = (struct lys_tpdf *)llist;
Michal Vasko88c29542015-11-27 14:57:53 +01003261 if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DER, parent, LOGLINE(sub))) {
3262 llist->type.der = NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003263 goto error;
3264 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003265 has_type = 1;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003266 } else if (!strcmp(sub->name, "units")) {
3267 if (llist->units) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003268 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003269 goto error;
3270 }
3271 GETVAL(value, sub, "name");
3272 llist->units = lydict_insert(module->ctx, value, strlen(value));
3273 } else if (!strcmp(sub->name, "ordered-by")) {
3274 if (f_ordr) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003275 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003276 goto error;
3277 }
3278 /* just checking the flags in llist is not sufficient, we would
3279 * allow multiple ordered-by statements with the "system" value
3280 */
3281 f_ordr = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003282
Radek Krejci1574a8d2015-08-03 14:16:52 +02003283 if (llist->flags & LYS_CONFIG_R) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003284 /* RFC 6020, 7.7.5 - ignore ordering when the list represents
3285 * state data
3286 */
Michal Vasko345da0a2015-12-02 10:35:55 +01003287 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003288 continue;
3289 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003290
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003291 GETVAL(value, sub, "value");
3292 if (!strcmp(value, "user")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003293 llist->flags |= LYS_USERORDERED;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003294 } else if (strcmp(value, "system")) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003295 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003296 goto error;
Radek Krejci41882de2015-07-02 16:34:58 +02003297 } /* else system is the default value, so we can ignore it */
3298
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003299 } else if (!strcmp(sub->name, "must")) {
3300 c_must++;
Radek Krejci41882de2015-07-02 16:34:58 +02003301 continue;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003302 } else if (!strcmp(sub->name, "if-feature")) {
3303 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003304 continue;
Radek Krejci41882de2015-07-02 16:34:58 +02003305
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003306 } else if (!strcmp(sub->name, "min-elements")) {
3307 if (f_min) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003308 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003309 goto error;
3310 }
3311 f_min = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003312
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003313 GETVAL(value, sub, "value");
3314 while (isspace(value[0])) {
3315 value++;
3316 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003317
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003318 /* convert it to uint32_t */
3319 errno = 0;
3320 endptr = NULL;
3321 val = strtoul(value, &endptr, 10);
3322 if (*endptr || value[0] == '-' || errno || val > UINT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003323 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003324 goto error;
3325 }
3326 llist->min = (uint32_t) val;
3327 } else if (!strcmp(sub->name, "max-elements")) {
3328 if (f_max) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003329 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003330 goto error;
3331 }
3332 f_max = 1;
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003333
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003334 GETVAL(value, sub, "value");
3335 while (isspace(value[0])) {
3336 value++;
3337 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003338
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003339 /* convert it to uint32_t */
3340 errno = 0;
3341 endptr = NULL;
3342 val = strtoul(value, &endptr, 10);
3343 if (*endptr || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003344 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003345 goto error;
3346 }
3347 llist->max = (uint32_t) val;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003348 } else if (!strcmp(sub->name, "when")) {
3349 if (llist->when) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003350 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003351 goto error;
3352 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003353
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003354 llist->when = read_yin_when(module, sub);
3355 if (!llist->when) {
3356 goto error;
3357 }
Michal Vaskofcdac172015-10-07 09:35:05 +02003358 if (lyxp_syntax_check(llist->when->cond, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003359 goto error;
3360 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003361 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003362 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003363 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003364 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003365
Michal Vasko88c29542015-11-27 14:57:53 +01003366 /* do not free sub, it could have been unlinked and stored in unres */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003367 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003368
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003369 /* check constraints */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02003370 if (!has_type) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003371 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "type", yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003372 goto error;
3373 }
3374 if (llist->max && llist->min > llist->max) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003375 LOGVAL(LYE_SPEC, LOGLINE(yin), "\"min-elements\" is bigger than \"max-elements\".");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003376 goto error;
3377 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003378
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003379 /* middle part - process nodes with cardinality of 0..n */
3380 if (c_must) {
3381 llist->must = calloc(c_must, sizeof *llist->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003382 if (!llist->must) {
3383 LOGMEM;
3384 goto error;
3385 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003386 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003387 if (c_ftrs) {
3388 llist->features = calloc(c_ftrs, sizeof *llist->features);
Michal Vasko253035f2015-12-17 16:58:13 +01003389 if (!llist->features) {
3390 LOGMEM;
3391 goto error;
3392 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003393 }
Radek Krejci8b4f23c2015-06-02 16:09:25 +02003394
Radek Krejci73adb602015-07-02 18:07:40 +02003395 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003396 if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003397 r = fill_yin_must(module, sub, &llist->must[llist->must_size]);
3398 llist->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003399 if (r) {
3400 goto error;
3401 }
Michal Vasko0d204592015-10-07 09:50:04 +02003402 if (lyxp_syntax_check(llist->must[llist->must_size-1].expr, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003403 goto error;
3404 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003405 } else if (!strcmp(sub->name, "if-feature")) {
3406 GETVAL(value, sub, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02003407 if (!(value = transform_schema2json(module, value, LOGLINE(sub)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02003408 goto error;
3409 }
Radek Krejcicf509982015-12-15 09:22:44 +01003410 /* hack - store pointer to the parent node for later status check */
3411 llist->features[llist->features_size] = (struct lys_feature *)llist;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003412 r = unres_schema_add_str(module, unres, &llist->features[llist->features_size], UNRES_IFFEAT, value,
Michal Vasko2d851a92015-10-20 16:16:36 +02003413 LOGLINE(sub));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003414 llist->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02003415 lydict_remove(module->ctx, value);
3416 if (r == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003417 goto error;
3418 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003419 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003420 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003421
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003422 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003423
3424error:
3425
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003426 lys_node_free(retval, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003427
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003428 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003429}
3430
Michal Vasko0d343d12015-08-24 14:57:36 +02003431/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003432static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02003433read_yin_list(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
3434 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003435{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003436 struct lys_node *retval, *node;
Radek Krejcib8048692015-08-05 13:36:34 +02003437 struct lys_node_list *list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003438 struct lyxml_elem *sub, *next, root, uniq;
Michal Vaskoe38af6e2015-08-03 14:39:27 +02003439 int r, key_line;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003440 int c_tpdf = 0, c_must = 0, c_uniq = 0, c_ftrs = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003441 int f_ordr = 0, f_max = 0, f_min = 0;
Radek Krejcieb00f512015-07-01 16:44:58 +02003442 const char *key_str = NULL, *value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003443 char *auxs;
3444 unsigned long val;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003445
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003446 /* init */
3447 memset(&root, 0, sizeof root);
3448 memset(&uniq, 0, sizeof uniq);
Radek Krejcie0674f82015-06-15 13:58:51 +02003449
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003450 list = calloc(1, sizeof *list);
Michal Vasko253035f2015-12-17 16:58:13 +01003451 if (!list) {
3452 LOGMEM;
3453 return NULL;
3454 }
Radek Krejci76512572015-08-04 09:47:08 +02003455 list->nodetype = LYS_LIST;
3456 list->prev = (struct lys_node *)list;
3457 retval = (struct lys_node *)list;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003458
Michal Vaskoe0c59842015-09-24 13:52:20 +02003459 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
3460 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003461 goto error;
3462 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003463
Radek Krejcia9544502015-08-14 08:24:29 +02003464 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
3465
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003466 /* process list's specific children */
3467 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003468 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3469 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003470 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003471 continue;
3472 }
3473
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003474 /* data statements */
3475 if (!strcmp(sub->name, "container") ||
3476 !strcmp(sub->name, "leaf-list") ||
3477 !strcmp(sub->name, "leaf") ||
3478 !strcmp(sub->name, "list") ||
3479 !strcmp(sub->name, "choice") ||
3480 !strcmp(sub->name, "uses") ||
3481 !strcmp(sub->name, "grouping") ||
3482 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02003483 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02003484 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003485
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003486 /* array counters */
3487 } else if (!strcmp(sub->name, "key")) {
3488 /* check cardinality 0..1 */
3489 if (list->keys_size) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003490 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, list->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003491 goto error;
3492 }
Radek Krejcid7f0d012015-05-25 15:04:52 +02003493
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003494 /* count the number of keys */
3495 GETVAL(value, sub, "value");
3496 key_str = value;
Michal Vaskoe38af6e2015-08-03 14:39:27 +02003497 key_line = LOGLINE(sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003498 while ((value = strpbrk(value, " \t\n"))) {
3499 list->keys_size++;
3500 while (isspace(*value)) {
3501 value++;
3502 }
3503 }
3504 list->keys_size++;
3505 list->keys = calloc(list->keys_size, sizeof *list->keys);
Michal Vasko253035f2015-12-17 16:58:13 +01003506 if (!list->keys) {
3507 LOGMEM;
3508 goto error;
3509 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003510 } else if (!strcmp(sub->name, "unique")) {
3511 c_uniq++;
Michal Vaskof3930de2015-10-22 12:03:59 +02003512 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02003513 lyxml_add_child(module->ctx, &uniq, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003514 } else if (!strcmp(sub->name, "typedef")) {
3515 c_tpdf++;
3516 } else if (!strcmp(sub->name, "must")) {
3517 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003518 } else if (!strcmp(sub->name, "if-feature")) {
3519 c_ftrs++;
Radek Krejci345ad742015-06-03 11:04:18 +02003520
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003521 /* optional stetments */
3522 } else if (!strcmp(sub->name, "ordered-by")) {
3523 if (f_ordr) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003524 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003525 goto error;
3526 }
3527 /* just checking the flags in llist is not sufficient, we would
3528 * allow multiple ordered-by statements with the "system" value
3529 */
3530 f_ordr = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02003531
Radek Krejci1574a8d2015-08-03 14:16:52 +02003532 if (list->flags & LYS_CONFIG_R) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003533 /* RFC 6020, 7.7.5 - ignore ordering when the list represents
3534 * state data
3535 */
Michal Vasko345da0a2015-12-02 10:35:55 +01003536 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003537 continue;
3538 }
Radek Krejci345ad742015-06-03 11:04:18 +02003539
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003540 GETVAL(value, sub, "value");
3541 if (!strcmp(value, "user")) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003542 list->flags |= LYS_USERORDERED;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003543 } else if (strcmp(value, "system")) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003544 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003545 goto error;
3546 }
3547 /* else system is the default value, so we can ignore it */
Michal Vasko345da0a2015-12-02 10:35:55 +01003548 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003549 } else if (!strcmp(sub->name, "min-elements")) {
3550 if (f_min) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003551 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003552 goto error;
3553 }
3554 f_min = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02003555
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003556 GETVAL(value, sub, "value");
3557 while (isspace(value[0])) {
3558 value++;
3559 }
Radek Krejci345ad742015-06-03 11:04:18 +02003560
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003561 /* convert it to uint32_t */
3562 errno = 0;
3563 auxs = NULL;
3564 val = strtoul(value, &auxs, 10);
3565 if (*auxs || value[0] == '-' || errno || val > UINT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003566 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003567 goto error;
3568 }
3569 list->min = (uint32_t) val;
Michal Vasko345da0a2015-12-02 10:35:55 +01003570 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003571 } else if (!strcmp(sub->name, "max-elements")) {
3572 if (f_max) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003573 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003574 goto error;
3575 }
3576 f_max = 1;
Radek Krejci345ad742015-06-03 11:04:18 +02003577
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003578 GETVAL(value, sub, "value");
3579 while (isspace(value[0])) {
3580 value++;
3581 }
Radek Krejci345ad742015-06-03 11:04:18 +02003582
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003583 /* convert it to uint32_t */
3584 errno = 0;
3585 auxs = NULL;
3586 val = strtoul(value, &auxs, 10);
3587 if (*auxs || value[0] == '-' || errno || val == 0 || val > UINT32_MAX) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003588 LOGVAL(LYE_INARG, LOGLINE(sub), value, sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003589 goto error;
3590 }
3591 list->max = (uint32_t) val;
Michal Vasko345da0a2015-12-02 10:35:55 +01003592 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003593 } else if (!strcmp(sub->name, "when")) {
3594 if (list->when) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003595 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003596 goto error;
3597 }
3598
3599 list->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003600 if (!list->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003601 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003602 goto error;
3603 }
Michal Vaskofcdac172015-10-07 09:35:05 +02003604 if (lyxp_syntax_check(list->when->cond, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003605 goto error;
3606 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003607 lyxml_free(module->ctx, sub);
Radek Krejci3cf9e222015-06-18 11:37:50 +02003608 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003609 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci3cf9e222015-06-18 11:37:50 +02003610 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003611 }
3612 }
Radek Krejci345ad742015-06-03 11:04:18 +02003613
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003614 /* check - if list is configuration, key statement is mandatory */
Radek Krejci1574a8d2015-08-03 14:16:52 +02003615 if ((list->flags & LYS_CONFIG_W) && !key_str) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003616 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "key", "list");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003617 goto error;
3618 }
3619 if (list->max && list->min > list->max) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003620 LOGVAL(LYE_SPEC, LOGLINE(yin), "\"min-elements\" is bigger than \"max-elements\".");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003621 goto error;
3622 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003623
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003624 /* middle part - process nodes with cardinality of 0..n except the data nodes */
3625 if (c_tpdf) {
3626 list->tpdf = calloc(c_tpdf, sizeof *list->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01003627 if (!list->tpdf) {
3628 LOGMEM;
3629 goto error;
3630 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003631 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003632 if (c_must) {
3633 list->must = calloc(c_must, sizeof *list->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003634 if (!list->must) {
3635 LOGMEM;
3636 goto error;
3637 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003638 }
3639 if (c_ftrs) {
3640 list->features = calloc(c_ftrs, sizeof *list->features);
Michal Vasko253035f2015-12-17 16:58:13 +01003641 if (!list->features) {
3642 LOGMEM;
3643 goto error;
3644 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003645 }
Radek Krejci73adb602015-07-02 18:07:40 +02003646 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003647 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003648 r = fill_yin_typedef(module, retval, sub, &list->tpdf[list->tpdf_size], unres);
3649 list->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003650 if (r) {
3651 goto error;
3652 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003653 } else if (!strcmp(sub->name, "if-feature")) {
3654 GETVAL(value, sub, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02003655 if (!(value = transform_schema2json(module, value, LOGLINE(sub)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02003656 goto error;
3657 }
Radek Krejcicf509982015-12-15 09:22:44 +01003658 /* hack - store pointer to the parent node for later status check */
3659 list->features[list->features_size] = (struct lys_feature *)list;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003660 r = unres_schema_add_str(module, unres, &list->features[list->features_size], UNRES_IFFEAT, value,
Radek Krejcife8582c2015-12-15 15:39:02 +01003661 LOGLINE(sub));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003662 list->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02003663 lydict_remove(module->ctx, value);
3664 if (r == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003665 goto error;
3666 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003667 } else if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003668 r = fill_yin_must(module, sub, &list->must[list->must_size]);
3669 list->must_size++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003670 if (r) {
3671 goto error;
3672 }
Michal Vasko0d204592015-10-07 09:50:04 +02003673 if (lyxp_syntax_check(list->must[list->must_size-1].expr, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003674 goto error;
3675 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003676 }
3677 }
Radek Krejci25d782a2015-05-22 15:03:23 +02003678
Radek Krejcic071c542016-01-27 14:57:51 +01003679 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003680 goto error;
3681 }
3682
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003683 /* last part - process data nodes */
3684 LY_TREE_FOR_SAFE(root.child, next, sub) {
3685 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003686 node = read_yin_container(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003687 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003688 node = read_yin_leaflist(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003689 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003690 node = read_yin_leaf(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003691 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003692 node = read_yin_list(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003693 } else if (!strcmp(sub->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003694 node = read_yin_choice(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003695 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003696 node = read_yin_uses(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003697 } else if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003698 node = read_yin_grouping(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003699 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003700 node = read_yin_anyxml(module, retval, sub, resolve, unres);
Michal Vaskoc07187d2015-08-13 15:20:57 +02003701 } else {
3702 LOGINT;
3703 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003704 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003705 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003706 goto error;
3707 }
Radek Krejci73adb602015-07-02 18:07:40 +02003708
Michal Vasko345da0a2015-12-02 10:35:55 +01003709 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003710 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003711
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003712 if (!key_str) {
3713 /* config false list without a key */
3714 return retval;
3715 }
Michal Vasko0bd29d12015-08-19 11:45:49 +02003716 if (unres_schema_add_str(module, unres, list, UNRES_LIST_KEYS, key_str, key_line) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003717 goto error;
3718 }
Radek Krejci812b10a2015-05-28 16:48:25 +02003719
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003720 /* process unique statements */
3721 if (c_uniq) {
3722 list->unique = calloc(c_uniq, sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01003723 if (!list->unique) {
3724 LOGMEM;
3725 goto error;
3726 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003727 }
3728 LY_TREE_FOR_SAFE(uniq.child, next, sub) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003729 r = fill_yin_unique(module, retval, sub, &list->unique[list->unique_size], unres);
3730 list->unique_size++;
3731 if (r) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003732 goto error;
3733 }
Radek Krejci1e9b9992015-06-04 17:57:04 +02003734
Michal Vasko345da0a2015-12-02 10:35:55 +01003735 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003736 }
Radek Krejci1e9b9992015-06-04 17:57:04 +02003737
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003738 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003739
3740error:
3741
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003742 lys_node_free(retval, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003743 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003744 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003745 }
3746 while (uniq.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003747 lyxml_free(module->ctx, uniq.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003748 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003749
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003750 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003751}
3752
Michal Vasko0d343d12015-08-24 14:57:36 +02003753/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003754static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02003755read_yin_container(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
3756 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003757{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003758 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02003759 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02003760 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003761 struct lys_node_container *cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003762 const char *value;
3763 int r;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003764 int c_tpdf = 0, c_must = 0, c_ftrs = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003765
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003766 /* init */
3767 memset(&root, 0, sizeof root);
Radek Krejcie0674f82015-06-15 13:58:51 +02003768
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003769 cont = calloc(1, sizeof *cont);
Michal Vasko253035f2015-12-17 16:58:13 +01003770 if (!cont) {
3771 LOGMEM;
3772 return NULL;
3773 }
Radek Krejci76512572015-08-04 09:47:08 +02003774 cont->nodetype = LYS_CONTAINER;
3775 cont->prev = (struct lys_node *)cont;
3776 retval = (struct lys_node *)cont;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003777
Michal Vaskoe0c59842015-09-24 13:52:20 +02003778 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_CONFIG
3779 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003780 goto error;
3781 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003782
Radek Krejcia9544502015-08-14 08:24:29 +02003783 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
3784
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003785 /* process container's specific children */
3786 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci6764bb32015-07-03 15:16:04 +02003787 if (!sub->ns) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003788 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003789 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003790 continue;
3791 }
3792
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003793 if (!strcmp(sub->name, "presence")) {
3794 if (cont->presence) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003795 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003796 goto error;
3797 }
3798 GETVAL(value, sub, "value");
3799 cont->presence = lydict_insert(module->ctx, value, strlen(value));
Radek Krejci800af702015-06-02 13:46:01 +02003800
Michal Vasko345da0a2015-12-02 10:35:55 +01003801 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003802 } else if (!strcmp(sub->name, "when")) {
3803 if (cont->when) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003804 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003805 goto error;
3806 }
3807
3808 cont->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003809 if (!cont->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003810 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02003811 goto error;
3812 }
Michal Vaskofcdac172015-10-07 09:35:05 +02003813 if (lyxp_syntax_check(cont->when->cond, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003814 goto error;
3815 }
Michal Vasko345da0a2015-12-02 10:35:55 +01003816 lyxml_free(module->ctx, sub);
Radek Krejci4c31f122015-06-02 14:51:22 +02003817
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003818 /* data statements */
3819 } else if (!strcmp(sub->name, "container") ||
3820 !strcmp(sub->name, "leaf-list") ||
3821 !strcmp(sub->name, "leaf") ||
3822 !strcmp(sub->name, "list") ||
3823 !strcmp(sub->name, "choice") ||
3824 !strcmp(sub->name, "uses") ||
3825 !strcmp(sub->name, "grouping") ||
3826 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02003827 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02003828 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003829
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003830 /* array counters */
3831 } else if (!strcmp(sub->name, "typedef")) {
3832 c_tpdf++;
3833 } else if (!strcmp(sub->name, "must")) {
3834 c_must++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02003835 } else if (!strcmp(sub->name, "if-feature")) {
3836 c_ftrs++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003837 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003838 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003839 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003840 }
3841 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003842
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003843 /* middle part - process nodes with cardinality of 0..n except the data nodes */
3844 if (c_tpdf) {
3845 cont->tpdf = calloc(c_tpdf, sizeof *cont->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01003846 if (!cont->tpdf) {
3847 LOGMEM;
3848 goto error;
3849 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003850 }
3851 if (c_must) {
3852 cont->must = calloc(c_must, sizeof *cont->must);
Michal Vasko253035f2015-12-17 16:58:13 +01003853 if (!cont->must) {
3854 LOGMEM;
3855 goto error;
3856 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003857 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003858 if (c_ftrs) {
3859 cont->features = calloc(c_ftrs, sizeof *cont->features);
Michal Vasko253035f2015-12-17 16:58:13 +01003860 if (!cont->features) {
3861 LOGMEM;
3862 goto error;
3863 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003864 }
Radek Krejci800af702015-06-02 13:46:01 +02003865
Radek Krejci73adb602015-07-02 18:07:40 +02003866 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003867 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003868 r = fill_yin_typedef(module, retval, sub, &cont->tpdf[cont->tpdf_size], unres);
3869 cont->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003870 if (r) {
3871 goto error;
3872 }
3873 } else if (!strcmp(sub->name, "must")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003874 r = fill_yin_must(module, sub, &cont->must[cont->must_size]);
3875 cont->must_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003876 if (r) {
3877 goto error;
3878 }
Michal Vasko0d204592015-10-07 09:50:04 +02003879 if (lyxp_syntax_check(cont->must[cont->must_size-1].expr, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003880 goto error;
3881 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02003882 } else if (!strcmp(sub->name, "if-feature")) {
3883 GETVAL(value, sub, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02003884 if (!(value = transform_schema2json(module, value, LOGLINE(sub)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02003885 goto error;
3886 }
Radek Krejcicf509982015-12-15 09:22:44 +01003887 /* hack - store pointer to the parent node for later status check */
3888 cont->features[cont->features_size] = (struct lys_feature *)cont;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003889 r = unres_schema_add_str(module, unres, &cont->features[cont->features_size], UNRES_IFFEAT, value,
Radek Krejcife8582c2015-12-15 15:39:02 +01003890 LOGLINE(sub));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01003891 cont->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02003892 lydict_remove(module->ctx, value);
3893 if (r == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02003894 goto error;
3895 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003896 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003897 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003898
Radek Krejcic071c542016-01-27 14:57:51 +01003899 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02003900 goto error;
3901 }
3902
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003903 /* last part - process data nodes */
3904 LY_TREE_FOR_SAFE(root.child, next, sub) {
3905 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003906 node = read_yin_container(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003907 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003908 node = read_yin_leaflist(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003909 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003910 node = read_yin_leaf(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003911 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003912 node = read_yin_list(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003913 } else if (!strcmp(sub->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003914 node = read_yin_choice(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003915 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003916 node = read_yin_uses(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003917 } else if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003918 node = read_yin_grouping(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003919 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02003920 node = read_yin_anyxml(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003921 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02003922 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003923 goto error;
3924 }
Radek Krejci73adb602015-07-02 18:07:40 +02003925
Michal Vasko345da0a2015-12-02 10:35:55 +01003926 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003927 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003928
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003929 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003930
3931error:
3932
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003933 lys_node_free(retval, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003934 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01003935 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003936 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003937
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003938 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003939}
3940
Michal Vasko0d343d12015-08-24 14:57:36 +02003941/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02003942static struct lys_node *
Radek Krejci1d82ef62015-08-07 14:44:40 +02003943read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
Michal Vaskof02e3742015-08-05 16:27:02 +02003944 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003945{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003946 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02003947 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02003948 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02003949 struct lys_node_grp *grp;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003950 int r;
3951 int c_tpdf = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003952
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003953 /* init */
3954 memset(&root, 0, sizeof root);
Radek Krejcie0674f82015-06-15 13:58:51 +02003955
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003956 grp = calloc(1, sizeof *grp);
Michal Vasko253035f2015-12-17 16:58:13 +01003957 if (!grp) {
3958 LOGMEM;
3959 return NULL;
3960 }
Radek Krejci76512572015-08-04 09:47:08 +02003961 grp->nodetype = LYS_GROUPING;
3962 grp->prev = (struct lys_node *)grp;
3963 retval = (struct lys_node *)grp;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003964
Michal Vasko71e1aa82015-08-12 12:17:51 +02003965 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003966 goto error;
3967 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003968
Radek Krejcia9544502015-08-14 08:24:29 +02003969 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
3970
Radek Krejci1d82ef62015-08-07 14:44:40 +02003971 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02003972 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
3973 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01003974 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02003975 continue;
3976 }
3977
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003978 /* data statements */
3979 if (!strcmp(sub->name, "container") ||
3980 !strcmp(sub->name, "leaf-list") ||
3981 !strcmp(sub->name, "leaf") ||
3982 !strcmp(sub->name, "list") ||
3983 !strcmp(sub->name, "choice") ||
3984 !strcmp(sub->name, "uses") ||
3985 !strcmp(sub->name, "grouping") ||
3986 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02003987 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02003988 lyxml_add_child(module->ctx, &root, sub);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003989
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003990 /* array counters */
3991 } else if (!strcmp(sub->name, "typedef")) {
3992 c_tpdf++;
3993 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02003994 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003995 goto error;
3996 }
3997 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003998
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003999 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4000 if (c_tpdf) {
4001 grp->tpdf = calloc(c_tpdf, sizeof *grp->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004002 if (!grp->tpdf) {
4003 LOGMEM;
4004 goto error;
4005 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004006 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004007 LY_TREE_FOR(yin->child, sub) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004008 r = fill_yin_typedef(module, retval, sub, &grp->tpdf[grp->tpdf_size], unres);
4009 grp->tpdf_size++;
Radek Krejci73adb602015-07-02 18:07:40 +02004010 if (r) {
4011 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004012 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004013 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004014
Radek Krejcic071c542016-01-27 14:57:51 +01004015 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004016 goto error;
4017 }
4018
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004019 /* last part - process data nodes */
4020 LY_TREE_FOR_SAFE(root.child, next, sub) {
4021 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004022 node = read_yin_container(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004023 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004024 node = read_yin_leaflist(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004025 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004026 node = read_yin_leaf(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004027 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004028 node = read_yin_list(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004029 } else if (!strcmp(sub->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004030 node = read_yin_choice(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004031 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004032 node = read_yin_uses(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004033 } else if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004034 node = read_yin_grouping(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004035 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004036 node = read_yin_anyxml(module, retval, sub, resolve, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004037 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004038 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004039 goto error;
4040 }
Radek Krejci73adb602015-07-02 18:07:40 +02004041
Michal Vasko345da0a2015-12-02 10:35:55 +01004042 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004043 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004044
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004045 return retval;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004046
4047error:
4048
Radek Krejcifa0b5e02016-02-04 13:57:03 +01004049 lys_node_free(retval, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004050 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004051 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004052 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004053
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004054 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004055}
4056
Michal Vasko0d343d12015-08-24 14:57:36 +02004057/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004058static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02004059read_yin_input_output(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
4060 struct unres_schema *unres)
Michal Vasko38d01f72015-06-15 09:41:06 +02004061{
Radek Krejcie0674f82015-06-15 13:58:51 +02004062 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004063 struct lys_node *node = NULL;
Radek Krejci31fc30d2015-08-13 08:27:38 +02004064 struct lys_node *retval = NULL;
Radek Krejci4608ada2015-08-05 16:04:37 +02004065 struct lys_node_rpc_inout *inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02004066 int r;
4067 int c_tpdf = 0;
4068
Radek Krejcie0674f82015-06-15 13:58:51 +02004069 /* init */
4070 memset(&root, 0, sizeof root);
4071
Michal Vasko38d01f72015-06-15 09:41:06 +02004072 inout = calloc(1, sizeof *inout);
Michal Vasko253035f2015-12-17 16:58:13 +01004073 if (!inout) {
4074 LOGMEM;
4075 return NULL;
4076 }
Radek Krejci6acc8012015-08-13 09:07:04 +02004077 inout->prev = (struct lys_node *)inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02004078
4079 if (!strcmp(yin->name, "input")) {
Radek Krejci76512572015-08-04 09:47:08 +02004080 inout->nodetype = LYS_INPUT;
Michal Vasko38d01f72015-06-15 09:41:06 +02004081 } else if (!strcmp(yin->name, "output")) {
Radek Krejci76512572015-08-04 09:47:08 +02004082 inout->nodetype = LYS_OUTPUT;
Michal Vasko38d01f72015-06-15 09:41:06 +02004083 } else {
Michal Vasko0c888fd2015-08-11 15:54:08 +02004084 LOGINT;
Radek Krejci6acc8012015-08-13 09:07:04 +02004085 free(inout);
Michal Vasko0c888fd2015-08-11 15:54:08 +02004086 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02004087 }
4088
Radek Krejci76512572015-08-04 09:47:08 +02004089 retval = (struct lys_node *)inout;
Michal Vasko38d01f72015-06-15 09:41:06 +02004090
Radek Krejci6a113852015-07-03 16:04:20 +02004091 if (read_yin_common(module, parent, retval, yin, OPT_MODULE | OPT_NACMEXT)) {
Michal Vaskoebeac942015-06-15 12:11:50 +02004092 goto error;
4093 }
4094
Radek Krejcia9544502015-08-14 08:24:29 +02004095 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4096
Michal Vasko38d01f72015-06-15 09:41:06 +02004097 /* data statements */
4098 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004099 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4100 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004101 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004102 continue;
4103 }
4104
Michal Vasko38d01f72015-06-15 09:41:06 +02004105 if (!strcmp(sub->name, "container") ||
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004106 !strcmp(sub->name, "leaf-list") ||
4107 !strcmp(sub->name, "leaf") ||
4108 !strcmp(sub->name, "list") ||
4109 !strcmp(sub->name, "choice") ||
4110 !strcmp(sub->name, "uses") ||
4111 !strcmp(sub->name, "grouping") ||
4112 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004113 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004114 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004115
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004116 /* array counters */
Michal Vasko38d01f72015-06-15 09:41:06 +02004117 } else if (!strcmp(sub->name, "typedef")) {
4118 c_tpdf++;
Radek Krejcid2bfa792015-07-02 16:45:29 +02004119
Michal Vasko38d01f72015-06-15 09:41:06 +02004120 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02004121 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02004122 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02004123 }
4124 }
4125
4126 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4127 if (c_tpdf) {
4128 inout->tpdf = calloc(c_tpdf, sizeof *inout->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004129 if (!inout->tpdf) {
4130 LOGMEM;
4131 goto error;
4132 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004133 }
4134
Radek Krejci73adb602015-07-02 18:07:40 +02004135 LY_TREE_FOR(yin->child, sub) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004136 r = fill_yin_typedef(module, retval, sub, &inout->tpdf[inout->tpdf_size], unres);
4137 inout->tpdf_size++;
Radek Krejci73adb602015-07-02 18:07:40 +02004138 if (r) {
4139 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02004140 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004141 }
4142
Radek Krejcic071c542016-01-27 14:57:51 +01004143 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004144 goto error;
4145 }
4146
Michal Vasko38d01f72015-06-15 09:41:06 +02004147 /* last part - process data nodes */
4148 LY_TREE_FOR_SAFE(root.child, next, sub) {
4149 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004150 node = read_yin_container(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004151 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004152 node = read_yin_leaflist(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004153 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004154 node = read_yin_leaf(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004155 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004156 node = read_yin_list(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004157 } else if (!strcmp(sub->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004158 node = read_yin_choice(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004159 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004160 node = read_yin_uses(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004161 } else if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004162 node = read_yin_grouping(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004163 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004164 node = read_yin_anyxml(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004165 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004166 if (!node) {
Michal Vasko38d01f72015-06-15 09:41:06 +02004167 goto error;
4168 }
Radek Krejci73adb602015-07-02 18:07:40 +02004169
Michal Vasko345da0a2015-12-02 10:35:55 +01004170 lyxml_free(module->ctx, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004171 }
4172
Michal Vasko38d01f72015-06-15 09:41:06 +02004173 return retval;
4174
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004175error:
Michal Vasko38d01f72015-06-15 09:41:06 +02004176
Radek Krejcifa0b5e02016-02-04 13:57:03 +01004177 lys_node_free(retval, NULL);
Michal Vasko3a9abf82015-06-17 10:18:26 +02004178 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004179 lyxml_free(module->ctx, root.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02004180 }
4181
4182 return NULL;
4183}
4184
Michal Vasko0d343d12015-08-24 14:57:36 +02004185/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004186static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02004187read_yin_notif(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
4188 struct unres_schema *unres)
Michal Vasko0ea41032015-06-16 08:53:55 +02004189{
Michal Vaskoc6551b32015-06-16 10:51:43 +02004190 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004191 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02004192 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004193 struct lys_node_notif *notif;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004194 const char *value;
Michal Vasko0ea41032015-06-16 08:53:55 +02004195 int r;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004196 int c_tpdf = 0, c_ftrs = 0;
Michal Vasko0ea41032015-06-16 08:53:55 +02004197
Michal Vaskoc6551b32015-06-16 10:51:43 +02004198 memset(&root, 0, sizeof root);
4199
Michal Vasko0ea41032015-06-16 08:53:55 +02004200 notif = calloc(1, sizeof *notif);
Michal Vasko253035f2015-12-17 16:58:13 +01004201 if (!notif) {
4202 LOGMEM;
4203 return NULL;
4204 }
Radek Krejci76512572015-08-04 09:47:08 +02004205 notif->nodetype = LYS_NOTIF;
4206 notif->prev = (struct lys_node *)notif;
4207 retval = (struct lys_node *)notif;
Michal Vasko0ea41032015-06-16 08:53:55 +02004208
Radek Krejci6a113852015-07-03 16:04:20 +02004209 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_NACMEXT)) {
Michal Vasko0ea41032015-06-16 08:53:55 +02004210 goto error;
4211 }
4212
Radek Krejcia9544502015-08-14 08:24:29 +02004213 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4214
Michal Vasko0ea41032015-06-16 08:53:55 +02004215 /* process rpc's specific children */
4216 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004217 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4218 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004219 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004220 continue;
4221 }
4222
Michal Vasko0ea41032015-06-16 08:53:55 +02004223 /* data statements */
4224 if (!strcmp(sub->name, "container") ||
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004225 !strcmp(sub->name, "leaf-list") ||
4226 !strcmp(sub->name, "leaf") ||
4227 !strcmp(sub->name, "list") ||
4228 !strcmp(sub->name, "choice") ||
4229 !strcmp(sub->name, "uses") ||
4230 !strcmp(sub->name, "grouping") ||
4231 !strcmp(sub->name, "anyxml")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004232 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004233 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko0ea41032015-06-16 08:53:55 +02004234
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004235 /* array counters */
Michal Vasko0ea41032015-06-16 08:53:55 +02004236 } else if (!strcmp(sub->name, "typedef")) {
4237 c_tpdf++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004238 } else if (!strcmp(sub->name, "if-feature")) {
4239 c_ftrs++;
Michal Vasko0ea41032015-06-16 08:53:55 +02004240 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02004241 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Michal Vasko0ea41032015-06-16 08:53:55 +02004242 goto error;
Michal Vasko0ea41032015-06-16 08:53:55 +02004243 }
4244 }
4245
4246 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4247 if (c_tpdf) {
4248 notif->tpdf = calloc(c_tpdf, sizeof *notif->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004249 if (!notif->tpdf) {
4250 LOGMEM;
4251 goto error;
4252 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004253 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004254 if (c_ftrs) {
4255 notif->features = calloc(c_ftrs, sizeof *notif->features);
Michal Vasko253035f2015-12-17 16:58:13 +01004256 if (!notif->features) {
4257 LOGMEM;
4258 goto error;
4259 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004260 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004261
Radek Krejci73adb602015-07-02 18:07:40 +02004262 LY_TREE_FOR(yin->child, sub) {
Michal Vasko0ea41032015-06-16 08:53:55 +02004263 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004264 r = fill_yin_typedef(module, retval, sub, &notif->tpdf[notif->tpdf_size], unres);
4265 notif->tpdf_size++;
Michal Vasko0ea41032015-06-16 08:53:55 +02004266 if (r) {
4267 goto error;
4268 }
Radek Krejci0b24d752015-07-02 15:02:27 +02004269 } else if (!strcmp(sub->name, "if-features")) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02004270 GETVAL(value, sub, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02004271 if (!(value = transform_schema2json(module, value, LOGLINE(sub)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02004272 goto error;
4273 }
Radek Krejcicf509982015-12-15 09:22:44 +01004274 /* hack - store pointer to the parent node for later status check */
4275 notif->features[notif->features_size] = (struct lys_feature *)notif;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004276 r = unres_schema_add_str(module, unres, &notif->features[notif->features_size], UNRES_IFFEAT, value,
Radek Krejcife8582c2015-12-15 15:39:02 +01004277 LOGLINE(sub));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004278 notif->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02004279 lydict_remove(module->ctx, value);
4280 if (r == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004281 goto error;
4282 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004283 }
Michal Vasko0ea41032015-06-16 08:53:55 +02004284 }
4285
Radek Krejcic071c542016-01-27 14:57:51 +01004286 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004287 goto error;
4288 }
4289
Michal Vasko0ea41032015-06-16 08:53:55 +02004290 /* last part - process data nodes */
4291 LY_TREE_FOR_SAFE(root.child, next, sub) {
4292 if (!strcmp(sub->name, "container")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004293 node = read_yin_container(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004294 } else if (!strcmp(sub->name, "leaf-list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004295 node = read_yin_leaflist(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004296 } else if (!strcmp(sub->name, "leaf")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004297 node = read_yin_leaf(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004298 } else if (!strcmp(sub->name, "list")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004299 node = read_yin_list(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004300 } else if (!strcmp(sub->name, "choice")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004301 node = read_yin_choice(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004302 } else if (!strcmp(sub->name, "uses")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004303 node = read_yin_uses(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004304 } else if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004305 node = read_yin_grouping(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004306 } else if (!strcmp(sub->name, "anyxml")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004307 node = read_yin_anyxml(module, retval, sub, resolve, unres);
Michal Vasko0ea41032015-06-16 08:53:55 +02004308 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004309 if (!node) {
Michal Vasko0ea41032015-06-16 08:53:55 +02004310 goto error;
4311 }
Radek Krejci73adb602015-07-02 18:07:40 +02004312
Michal Vasko345da0a2015-12-02 10:35:55 +01004313 lyxml_free(module->ctx, sub);
Michal Vasko0ea41032015-06-16 08:53:55 +02004314 }
4315
Michal Vasko0ea41032015-06-16 08:53:55 +02004316 return retval;
4317
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004318error:
Michal Vasko0ea41032015-06-16 08:53:55 +02004319
Radek Krejcifa0b5e02016-02-04 13:57:03 +01004320 lys_node_free(retval, NULL);
Michal Vasko0ea41032015-06-16 08:53:55 +02004321 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004322 lyxml_free(module->ctx, root.child);
Michal Vasko0ea41032015-06-16 08:53:55 +02004323 }
4324
4325 return NULL;
4326}
4327
Michal Vasko0d343d12015-08-24 14:57:36 +02004328/* logs directly */
Radek Krejci76512572015-08-04 09:47:08 +02004329static struct lys_node *
Michal Vaskof02e3742015-08-05 16:27:02 +02004330read_yin_rpc(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
4331 struct unres_schema *unres)
Michal Vasko38d01f72015-06-15 09:41:06 +02004332{
Radek Krejcie0674f82015-06-15 13:58:51 +02004333 struct lyxml_elem *sub, *next, root;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004334 struct lys_node *node = NULL;
Radek Krejci76512572015-08-04 09:47:08 +02004335 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004336 struct lys_node_rpc *rpc;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004337 const char *value;
Michal Vasko38d01f72015-06-15 09:41:06 +02004338 int r;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004339 int c_tpdf = 0, c_ftrs = 0;
Michal Vasko38d01f72015-06-15 09:41:06 +02004340
Radek Krejcie0674f82015-06-15 13:58:51 +02004341 /* init */
4342 memset(&root, 0, sizeof root);
4343
Michal Vasko38d01f72015-06-15 09:41:06 +02004344 rpc = calloc(1, sizeof *rpc);
Michal Vasko253035f2015-12-17 16:58:13 +01004345 if (!rpc) {
4346 LOGMEM;
4347 return NULL;
4348 }
Radek Krejci76512572015-08-04 09:47:08 +02004349 rpc->nodetype = LYS_RPC;
4350 rpc->prev = (struct lys_node *)rpc;
4351 retval = (struct lys_node *)rpc;
Michal Vasko38d01f72015-06-15 09:41:06 +02004352
Radek Krejci6a113852015-07-03 16:04:20 +02004353 if (read_yin_common(module, parent, retval, yin, OPT_IDENT | OPT_MODULE | OPT_NACMEXT)) {
Michal Vasko38d01f72015-06-15 09:41:06 +02004354 goto error;
4355 }
4356
Radek Krejcia9544502015-08-14 08:24:29 +02004357 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4358
Michal Vasko38d01f72015-06-15 09:41:06 +02004359 /* process rpc's specific children */
4360 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004361 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4362 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004363 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004364 continue;
4365 }
4366
Michal Vasko38d01f72015-06-15 09:41:06 +02004367 if (!strcmp(sub->name, "input")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004368 if (rpc->child
Radek Krejci76512572015-08-04 09:47:08 +02004369 && (rpc->child->nodetype == LYS_INPUT
4370 || (rpc->child->next && rpc->child->next->nodetype == LYS_INPUT))) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02004371 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02004372 goto error;
4373 }
Michal Vaskof3930de2015-10-22 12:03:59 +02004374 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004375 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004376 } else if (!strcmp(sub->name, "output")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004377 if (rpc->child
Radek Krejci76512572015-08-04 09:47:08 +02004378 && (rpc->child->nodetype == LYS_INPUT
4379 || (rpc->child->next && rpc->child->next->nodetype == LYS_INPUT))) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02004380 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02004381 goto error;
4382 }
Michal Vaskof3930de2015-10-22 12:03:59 +02004383 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004384 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004385
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004386 /* data statements */
Michal Vasko38d01f72015-06-15 09:41:06 +02004387 } else if (!strcmp(sub->name, "grouping")) {
Michal Vaskof3930de2015-10-22 12:03:59 +02004388 lyxml_unlink_elem(module->ctx, sub, 2);
Michal Vaskof8879c22015-08-21 09:07:36 +02004389 lyxml_add_child(module->ctx, &root, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004390
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004391 /* array counters */
Michal Vasko38d01f72015-06-15 09:41:06 +02004392 } else if (!strcmp(sub->name, "typedef")) {
4393 c_tpdf++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004394 } else if (!strcmp(sub->name, "if-feature")) {
4395 c_ftrs++;
Michal Vasko38d01f72015-06-15 09:41:06 +02004396 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02004397 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Michal Vasko38d01f72015-06-15 09:41:06 +02004398 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02004399 }
4400 }
4401
4402 /* middle part - process nodes with cardinality of 0..n except the data nodes */
4403 if (c_tpdf) {
4404 rpc->tpdf = calloc(c_tpdf, sizeof *rpc->tpdf);
Michal Vasko253035f2015-12-17 16:58:13 +01004405 if (!rpc->tpdf) {
4406 LOGMEM;
4407 goto error;
4408 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004409 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004410 if (c_ftrs) {
4411 rpc->features = calloc(c_ftrs, sizeof *rpc->features);
Michal Vasko253035f2015-12-17 16:58:13 +01004412 if (!rpc->features) {
4413 LOGMEM;
4414 goto error;
4415 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004416 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004417
Radek Krejci73adb602015-07-02 18:07:40 +02004418 LY_TREE_FOR(yin->child, sub) {
Michal Vasko38d01f72015-06-15 09:41:06 +02004419 if (!strcmp(sub->name, "typedef")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004420 r = fill_yin_typedef(module, retval, sub, &rpc->tpdf[rpc->tpdf_size], unres);
4421 rpc->tpdf_size++;
Michal Vasko38d01f72015-06-15 09:41:06 +02004422 if (r) {
4423 goto error;
4424 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004425 } else if (!strcmp(sub->name, "if-feature")) {
4426 GETVAL(value, sub, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02004427 if (!(value = transform_schema2json(module, value, LOGLINE(sub)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02004428 goto error;
4429 }
Radek Krejcicf509982015-12-15 09:22:44 +01004430 /* hack - store pointer to the parent node for later status check */
4431 rpc->features[rpc->features_size] = (struct lys_feature *)rpc;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004432 r = unres_schema_add_str(module, unres, &rpc->features[rpc->features_size], UNRES_IFFEAT, value,
Radek Krejcife8582c2015-12-15 15:39:02 +01004433 LOGLINE(sub));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004434 rpc->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02004435 lydict_remove(module->ctx, value);
4436 if (r == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004437 goto error;
4438 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004439 }
Michal Vasko38d01f72015-06-15 09:41:06 +02004440 }
4441
Radek Krejcic071c542016-01-27 14:57:51 +01004442 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004443 goto error;
4444 }
4445
Michal Vasko38d01f72015-06-15 09:41:06 +02004446 /* last part - process data nodes */
4447 LY_TREE_FOR_SAFE(root.child, next, sub) {
4448 if (!strcmp(sub->name, "grouping")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004449 node = read_yin_grouping(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004450 } else if (!strcmp(sub->name, "input")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004451 node = read_yin_input_output(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004452 } else if (!strcmp(sub->name, "output")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004453 node = read_yin_input_output(module, retval, sub, resolve, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02004454 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004455 if (!node) {
Michal Vasko38d01f72015-06-15 09:41:06 +02004456 goto error;
4457 }
Radek Krejci73adb602015-07-02 18:07:40 +02004458
Michal Vasko345da0a2015-12-02 10:35:55 +01004459 lyxml_free(module->ctx, sub);
Michal Vasko38d01f72015-06-15 09:41:06 +02004460 }
4461
Michal Vasko38d01f72015-06-15 09:41:06 +02004462 return retval;
4463
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004464error:
Michal Vasko38d01f72015-06-15 09:41:06 +02004465
Radek Krejcifa0b5e02016-02-04 13:57:03 +01004466 lys_node_free(retval, NULL);
Michal Vasko3a9abf82015-06-17 10:18:26 +02004467 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004468 lyxml_free(module->ctx, root.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02004469 }
4470
4471 return NULL;
4472}
4473
Michal Vasko0d343d12015-08-24 14:57:36 +02004474/* logs directly
4475 *
Radek Krejci74705112015-06-05 10:25:44 +02004476 * resolve - referenced grouping should be bounded to the namespace (resolved)
4477 * only when uses does not appear in grouping. In a case of grouping's uses,
4478 * we just get information but we do not apply augment or refine to it.
4479 */
Radek Krejci76512572015-08-04 09:47:08 +02004480static struct lys_node *
Radek Krejcia9544502015-08-14 08:24:29 +02004481read_yin_uses(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int resolve,
Michal Vaskof02e3742015-08-05 16:27:02 +02004482 struct unres_schema *unres)
Radek Krejci74705112015-06-05 10:25:44 +02004483{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004484 struct lyxml_elem *sub, *next;
Radek Krejci76512572015-08-04 09:47:08 +02004485 struct lys_node *retval;
Radek Krejcib8048692015-08-05 13:36:34 +02004486 struct lys_node_uses *uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004487 const char *value;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004488 int c_ref = 0, c_aug = 0, c_ftrs = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004489 int r;
Radek Krejci74705112015-06-05 10:25:44 +02004490
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004491 uses = calloc(1, sizeof *uses);
Michal Vasko253035f2015-12-17 16:58:13 +01004492 if (!uses) {
4493 LOGMEM;
4494 return NULL;
4495 }
Radek Krejci76512572015-08-04 09:47:08 +02004496 uses->nodetype = LYS_USES;
4497 uses->prev = (struct lys_node *)uses;
4498 retval = (struct lys_node *)uses;
Radek Krejci74705112015-06-05 10:25:44 +02004499
Radek Krejcia9544502015-08-14 08:24:29 +02004500 GETVAL(value, yin, "name");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004501 uses->name = lydict_insert(module->ctx, value, 0);
Radek Krejci106efc02015-06-10 14:36:27 +02004502
Michal Vaskoe0c59842015-09-24 13:52:20 +02004503 if (read_yin_common(module, parent, retval, yin, OPT_MODULE
4504 | (parent && (parent->nodetype == LYS_GROUPING) ? 0 : OPT_NACMEXT) | (resolve ? OPT_INHERIT : 0))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004505 goto error;
4506 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02004507
Radek Krejcia9544502015-08-14 08:24:29 +02004508 LOGDBG("YIN: parsing %s statement \"%s\"", yin->name, retval->name);
4509
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004510 /* get other properties of uses */
Radek Krejcia9544502015-08-14 08:24:29 +02004511 LY_TREE_FOR_SAFE(yin->child, next, sub) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004512 if (!sub->ns || strcmp(sub->ns->value, LY_NSYIN)) {
4513 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004514 lyxml_free(module->ctx, sub);
Radek Krejci0d70c372015-07-02 16:23:10 +02004515 continue;
4516 }
4517
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004518 if (!strcmp(sub->name, "refine")) {
4519 c_ref++;
4520 } else if (!strcmp(sub->name, "augment")) {
4521 c_aug++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004522 } else if (!strcmp(sub->name, "if-feature")) {
Radek Krejci56e89772015-06-19 10:00:54 +02004523 c_ftrs++;
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004524 } else if (!strcmp(sub->name, "when")) {
4525 if (uses->when) {
Radek Krejcia9544502015-08-14 08:24:29 +02004526 LOGVAL(LYE_TOOMANY, LOGLINE(sub), sub->name, yin->name);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004527 goto error;
4528 }
4529
4530 uses->when = read_yin_when(module, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004531 if (!uses->when) {
Michal Vasko345da0a2015-12-02 10:35:55 +01004532 lyxml_free(module->ctx, sub);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02004533 goto error;
4534 }
Michal Vaskofcdac172015-10-07 09:35:05 +02004535 if (lyxp_syntax_check(uses->when->cond, LOGLINE(sub))) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004536 goto error;
4537 }
Michal Vasko345da0a2015-12-02 10:35:55 +01004538 lyxml_free(module->ctx, sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004539 } else {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02004540 LOGVAL(LYE_INSTMT, LOGLINE(sub), sub->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004541 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004542 }
4543 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02004544
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004545 /* process properties with cardinality 0..n */
4546 if (c_ref) {
4547 uses->refine = calloc(c_ref, sizeof *uses->refine);
Michal Vasko253035f2015-12-17 16:58:13 +01004548 if (!uses->refine) {
4549 LOGMEM;
4550 goto error;
4551 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004552 }
4553 if (c_aug) {
4554 uses->augment = calloc(c_aug, sizeof *uses->augment);
Michal Vasko253035f2015-12-17 16:58:13 +01004555 if (!uses->augment) {
4556 LOGMEM;
4557 goto error;
4558 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004559 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004560 if (c_ftrs) {
4561 uses->features = calloc(c_ftrs, sizeof *uses->features);
Michal Vasko253035f2015-12-17 16:58:13 +01004562 if (!uses->features) {
4563 LOGMEM;
4564 goto error;
4565 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004566 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02004567
Radek Krejcic071c542016-01-27 14:57:51 +01004568 if (lys_node_addchild(parent, module->type ? ((struct lys_submodule *)module)->belongsto: module, retval)) {
Michal Vasko3a0043f2015-08-12 12:11:30 +02004569 goto error;
4570 }
4571
Radek Krejcia9544502015-08-14 08:24:29 +02004572 LY_TREE_FOR(yin->child, sub) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004573 if (!strcmp(sub->name, "refine")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004574 r = fill_yin_refine(module, sub, &uses->refine[uses->refine_size]);
4575 uses->refine_size++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004576 if (r) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02004577 goto error;
4578 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004579 } else if (!strcmp(sub->name, "augment")) {
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004580 r = fill_yin_augment(module, retval, sub, &uses->augment[uses->augment_size], unres);
4581 uses->augment_size++;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02004582 if (r) {
4583 goto error;
4584 }
4585 } else if (!strcmp(sub->name, "if-feature")) {
4586 GETVAL(value, sub, "name");
Michal Vaskofba15262015-10-21 12:10:28 +02004587 if (!(value = transform_schema2json(module, value, LOGLINE(sub)))) {
Michal Vasko2d851a92015-10-20 16:16:36 +02004588 goto error;
4589 }
Radek Krejcicf509982015-12-15 09:22:44 +01004590 /* hack - store pointer to the parent node for later status check */
4591 uses->features[uses->features_size] = (struct lys_feature *)uses;
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004592 r = unres_schema_add_str(module, unres, &uses->features[uses->features_size], UNRES_IFFEAT, value,
Michal Vasko2d851a92015-10-20 16:16:36 +02004593 LOGLINE(sub));
Radek Krejci7d74ebc2015-12-10 16:05:02 +01004594 uses->features_size++;
Michal Vasko2d851a92015-10-20 16:16:36 +02004595 lydict_remove(module->ctx, value);
4596 if (r == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004597 goto error;
4598 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004599 }
4600 }
Radek Krejci3bde87f2015-06-05 16:51:58 +02004601
Michal Vasko0bd29d12015-08-19 11:45:49 +02004602 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL, LOGLINE(yin)) == -1) {
Michal Vasko3ab70fc2015-08-17 14:06:23 +02004603 goto error;
4604 }
Radek Krejci74705112015-06-05 10:25:44 +02004605
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004606 if (resolve) {
4607 /* inherit config flag */
4608 if (parent) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02004609 retval->flags |= parent->flags & LYS_CONFIG_MASK;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004610 } else {
4611 /* default config is true */
Radek Krejci1574a8d2015-08-03 14:16:52 +02004612 retval->flags |= LYS_CONFIG_W;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004613 }
4614 }
Radek Krejcib388c152015-06-04 17:03:03 +02004615
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004616 return retval;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02004617
4618error:
4619
Radek Krejcifa0b5e02016-02-04 13:57:03 +01004620 lys_node_free(retval, NULL);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02004621
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004622 return NULL;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02004623}
4624
Michal Vasko0d343d12015-08-24 14:57:36 +02004625/* logs directly
4626 *
4627 * common code for yin_read_module() and yin_read_submodule()
4628 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004629static int
Radek Krejcic071c542016-01-27 14:57:51 +01004630read_sub_module(struct lys_module *module, struct lys_submodule *submodule, struct lyxml_elem *yin,
4631 struct unres_schema *unres)
Radek Krejcida04f4a2015-05-21 12:54:09 +02004632{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004633 struct ly_ctx *ctx = module->ctx;
Michal Vasko2f7925f2015-10-21 15:06:56 +02004634 struct lyxml_elem *next, *child, *child2, root, grps, augs;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004635 struct lys_node *node = NULL;
Radek Krejcic071c542016-01-27 14:57:51 +01004636 struct lys_module *trg;
4637 struct lys_import *aux_imp;
4638 struct lys_include *aux_inc, inc;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004639 const char *value;
Radek Krejcic071c542016-01-27 14:57:51 +01004640 int i, j, r;
4641 int inc_size_aux = 0;
4642 int version_flag = 0;
Radek Krejci3cf9e222015-06-18 11:37:50 +02004643 /* counters */
Radek Krejcieb00f512015-07-01 16:44:58 +02004644 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 +02004645
Radek Krejcic071c542016-01-27 14:57:51 +01004646 /* to simplify code, store the module/submodule being processed as trg */
4647 trg = submodule ? (struct lys_module*)submodule : module;
4648
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004649 /* init */
4650 memset(&root, 0, sizeof root);
4651 memset(&grps, 0, sizeof grps);
Michal Vasko2f7925f2015-10-21 15:06:56 +02004652 memset(&augs, 0, sizeof augs);
Radek Krejcie0674f82015-06-15 13:58:51 +02004653
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004654 /*
4655 * in the first run, we process elements with cardinality of 1 or 0..1 and
4656 * count elements with cardinality 0..n. Data elements (choices, containers,
4657 * leafs, lists, leaf-lists) are moved aside to be processed last, since we
4658 * need have all top-level and groupings already prepared at that time. In
4659 * the middle loop, we process other elements with carinality of 0..n since
4660 * we need to allocate arrays to store them.
4661 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004662 LY_TREE_FOR_SAFE(yin->child, next, child) {
4663 if (!child->ns || strcmp(child->ns->value, LY_NSYIN)) {
Radek Krejci0d70c372015-07-02 16:23:10 +02004664 /* garbage */
Michal Vasko345da0a2015-12-02 10:35:55 +01004665 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004666 continue;
4667 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004668
Radek Krejcic071c542016-01-27 14:57:51 +01004669 if (!submodule && !strcmp(child->name, "namespace")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004670 if (module->ns) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004671 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004672 goto error;
4673 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004674 GETVAL(value, child, "uri");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004675 module->ns = lydict_insert(ctx, value, strlen(value));
Michal Vasko345da0a2015-12-02 10:35:55 +01004676 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004677 } else if (!submodule && !strcmp(child->name, "prefix")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004678 if (module->prefix) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004679 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004680 goto error;
4681 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004682 GETVAL(value, child, "value");
Radek Krejcic6556022016-01-27 15:16:45 +01004683 if (lyp_check_identifier(value, LY_IDENT_PREFIX, LOGLINE(child), module, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004684 goto error;
4685 }
4686 module->prefix = lydict_insert(ctx, value, strlen(value));
Michal Vasko345da0a2015-12-02 10:35:55 +01004687 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004688 } else if (submodule && !strcmp(child->name, "belongs-to")) {
4689 if (submodule->prefix) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004690 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004691 goto error;
4692 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004693 GETVAL(value, child, "module");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004694 if (value != submodule->belongsto->name) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004695 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004696 goto error;
4697 }
Radek Krejcif3886932015-06-04 17:36:06 +02004698
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004699 /* get the prefix substatement, start with checks */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004700 if (!child->child) {
4701 LOGVAL(LYE_MISSSTMT2, LOGLINE(child), "prefix", child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004702 goto error;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004703 } else if (strcmp(child->child->name, "prefix")) {
4704 LOGVAL(LYE_INSTMT, LOGLINE(child->child), child->child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004705 goto error;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004706 } else if (child->child->next) {
4707 LOGVAL(LYE_INSTMT, LOGLINE(child->child->next), child->child->next->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004708 goto error;
4709 }
4710 /* and now finally get the value */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004711 GETVAL(value, child->child, "value");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004712 /* check here differs from a generic prefix check, since this prefix
4713 * don't have to be unique
Michal Vasko38d01f72015-06-15 09:41:06 +02004714 */
Radek Krejcic6556022016-01-27 15:16:45 +01004715 if (lyp_check_identifier(value, LY_IDENT_NAME, LOGLINE(child->child), NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004716 goto error;
4717 }
Radek Krejcic071c542016-01-27 14:57:51 +01004718 submodule->prefix = lydict_insert(ctx, value, strlen(value));
Radek Krejci0af13872015-05-30 11:50:52 +02004719
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004720 /* we are done with belongs-to */
Michal Vasko345da0a2015-12-02 10:35:55 +01004721 lyxml_free(ctx, child);
Radek Krejcieb00f512015-07-01 16:44:58 +02004722
4723 /* counters (statements with n..1 cardinality) */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004724 } else if (!strcmp(child->name, "import")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004725 c_imp++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004726 } else if (!strcmp(child->name, "revision")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004727 c_rev++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004728 } else if (!strcmp(child->name, "typedef")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004729 c_tpdf++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004730 } else if (!strcmp(child->name, "identity")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004731 c_ident++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004732 } else if (!strcmp(child->name, "include")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004733 c_inc++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004734 } else if (!strcmp(child->name, "augment")) {
Radek Krejcif5be10f2015-06-16 13:29:36 +02004735 c_aug++;
Michal Vasko2f7925f2015-10-21 15:06:56 +02004736 /* keep augments separated, processed last */
Radek Krejcic071c542016-01-27 14:57:51 +01004737 lyxml_unlink_elem(ctx, child, 2);
4738 lyxml_add_child(ctx, &augs, child);
Michal Vasko2f7925f2015-10-21 15:06:56 +02004739
Radek Krejci1d82ef62015-08-07 14:44:40 +02004740 } else if (!strcmp(child->name, "feature")) {
Radek Krejci3cf9e222015-06-18 11:37:50 +02004741 c_ftrs++;
Radek Krejci1d82ef62015-08-07 14:44:40 +02004742 } else if (!strcmp(child->name, "deviation")) {
Radek Krejcieb00f512015-07-01 16:44:58 +02004743 c_dev++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02004744
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004745 /* data statements */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004746 } else if (!strcmp(child->name, "container") ||
4747 !strcmp(child->name, "leaf-list") ||
4748 !strcmp(child->name, "leaf") ||
4749 !strcmp(child->name, "list") ||
4750 !strcmp(child->name, "choice") ||
4751 !strcmp(child->name, "uses") ||
Michal Vasko7ffc3052015-10-21 15:05:56 +02004752 !strcmp(child->name, "anyxml") ||
4753 !strcmp(child->name, "rpc") ||
4754 !strcmp(child->name, "notification")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004755 lyxml_unlink_elem(ctx, child, 2);
4756 lyxml_add_child(ctx, &root, child);
Michal Vasko7ffc3052015-10-21 15:05:56 +02004757
Radek Krejci1d82ef62015-08-07 14:44:40 +02004758 } else if (!strcmp(child->name, "grouping")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004759 /* keep groupings separated and process them before other data statements */
Radek Krejcic071c542016-01-27 14:57:51 +01004760 lyxml_unlink_elem(ctx, child, 2);
4761 lyxml_add_child(ctx, &grps, child);
Radek Krejcida04f4a2015-05-21 12:54:09 +02004762
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004763 /* optional statements */
Radek Krejci1d82ef62015-08-07 14:44:40 +02004764 } else if (!strcmp(child->name, "description")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004765 if (trg->dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004766 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004767 goto error;
4768 }
Radek Krejcic071c542016-01-27 14:57:51 +01004769 trg->dsc = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01004770 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004771 if (!trg->dsc) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004772 goto error;
4773 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004774 } else if (!strcmp(child->name, "reference")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004775 if (trg->ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004776 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004777 goto error;
4778 }
Radek Krejcic071c542016-01-27 14:57:51 +01004779 trg->ref = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01004780 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004781 if (!trg->ref) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004782 goto error;
4783 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004784 } else if (!strcmp(child->name, "organization")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004785 if (trg->org) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004786 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004787 goto error;
4788 }
Radek Krejcic071c542016-01-27 14:57:51 +01004789 trg->org = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01004790 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004791 if (!trg->org) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004792 goto error;
4793 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004794 } else if (!strcmp(child->name, "contact")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004795 if (trg->contact) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004796 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004797 goto error;
4798 }
Radek Krejcic071c542016-01-27 14:57:51 +01004799 trg->contact = read_yin_subnode(ctx, child, "text");
Michal Vasko345da0a2015-12-02 10:35:55 +01004800 lyxml_free(ctx, child);
Radek Krejcic071c542016-01-27 14:57:51 +01004801 if (!trg->contact) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004802 goto error;
4803 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004804 } else if (!strcmp(child->name, "yang-version")) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004805 /* TODO: support YANG 1.1 ? */
Radek Krejcic071c542016-01-27 14:57:51 +01004806 if (version_flag) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004807 LOGVAL(LYE_TOOMANY, LOGLINE(child), child->name, yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004808 goto error;
4809 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004810 GETVAL(value, child, "value");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004811 if (strcmp(value, "1")) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004812 LOGVAL(LYE_INARG, LOGLINE(child), value, "yang-version");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004813 goto error;
4814 }
Radek Krejcic071c542016-01-27 14:57:51 +01004815 version_flag = 1;
4816 if (!submodule) {
4817 module->version = 1;
4818 } /* 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 +01004819 lyxml_free(ctx, child);
Michal Vasko38d01f72015-06-15 09:41:06 +02004820
Radek Krejci1d82ef62015-08-07 14:44:40 +02004821 } else if (!strcmp(child->name, "extension")) {
4822 GETVAL(value, child, "name");
Radek Krejci5166a892015-07-02 16:44:24 +02004823
Radek Krejci3d468122015-10-02 13:36:12 +02004824 /* we have the following supported (hardcoded) extensions: */
4825 /* ietf-netconf's get-filter-element-attributes */
4826 if (!strcmp(module->ns, LY_NSNC) &&
4827 !strcmp(value, "get-filter-element-attributes")) {
4828 LOGDBG("NETCONF filter extension found");
4829 /* NACM's default-deny-write and default-deny-all */
4830 } else if (!strcmp(module->ns, LY_NSNACM) &&
4831 (!strcmp(value, "default-deny-write") || !strcmp(value, "default-deny-all"))) {
4832 LOGDBG("NACM extension found");
4833 /* other extensions are not supported, so inform about such an extension */
4834 } else {
Radek Krejci6764bb32015-07-03 15:16:04 +02004835 LOGWRN("Not supported \"%s\" extension statement found, ignoring.", value);
Michal Vasko345da0a2015-12-02 10:35:55 +01004836 lyxml_free(ctx, child);
Radek Krejci6764bb32015-07-03 15:16:04 +02004837 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004838 } else {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004839 LOGVAL(LYE_INSTMT, LOGLINE(child), child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004840 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004841 }
4842 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004843
Radek Krejcic071c542016-01-27 14:57:51 +01004844 /* check for mandatory statements */
4845 if (submodule && !submodule->prefix) {
Michal Vaskobdf51ef2015-12-10 12:11:21 +01004846 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "belongs-to", "submodule");
4847 goto error;
Radek Krejcic071c542016-01-27 14:57:51 +01004848 } else if (!submodule) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004849 if (!module->ns) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02004850 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "namespace", "module");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004851 goto error;
4852 }
4853 if (!module->prefix) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02004854 LOGVAL(LYE_MISSSTMT2, LOGLINE(yin), "prefix", "module");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004855 goto error;
4856 }
4857 }
Radek Krejcibb3257d2015-05-21 23:03:51 +02004858
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004859 /* allocate arrays for elements with cardinality of 0..n */
4860 if (c_imp) {
Radek Krejcic071c542016-01-27 14:57:51 +01004861 trg->imp = calloc(c_imp, sizeof *trg->imp);
4862 if (!trg->imp) {
Michal Vasko253035f2015-12-17 16:58:13 +01004863 LOGMEM;
4864 goto error;
4865 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004866 }
4867 if (c_rev) {
Radek Krejcic071c542016-01-27 14:57:51 +01004868 trg->rev = calloc(c_rev, sizeof *trg->rev);
4869 if (!trg->rev) {
Michal Vasko253035f2015-12-17 16:58:13 +01004870 LOGMEM;
4871 goto error;
4872 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004873 }
4874 if (c_tpdf) {
Radek Krejcic071c542016-01-27 14:57:51 +01004875 trg->tpdf = calloc(c_tpdf, sizeof *trg->tpdf);
4876 if (!trg->tpdf) {
Michal Vasko253035f2015-12-17 16:58:13 +01004877 LOGMEM;
4878 goto error;
4879 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004880 }
4881 if (c_ident) {
Radek Krejcic071c542016-01-27 14:57:51 +01004882 trg->ident = calloc(c_ident, sizeof *trg->ident);
4883 if (!trg->ident) {
Michal Vasko253035f2015-12-17 16:58:13 +01004884 LOGMEM;
4885 goto error;
4886 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004887 }
4888 if (c_inc) {
Radek Krejcic071c542016-01-27 14:57:51 +01004889 trg->inc = calloc(c_inc, sizeof *trg->inc);
4890 if (!trg->inc) {
Michal Vasko253035f2015-12-17 16:58:13 +01004891 LOGMEM;
4892 goto error;
4893 }
Radek Krejcic071c542016-01-27 14:57:51 +01004894 trg->inc_size = c_inc;
4895 /* trg->inc_size can be updated by the included submodules,
4896 * so we will use inc_size_aux here, trg->inc_size stores the
4897 * target size of the array
4898 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004899 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02004900 if (c_aug) {
Radek Krejcic071c542016-01-27 14:57:51 +01004901 trg->augment = calloc(c_aug, sizeof *trg->augment);
4902 if (!trg->augment) {
Michal Vasko253035f2015-12-17 16:58:13 +01004903 LOGMEM;
4904 goto error;
4905 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02004906 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004907 if (c_ftrs) {
Radek Krejcic071c542016-01-27 14:57:51 +01004908 trg->features = calloc(c_ftrs, sizeof *trg->features);
4909 if (!trg->features) {
Michal Vasko253035f2015-12-17 16:58:13 +01004910 LOGMEM;
4911 goto error;
4912 }
Radek Krejci3cf9e222015-06-18 11:37:50 +02004913 }
Radek Krejcieb00f512015-07-01 16:44:58 +02004914 if (c_dev) {
Radek Krejcic071c542016-01-27 14:57:51 +01004915 trg->deviation = calloc(c_dev, sizeof *trg->deviation);
4916 if (!trg->deviation) {
Michal Vasko253035f2015-12-17 16:58:13 +01004917 LOGMEM;
4918 goto error;
4919 }
Radek Krejcieb00f512015-07-01 16:44:58 +02004920 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02004921
Michal Vasko2f7925f2015-10-21 15:06:56 +02004922 /* middle part - process nodes with cardinality of 0..n except the data nodes and augments */
4923 LY_TREE_FOR_SAFE(yin->child, next, child) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004924 if (!strcmp(child->name, "import")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004925 r = fill_yin_import(trg, child, &trg->imp[trg->imp_size]);
4926 trg->imp_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004927 if (r) {
4928 goto error;
4929 }
Radek Krejcice7fb782015-05-29 16:52:34 +02004930
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004931 /* check duplicities in imported modules */
Radek Krejcic071c542016-01-27 14:57:51 +01004932 for (i = 0; i < trg->imp_size - 1; i++) {
4933 if (!strcmp(trg->imp[i].module->name, trg->imp[trg->imp_size - 1].module->name)) {
4934 LOGVAL(LYE_SPEC, LOGLINE(child), "Importing module \"%s\" repeatedly.", trg->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004935 goto error;
4936 }
4937 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02004938
Radek Krejci1d82ef62015-08-07 14:44:40 +02004939 } else if (!strcmp(child->name, "include")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004940 memset(&inc, 0, sizeof inc);
4941 /* 1) pass module, not trg, since we want to pass the main module
4942 * 2) we cannot pass directly the structure in the array since
4943 * submodule parser can realloc our array of includes */
4944 r = fill_yin_include(module, child, &inc, unres);
4945 memcpy(&trg->inc[inc_size_aux], &inc, sizeof inc);
4946 inc_size_aux++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004947 if (r) {
4948 goto error;
4949 }
Radek Krejcice7fb782015-05-29 16:52:34 +02004950
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004951 /* check duplications in include submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01004952 for (i = 0; i < inc_size_aux - 1; i++) {
4953 if (!strcmp(trg->inc[i].submodule->name, trg->inc[inc_size_aux - 1].submodule->name)) {
4954 LOGVAL(LYE_SPEC, LOGLINE(child), "Including submodule \"%s\" repeatedly.",
4955 trg->inc[i].submodule->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, "revision")) {
4961 GETVAL(value, child, "date");
Radek Krejcic6556022016-01-27 15:16:45 +01004962 if (lyp_check_date(value, LOGLINE(child))) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004963 goto error;
4964 }
Radek Krejcic071c542016-01-27 14:57:51 +01004965 memcpy(trg->rev[trg->rev_size].date, value, LY_REV_SIZE - 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004966 /* check uniqueness of the revision date - not required by RFC */
Radek Krejcic071c542016-01-27 14:57:51 +01004967 for (i = 0; i < trg->rev_size; i++) {
4968 if (!strcmp(value, trg->rev[i].date)) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004969 LOGVAL(LYE_INARG, LOGLINE(child), value, child->name);
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02004970 LOGVAL(LYE_SPEC, 0, "Revision is not unique.");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004971 }
4972 }
Radek Krejcice7fb782015-05-29 16:52:34 +02004973
Radek Krejci1d82ef62015-08-07 14:44:40 +02004974 LY_TREE_FOR(child->child, child2) {
4975 if (!strcmp(child2->name, "description")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004976 if (trg->rev[trg->rev_size].dsc) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004977 LOGVAL(LYE_TOOMANY, LOGLINE(child), child2->name, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004978 goto error;
4979 }
Radek Krejcic071c542016-01-27 14:57:51 +01004980 trg->rev[trg->rev_size].dsc = read_yin_subnode(ctx, child2, "text");
4981 if (!trg->rev[trg->rev_size].dsc) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004982 goto error;
4983 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02004984 } else if (!strcmp(child2->name, "reference")) {
Radek Krejcic071c542016-01-27 14:57:51 +01004985 if (trg->rev[trg->rev_size].ref) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004986 LOGVAL(LYE_TOOMANY, LOGLINE(child), child2->name, child->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004987 goto error;
4988 }
Radek Krejcic071c542016-01-27 14:57:51 +01004989 trg->rev[trg->rev_size].ref = read_yin_subnode(ctx, child2, "text");
4990 if (!trg->rev[trg->rev_size].ref) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004991 goto error;
4992 }
4993 } else {
Radek Krejci1d82ef62015-08-07 14:44:40 +02004994 LOGVAL(LYE_INSTMT, LOGLINE(child2), child2->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004995 goto error;
4996 }
4997 }
Radek Krejcice7fb782015-05-29 16:52:34 +02004998
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02004999 /* keep the latest revision at position 0 */
Radek Krejcic071c542016-01-27 14:57:51 +01005000 if (trg->rev_size && strcmp(trg->rev[trg->rev_size].date, trg->rev[0].date) > 0) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005001 /* switch their position */
Radek Krejcic071c542016-01-27 14:57:51 +01005002 value = strdup(trg->rev[0].date);
Michal Vasko253035f2015-12-17 16:58:13 +01005003 if (!value) {
5004 LOGMEM;
5005 goto error;
5006 }
Radek Krejcic071c542016-01-27 14:57:51 +01005007 memcpy(trg->rev[0].date, trg->rev[trg->rev_size].date, LY_REV_SIZE - 1);
5008 memcpy(trg->rev[trg->rev_size].date, value, LY_REV_SIZE - 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005009 free((char *)value);
Radek Krejcice7fb782015-05-29 16:52:34 +02005010
Radek Krejcic071c542016-01-27 14:57:51 +01005011 if (trg->rev[0].dsc != trg->rev[trg->rev_size].dsc) {
5012 value = trg->rev[0].dsc;
5013 trg->rev[0].dsc = trg->rev[trg->rev_size].dsc;
5014 trg->rev[trg->rev_size].dsc = value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005015 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005016
Radek Krejcic071c542016-01-27 14:57:51 +01005017 if (trg->rev[0].ref != trg->rev[trg->rev_size].ref) {
5018 value = trg->rev[0].ref;
5019 trg->rev[0].ref = trg->rev[trg->rev_size].ref;
5020 trg->rev[trg->rev_size].ref = value;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005021 }
5022 }
Radek Krejcice7fb782015-05-29 16:52:34 +02005023
Radek Krejcic071c542016-01-27 14:57:51 +01005024 trg->rev_size++;
Michal Vasko2f7925f2015-10-21 15:06:56 +02005025
Radek Krejci1d82ef62015-08-07 14:44:40 +02005026 } else if (!strcmp(child->name, "typedef")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005027 r = fill_yin_typedef(trg, NULL, child, &trg->tpdf[trg->tpdf_size], unres);
5028 trg->tpdf_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005029 if (r) {
5030 goto error;
5031 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02005032
Radek Krejci1d82ef62015-08-07 14:44:40 +02005033 } else if (!strcmp(child->name, "identity")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005034 r = fill_yin_identity(trg, child, &trg->ident[trg->ident_size], unres);
5035 trg->ident_size++;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005036 if (r) {
5037 goto error;
5038 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02005039
Radek Krejci1d82ef62015-08-07 14:44:40 +02005040 } else if (!strcmp(child->name, "feature")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005041 r = fill_yin_feature(trg, child, &trg->features[trg->features_size], unres);
5042 trg->features_size++;
Radek Krejci3cf9e222015-06-18 11:37:50 +02005043 if (r) {
5044 goto error;
5045 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02005046
Radek Krejci1d82ef62015-08-07 14:44:40 +02005047 } else if (!strcmp(child->name, "deviation")) {
Radek Krejcic071c542016-01-27 14:57:51 +01005048 r = fill_yin_deviation(trg, child, &trg->deviation[trg->deviation_size], unres);
5049 trg->deviation_size++;
Radek Krejcieb00f512015-07-01 16:44:58 +02005050 if (r) {
5051 goto error;
5052 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02005053
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005054 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005055 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02005056
Radek Krejcic071c542016-01-27 14:57:51 +01005057 if (submodule) {
5058 /* propagate imports into the main module */
5059 for (i = r = 0; i < submodule->imp_size; i++) {
5060 for (j = 0; j < module->imp_size; j++) {
5061 if (submodule->imp[i].module == module->imp[j].module &&
5062 !strcmp(submodule->imp[i].rev, module->imp[j].rev)) {
5063 /* check prefix match */
5064 if (submodule->imp[i].prefix != module->imp[j].prefix) {
5065 LOGVAL(LYE_INID, LOGLINE(yin), submodule->imp[i].prefix,
5066 "non-matching prefixes of imported module in main module and submodule");
5067 goto error;
5068 }
5069 break;
5070 }
5071 }
5072 if (j == module->imp_size) {
5073 /* new import */
5074 r++;
5075 }
5076 }
5077 if (r) {
5078 aux_imp = realloc(module->imp, (module->imp_size + r) * sizeof *module->imp);
5079 if (!aux_imp) {
5080 LOGMEM;
5081 goto error;
5082 }
5083 module->imp = aux_imp;
5084 for (i = r = 0; i < submodule->imp_size; i++) {
5085 for (j = 0; j < module->imp_size; j++) {
5086 if (submodule->imp[i].module == module->imp[j].module) {
5087 break;
5088 }
5089 }
5090 if (j == module->imp_size) {
5091 /* new import */
5092 /* check prefix uniqueness */
5093 if (dup_prefix_check(submodule->imp[i].prefix, module)) {
5094 LOGVAL(LYE_DUPID, LOGLINE(yin), "prefix", submodule->imp[i].prefix);
5095 goto error;
5096 }
5097 memcpy(&module->imp[module->imp_size + r], &submodule->imp[i], sizeof *submodule->imp);
5098 module->imp[module->imp_size + r].external = 1;
5099 r++;
5100 }
5101 }
5102 module->imp_size += r;
5103 }
5104
5105 /* propagate imports into the main module */
5106 for (i = r = 0; i < submodule->inc_size; i++) {
5107 for (j = 0; j < module->inc_size; j++) {
5108 if (submodule->inc[i].submodule == module->inc[j].submodule) {
5109 break;
5110 }
5111 }
5112 if (j == module->inc_size) {
5113 /* new include */
5114 r++;
5115 }
5116 }
5117
5118 if (r) {
5119 aux_inc = realloc(module->inc, (module->inc_size + r) * sizeof *module->inc);
5120 if (!aux_inc) {
5121 LOGMEM;
5122 goto error;
5123 }
5124 module->inc = aux_inc;
5125 for (i = r = 0; i < submodule->inc_size; i++) {
5126 for (j = 0; j < module->inc_size; j++) {
5127 if (submodule->inc[i].submodule == module->inc[j].submodule) {
5128 break;
5129 }
5130 }
5131 if (j == module->inc_size) {
5132 /* new include */
5133 memcpy(&module->inc[module->inc_size + r], &submodule->inc[i], sizeof *submodule->inc);
5134 module->inc[module->inc_size + r].external = 1;
5135 r++;
5136 }
5137 }
5138 module->inc_size += r;
5139 }
5140 }
5141
Radek Krejcif5be10f2015-06-16 13:29:36 +02005142 /* process data nodes. Start with groupings to allow uses
Radek Krejcic071c542016-01-27 14:57:51 +01005143 * refer to them. Submodule's data nodes are stored in the
5144 * main module data tree.
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005145 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005146 LY_TREE_FOR_SAFE(grps.child, next, child) {
Radek Krejcic071c542016-01-27 14:57:51 +01005147 node = read_yin_grouping(trg, NULL, child, 0, unres);
Radek Krejci1d82ef62015-08-07 14:44:40 +02005148 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005149 goto error;
5150 }
Radek Krejci74705112015-06-05 10:25:44 +02005151
Michal Vasko345da0a2015-12-02 10:35:55 +01005152 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005153 }
Radek Krejci74705112015-06-05 10:25:44 +02005154
Radek Krejcif5be10f2015-06-16 13:29:36 +02005155 /* parse data nodes, ... */
Radek Krejci1d82ef62015-08-07 14:44:40 +02005156 LY_TREE_FOR_SAFE(root.child, next, child) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02005157
Radek Krejci1d82ef62015-08-07 14:44:40 +02005158 if (!strcmp(child->name, "container")) {
5159 node = read_yin_container(module, NULL, child, 1, unres);
5160 } else if (!strcmp(child->name, "leaf-list")) {
5161 node = read_yin_leaflist(module, NULL, child, 1, unres);
5162 } else if (!strcmp(child->name, "leaf")) {
5163 node = read_yin_leaf(module, NULL, child, 1, unres);
5164 } else if (!strcmp(child->name, "list")) {
5165 node = read_yin_list(module, NULL, child, 1, unres);
5166 } else if (!strcmp(child->name, "choice")) {
5167 node = read_yin_choice(module, NULL, child, 1, unres);
5168 } else if (!strcmp(child->name, "uses")) {
5169 node = read_yin_uses(module, NULL, child, 1, unres);
5170 } else if (!strcmp(child->name, "anyxml")) {
5171 node = read_yin_anyxml(module, NULL, child, 1, unres);
Michal Vasko7ffc3052015-10-21 15:05:56 +02005172 } else if (!strcmp(child->name, "rpc")) {
5173 node = read_yin_rpc(module, NULL, child, 0, unres);
5174 } else if (!strcmp(child->name, "notification")) {
5175 node = read_yin_notif(module, NULL, child, 0, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005176 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02005177 if (!node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005178 goto error;
5179 }
Radek Krejci25d782a2015-05-22 15:03:23 +02005180
Michal Vasko345da0a2015-12-02 10:35:55 +01005181 lyxml_free(ctx, child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005182 }
Radek Krejcif5be10f2015-06-16 13:29:36 +02005183
Michal Vasko2f7925f2015-10-21 15:06:56 +02005184 /* ... and finally augments (last, so we can augment our data, for instance) */
5185 LY_TREE_FOR_SAFE(augs.child, next, child) {
Radek Krejcic071c542016-01-27 14:57:51 +01005186 r = fill_yin_augment(trg, NULL, child, &trg->augment[trg->augment_size], unres);
5187 trg->augment_size++;
Radek Krejcif5be10f2015-06-16 13:29:36 +02005188
Michal Vasko2f7925f2015-10-21 15:06:56 +02005189 if (r) {
Radek Krejcif5be10f2015-06-16 13:29:36 +02005190 goto error;
5191 }
Michal Vasko345da0a2015-12-02 10:35:55 +01005192 lyxml_free(ctx, child);
Radek Krejcif5be10f2015-06-16 13:29:36 +02005193 }
5194
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005195 return EXIT_SUCCESS;
Radek Krejciefaeba32015-05-27 14:30:57 +02005196
5197error:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005198 /* cleanup */
5199 while (root.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005200 lyxml_free(module->ctx, root.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005201 }
5202 while (grps.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005203 lyxml_free(module->ctx, grps.child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005204 }
Michal Vasko2f7925f2015-10-21 15:06:56 +02005205 while (augs.child) {
Michal Vasko345da0a2015-12-02 10:35:55 +01005206 lyxml_free(module->ctx, augs.child);
Michal Vasko38d01f72015-06-15 09:41:06 +02005207 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005208
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005209 return EXIT_FAILURE;
Radek Krejciefaeba32015-05-27 14:30:57 +02005210}
5211
Michal Vasko0d343d12015-08-24 14:57:36 +02005212/* logs directly */
Radek Krejcib8048692015-08-05 13:36:34 +02005213struct lys_submodule *
Radek Krejcic071c542016-01-27 14:57:51 +01005214yin_read_submodule(struct lys_module *module, const char *data, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02005215{
Michal Vasko9f258e42016-02-11 11:36:27 +01005216 struct lys_node *next, *elem;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005217 struct lyxml_elem *yin;
Radek Krejcib8048692015-08-05 13:36:34 +02005218 struct lys_submodule *submodule = NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005219 const char *value;
Michal Vasko9f258e42016-02-11 11:36:27 +01005220 uint8_t i;
Radek Krejciefaeba32015-05-27 14:30:57 +02005221
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005222 assert(module->ctx);
Radek Krejciefaeba32015-05-27 14:30:57 +02005223
Radek Krejci722b0072016-02-01 17:09:45 +01005224 yin = lyxml_parse_mem(module->ctx, data, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005225 if (!yin) {
5226 return NULL;
5227 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005228
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005229 /* check root element */
5230 if (!yin->name || strcmp(yin->name, "submodule")) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02005231 LOGVAL(LYE_INSTMT, LOGLINE(yin), yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005232 goto error;
5233 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005234
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005235 GETVAL(value, yin, "name");
Radek Krejcic6556022016-01-27 15:16:45 +01005236 if (lyp_check_identifier(value, LY_IDENT_NAME, LOGLINE(yin), NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005237 goto error;
5238 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005239
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005240 submodule = calloc(1, sizeof *submodule);
5241 if (!submodule) {
5242 LOGMEM;
5243 goto error;
5244 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005245
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005246 submodule->ctx = module->ctx;
5247 submodule->name = lydict_insert(submodule->ctx, value, strlen(value));
5248 submodule->type = 1;
5249 submodule->belongsto = module;
Radek Krejciefaeba32015-05-27 14:30:57 +02005250
Michal Vasko9f258e42016-02-11 11:36:27 +01005251 LOGVRB("Reading submodule \"%s\".", submodule->name);
Radek Krejcic071c542016-01-27 14:57:51 +01005252 if (read_sub_module(module, submodule, yin, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005253 goto error;
5254 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005255
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005256 /* cleanup */
Michal Vasko345da0a2015-12-02 10:35:55 +01005257 lyxml_free(module->ctx, yin);
Radek Krejciefaeba32015-05-27 14:30:57 +02005258
Michal Vasko9f258e42016-02-11 11:36:27 +01005259 LOGVRB("Submodule \"%s\" successfully parsed.", submodule->name);
Radek Krejciefaeba32015-05-27 14:30:57 +02005260
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005261 return submodule;
Radek Krejciefaeba32015-05-27 14:30:57 +02005262
5263error:
Michal Vasko9f258e42016-02-11 11:36:27 +01005264 LOGERR(ly_errno, "Submodule \"%s\" parsing failed.", submodule->name);
5265
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005266 /* cleanup */
Radek Krejcic071c542016-01-27 14:57:51 +01005267 unres_schema_free((struct lys_module *)submodule, &unres);
Michal Vasko345da0a2015-12-02 10:35:55 +01005268 lyxml_free(module->ctx, yin);
Michal Vasko9f258e42016-02-11 11:36:27 +01005269
5270 /* warn about applied deviations */
5271 for (i = 0; i < submodule->deviation_size; ++i) {
5272 if (submodule->deviation[i].target) {
5273 LOGERR(ly_errno, "Submodule parsing failed, but successfully deviated %smodule \"%s\".",
5274 (submodule->deviation[i].target->module->type ? "sub" : ""),
5275 submodule->deviation[i].target->module->name);
5276 }
5277 }
5278
5279 /* remove applied augments */
5280 for (i = 0; i < submodule->augment_size; ++i) {
5281 if (submodule->augment[i].target) {
5282 LY_TREE_FOR_SAFE(submodule->augment[i].target->child, next, elem) {
5283 if (elem->parent == (struct lys_node *)&submodule->augment[i]) {
5284 lys_node_free(elem, NULL);
5285 }
5286 }
5287 }
5288 }
5289
5290 lys_submodule_free(submodule, NULL);
Radek Krejciefaeba32015-05-27 14:30:57 +02005291
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005292 return NULL;
Radek Krejciefaeba32015-05-27 14:30:57 +02005293}
5294
Michal Vasko0d343d12015-08-24 14:57:36 +02005295/* logs directly */
Radek Krejcib8048692015-08-05 13:36:34 +02005296struct lys_module *
Radek Krejcic071c542016-01-27 14:57:51 +01005297yin_read_module(struct ly_ctx *ctx, const char *data, int implement)
Radek Krejciefaeba32015-05-27 14:30:57 +02005298{
Michal Vasko9f258e42016-02-11 11:36:27 +01005299 struct lys_node *next, *elem;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005300 struct lyxml_elem *yin;
Radek Krejcib8048692015-08-05 13:36:34 +02005301 struct lys_module *module = NULL, **newlist = NULL;
Radek Krejcic071c542016-01-27 14:57:51 +01005302 struct unres_schema *unres;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005303 const char *value;
5304 int i;
Radek Krejciefaeba32015-05-27 14:30:57 +02005305
Radek Krejcic071c542016-01-27 14:57:51 +01005306 unres = calloc(1, sizeof *unres);
5307 if (!unres) {
5308 LOGMEM;
5309 return NULL;
5310 }
5311
Radek Krejci722b0072016-02-01 17:09:45 +01005312 yin = lyxml_parse_mem(ctx, data, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005313 if (!yin) {
Radek Krejcic071c542016-01-27 14:57:51 +01005314 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005315 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005316
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005317 /* check root element */
5318 if (!yin->name || strcmp(yin->name, "module")) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02005319 LOGVAL(LYE_INSTMT, LOGLINE(yin), yin->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005320 goto error;
5321 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005322
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005323 GETVAL(value, yin, "name");
Radek Krejcic6556022016-01-27 15:16:45 +01005324 if (lyp_check_identifier(value, LY_IDENT_NAME, LOGLINE(yin), NULL, NULL)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005325 goto error;
5326 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005327
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005328 module = calloc(1, sizeof *module);
5329 if (!module) {
5330 LOGMEM;
5331 goto error;
5332 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005333
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005334 module->ctx = ctx;
5335 module->name = lydict_insert(ctx, value, strlen(value));
5336 module->type = 0;
Michal Vaskod8aa32d2015-07-24 11:50:01 +02005337 module->implemented = (implement ? 1 : 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02005338
Michal Vasko9f258e42016-02-11 11:36:27 +01005339 LOGVRB("Reading module \"%s\".", module->name);
Radek Krejcic071c542016-01-27 14:57:51 +01005340 if (read_sub_module(module, NULL, yin, unres)) {
5341 goto error;
5342 }
5343
5344 /* resolve rest of unres items */
5345 if (unres->count && resolve_unres_schema(module, unres)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005346 goto error;
5347 }
Radek Krejciefaeba32015-05-27 14:30:57 +02005348
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005349 /* add to the context's list of modules */
5350 if (ctx->models.used == ctx->models.size) {
Michal Vaskof2e1a992015-11-09 09:54:47 +01005351 newlist = realloc(ctx->models.list, (2 * ctx->models.size) * sizeof *newlist);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005352 if (!newlist) {
5353 LOGMEM;
5354 goto error;
5355 }
5356 for (i = ctx->models.size; i < ctx->models.size * 2; i++) {
5357 newlist[i] = NULL;
5358 }
5359 ctx->models.size *= 2;
5360 ctx->models.list = newlist;
5361 }
5362 for (i = 0; ctx->models.list[i]; i++) {
5363 /* check name (name/revision) and namespace uniqueness */
5364 if (!strcmp(ctx->models.list[i]->name, module->name)) {
Radek Krejci63a91a92015-07-29 13:31:04 +02005365 if (ctx->models.list[i]->rev_size == module->rev_size) {
5366 /* both have the same number of revisions */
5367 if (!module->rev_size || !strcmp(ctx->models.list[i]->rev[0].date, module->rev[0].date)) {
5368 /* both have the same revision -> we already have the same module */
5369 /* so free the new one and update the old one's implement flag if needed */
Michal Vasko9f258e42016-02-11 11:36:27 +01005370 LOGVRB("Module \"%s\" already in context.", ctx->models.list[i]->name);
Radek Krejci63a91a92015-07-29 13:31:04 +02005371
Michal Vasko9f258e42016-02-11 11:36:27 +01005372 lys_free(module, NULL, 1);
Radek Krejcic071c542016-01-27 14:57:51 +01005373 module = ctx->models.list[i];
5374 if (implement && !module->implemented) {
5375 lyp_set_implemented(module);
Radek Krejci63a91a92015-07-29 13:31:04 +02005376 }
Radek Krejcic071c542016-01-27 14:57:51 +01005377
5378 goto success;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005379 }
5380 }
Radek Krejcif647e612015-07-30 11:36:07 +02005381 /* else (both elses) keep searching, for now the caller is just adding
5382 * another revision of an already present schema
5383 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005384 } else if (!strcmp(ctx->models.list[i]->ns, module->ns)) {
Michal Vasko9f258e42016-02-11 11:36:27 +01005385 LOGERR(LY_EINVAL, "Two different modules (\"%s\" and \"%s\") have the same namespace \"%s\".",
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005386 ctx->models.list[i]->name, module->name, module->ns);
5387 goto error;
5388 }
5389 }
5390 ctx->models.list[i] = module;
5391 ctx->models.used++;
Michal Vasko68cffd72015-08-03 12:50:11 +02005392 ctx->models.module_set_id++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005393
Radek Krejcic071c542016-01-27 14:57:51 +01005394success:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005395 /* cleanup */
Michal Vasko345da0a2015-12-02 10:35:55 +01005396 lyxml_free(ctx, yin);
Radek Krejcic071c542016-01-27 14:57:51 +01005397 unres_schema_free(NULL, &unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02005398
Michal Vasko9f258e42016-02-11 11:36:27 +01005399 LOGVRB("Module \"%s\" successfully parsed.", module->name);
Radek Krejcida04f4a2015-05-21 12:54:09 +02005400
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005401 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005402
5403error:
Michal Vasko9f258e42016-02-11 11:36:27 +01005404 LOGERR(ly_errno, "Module \"%s\" parsing failed.", module->name);
5405
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005406 /* cleanup */
Radek Krejcic071c542016-01-27 14:57:51 +01005407 unres_schema_free(module, &unres);
Radek Krejcic071c542016-01-27 14:57:51 +01005408 lyxml_free(ctx, yin);
Radek Krejcida04f4a2015-05-21 12:54:09 +02005409
Michal Vasko9f258e42016-02-11 11:36:27 +01005410 /* warn about applied deviations */
5411 for (i = 0; i < module->deviation_size; ++i) {
5412 if (module->deviation[i].target) {
5413 LOGERR(ly_errno, "Module parsing failed, but successfully deviated %smodule \"%s\".",
5414 (module->deviation[i].target->module->type ? "sub" : ""),
5415 module->deviation[i].target->module->name);
5416 }
5417 }
5418
5419 /* remove applied augments */
5420 for (i = 0; i < module->augment_size; ++i) {
5421 if (module->augment[i].target) {
5422 LY_TREE_FOR_SAFE(module->augment[i].target->child, next, elem) {
5423 if (elem->parent == (struct lys_node *)&module->augment[i]) {
5424 lys_node_free(elem, NULL);
5425 }
5426 }
5427 }
5428 }
5429
5430 lys_free(module, NULL, 1);
5431
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02005432 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02005433}