blob: 7bf278abf8200fd0584876a3cf1d4bf6cabe60fe [file] [log] [blame]
David Sedlákb1ce3f82019-06-05 14:37:26 +02001/**
2 * @file parser_yin.h
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
David Sedlákc5b20842019-08-13 10:18:31 +02004 * @brief YIN parser header file.
David Sedlákb1ce3f82019-06-05 14:37:26 +02005 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
David Sedlák2b626ee2019-06-03 16:40:18 +020014
15#ifndef LY_PARSER_YIN_H_
16#define LY_PARSER_YIN_H_
17
18#include <stdio.h>
David Sedlákb1ce3f82019-06-05 14:37:26 +020019#include <stdlib.h>
20
David Sedlák2b626ee2019-06-03 16:40:18 +020021#include "log.h"
22#include "xml.h"
David Sedlák2b626ee2019-06-03 16:40:18 +020023
David Sedlákf6251182019-06-06 10:22:13 +020024/* list of yin attribute strings */
25extern const char *const yin_attr_list[];
26#define yin_attr2str(STMT) yin_attr_list[STMT]
27
David Sedlák2b214ac2019-06-06 16:11:03 +020028#define YIN_NS_URI "urn:ietf:params:xml:ns:yang:yin:1"
David Sedlák5c2ed5f2019-08-19 12:00:29 +020029
30/**
31 * @brief Get start of full name including possible prefix.
32 *
33 * @param[in] name Start of name in input data.
34 * @param[in] prefix_len Lenght of prefix.
35 *
36 * @return Pointer to begining of full element name including prefix.
37 */
David Sedláka82ecff2019-08-16 10:51:48 +020038#define name2fname(name, prefix_len) (prefix_len != 0 ? name - (prefix_len + 1) : name)
David Sedlák5c2ed5f2019-08-19 12:00:29 +020039
40/**
41 * @brief Get length of full name including possible prefix.
42 *
43 * @param[in] name_len Lenght of name without prefix.
44 * @param[in] prefix_len Lenght of prefix.
45 *
46 * @return Lenght of full name including possible prefix and ':' delimiter.
47 */
David Sedláka82ecff2019-08-16 10:51:48 +020048#define len2flen(name_len, prefix_len) (prefix_len != 0 ? name_len + prefix_len + 1 : name_len)
David Sedlák2b214ac2019-06-06 16:11:03 +020049
David Sedlák26ea1432019-08-14 13:42:23 +020050#define VALID_VALS1 " Only valid value is \"%s\"."
51#define VALID_VALS2 " Valid values are \"%s\" and \"%s\"."
52#define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"."
53#define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"."
54
David Sedlák4ffcec82019-07-25 15:10:21 +020055/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
56#define isdevsub(kw) (kw == YANG_CONFIG || kw == YANG_DEFAULT || kw == YANG_MANDATORY || \
57 kw == YANG_MAX_ELEMENTS || kw == YANG_MIN_ELEMENTS || \
58 kw == YANG_MUST || kw == YANG_TYPE || kw == YANG_UNIQUE || \
59 kw == YANG_UNITS || kw == YANG_CUSTOM)
60
David Sedlák169cc522019-08-15 13:23:45 +020061/**
62 * @brief insert string into dictionary and store as target.
63 *
64 * @param[in] CTX libyang context.
65 * @param[out] TARGET variable where to store the pointer to the inserted value.
66 * @param[in] DYNAMIC Set to 1 if STR is dynamically allocated, 0 otherwise. If set to 1, zerocopy version of lydict_insert is used.
67 * @param[in] STR string to store.
68 * @param[in] LEN length of the string in WORD to store.
69 */
70#define INSERT_STRING(CTX, TARGET, DYNAMIC, STR, LEN) \
David Sedlák26281532019-08-16 10:56:01 +020071 if (DYNAMIC) {(TARGET) = lydict_insert_zc(CTX, STR);} \
72 else {(TARGET) = lydict_insert(CTX, LEN ? STR : "", LEN);}
David Sedlák169cc522019-08-15 13:23:45 +020073
David Sedlákc5b20842019-08-13 10:18:31 +020074enum yin_argument {
David Sedlák1bccdfa2019-06-17 15:55:27 +020075 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
David Sedlák2b626ee2019-06-03 16:40:18 +020076 YIN_ARG_NAME, /**< argument name */
77 YIN_ARG_TARGET_NODE, /**< argument target-node */
78 YIN_ARG_MODULE, /**< argument module */
79 YIN_ARG_VALUE, /**< argument value */
80 YIN_ARG_TEXT, /**< argument text */
81 YIN_ARG_CONDITION, /**< argument condition */
82 YIN_ARG_URI, /**< argument uri */
83 YIN_ARG_DATE, /**< argument data */
84 YIN_ARG_TAG, /**< argument tag */
David Sedlák1bccdfa2019-06-17 15:55:27 +020085 YIN_ARG_NONE, /**< empty (special value) */
David Sedlák7ff55a92019-06-17 11:11:41 +020086};
87
David Sedlák1bccdfa2019-06-17 15:55:27 +020088/**
89 * @brief structure to store instance of xml attribute
90 */
David Sedlák7ff55a92019-06-17 11:11:41 +020091struct yin_arg_record {
David Sedlák1bccdfa2019-06-17 15:55:27 +020092 const char *prefix; /**< start of prefix */
93 size_t prefix_len; /**< length of prefix */
94 const char *name; /**< start of name */
95 size_t name_len; /**< length of name */
96 char *content; /**< start of content */
97 size_t content_len; /**< length of content */
98 int dynamic_content; /**< is set to 1 iff content is dynamically allocated 0 otherwise */
David Sedlák2b626ee2019-06-03 16:40:18 +020099};
100
David Sedlákc5b20842019-08-13 10:18:31 +0200101/* flags to set constraints of subelements */
David Sedlák3ffbc522019-07-02 17:49:28 +0200102#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
103#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
David Sedláke1a30302019-07-10 13:49:38 +0200104#define YIN_SUBELEM_FIRST 0x04 /**< is set when subelement is actually yang argument mapped to yin element */
David Sedlák0c2bab92019-07-22 15:33:19 +0200105#define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
David Sedlák21f87cd2019-07-03 16:53:23 +0200106
107#define YIN_SUBELEM_PARSED 0x80 /**< is set during parsing when given subelement is encountered for the first
David Sedlák3ffbc522019-07-02 17:49:28 +0200108 time to simply check validity of given constraints */
109
110struct yin_subelement {
111 enum yang_keyword type; /**< type of keyword */
David Sedlák4a650532019-07-10 11:55:18 +0200112 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
David Sedlákc5b20842019-08-13 10:18:31 +0200113 uint8_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY, YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST and YIN_SUBELEM_VER2 */
David Sedlák3ffbc522019-07-02 17:49:28 +0200114};
115
David Sedlákb4e44562019-07-04 15:42:12 +0200116/* Meta information passed to yin_parse_argument function,
117 holds information about where content of argument element will be stored. */
David Sedlák3ffbc522019-07-02 17:49:28 +0200118struct yin_argument_meta {
David Sedlákb4e44562019-07-04 15:42:12 +0200119 uint16_t *flags; /**< Argument flags */
120 const char **argument; /**< Argument value */
David Sedlák3ffbc522019-07-02 17:49:28 +0200121};
122
David Sedlák6881b512019-08-13 12:52:00 +0200123/**
124 * @brief Meta information passed to functions working with tree_schema,
125 * that require additional information about parent node.
126 */
David Sedlák8a83bbb2019-07-18 14:46:00 +0200127struct tree_node_meta {
128 struct lysp_node *parent; /**< parent node */
David Sedlákbf8a2b72019-08-14 16:48:10 +0200129 struct lysp_node **nodes; /**< linked list of siblings */
David Sedlák8a83bbb2019-07-18 14:46:00 +0200130};
131
David Sedlák6881b512019-08-13 12:52:00 +0200132/**
133 * @brief Meta information passed to yin_parse_import function.
134 */
David Sedlák298ff6d2019-07-26 14:29:03 +0200135struct import_meta {
136 const char *prefix; /**< module prefix. */
137 struct lysp_import **imports; /**< imports to add to. */
138};
139
David Sedlák6881b512019-08-13 12:52:00 +0200140/**
141 * @brief Meta information passed to yin_parse_include function.
142 */
143struct include_meta {
144 const char *name; /**< Module/submodule name. */
145 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
146};
147
148/**
David Sedlák81497a32019-08-13 16:56:26 +0200149 * @brief Meta information passed to yin_parse_inout function.
David Sedlák6881b512019-08-13 12:52:00 +0200150 */
151struct inout_meta {
152 struct lysp_node *parent; /**< Parent node. */
153 struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
154};
155
156/**
157 * @brief Meta information passed to yin_parse_minmax function.
158 */
David Sedlák4ffcec82019-07-25 15:10:21 +0200159struct minmax_dev_meta {
David Sedlák6881b512019-08-13 12:52:00 +0200160 uint32_t *lim; /**< min/max value to write to. */
161 uint16_t *flags; /**< min/max flags to write to. */
162 struct lysp_ext_instance **exts; /**< extension instances to add to. */
David Sedlák4ffcec82019-07-25 15:10:21 +0200163};
164
David Sedlák2b626ee2019-06-03 16:40:18 +0200165/**
166 * @brief Match argument name.
167 *
168 * @param[in] name String representing name.
169 * @param[in] len Lenght of the name.
170 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200171 * @return yin_argument values.
David Sedlák2b626ee2019-06-03 16:40:18 +0200172 */
David Sedlákc5b20842019-08-13 10:18:31 +0200173enum yin_argument yin_match_argument_name(const char *name, size_t len);
David Sedlák2b626ee2019-06-03 16:40:18 +0200174
175/**
David Sedlák555c7202019-07-04 12:14:12 +0200176 * @brief Generic function for content parsing
David Sedlák2b626ee2019-06-03 16:40:18 +0200177 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200178 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlák555c7202019-07-04 12:14:12 +0200179 * @param[in] subelem_info array of valid subelement types and meta information,
180 * array must be ordered by subelem_info->type in ascending order.
181 * @param[in] subelem_info_size Size of subelem_info array.
182 * @param[in,out] data Data to read from, always moved to currently handled character.
183 * @param[in] current_element Type of current element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200184 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
185 * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
David Sedlákc5b20842019-08-13 10:18:31 +0200186 *
David Sedlák2b214ac2019-06-06 16:11:03 +0200187 * @return LY_ERR values.
David Sedlák2b626ee2019-06-03 16:40:18 +0200188 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200189LY_ERR yin_parse_content(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info, signed char subelem_info_size,
David Sedlák555c7202019-07-04 12:14:12 +0200190 const char **data, enum yang_keyword current_element, const char **text_content,
191 struct lysp_ext_instance **exts);
David Sedlák2b626ee2019-06-03 16:40:18 +0200192
David Sedlákd9d3a312019-06-04 09:47:10 +0200193/**
David Sedlák4a650532019-07-10 11:55:18 +0200194 * @brief Check that val is valid UTF8 character sequence of val_type.
195 * Doesn't check empty string, only character validity.
196 *
197 * @param[in] ctx Yin parser context for logging.
198 * @param[in] val_type Type of the input string to select method of checking character validity.
199 * @param[in] val Input to validate.
200 * @param[in] len Length of input.
201 *
202 * @return LY_ERR values.
203 */
204LY_ERR yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len);
205
206/**
David Sedlákb4e44562019-07-04 15:42:12 +0200207 * @brief Match yang keyword from yin data.
David Sedlák1bccdfa2019-06-17 15:55:27 +0200208 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200209 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200210 * @param[in] name Start of keyword name
David Sedlák8f7a1172019-06-20 14:42:18 +0200211 * @param[in] name_len Lenght of keyword name.
212 * @param[in] prefix Start of keyword prefix.
David Sedlákc5b20842019-08-13 10:18:31 +0200213 * @param[in] prefix_len Lenght of prefix.
David Sedlákc1771b12019-07-10 15:55:46 +0200214 * @param[in] parrent Identification of parrent element, use YANG_NONE for elements without parrent.
David Sedlák1bccdfa2019-06-17 15:55:27 +0200215 *
216 * @return yang_keyword values.
217 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200218enum yang_keyword yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len,
David Sedlákc1771b12019-07-10 15:55:46 +0200219 const char *prefix, size_t prefix_len, enum yang_keyword parrent);
David Sedlák1bccdfa2019-06-17 15:55:27 +0200220
David Sedlákb6e65972019-06-19 10:44:13 +0200221/**
David Sedlákc5b20842019-08-13 10:18:31 +0200222 * @brief Load all attributes of element into ([sized array](@ref sizedarrays)). Caller is suposed to free the array.
David Sedlák8f7a1172019-06-20 14:42:18 +0200223 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200224 * @param[in,out] ctx Yin parser context for logging and to store current state.
225 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákbba38e52019-07-09 15:20:01 +0200226 * @param[out] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlák8f7a1172019-06-20 14:42:18 +0200227 *
228 * @return LY_ERR values.
229 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200230LY_ERR yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs);
David Sedlákb6e65972019-06-19 10:44:13 +0200231
David Sedlák554e36d2019-06-20 16:00:04 +0200232/**
David Sedlákb1a78352019-06-28 16:16:29 +0200233 * @brief Parse instance of extension.
234 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200235 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200236 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of extension instance.
David Sedlákb1a78352019-06-28 16:16:29 +0200237 * @param[in,out] data Data to read from, always moved to currently handled character.
238 * @param[in] ext_name Name of the extension element.
239 * @param[in] ext_name_len Length of extension name.
David Sedlákb4e44562019-07-04 15:42:12 +0200240 * @param[in] subelem Type of the keyword this extension instance is a subelement of.
241 * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
242 * @param[in,out] exts Extension instance to add to.
David Sedlákb1a78352019-06-28 16:16:29 +0200243 *
244 * @return LY_ERR values.
245 */
David Sedlák1f90d252019-07-10 17:09:32 +0200246LY_ERR yin_parse_extension_instance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák619db942019-07-03 14:47:30 +0200247 const char *ext_name, int ext_name_len, LYEXT_SUBSTMT subelem,
248 uint32_t subelem_index, struct lysp_ext_instance **exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200249
250/**
251 * @brief Parse yin element into generic structure.
252 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200253 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb1a78352019-06-28 16:16:29 +0200254 * @param[in] name Name of element.
255 * @param[in] name_len Length of elements Name.
256 * @param[in,out] data Data to read from, always moved to currently handled character.
257 * @param[out] element Where the element structure should be stored.
258 *
259 * @return LY_ERR values.
260 */
David Sedlák4ffcec82019-07-25 15:10:21 +0200261LY_ERR yin_parse_element_generic(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char **data,
262 struct lysp_stmt **element);
David Sedlákb1a78352019-06-28 16:16:29 +0200263
David Sedlák4f03b932019-07-26 13:01:47 +0200264/**
David Sedlák298ff6d2019-07-26 14:29:03 +0200265 * @brief Parse module element.
David Sedlák4f03b932019-07-26 13:01:47 +0200266 *
267 * @param[in,out] ctx Yin parser context for logging and to store current state.
268 * @param[in] mod_attrs Attributes of module element.
269 * @param[in,out] data Data to read from.
270 * @param[out] mod Parsed module structure.
271 *
272 * @return LY_ERR values.
273 */
David Sedlák298ff6d2019-07-26 14:29:03 +0200274LY_ERR yin_parse_mod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs,
275 const char **data, struct lysp_module *mod);
David Sedlák4f03b932019-07-26 13:01:47 +0200276
277
278/**
David Sedlák298ff6d2019-07-26 14:29:03 +0200279 * @brief Parse submodule element.
280 *
281 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200282 * @param[in] mod_attrs Attributes of submodule element.
David Sedlák298ff6d2019-07-26 14:29:03 +0200283 * @param[in,out] data Data to read from.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200284 * @param[out] submod Parsed submodule structure.
David Sedlák298ff6d2019-07-26 14:29:03 +0200285 *
286 * @return LY_ERR values.
287 */
288LY_ERR yin_parse_submod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs,
289 const char **data, struct lysp_submodule *submod);
290
291/**
David Sedlák4f03b932019-07-26 13:01:47 +0200292 * @brief free argument record, content loaded from lyxml_get_string() can be
293 * dynamically allocated in some cases so it must be also freed.
294 *
David Sedlákc5b20842019-08-13 10:18:31 +0200295 * @param ctx unused just to fulfill signature of callback for FREE_ARRAY.
296 * @param[in] record Record to free.
David Sedlák4f03b932019-07-26 13:01:47 +0200297 */
298void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record);
299
David Sedlák2b626ee2019-06-03 16:40:18 +0200300#endif /* LY_PARSER_YIN_H_*/