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 | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 14 | #include "yang_move.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 | bool operator==(const set_& b) const; |
| 129 | }; |
| 130 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 131 | struct get_ : x3::position_tagged { |
Václav Kubernát | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 132 | static constexpr auto name = "get"; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 133 | 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át | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 144 | bool operator==(const get_& b) const; |
Václav Kubernát | 8f6bff3 | 2020-06-10 14:51:10 +0200 | [diff] [blame^] | 145 | boost::optional<boost::variant<dataPath_, module_>> m_path; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 146 | }; |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 147 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 148 | struct 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át | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 167 | struct 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át | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 182 | enum class MoveMode { |
| 183 | Begin, |
| 184 | End, |
| 185 | Before, |
| 186 | After |
| 187 | }; |
| 188 | |
| 189 | struct 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át | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 209 | struct help_; |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 210 | using CommandTypes = boost::mpl::vector<cd_, commit_, copy_, create_, delete_, describe_, discard_, get_, help_, ls_, move_, set_>; |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 211 | struct 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át | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 238 | // 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át | 5727242 | 2019-02-08 12:48:24 +0100 | [diff] [blame] | 240 | using command_ = boost::make_variant_over<CommandTypes>::type; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 241 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 242 | BOOST_FUSION_ADAPT_STRUCT(ls_, m_options, m_path) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 243 | BOOST_FUSION_ADAPT_STRUCT(cd_, m_path) |
| 244 | BOOST_FUSION_ADAPT_STRUCT(create_, m_path) |
| 245 | BOOST_FUSION_ADAPT_STRUCT(delete_, m_path) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 246 | BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data) |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 247 | BOOST_FUSION_ADAPT_STRUCT(enum_, m_value) |
| 248 | BOOST_FUSION_ADAPT_STRUCT(binary_, m_value) |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 249 | BOOST_FUSION_ADAPT_STRUCT(identityRef_, m_prefix, m_value) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 250 | BOOST_FUSION_ADAPT_STRUCT(commit_) |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 251 | BOOST_FUSION_ADAPT_STRUCT(describe_, m_path) |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 252 | BOOST_FUSION_ADAPT_STRUCT(help_, m_cmd) |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 253 | BOOST_FUSION_ADAPT_STRUCT(discard_) |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 254 | BOOST_FUSION_ADAPT_STRUCT(get_, m_path) |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 255 | BOOST_FUSION_ADAPT_STRUCT(copy_, m_source, m_destination) |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 256 | BOOST_FUSION_ADAPT_STRUCT(move_, m_source, m_destination) |