blob: b58613c9f3d75b87f70810cbf7dc7eb2b6d59ae1 [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át744f57f2018-06-29 22:46:26 +0200207struct cd_class {
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200208 template <typename Iterator, typename Exception, typename Context>
209 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
210 {
211 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200212 if (parserContext.m_errorMsg.empty())
213 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
214 return x3::error_handler_result::rethrow;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200215 }
216};
Václav Kubernátb61336d2018-05-28 17:35:03 +0200217
218struct presenceContainerPathHandler {
219 template <typename T, typename Iterator, typename Context>
220 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
221 {
222 auto& parserContext = x3::get<parser_context_tag>(context);
223 const auto& schema = parserContext.m_schema;
224 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200225 boost::optional<std::string> module;
226 if (ast.m_path.m_nodes.back().m_prefix)
227 module = ast.m_path.m_nodes.back().m_prefix.value().m_name;
228 container_ cont = boost::get<container_>(ast.m_path.m_nodes.back().m_suffix);
Václav Kubernátebca2552018-06-08 19:06:02 +0200229 path_ location = pathWithoutLastNode(parserContext.m_curPath);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200230
Václav Kubernát744f57f2018-06-29 22:46:26 +0200231 if (!schema.isPresenceContainer(location, {module, cont.m_name})) {
Václav Kubernát41378452018-06-06 16:29:40 +0200232 parserContext.m_errorMsg = "This container is not a presence container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200233 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200234 }
235 } catch (boost::bad_get&) {
Václav Kubernát41378452018-06-06 16:29:40 +0200236 parserContext.m_errorMsg = "This is not a container.";
Václav Kubernátb61336d2018-05-28 17:35:03 +0200237 _pass(context) = false;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200238 }
239 }
240
241 template <typename Iterator, typename Exception, typename Context>
Václav Kubernát41378452018-06-06 16:29:40 +0200242 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200243 {
244 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200245 if (parserContext.m_errorMsg.empty())
246 parserContext.m_errorMsg = "Couldn't parse create/delete command.";
247 return x3::error_handler_result::rethrow;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200248 }
249};
250
251struct create_class : public presenceContainerPathHandler {
252};
253
254struct delete_class : public presenceContainerPathHandler {
255};
256
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200257struct leaf_path_class {
258 template <typename T, typename Iterator, typename Context>
259 void on_success(Iterator const&, Iterator const&, T& ast, Context const& context)
260 {
261 auto& parserContext = x3::get<parser_context_tag>(context);
262 try {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200263 auto leaf = boost::get<leaf_>(ast.m_nodes.back().m_suffix);
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200264 } catch (boost::bad_get&) {
265 parserContext.m_errorMsg = "This is not a path to leaf.";
266 _pass(context) = false;
267 }
268 }
269};
270
Václav Kubernátebca2552018-06-08 19:06:02 +0200271struct leaf_data_class {
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200272 template <typename Iterator, typename Exception, typename Context>
273 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const&, Context const& context)
274 {
275 auto& parserContext = x3::get<parser_context_tag>(context);
276 auto& schema = parserContext.m_schema;
277 if (parserContext.m_errorMsg.empty()) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200278 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200279 path_ location = pathWithoutLastNode(parserContext.m_curPath);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200280 parserContext.m_errorMsg = "Expected " + leafDataTypeToString(schema.leafType(location, {parserContext.m_curModule, leaf.m_name})) + " here:";
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200281 return x3::error_handler_result::fail;
282 }
283 return x3::error_handler_result::rethrow;
284 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200285};
286
287struct leaf_data_base_class {
288 yang::LeafDataTypes m_type;
289
290 leaf_data_base_class(yang::LeafDataTypes type)
291 : m_type(type)
292 {
293 }
294
295 template <typename T, typename Iterator, typename Context>
296 void on_success(Iterator const&, Iterator const&, T&, Context const& context)
297 {
298 auto& parserContext = x3::get<parser_context_tag>(context);
299 auto& schema = parserContext.m_schema;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200300 boost::optional<std::string> module;
301 if (parserContext.m_curPath.m_nodes.back().m_prefix)
302 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
Václav Kubernátebca2552018-06-08 19:06:02 +0200303
Václav Kubernát744f57f2018-06-29 22:46:26 +0200304 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernátebca2552018-06-08 19:06:02 +0200305 path_ location = pathWithoutLastNode(parserContext.m_curPath);
306
Václav Kubernát744f57f2018-06-29 22:46:26 +0200307 if (schema.leafType(location, {module, leaf.m_name}) != m_type) {
Václav Kubernátebca2552018-06-08 19:06:02 +0200308 _pass(context) = false;
309 }
310 }
311};
312
313struct leaf_data_enum_class : leaf_data_base_class {
314 leaf_data_enum_class()
315 : leaf_data_base_class(yang::LeafDataTypes::Enum)
316 {
317 }
318
319 template <typename T, typename Iterator, typename Context>
320 void on_success(Iterator const& start, Iterator const& end, T& ast, Context const& context)
321 {
322 leaf_data_base_class::on_success(start, end, ast, context);
323 auto& parserContext = x3::get<parser_context_tag>(context);
324 auto& schema = parserContext.m_schema;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200325 boost::optional<std::string> module;
326 if (parserContext.m_curPath.m_nodes.back().m_prefix)
327 module = parserContext.m_curPath.m_nodes.back().m_prefix.value().m_name;
Václav Kubernátebca2552018-06-08 19:06:02 +0200328
Václav Kubernát744f57f2018-06-29 22:46:26 +0200329 leaf_ leaf = boost::get<leaf_>(parserContext.m_curPath.m_nodes.back().m_suffix);
Václav Kubernátebca2552018-06-08 19:06:02 +0200330 path_ location = pathWithoutLastNode(parserContext.m_curPath);
331
Václav Kubernát744f57f2018-06-29 22:46:26 +0200332 if (!schema.leafEnumHasValue(location, {module, leaf.m_name}, ast.m_value)) {
Václav Kubernátebca2552018-06-08 19:06:02 +0200333 _pass(context) = false;
334 }
335 }
336};
337
338struct leaf_data_decimal_class : leaf_data_base_class {
339 leaf_data_decimal_class()
340 : leaf_data_base_class(yang::LeafDataTypes::Decimal)
341 {
342 }
343};
344
345struct leaf_data_bool_class : leaf_data_base_class {
346 leaf_data_bool_class()
347 : leaf_data_base_class(yang::LeafDataTypes::Bool)
348 {
349 }
350};
351
352struct leaf_data_int_class : leaf_data_base_class {
353 leaf_data_int_class()
354 : leaf_data_base_class(yang::LeafDataTypes::Int)
355 {
356 }
357};
358
359struct leaf_data_uint_class : leaf_data_base_class {
360 leaf_data_uint_class()
361 : leaf_data_base_class(yang::LeafDataTypes::Uint)
362 {
363 }
364};
365
366struct leaf_data_string_class : leaf_data_base_class {
367 leaf_data_string_class()
368 : leaf_data_base_class(yang::LeafDataTypes::String)
369 {
370 }
371};
372
Václav Kubernát07204242018-06-04 18:12:09 +0200373struct set_class {
Václav Kubernát07204242018-06-04 18:12:09 +0200374 template <typename Iterator, typename Exception, typename Context>
375 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
376 {
377 auto& parserContext = x3::get<parser_context_tag>(context);
Václav Kubernát41378452018-06-06 16:29:40 +0200378 if (parserContext.m_errorMsg.empty())
379 parserContext.m_errorMsg = "Expected " + x.which() + " here:";
380 return x3::error_handler_result::rethrow;
Václav Kubernát07204242018-06-04 18:12:09 +0200381 }
382};
383
Václav Kubernátb61336d2018-05-28 17:35:03 +0200384struct command_class {
385 template <typename Iterator, typename Exception, typename Context>
386 x3::error_handler_result on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
387 {
388 auto& parserContext = x3::get<parser_context_tag>(context);
389 auto& error_handler = x3::get<x3::error_handler_tag>(context).get();
Václav Kubernát41378452018-06-06 16:29:40 +0200390 if (parserContext.m_errorMsg.empty()) {
391 parserContext.m_errorMsg = "Unknown command.";
392 }
393 error_handler(x.where(), parserContext.m_errorMsg);
Václav Kubernátb61336d2018-05-28 17:35:03 +0200394 return x3::error_handler_result::fail;
395 }
396};