blob: 2953dad0fc084f8dae0651e627c13a84bd6a1ed2 [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
56struct module_identifier_class;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020057
58struct listPrefix_class {
59 template <typename T, typename Iterator, typename Context>
60 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
61 {
62 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020063 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020064
Václav Kubernát744f57f2018-06-29 22:46:26 +020065 if (schema.isList(parserContext.m_curPath, {parserContext.m_curModule, ast})) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020066 parserContext.m_tmpListName = ast;
67 } else {
68 _pass(context) = false;
69 }
70 }
71};
72
73struct listSuffix_class {
74 template <typename T, typename Iterator, typename Context>
75 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
76 {
77 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +020078 const Schema& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020079
Václav Kubernát744f57f2018-06-29 22:46:26 +020080 const auto& keysNeeded = schema.listKeys(parserContext.m_curPath, {parserContext.m_curModule, parserContext.m_tmpListName});
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020081 std::set<std::string> keysSupplied;
82 for (const auto& it : ast)
83 keysSupplied.insert(it.first);
84
85 if (keysNeeded != keysSupplied) {
86 parserContext.m_errorMsg = "Not enough keys for " + parserContext.m_tmpListName + ". " +
87 "These keys were not supplied:";
88 std::set<std::string> missingKeys;
89 std::set_difference(keysNeeded.begin(), keysNeeded.end(),
90 keysSupplied.begin(), keysSupplied.end(),
91 std::inserter(missingKeys, missingKeys.end()));
92
93 for (const auto& it : missingKeys)
94 parserContext.m_errorMsg += " " + it;
95 parserContext.m_errorMsg += ".";
96
97 _pass(context) = false;
98 }
99 }
Václav Kubernát41378452018-06-06 16:29:40 +0200100
101 template <typename Iterator, typename Exception, typename Context>
102 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
103 {
104 auto& parserContext = x3::get<parser_context_tag>(context);
105 if (parserContext.m_errorMsg.empty())
106 parserContext.m_errorMsg = "Expecting ']' here:";
107 return x3::error_handler_result::rethrow;
Václav Kubernát41378452018-06-06 16:29:40 +0200108 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200109};
110struct listElement_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200111 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200112 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200113 {
114 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200115 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200116 return x3::error_handler_result::fail;
Václav Kubernát41378452018-06-06 16:29:40 +0200117 } else {
118 return x3::error_handler_result::rethrow;
119 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200120 }
121};
122
Václav Kubernát60d6f292018-05-25 09:45:32 +0200123struct nodeup_class {
124 template <typename T, typename Iterator, typename Context>
125 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
126 {
127 auto& parserContext = x3::get<parser_context_tag>(context);
128
Václav Kubernát744f57f2018-06-29 22:46:26 +0200129 if (parserContext.m_curPath.m_nodes.empty())
Václav Kubernát60d6f292018-05-25 09:45:32 +0200130 _pass(context) = false;
Václav Kubernát60d6f292018-05-25 09:45:32 +0200131 }
132};
133
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200134struct container_class {
135 template <typename T, typename Iterator, typename Context>
136 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
137 {
138 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát48fc3832018-05-28 14:21:22 +0200139 const auto& schema = parserContext.m_schema;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200140
Václav Kubernát744f57f2018-06-29 22:46:26 +0200141 if (!schema.isContainer(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name}))
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200142 _pass(context) = false;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200143 }
144};
145
Václav Kubernát07204242018-06-04 18:12:09 +0200146struct leaf_class {
147 template <typename T, typename Iterator, typename Context>
148 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
149 {
150 auto& parserContext = x3::get<parser_context_tag>(context);
151 const auto& schema = parserContext.m_schema;
152
Václav Kubernát744f57f2018-06-29 22:46:26 +0200153 if (!schema.isLeaf(parserContext.m_curPath, {parserContext.m_curModule, ast.m_name}))
154 _pass(context) = false;
155 }
156};
157
158
159struct module_class {
160 template <typename T, typename Iterator, typename Context>
161 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
162 {
163 auto& parserContext = x3::get<parser_context_tag>(context);
164 const auto& schema = parserContext.m_schema;
165
166 if (schema.isModule(parserContext.m_curPath, ast.m_name)) {
167 parserContext.m_curModule = ast.m_name;
168 parserContext.m_topLevelModulePresent = true;
Václav Kubernát07204242018-06-04 18:12:09 +0200169 } else {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200170 parserContext.m_errorMsg = "Invalid module name.";
Václav Kubernát07204242018-06-04 18:12:09 +0200171 _pass(context) = false;
172 }
173 }
174};
175
Václav Kubernát744f57f2018-06-29 22:46:26 +0200176struct node_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200177 template <typename T, typename Iterator, typename Context>
Václav Kubernát744f57f2018-06-29 22:46:26 +0200178 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200179 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200180 auto& parserContext = x3::get<parser_context_tag>(context);
181 if (ast.m_suffix.type() == typeid(nodeup_)) {
182 parserContext.m_curPath.m_nodes.pop_back();
183 if (parserContext.m_curPath.m_nodes.empty())
184 parserContext.m_topLevelModulePresent = false;
185 } else {
186 parserContext.m_curPath.m_nodes.push_back(ast);
187 parserContext.m_curModule = boost::none;
188 }
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200189 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200190};
Václav Kubernát07204242018-06-04 18:12:09 +0200191
Václav Kubernát744f57f2018-06-29 22:46:26 +0200192struct path_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200193 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200194 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernát07204242018-06-04 18:12:09 +0200195 {
196 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200197 if (parserContext.m_errorMsg.empty()) {
198 parserContext.m_errorMsg = "Expected path.";
Václav Kubernát07204242018-06-04 18:12:09 +0200199 return x3::error_handler_result::fail;
Václav Kubernát41378452018-06-06 16:29:40 +0200200 } else {
201 return x3::error_handler_result::rethrow;
202 }
Václav Kubernát07204242018-06-04 18:12:09 +0200203 }
204};
205
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200206
Václav Kubernát11afac72018-07-18 14:59:53 +0200207struct ls_class;
208
Václav Kubernát744f57f2018-06-29 22:46:26 +0200209struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200210 template <typename Iterator, typename Exception, typename Context>
211 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
212 {
213 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200214 if (parserContext.m_errorMsg.empty())
215 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
216 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200217 }
218};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200219
220struct presenceContainerPathHandler {
221 template <typename T, typename Iterator, typename Context>
222 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
223 {
224 auto& parserContext = x3::get<parser_context_tag>(context);
225 const auto& schema = parserContext.m_schema;
226 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200227 boost::optional<std::string> module;
228 if (ast.m_path.m_nodes.back().m_prefix)
229 module = ast.m_path.m_nodes.back().m_prefix.value().m_name;
230 container_ cont = boost::get<container_>(ast.m_path.m_nodes.back().m_suffix);
Václav Kubernátebca2552018-06-08 19:06:02 +0200231 path_ location = pathWithoutLastNode(parserContext.m_curPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200232
Václav Kubernát744f57f2018-06-29 22:46:26 +0200233 if (!schema.isPresenceContainer(location, {module, cont.m_name})) {
Václav Kubernát41378452018-06-06 16:29:40 +0200234 parserContext.m_errorMsg = "This container is not a presence container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200235 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200236 }
237 } catch (boost::bad_get&) {
Václav Kubernát41378452018-06-06 16:29:40 +0200238 parserContext.m_errorMsg = "This is not a container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200239 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200240 }
241 }
242
243 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200244 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200245 {
246 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200247 if (parserContext.m_errorMsg.empty())
248 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
249 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200250 }
251};
252
253struct create_class : public presenceContainerPathHandler {
254};
255
256struct delete_class : public presenceContainerPathHandler {
257};
258
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200259struct leaf_path_class {
260 template <typename T, typename Iterator, typename Context>
261 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
262 {
263 auto& parserContext = x3::get<parser_context_tag>(context);
264 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200265 auto leaf = boost::get<leaf_>(ast.m_nodes.back().m_suffix);
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200266 } catch (boost::bad_get&) {
267 parserContext.m_errorMsg = "This is not a path to leaf.";
268 _pass(context) = false;
269 }
270 }
271};
272
Václav Kubernátebca2552018-06-08 19:06:02 +0200273struct leaf_data_class {
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200274 template <typename Iterator, typename Exception, typename Context>
275 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
276 {
277 auto& parserContext = x3::get<parser_context_tag>(context);
278 auto& schema = parserContext.m_schema;
279 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200280 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200281 path_ location = pathWithoutLastNode(parserContext.m_curPath);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200282 parserContext.m_errorMsg = "Expected " + leafDataTypeToString(schema.leafType(location, {parserContext.m_curModule, leaf.m_name})) + " here:";
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200283 return x3::error_handler_result::fail;
284 }
285 return x3::error_handler_result::rethrow;
286 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200287};
288
289struct leaf_data_base_class {
290 yang::LeafDataTypes m_type;
291
292 leaf_data_base_class(yang::LeafDataTypes type)
293 : m_type(type)
294 {
295 }
296
297 template <typename T, typename Iterator, typename Context>
298 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
299 {
300 auto& parserContext = x3::get<parser_context_tag>(context);
301 auto& schema = parserContext.m_schema;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200302 boost::optional<std::string> module;
303 if (parserContext.m_curPath.m_nodes.back().m_prefix)
304 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
Václav Kubernátebca2552018-06-08 19:06:02 +0200305
Václav Kubernát744f57f2018-06-29 22:46:26 +0200306 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernátebca2552018-06-08 19:06:02 +0200307 path_ location = pathWithoutLastNode(parserContext.m_curPath);
308
Václav Kubernát744f57f2018-06-29 22:46:26 +0200309 if (schema.leafType(location, {module, leaf.m_name}) != m_type) {
Václav Kubernátebca2552018-06-08 19:06:02 +0200310 _pass(context) = false;
311 }
312 }
313};
314
315struct leaf_data_enum_class : leaf_data_base_class {
316 leaf_data_enum_class()
317 : leaf_data_base_class(yang::LeafDataTypes::Enum)
318 {
319 }
320
321 template <typename T, typename Iterator, typename Context>
322 void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context)
323 {
324 leaf_data_base_class::on_success(start, end, ast, context);
325 auto& parserContext = x3::get<parser_context_tag>(context);
326 auto& schema = parserContext.m_schema;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200327 boost::optional<std::string> module;
328 if (parserContext.m_curPath.m_nodes.back().m_prefix)
329 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
Václav Kubernátebca2552018-06-08 19:06:02 +0200330
Václav Kubernát744f57f2018-06-29 22:46:26 +0200331 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernátebca2552018-06-08 19:06:02 +0200332 path_ location = pathWithoutLastNode(parserContext.m_curPath);
333
Václav Kubernát744f57f2018-06-29 22:46:26 +0200334 if (!schema.leafEnumHasValue(location, {module, leaf.m_name}, ast.m_value)) {
Václav Kubernátebca2552018-06-08 19:06:02 +0200335 _pass(context) = false;
336 }
337 }
338};
339
340struct leaf_data_decimal_class : leaf_data_base_class {
341 leaf_data_decimal_class()
342 : leaf_data_base_class(yang::LeafDataTypes::Decimal)
343 {
344 }
345};
346
347struct leaf_data_bool_class : leaf_data_base_class {
348 leaf_data_bool_class()
349 : leaf_data_base_class(yang::LeafDataTypes::Bool)
350 {
351 }
352};
353
354struct leaf_data_int_class : leaf_data_base_class {
355 leaf_data_int_class()
356 : leaf_data_base_class(yang::LeafDataTypes::Int)
357 {
358 }
359};
360
361struct leaf_data_uint_class : leaf_data_base_class {
362 leaf_data_uint_class()
363 : leaf_data_base_class(yang::LeafDataTypes::Uint)
364 {
365 }
366};
367
368struct leaf_data_string_class : leaf_data_base_class {
369 leaf_data_string_class()
370 : leaf_data_base_class(yang::LeafDataTypes::String)
371 {
372 }
373};
374
Václav Kubernát07204242018-06-04 18:12:09 +0200375struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200376 template <typename Iterator, typename Exception, typename Context>
377 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
378 {
379 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200380 if (parserContext.m_errorMsg.empty())
381 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
382 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200383 }
384};
385
Václav Kubernátb61336d2018-05-28 17:35:03 +0200386struct command_class {
387 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);
391 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
Václav Kubernát41378452018-06-06 16:29:40 +0200392 if (parserContext.m_errorMsg.empty()) {
393 parserContext.m_errorMsg = "Unknown command.";
394 }
395 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200396 return x3::error_handler_result::fail;
397 }
398};