blob: 580ea86dbc3585f9ab130a45d16fa69192a9210b [file] [log] [blame]
Václav Kubernát0a2a2e82018-05-11 13:59:12 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
9#pragma once
Václav Kubernát195eeea2018-05-18 13:52:36 +020010
Václav Kubernát195eeea2018-05-18 13:52:36 +020011#include "parser_context.hpp"
Václav Kubernát48fc3832018-05-28 14:21:22 +020012#include "schema.hpp"
Václav Kubernátebca2552018-06-08 19:06:02 +020013#include "utils.hpp"
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020014
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020015struct keyValue_class {
16 template <typename T, typename Iterator, typename Context>
17 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
18 {
19 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020020 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020021
22 if (parserContext.m_tmpListKeys.find(ast.first) != parserContext.m_tmpListKeys.end()) {
23 _pass(context) = false;
24 parserContext.m_errorMsg = "Key \"" + ast.first + "\" was entered more than once.";
Václav Kubernát744f57f2018-06-29 22:46:26 +020025 } else if (!schema.listHasKey(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName}, ast.first)) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020026 _pass(context) = false;
27 parserContext.m_errorMsg = parserContext.m_tmpListName + " is not indexed by \"" + ast.first + "\".";
Václav Kubernát41378452018-06-06 16:29:40 +020028 } else {
29 parserContext.m_tmpListKeys.insert(ast.first);
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020030 }
31 }
Václav Kubernát41378452018-06-06 16:29:40 +020032
33 template <typename Iterator, typename Exception, typename Context>
34 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
35 {
36 auto& parserContext = x3::get<parser_context_tag>(context);
37 parserContext.m_errorMsg = "Error parsing key values here:";
38 return x3::error_handler_result::rethrow;
39 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020040};
Václav Kubernát41378452018-06-06 16:29:40 +020041
Václav Kubernát744f57f2018-06-29 22:46:26 +020042struct node_identifier_class {
43 template <typename T, typename Iterator, typename Context>
44 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
45 {
46 auto& parserContext = x3::get<parser_context_tag>(context);
47
48 if (!parserContext.m_topLevelModulePresent) {
49 if (parserContext.m_errorMsg.empty())
50 parserContext.m_errorMsg = "You have to specify a top level module.";
51 _pass(context) = false;
52 }
53 }
54};
55
Václav Kubernát89728d82018-09-13 16:28:28 +020056struct key_identifier_class;
57
Václav Kubernát744f57f2018-06-29 22:46:26 +020058struct module_identifier_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020059
60struct listPrefix_class {
61 template <typename T, typename Iterator, typename Context>
62 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
63 {
64 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020065 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020066
Václav Kubernát744f57f2018-06-29 22:46:26 +020067 if (schema.isList(parserContext.m_curPath, {parserContext.m_curModule, ast})) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020068 parserContext.m_tmpListName = ast;
69 } else {
70 _pass(context) = false;
71 }
72 }
73};
74
75struct listSuffix_class {
76 template <typename T, typename Iterator, typename Context>
77 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
78 {
79 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020080 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020081
Václav Kubernát744f57f2018-06-29 22:46:26 +020082 const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020083 std::set<std::string> keysSupplied;
84 for (const auto& it : ast)
85 keysSupplied.insert(it.first);
86
87 if (keysNeeded != keysSupplied) {
88 parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " +
89 "These keys were not supplied:";
90 std::set<std::string> missingKeys;
91 std::set_difference(keysNeeded.begin(), keysNeeded.end(),
92 keysSupplied.begin(), keysSupplied.end(),
93 std::inserter(missingKeys, missingKeys.end()));
94
95 for (const auto& it : missingKeys)
96 parserContext.m_errorMsg += " " + it;
97 parserContext.m_errorMsg += ".";
98
99 _pass(context) = false;
100 }
101 }
Václav Kubernát41378452018-06-06 16:29:40 +0200102
103 template <typename Iterator, typename Exception, typename Context>
104 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
105 {
106 auto& parserContext = x3::get<parser_context_tag>(context);
107 if (parserContext.m_errorMsg.empty())
108 parserContext.m_errorMsg = "Expecting ']' here:";
109 return x3::error_handler_result::rethrow;
Václav Kubernát41378452018-06-06 16:29:40 +0200110 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200111};
112struct listElement_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200113 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200114 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200115 {
116 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200117 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200118 return x3::error_handler_result::fail;
Václav Kubernát41378452018-06-06 16:29:40 +0200119 } else {
120 return x3::error_handler_result::rethrow;
121 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200122 }
123};
124
Václav Kubernát60d6f292018-05-25 09:45:32 +0200125struct nodeup_class {
126 template <typename T, typename Iterator, typename Context>
127 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
128 {
129 auto& parserContext = x3::get<parser_context_tag>(context);
130
Václav Kubernát744f57f2018-06-29 22:46:26 +0200131 if (parserContext.m_curPath.m_nodes.empty())
Václav Kubernát60d6f292018-05-25 09:45:32 +0200132 _pass(context) = false;
Václav Kubernát60d6f292018-05-25 09:45:32 +0200133 }
134};
135
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200136struct container_class {
137 template <typename T, typename Iterator, typename Context>
138 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
139 {
140 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +0200141 const auto& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200142
Václav Kubernát744f57f2018-06-29 22:46:26 +0200143 if (!schema.isContainer(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name}))
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200144 _pass(context) = false;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200145 }
146};
147
Václav Kubernát07204242018-06-04 18:12:09 +0200148struct leaf_class {
149 template <typename T, typename Iterator, typename Context>
150 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
151 {
152 auto& parserContext = x3::get<parser_context_tag>(context);
153 const auto& schema = parserContext.m_schema;
154
Václav Kubernát744f57f2018-06-29 22:46:26 +0200155 if (!schema.isLeaf(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name}))
156 _pass(context) = false;
157 }
158};
159
160
161struct module_class {
162 template <typename T, typename Iterator, typename Context>
163 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
164 {
165 auto& parserContext = x3::get<parser_context_tag>(context);
166 const auto& schema = parserContext.m_schema;
167
168 if (schema.isModule(parserContext.m_curPath, ast.m_name)) {
169 parserContext.m_curModule = ast.m_name;
170 parserContext.m_topLevelModulePresent = true;
Václav Kubernát07204242018-06-04 18:12:09 +0200171 } else {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200172 parserContext.m_errorMsg = "Invalid module name.";
Václav Kubernát07204242018-06-04 18:12:09 +0200173 _pass(context) = false;
174 }
175 }
176};
177
Václav Kubernát744f57f2018-06-29 22:46:26 +0200178struct node_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200179 template <typename T, typename Iterator, typename Context>
Václav Kubernát744f57f2018-06-29 22:46:26 +0200180 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200181 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200182 auto& parserContext = x3::get<parser_context_tag>(context);
183 if (ast.m_suffix.type() == typeid(nodeup_)) {
184 parserContext.m_curPath.m_nodes.pop_back();
185 if (parserContext.m_curPath.m_nodes.empty())
186 parserContext.m_topLevelModulePresent = false;
187 } else {
188 parserContext.m_curPath.m_nodes.push_back(ast);
189 parserContext.m_curModule = boost::none;
190 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200191 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200192};
Václav Kubernát07204242018-06-04 18:12:09 +0200193
Václav Kubernát37171a12018-08-31 17:01:48 +0200194struct absoluteStart_class {
195 template <typename T, typename Iterator, typename Context>
196 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
197 {
198 auto& parserContext = x3::get<parser_context_tag>(context);
199 parserContext.m_curPath.m_nodes.clear();
200 }
201};
202
Václav Kubernát744f57f2018-06-29 22:46:26 +0200203struct path_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200204 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200205 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernát07204242018-06-04 18:12:09 +0200206 {
207 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200208 if (parserContext.m_errorMsg.empty()) {
209 parserContext.m_errorMsg = "Expected path.";
Václav Kubernát07204242018-06-04 18:12:09 +0200210 return x3::error_handler_result::fail;
Václav Kubernát41378452018-06-06 16:29:40 +0200211 } else {
212 return x3::error_handler_result::rethrow;
213 }
Václav Kubernát07204242018-06-04 18:12:09 +0200214 }
215};
216
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200217
Václav Kubernát11afac72018-07-18 14:59:53 +0200218struct ls_class;
219
Václav Kubernát744f57f2018-06-29 22:46:26 +0200220struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200221 template <typename Iterator, typename Exception, typename Context>
222 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
223 {
224 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200225 if (parserContext.m_errorMsg.empty())
226 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
227 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200228 }
229};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200230
231struct presenceContainerPathHandler {
232 template <typename T, typename Iterator, typename Context>
233 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
234 {
235 auto& parserContext = x3::get<parser_context_tag>(context);
236 const auto& schema = parserContext.m_schema;
237 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200238 boost::optional<std::string> module;
239 if (ast.m_path.m_nodes.back().m_prefix)
240 module = ast.m_path.m_nodes.back().m_prefix.value().m_name;
241 container_ cont = boost::get<container_>(ast.m_path.m_nodes.back().m_suffix);
Václav Kubernátebca2552018-06-08 19:06:02 +0200242 path_ location = pathWithoutLastNode(parserContext.m_curPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200243
Václav Kubernát744f57f2018-06-29 22:46:26 +0200244 if (!schema.isPresenceContainer(location, {module, cont.m_name})) {
Václav Kubernát41378452018-06-06 16:29:40 +0200245 parserContext.m_errorMsg = "This container is not a presence container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200246 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200247 }
248 } catch (boost::bad_get&) {
Václav Kubernát41378452018-06-06 16:29:40 +0200249 parserContext.m_errorMsg = "This is not a container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200250 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200251 }
252 }
253
254 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200255 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200256 {
257 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200258 if (parserContext.m_errorMsg.empty())
259 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
260 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200261 }
262};
263
264struct create_class : public presenceContainerPathHandler {
265};
266
267struct delete_class : public presenceContainerPathHandler {
268};
269
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200270struct leaf_path_class {
271 template <typename T, typename Iterator, typename Context>
272 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
273 {
274 auto& parserContext = x3::get<parser_context_tag>(context);
275 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200276 auto leaf = boost::get<leaf_>(ast.m_nodes.back().m_suffix);
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200277 } catch (boost::bad_get&) {
278 parserContext.m_errorMsg = "This is not a path to leaf.";
279 _pass(context) = false;
280 }
281 }
282};
283
Václav Kubernátebca2552018-06-08 19:06:02 +0200284struct leaf_data_class {
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200285 template <typename Iterator, typename Exception, typename Context>
286 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
287 {
288 auto& parserContext = x3::get<parser_context_tag>(context);
289 auto& schema = parserContext.m_schema;
290 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200291 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200292 path_ location = pathWithoutLastNode(parserContext.m_curPath);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200293 parserContext.m_errorMsg = "Expected " + leafDataTypeToString(schema.leafType(location, {parserContext.m_curModule, leaf.m_name})) + " here:";
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200294 return x3::error_handler_result::fail;
295 }
296 return x3::error_handler_result::rethrow;
297 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200298};
299
300struct leaf_data_base_class {
301 yang::LeafDataTypes m_type;
302
303 leaf_data_base_class(yang::LeafDataTypes type)
304 : m_type(type)
305 {
306 }
307
308 template <typename T, typename Iterator, typename Context>
309 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
310 {
311 auto& parserContext = x3::get<parser_context_tag>(context);
312 auto& schema = parserContext.m_schema;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200313 boost::optional<std::string> module;
314 if (parserContext.m_curPath.m_nodes.back().m_prefix)
315 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
Václav Kubernátebca2552018-06-08 19:06:02 +0200316
Václav Kubernát744f57f2018-06-29 22:46:26 +0200317 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernátebca2552018-06-08 19:06:02 +0200318 path_ location = pathWithoutLastNode(parserContext.m_curPath);
319
Václav Kubernát744f57f2018-06-29 22:46:26 +0200320 if (schema.leafType(location, {module, leaf.m_name}) != m_type) {
Václav Kubernátebca2552018-06-08 19:06:02 +0200321 _pass(context) = false;
322 }
323 }
324};
325
326struct leaf_data_enum_class : leaf_data_base_class {
327 leaf_data_enum_class()
328 : leaf_data_base_class(yang::LeafDataTypes::Enum)
329 {
330 }
331
332 template <typename T, typename Iterator, typename Context>
333 void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context)
334 {
335 leaf_data_base_class::on_success(start, end, ast, context);
336 auto& parserContext = x3::get<parser_context_tag>(context);
337 auto& schema = parserContext.m_schema;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200338 boost::optional<std::string> module;
339 if (parserContext.m_curPath.m_nodes.back().m_prefix)
340 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
Václav Kubernátebca2552018-06-08 19:06:02 +0200341
Václav Kubernát744f57f2018-06-29 22:46:26 +0200342 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernátebca2552018-06-08 19:06:02 +0200343 path_ location = pathWithoutLastNode(parserContext.m_curPath);
344
Václav Kubernát744f57f2018-06-29 22:46:26 +0200345 if (!schema.leafEnumHasValue(location, {module, leaf.m_name}, ast.m_value)) {
Václav Kubernátebca2552018-06-08 19:06:02 +0200346 _pass(context) = false;
347 }
348 }
349};
350
351struct leaf_data_decimal_class : leaf_data_base_class {
352 leaf_data_decimal_class()
353 : leaf_data_base_class(yang::LeafDataTypes::Decimal)
354 {
355 }
356};
357
358struct leaf_data_bool_class : leaf_data_base_class {
359 leaf_data_bool_class()
360 : leaf_data_base_class(yang::LeafDataTypes::Bool)
361 {
362 }
363};
364
365struct leaf_data_int_class : leaf_data_base_class {
366 leaf_data_int_class()
367 : leaf_data_base_class(yang::LeafDataTypes::Int)
368 {
369 }
370};
371
372struct leaf_data_uint_class : leaf_data_base_class {
373 leaf_data_uint_class()
374 : leaf_data_base_class(yang::LeafDataTypes::Uint)
375 {
376 }
377};
378
379struct leaf_data_string_class : leaf_data_base_class {
380 leaf_data_string_class()
381 : leaf_data_base_class(yang::LeafDataTypes::String)
382 {
383 }
384};
385
Václav Kubernát07204242018-06-04 18:12:09 +0200386struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200387 template <typename Iterator, typename Exception, typename Context>
388 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
389 {
390 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200391 if (parserContext.m_errorMsg.empty())
392 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
393 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200394 }
395};
396
Václav Kubernát812ee282018-08-30 17:10:03 +0200397struct commit_class;
398
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200399struct get_class;
400
Václav Kubernátb61336d2018-05-28 17:35:03 +0200401struct command_class {
402 template <typename Iterator, typename Exception, typename Context>
403 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
404 {
405 auto& parserContext = x3::get<parser_context_tag>(context);
406 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
Václav Kubernát41378452018-06-06 16:29:40 +0200407 if (parserContext.m_errorMsg.empty()) {
408 parserContext.m_errorMsg = "Unknown command.";
409 }
410 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200411 return x3::error_handler_result::fail;
412 }
413};