1 /**
2  * @file parser_yang.h
3  * @author Pavol Vican
4  * @brief Parsers for libyang
5  *
6  * Copyright (c) 2015 - 2018 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  */
14 
15 #ifndef LY_PARSER_YANG_H_
16 #define LY_PARSER_YANG_H_
17 
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include "libyang.h"
22 #include "resolve.h"
23 #include "common.h"
24 #include "tree_schema.h"
25 #include "context.h"
26 
27 #define LYS_SYSTEMORDERED 0x40
28 #define LYS_ORDERED_MASK 0xC0
29 #define LYS_MIN_ELEMENTS 0x01
30 #define LYS_MAX_ELEMENTS 0x02
31 #define LYS_RPC_INPUT 0x01
32 #define LYS_RPC_OUTPUT 0x02
33 #define LYS_DATADEF 0x04
34 #define LYS_TYPE_DEF 0x08
35 #define LYS_CHOICE_DEFAULT 0x10
36 #define LYS_NO_ERASE_IDENTITY 0x20
37 #define LY_YANG_ARRAY_SIZE 8
38 #define YANG_REMOVE_IMPORT 0x01
39 #define YANG_EXIST_MODULE 0x02
40 #define EXT_INSTANCE_SUBSTMT 0x04
41 
42 struct type_node {
43     union {
44         struct lys_node_leaflist *ptr_leaflist;
45         struct lys_node_list *ptr_list;
46         struct lys_node_leaf *ptr_leaf;
47         struct lys_tpdf *ptr_tpdf;
48         struct lys_node_anydata *ptr_anydata;
49         struct lys_node_rpc_action *ptr_rpc;
50         struct lys_node_choice *ptr_choice;
51     };
52     uint flag;
53 };
54 
55 struct yang_parameter {
56     struct lys_module *module;
57     struct lys_submodule *submodule;
58     struct unres_schema *unres;
59     struct lys_node **node;
60     char **value;
61     void **data_node;
62     void **actual_node;
63     uint8_t flags;
64 };
65 
66 struct yang_ext_substmt {
67     char *ext_substmt;  /* pointer to string, which contains substmts without module statement */
68     char **ext_modules; /* array of char *, which contains module statements */
69 };
70 
71 struct yang_type {
72     char flags;       /**< this is used to distinguish lyxml_elem * from a YANG temporary parsing structure */
73     LY_DATA_TYPE base;
74     const char *name;
75     struct lys_type *type;
76 };
77 
78 #include "parser_yang_bis.h"
79 
80 char *yang_read_string(struct ly_ctx *ctx, const char *input, char *output, int size, int offset, int indent);
81 
82 int yang_read_common(struct lys_module *module,char *value, enum yytokentype type);
83 
84 int yang_read_prefix(struct lys_module *module, struct lys_import *imp, char *value);
85 
86 int yang_check_version(struct lys_module *module, struct lys_submodule *submodule, char *value, int repeat);
87 
88 int yang_check_imports(struct lys_module *module, struct unres_schema *unres);
89 
90 int yang_read_description(struct lys_module *module, void *node, char *value, char *where, enum yytokentype type);
91 
92 int yang_read_reference(struct lys_module *module, void *node, char *value, char *where, enum yytokentype type);
93 
94 int yang_read_message(struct lys_module *module,struct lys_restr *save,char *value, char *what, int message);
95 
96 int yang_read_presence(struct lys_module *module, struct lys_node_container *cont, char *value);
97 
98 int yang_read_config(void *node, int value, enum yytokentype type);
99 
100 void *yang_read_when(struct lys_module *module, struct lys_node *node, enum yytokentype type, char *value);
101 
102 /**
103  * @brief Allocate memory for node and add to the tree
104  *
105  * @param[in/out] node Pointer to the array.
106  * @param[in] parent Pointer to the parent.
107  * @param[in] root Pointer to the root of schema tree.
108  * @param[in] value Name of node
109  * @param[in] nodetype Type of node
110  * @param[in] sizeof_struct Size of struct
111  * @return Pointer to the node, NULL on error.
112 */
113 void *yang_read_node(struct lys_module *module, struct lys_node *parent, struct lys_node **root,
114                      char *value, int nodetype, int sizeof_struct);
115 
116 int yang_read_default(struct lys_module *module, void *node, char *value, enum yytokentype type);
117 
118 int yang_read_units(struct lys_module *module, void *node, char *value, enum yytokentype type);
119 
120 int yang_read_key(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres);
121 
122 int yang_read_unique(struct lys_module *module, struct lys_node_list *list, struct unres_schema *unres);
123 
124 void *yang_read_type(struct ly_ctx *ctx, void *parent, char *value, enum yytokentype type);
125 
126 void *yang_read_length(struct ly_ctx *ctx, struct yang_type *stype, char *value, int is_ext_instance);
127 
128 int yang_check_type(struct lys_module *module, struct lys_node *parent, struct yang_type *typ, struct lys_type *type, int tpdftype, struct unres_schema *unres);
129 
130 int yang_fill_type(struct lys_module *module, struct lys_type *type, struct yang_type *stype,
131                    void *parent, struct unres_schema *unres);
132 
133 void yang_free_type_union(struct ly_ctx *ctx, struct lys_type *type);
134 
135 void yang_type_free(struct ly_ctx *ctx, struct lys_type *type);
136 
137 int yang_read_leafref_path(struct lys_module *module, struct yang_type *stype, char *value);
138 
139 int yang_read_require_instance(struct ly_ctx *ctx, struct yang_type *stype, int req);
140 
141 int yang_read_pattern(struct ly_ctx *ctx, struct lys_restr *pattern, void **precomp, char *value, char modifier);
142 
143 void *yang_read_range(struct ly_ctx *ctx, struct yang_type *stype, char *value, int is_ext_instance);
144 
145 int yang_read_fraction(struct ly_ctx *ctx, struct yang_type *typ, uint32_t value);
146 
147 int yang_read_enum(struct ly_ctx *ctx, struct yang_type *typ, struct lys_type_enum *enm, char *value);
148 
149 int yang_check_enum(struct ly_ctx *ctx, struct yang_type *typ, struct lys_type_enum *enm, int64_t *value, int assign);
150 
151 int yang_read_bit(struct ly_ctx *ctx, struct yang_type *typ, struct lys_type_bit *bit, char *value);
152 
153 int yang_check_bit(struct ly_ctx *ctx, struct yang_type *typ, struct lys_type_bit *bit, int64_t *value, int assign);
154 
155 void *yang_read_typedef(struct lys_module *module, struct lys_node *parent, char *value);
156 
157 int yang_read_augment(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *aug, char *value);
158 
159 void *yang_read_deviate(struct ly_ctx *ctx, struct lys_deviation *dev, LYS_DEVIATE_TYPE mod);
160 
161 void *yang_read_deviate_unsupported(struct ly_ctx *ctx, struct lys_deviation *dev);
162 
163 int yang_fill_unique(struct lys_module *module, struct lys_node_list *list, struct lys_unique *unique, char *value, struct unres_schema *unres);
164 
165 int yang_fill_iffeature(struct lys_module *module, struct lys_iffeature *iffeature, void *parent,
166                         char *value, struct unres_schema *unres, int parent_is_feature);
167 
168 void yang_free_ext_data(struct yang_ext_substmt *substmt);
169 
170 void *yang_read_ext(struct lys_module *module, void *actual, char *ext_name, char *ext_arg,
171                     enum yytokentype actual_type, enum yytokentype backup_type, int is_ext_instance);
172 
173 int yang_check_ext_instance(struct lys_module *module, struct lys_ext_instance ***ext, uint8_t *size,
174                             void *parent, struct unres_schema *unres);
175 
176 int yang_read_extcomplex_str(struct lys_module *module, struct lys_ext_instance_complex *ext, const char *arg_name,
177                              const char *parent_name, char **value, int parent_stmt, LY_STMT stmt);
178 
179 void **yang_getplace_for_extcomplex_struct(struct lys_ext_instance_complex *ext, int *index,
180                                     char *parent_name, char *node_name, LY_STMT stmt);
181 
182 int yang_extcomplex_node(struct lys_ext_instance_complex *ext, char *parent_name, char *node_name,
183                          struct lys_node *node, LY_STMT stmt);
184 
185 int yang_fill_extcomplex_flags(struct lys_ext_instance_complex *ext, char *parent_name, char *node_name,
186                                LY_STMT stmt, uint16_t value, uint16_t mask);
187 
188 int yang_fill_extcomplex_uint8(struct lys_ext_instance_complex *ext, char *parent_name, char *node_name,
189                                LY_STMT stmt, uint8_t value);
190 
191 int yang_parse_ext_substatement(struct lys_module *module, struct unres_schema *unres, const char *data,
192                                 char *ext_name, struct lys_ext_instance_complex *ext);
193 
194 int yang_fill_extcomplex_module(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext,
195                                 char *parent_name, char **values, int implemented);
196 
197 
198 /* **
199  * @brief Parse YANG from in-memory string
200  *
201  * yang parser expected at the end of the input string 2 zero byte
202  *
203  * @param[in] module Pointer to the libyang module.
204  * @param[in] submodule Pointer to the libyang submodule.
205  * @param[in] unres Pointer to a unres_schema
206  * @param[in] data Pointer to a NULL-terminated string containing YANG data to parse.
207  * @param[in] size_data Size of input string
208  * @param[in/out] node Pointer to node
209  * @return 0 on success, -1 on error, 1 on module is already in context.
210  */
211 int yang_parse_mem(struct lys_module *module, struct lys_submodule *submodule, struct unres_schema *unres,
212                    const char *data, unsigned int size_data, struct lys_node **node);
213 
214 struct lys_module *yang_read_module(struct ly_ctx *ctx, const char* data, unsigned int size, const char *revision, int implement);
215 
216 struct lys_submodule *yang_read_submodule(struct lys_module *module, const char *data, unsigned int size, struct unres_schema *unres);
217 
218 #endif /* LY_PARSER_YANG_H_ */
219