blob: 9c56d4f44eadbb0b476b253bf8e82f2fc04128e6 [file] [log] [blame]
Václav Kubernát24df80e2018-06-06 15:18:03 +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#pragma once
9
Václav Kubernát57272422019-02-08 12:48:24 +010010#include <boost/mpl/vector.hpp>
Václav Kubernát509ce652019-05-29 19:46:44 +020011#include <boost/spirit/home/x3/support/ast/position_tagged.hpp>
Václav Kubernát24df80e2018-06-06 15:18:03 +020012#include "ast_path.hpp"
Václav Kubernát627f6152018-08-29 13:23:56 +020013#include "ast_values.hpp"
Václav Kubernátbf65dd72020-05-28 02:32:31 +020014#include "yang_move.hpp"
Václav Kubernát24df80e2018-06-06 15:18:03 +020015
16namespace x3 = boost::spirit::x3;
Václav Kubernát24df80e2018-06-06 15:18:03 +020017
Václav Kubernát7707cae2020-01-16 12:04:53 +010018using keyValue_ = std::pair<std::string, leaf_data_>;
Václav Kubernát24df80e2018-06-06 15:18:03 +020019
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020020enum class LsOption {
21 Recursive
22};
23
Václav Kubernát6d791432018-10-25 16:00:35 +020024struct discard_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010025 static constexpr auto name = "discard";
Václav Kubernát054cc992019-02-21 14:23:52 +010026 static constexpr auto shortHelp = "discard - Discard current changes.";
27 static constexpr auto longHelp = R"(
28 discard
29
30 Discards current changes. Accepts no arguments.
31
32 Usage:
33 /> discard)";
Václav Kubernát6d791432018-10-25 16:00:35 +020034 bool operator==(const discard_& b) const;
35};
36
Václav Kubernát11afac72018-07-18 14:59:53 +020037struct ls_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010038 static constexpr auto name = "ls";
Václav Kubernát054cc992019-02-21 14:23:52 +010039 static constexpr auto shortHelp = "ls - List available nodes.";
40 static constexpr auto longHelp = R"(
41 ls [--recursive] [path]
42
43 Lists available nodes in the current directory. Optionally
44 accepts a path argument. Accepts both schema paths and data
45 paths. Path starting with a forward slash means an absolute
46 path.
47
48 Usage:
49 /> ls
50 /> ls --recursive module:node
51 /> ls /module:node)";
Václav Kubernát11afac72018-07-18 14:59:53 +020052 bool operator==(const ls_& b) const;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020053 std::vector<LsOption> m_options;
Václav Kubernátbeaa8aa2020-04-29 22:39:34 +020054 boost::optional<boost::variant<dataPath_, schemaPath_, module_>> m_path;
Václav Kubernát11afac72018-07-18 14:59:53 +020055};
56
Václav Kubernát24df80e2018-06-06 15:18:03 +020057struct cd_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010058 static constexpr auto name = "cd";
Václav Kubernát054cc992019-02-21 14:23:52 +010059 static constexpr auto shortHelp = "cd - Enter a different node.";
60 static constexpr auto longHelp = R"(
61 cd path
62
63 Enters a node specified by path. Only accepts data paths.
64
65 Usage:
66 /> cd /module:node/node2
67 /> cd ..)";
Václav Kubernát24df80e2018-06-06 15:18:03 +020068 bool operator==(const cd_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020069 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +020070};
71
72struct create_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010073 static constexpr auto name = "create";
Václav Kubernát054cc992019-02-21 14:23:52 +010074 static constexpr auto shortHelp = "create - Create a presence container.";
75 static constexpr auto longHelp = R"(
Václav Kubernátf5f64f02019-03-19 17:15:47 +010076 create path
Václav Kubernát054cc992019-02-21 14:23:52 +010077
Václav Kubernátf5f64f02019-03-19 17:15:47 +010078 Creates a presence container or a list instance specified by path.
Václav Kubernát054cc992019-02-21 14:23:52 +010079
80 Usage:
Václav Kubernátf5f64f02019-03-19 17:15:47 +010081 /> create /module:pContainer
82 /> create /module:list[key=value][anotherKey=value])";
Václav Kubernát24df80e2018-06-06 15:18:03 +020083 bool operator==(const create_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020084 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +020085};
86
87struct delete_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +010088 static constexpr auto name = "delete";
Václav Kubernát054cc992019-02-21 14:23:52 +010089 static constexpr auto shortHelp = "delete - Delete a presence container.";
90 static constexpr auto longHelp = R"(
Václav Kubernátf5f64f02019-03-19 17:15:47 +010091 delete path
Václav Kubernát054cc992019-02-21 14:23:52 +010092
Václav Kubernátf5f64f02019-03-19 17:15:47 +010093 Deletes a presence container or a list instance specified by path.
Václav Kubernát054cc992019-02-21 14:23:52 +010094
95 Usage:
Václav Kubernátf5f64f02019-03-19 17:15:47 +010096 /> delete /module:pContainer
97 /> delete /module:list[key=value][anotherKey=value])";
Václav Kubernát24df80e2018-06-06 15:18:03 +020098 bool operator==(const delete_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020099 dataPath_ m_path;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200100};
101
102struct set_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100103 static constexpr auto name = "set";
Václav Kubernát054cc992019-02-21 14:23:52 +0100104 static constexpr auto shortHelp = "set - Change value of a leaf.";
105 static constexpr auto longHelp = R"(
106 set path_to_leaf value
107
108 Changes the leaf specified by path to value.
109
110 Usage:
111 /> set /module:leaf 123
112 /> set /module:leaf abc)";
Václav Kubernát24df80e2018-06-06 15:18:03 +0200113 bool operator==(const set_& b) const;
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200114 dataPath_ m_path;
Václav Kubernátebca2552018-06-08 19:06:02 +0200115 leaf_data_ m_data;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200116};
117
Václav Kubernát812ee282018-08-30 17:10:03 +0200118struct commit_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100119 static constexpr auto name = "commit";
Václav Kubernát054cc992019-02-21 14:23:52 +0100120 static constexpr auto shortHelp = "commit - Commit current changes.";
121 static constexpr auto longHelp = R"(
122 commit
123
124 Commits the current changes. Accepts no arguments.
125
126 Usage:
127 /> commit)";
Václav Kubernát812ee282018-08-30 17:10:03 +0200128 bool operator==(const set_& b) const;
129};
130
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200131struct get_ : x3::position_tagged {
Václav Kubernát57272422019-02-08 12:48:24 +0100132 static constexpr auto name = "get";
Václav Kubernát054cc992019-02-21 14:23:52 +0100133 static constexpr auto shortHelp = "get - Retrieve configuration from the server.";
134 static constexpr auto longHelp = R"(
135 get [path]
136
137 Retrieves configuration of the current node. Works recursively.
138 Optionally takes an argument specifying a path, the output will,
139 as if the user was in that node.
140
141 Usage:
142 /> get
143 /> get /module:path)";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200144 bool operator==(const get_& b) const;
Václav Kubernátd6247992020-05-27 00:17:56 +0200145 boost::optional<boost::variant<dataPath_, schemaPath_, module_>> m_path;
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200146};
Václav Kubernát812ee282018-08-30 17:10:03 +0200147
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100148struct describe_ : x3::position_tagged {
149 static constexpr auto name = "describe";
150 static constexpr auto shortHelp = "describe - Print information about YANG tree path.";
151 static constexpr auto longHelp = R"(
152 describe <path>
153
154 Show documentation of YANG tree paths. In the YANG model, each item may
155 have an optional `description` which often explains the function of that
156 node to the end user. This command takes the description from the YANG
157 model and shows it to the user along with additional data, such as the type
158 of the node, units of leaf values, etc.
159
160 Usage:
161 /> describe /module:node)";
162 bool operator==(const describe_& b) const;
163
164 boost::variant<schemaPath_, dataPath_> m_path;
165};
166
Václav Kubernát7160a132020-04-03 02:11:01 +0200167struct copy_ : x3::position_tagged {
168 static constexpr auto name = "copy";
169 static constexpr auto shortHelp = "copy - copy configuration datastores around";
170 static constexpr auto longHelp = R"(
171 copy <source> <destination>
172
173 Usage:
174 /> copy running startup
175 /> copy startup running)";
176 bool operator==(const copy_& b) const;
177
178 Datastore m_source;
179 Datastore m_destination;
180};
181
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200182enum class MoveMode {
183 Begin,
184 End,
185 Before,
186 After
187};
188
189struct move_ : x3::position_tagged {
190 static constexpr auto name = "move";
191 static constexpr auto shortHelp = "move - move (leaf)list instances around";
192 static constexpr auto longHelp = R"(
193 move <list-instance-path> begin
194 move <list-instance-path> end
195 move <list-instance-path> before <key>
196 move <list-instance-path> after <key>
197
198 Usage:
199 /> move mod:leaflist['abc'] begin
200 /> move mod:leaflist['def'] after 'abc'
201 /> move mod:interfaces['eth0'] after ['eth1'])";
202 bool operator==(const move_& b) const;
203
204 dataPath_ m_source;
205
206 std::variant<yang::move::Absolute, yang::move::Relative> m_destination;
207};
208
Václav Kubernát054cc992019-02-21 14:23:52 +0100209struct help_;
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200210using CommandTypes = boost::mpl::vector<cd_, commit_, copy_, create_, delete_, describe_, discard_, get_, help_, ls_, move_, set_>;
Václav Kubernát054cc992019-02-21 14:23:52 +0100211struct help_ : x3::position_tagged {
212 static constexpr auto name = "help";
213 static constexpr auto shortHelp = "help - Print help for commands.";
214 static constexpr auto longHelp = R"(
215 help [command_name]
216
217 Print help for command_name. If used without an argument,
218 print short help for all commands.
219
220 Usage:
221 /> help
222 /> help cd
223 /> help help)";
224 bool operator==(const help_& b) const;
225
226 // The help command has got one optional argument – a command name (type).
227 // All commands are saved in CommandTypes, so we could just use that, but
228 // that way, Spirit would be default constructing the command structs,
229 // which is undesirable, so firstly we use mpl::transform to wrap
230 // CommandTypes with boost::type:
231 using WrappedCommandTypes = boost::mpl::transform<CommandTypes, boost::type<boost::mpl::_>>::type;
232 // Next, we create a variant over the wrapped types:
233 using CommandTypesVariant = boost::make_variant_over<WrappedCommandTypes>::type;
234 // Finally, we wrap the variant with boost::optional:
235 boost::optional<CommandTypesVariant> m_cmd;
236};
237
Václav Kubernát57272422019-02-08 12:48:24 +0100238// TODO: The usage of MPL won't be necessary after std::variant support is added to Spirit
239// https://github.com/boostorg/spirit/issues/270
Václav Kubernát57272422019-02-08 12:48:24 +0100240using command_ = boost::make_variant_over<CommandTypes>::type;
Václav Kubernát24df80e2018-06-06 15:18:03 +0200241
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200242BOOST_FUSION_ADAPT_STRUCT(ls_, m_options, m_path)
Václav Kubernát24df80e2018-06-06 15:18:03 +0200243BOOST_FUSION_ADAPT_STRUCT(cd_, m_path)
244BOOST_FUSION_ADAPT_STRUCT(create_, m_path)
245BOOST_FUSION_ADAPT_STRUCT(delete_, m_path)
Václav Kubernát24df80e2018-06-06 15:18:03 +0200246BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data)
Václav Kubernátab538992019-03-06 15:30:50 +0100247BOOST_FUSION_ADAPT_STRUCT(enum_, m_value)
248BOOST_FUSION_ADAPT_STRUCT(binary_, m_value)
Václav Kubernáteeb38842019-03-20 19:46:05 +0100249BOOST_FUSION_ADAPT_STRUCT(identityRef_, m_prefix, m_value)
Václav Kubernát812ee282018-08-30 17:10:03 +0200250BOOST_FUSION_ADAPT_STRUCT(commit_)
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100251BOOST_FUSION_ADAPT_STRUCT(describe_, m_path)
Václav Kubernát054cc992019-02-21 14:23:52 +0100252BOOST_FUSION_ADAPT_STRUCT(help_, m_cmd)
Václav Kubernát6d791432018-10-25 16:00:35 +0200253BOOST_FUSION_ADAPT_STRUCT(discard_)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200254BOOST_FUSION_ADAPT_STRUCT(get_, m_path)
Václav Kubernát7160a132020-04-03 02:11:01 +0200255BOOST_FUSION_ADAPT_STRUCT(copy_, m_source, m_destination)
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200256BOOST_FUSION_ADAPT_STRUCT(move_, m_source, m_destination)