Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 1 | /* |
| 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át | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 10 | #include <boost/mpl/vector.hpp> |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 11 | #include <boost/spirit/home/x3/support/ast/position_tagged.hpp> |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 12 | #include "ast_path.hpp" |
Václav Kubernát | 627f615 | 2018-08-29 13:23:56 +0200 | [diff] [blame] | 13 | #include "ast_values.hpp" |
Václav Kubernát | 6a7dd4d | 2020-06-23 22:44:13 +0200 | [diff] [blame] | 14 | #include "yang_operations.hpp" |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 15 | |
| 16 | namespace x3 = boost::spirit::x3; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 17 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 18 | using keyValue_ = std::pair<std::string, leaf_data_>; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 19 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 20 | enum class LsOption { |
| 21 | Recursive |
| 22 | }; |
| 23 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 24 | struct discard_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 25 | static constexpr auto name = "discard"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 26 | 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át | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 34 | bool operator==(const discard_& b) const; |
| 35 | }; |
| 36 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 37 | struct ls_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 38 | static constexpr auto name = "ls"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 39 | 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át | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 52 | bool operator==(const ls_& b) const; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 53 | std::vector<LsOption> m_options; |
Václav Kubernát | beaa8aa | 2020-04-29 22:39:34 +0200 | [diff] [blame] | 54 | boost::optional<boost::variant<dataPath_, schemaPath_, module_>> m_path; |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 57 | struct cd_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 58 | static constexpr auto name = "cd"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 59 | 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át | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 68 | bool operator==(const cd_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 69 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | struct create_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 73 | static constexpr auto name = "create"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 74 | static constexpr auto shortHelp = "create - Create a presence container."; |
| 75 | static constexpr auto longHelp = R"( |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 76 | create path |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 77 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 78 | Creates a presence container or a list instance specified by path. |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 79 | |
| 80 | Usage: |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 81 | /> create /module:pContainer |
| 82 | /> create /module:list[key=value][anotherKey=value])"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 83 | bool operator==(const create_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 84 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | struct delete_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 88 | static constexpr auto name = "delete"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 89 | static constexpr auto shortHelp = "delete - Delete a presence container."; |
| 90 | static constexpr auto longHelp = R"( |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 91 | delete path |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 92 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 93 | Deletes a presence container or a list instance specified by path. |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 94 | |
| 95 | Usage: |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 96 | /> delete /module:pContainer |
| 97 | /> delete /module:list[key=value][anotherKey=value])"; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 98 | bool operator==(const delete_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 99 | dataPath_ m_path; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | struct set_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 103 | static constexpr auto name = "set"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 104 | 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át | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 113 | bool operator==(const set_& b) const; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 114 | dataPath_ m_path; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 115 | leaf_data_ m_data; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 118 | struct commit_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 119 | static constexpr auto name = "commit"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 120 | 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át | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 130 | struct get_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 131 | static constexpr auto name = "get"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 132 | static constexpr auto shortHelp = "get - Retrieve configuration from the server."; |
| 133 | static constexpr auto longHelp = R"( |
| 134 | get [path] |
| 135 | |
| 136 | Retrieves configuration of the current node. Works recursively. |
| 137 | Optionally takes an argument specifying a path, the output will, |
| 138 | as if the user was in that node. |
| 139 | |
| 140 | Usage: |
| 141 | /> get |
| 142 | /> get /module:path)"; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 143 | bool operator==(const get_& b) const; |
Václav Kubernát | 8f6bff3 | 2020-06-10 14:51:10 +0200 | [diff] [blame] | 144 | boost::optional<boost::variant<dataPath_, module_>> m_path; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 145 | }; |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 146 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 147 | struct describe_ : x3::position_tagged { |
| 148 | static constexpr auto name = "describe"; |
| 149 | static constexpr auto shortHelp = "describe - Print information about YANG tree path."; |
| 150 | static constexpr auto longHelp = R"( |
| 151 | describe <path> |
| 152 | |
| 153 | Show documentation of YANG tree paths. In the YANG model, each item may |
| 154 | have an optional `description` which often explains the function of that |
| 155 | node to the end user. This command takes the description from the YANG |
| 156 | model and shows it to the user along with additional data, such as the type |
| 157 | of the node, units of leaf values, etc. |
| 158 | |
| 159 | Usage: |
| 160 | /> describe /module:node)"; |
| 161 | bool operator==(const describe_& b) const; |
| 162 | |
| 163 | boost::variant<schemaPath_, dataPath_> m_path; |
| 164 | }; |
| 165 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 166 | struct copy_ : x3::position_tagged { |
| 167 | static constexpr auto name = "copy"; |
| 168 | static constexpr auto shortHelp = "copy - copy configuration datastores around"; |
| 169 | static constexpr auto longHelp = R"( |
| 170 | copy <source> <destination> |
| 171 | |
| 172 | Usage: |
| 173 | /> copy running startup |
| 174 | /> copy startup running)"; |
| 175 | bool operator==(const copy_& b) const; |
| 176 | |
| 177 | Datastore m_source; |
| 178 | Datastore m_destination; |
| 179 | }; |
| 180 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 181 | enum class MoveMode { |
| 182 | Begin, |
| 183 | End, |
| 184 | Before, |
| 185 | After |
| 186 | }; |
| 187 | |
| 188 | struct move_ : x3::position_tagged { |
| 189 | static constexpr auto name = "move"; |
| 190 | static constexpr auto shortHelp = "move - move (leaf)list instances around"; |
| 191 | static constexpr auto longHelp = R"( |
| 192 | move <list-instance-path> begin |
| 193 | move <list-instance-path> end |
| 194 | move <list-instance-path> before <key> |
| 195 | move <list-instance-path> after <key> |
| 196 | |
| 197 | Usage: |
| 198 | /> move mod:leaflist['abc'] begin |
| 199 | /> move mod:leaflist['def'] after 'abc' |
| 200 | /> move mod:interfaces['eth0'] after ['eth1'])"; |
| 201 | bool operator==(const move_& b) const; |
| 202 | |
| 203 | dataPath_ m_source; |
| 204 | |
| 205 | std::variant<yang::move::Absolute, yang::move::Relative> m_destination; |
| 206 | }; |
| 207 | |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 208 | struct dump_ : x3::position_tagged { |
| 209 | static constexpr auto name = "dump"; |
| 210 | static constexpr auto shortHelp = "dump - dump entire content of the datastore"; |
| 211 | static constexpr auto longHelp = R"( |
| 212 | dump xml|json |
| 213 | |
| 214 | Usage: |
| 215 | /> dump xml |
| 216 | /> dump json)"; |
| 217 | bool operator==(const dump_& other) const; |
| 218 | |
| 219 | DataFormat m_format; |
| 220 | }; |
| 221 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 222 | struct prepare_ : x3::position_tagged { |
| 223 | static constexpr auto name = "prepare"; |
| 224 | static constexpr auto shortHelp = "prepare - initiate RPC or action"; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 225 | static constexpr auto longHelp = R"( |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 226 | prepare <path-to-rpc-or-action> |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 227 | |
| 228 | This command puts you into a mode where you can set your input parameters. |
| 229 | |
| 230 | Usage: |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 231 | /> prepare <path-to-rpc-or-action>)"; |
| 232 | bool operator==(const prepare_& other) const; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 233 | |
| 234 | dataPath_ m_path; |
| 235 | }; |
| 236 | |
| 237 | struct exec_ : x3::position_tagged { |
| 238 | static constexpr auto name = "exec"; |
| 239 | static constexpr auto shortHelp = "exec - execute RPC"; |
| 240 | static constexpr auto longHelp = R"( |
| 241 | exec |
| 242 | |
| 243 | This command executes the RPC you have previously initiated. |
| 244 | |
| 245 | Usage: |
| 246 | /> exec)"; |
| 247 | bool operator==(const exec_& other) const; |
| 248 | }; |
| 249 | |
| 250 | struct cancel_ : x3::position_tagged { |
| 251 | static constexpr auto name = "cancel"; |
| 252 | static constexpr auto shortHelp = "cancel - cancel an ongoing RPC input"; |
| 253 | static constexpr auto longHelp = R"( |
| 254 | cancel |
| 255 | |
| 256 | This command cancels a previously entered RPC context. |
| 257 | |
| 258 | Usage: |
| 259 | /> cancel)"; |
| 260 | bool operator==(const cancel_& other) const; |
| 261 | }; |
| 262 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 263 | struct help_; |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 264 | using CommandTypes = boost::mpl::vector<cancel_, cd_, commit_, copy_, create_, delete_, describe_, discard_, dump_, exec_, get_, help_, ls_, move_, prepare_, set_>; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 265 | struct help_ : x3::position_tagged { |
| 266 | static constexpr auto name = "help"; |
| 267 | static constexpr auto shortHelp = "help - Print help for commands."; |
| 268 | static constexpr auto longHelp = R"( |
| 269 | help [command_name] |
| 270 | |
| 271 | Print help for command_name. If used without an argument, |
| 272 | print short help for all commands. |
| 273 | |
| 274 | Usage: |
| 275 | /> help |
| 276 | /> help cd |
| 277 | /> help help)"; |
| 278 | bool operator==(const help_& b) const; |
| 279 | |
| 280 | // The help command has got one optional argument – a command name (type). |
| 281 | // All commands are saved in CommandTypes, so we could just use that, but |
| 282 | // that way, Spirit would be default constructing the command structs, |
| 283 | // which is undesirable, so firstly we use mpl::transform to wrap |
| 284 | // CommandTypes with boost::type: |
| 285 | using WrappedCommandTypes = boost::mpl::transform<CommandTypes, boost::type<boost::mpl::_>>::type; |
| 286 | // Next, we create a variant over the wrapped types: |
| 287 | using CommandTypesVariant = boost::make_variant_over<WrappedCommandTypes>::type; |
| 288 | // Finally, we wrap the variant with boost::optional: |
| 289 | boost::optional<CommandTypesVariant> m_cmd; |
| 290 | }; |
| 291 | |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 292 | // TODO: The usage of MPL won't be necessary after std::variant support is added to Spirit |
| 293 | // https://github.com/boostorg/spirit/issues/270 |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 294 | using command_ = boost::make_variant_over<CommandTypes>::type; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 295 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 296 | BOOST_FUSION_ADAPT_STRUCT(ls_, m_options, m_path) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 297 | BOOST_FUSION_ADAPT_STRUCT(cd_, m_path) |
| 298 | BOOST_FUSION_ADAPT_STRUCT(create_, m_path) |
| 299 | BOOST_FUSION_ADAPT_STRUCT(delete_, m_path) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 300 | BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data) |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 301 | BOOST_FUSION_ADAPT_STRUCT(enum_, m_value) |
Václav Kubernát | dab73ca | 2020-10-26 23:44:43 +0100 | [diff] [blame] | 302 | BOOST_FUSION_ADAPT_STRUCT(bits_, m_bits) |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 303 | BOOST_FUSION_ADAPT_STRUCT(binary_, m_value) |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 304 | BOOST_FUSION_ADAPT_STRUCT(identityRef_, m_prefix, m_value) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 305 | BOOST_FUSION_ADAPT_STRUCT(commit_) |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 306 | BOOST_FUSION_ADAPT_STRUCT(describe_, m_path) |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 307 | BOOST_FUSION_ADAPT_STRUCT(help_, m_cmd) |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 308 | BOOST_FUSION_ADAPT_STRUCT(discard_) |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 309 | BOOST_FUSION_ADAPT_STRUCT(get_, m_path) |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 310 | BOOST_FUSION_ADAPT_STRUCT(copy_, m_source, m_destination) |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 311 | BOOST_FUSION_ADAPT_STRUCT(move_, m_source, m_destination) |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 312 | BOOST_FUSION_ADAPT_STRUCT(dump_, m_format) |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 313 | BOOST_FUSION_ADAPT_STRUCT(prepare_, m_path) |