| /* |
| * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| * |
| * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| * |
| */ |
| #pragma once |
| |
| #include <boost/mpl/vector.hpp> |
| #include <boost/spirit/home/x3/support/ast/position_tagged.hpp> |
| #include "ast_path.hpp" |
| #include "ast_values.hpp" |
| #include "datastore_access.hpp" |
| #include "yang_operations.hpp" |
| |
| namespace x3 = boost::spirit::x3; |
| |
| using keyValue_ = std::pair<std::string, leaf_data_>; |
| |
| enum class LsOption { |
| Recursive |
| }; |
| |
| struct discard_ : x3::position_tagged { |
| static constexpr auto name = "discard"; |
| static constexpr auto shortHelp = "discard - Discard current changes."; |
| static constexpr auto longHelp = R"( |
| discard |
| |
| Discards current changes. Accepts no arguments. |
| |
| Usage: |
| /> discard)"; |
| bool operator==(const discard_& b) const; |
| }; |
| |
| struct ls_ : x3::position_tagged { |
| static constexpr auto name = "ls"; |
| static constexpr auto shortHelp = "ls - List available nodes."; |
| static constexpr auto longHelp = R"( |
| ls [--recursive] [path] |
| |
| Lists available child nodes in the current context node. Optionally accepts |
| a path argument. Accepts both schema paths and data paths. Path starting |
| with a forward slash means an absolute path. |
| |
| Options: |
| --recursive makes `ls` work recursively |
| |
| Usage: |
| /> ls |
| /> ls --recursive module:node |
| /> ls /module:node)"; |
| bool operator==(const ls_& b) const; |
| std::vector<LsOption> m_options; |
| boost::optional<boost::variant<dataPath_, schemaPath_, module_>> m_path; |
| }; |
| |
| struct cd_ : x3::position_tagged { |
| static constexpr auto name = "cd"; |
| static constexpr auto shortHelp = "cd - Enter a different node."; |
| static constexpr auto longHelp = R"( |
| cd <path> |
| |
| Changes context to a node specified by <path>. Only accepts data paths |
| (paths with all list keys supplied). |
| |
| Usage: |
| /> cd /module:node/node2 |
| /> cd ..)"; |
| bool operator==(const cd_& b) const; |
| dataPath_ m_path; |
| }; |
| |
| struct create_ : x3::position_tagged { |
| static constexpr auto name = "create"; |
| static constexpr auto shortHelp = "create - Create a node."; |
| static constexpr auto longHelp = R"( |
| create <path> |
| |
| Creates a node specified by <path>. |
| Supported node types are list items, leaflist instance and presence |
| containers. |
| |
| Usage: |
| /> create /module:pContainer |
| /> create /module:leafList['value'] |
| /> create /module:list[key=value][anotherKey=value])"; |
| bool operator==(const create_& b) const; |
| dataPath_ m_path; |
| }; |
| |
| struct delete_ : x3::position_tagged { |
| static constexpr auto name = "delete"; |
| static constexpr auto shortHelp = "delete - Delete a node."; |
| static constexpr auto longHelp = R"( |
| delete <path> |
| |
| Deletes a node specified by <path>. |
| Supported node types are leafs, list items, leaflist instance and presence |
| containers. |
| |
| Usage: |
| /> delete /module:pContainer |
| /> delete /module:leafList['value'] |
| /> delete /module:list[key=value][anotherKey=value])"; |
| bool operator==(const delete_& b) const; |
| dataPath_ m_path; |
| }; |
| |
| struct set_ : x3::position_tagged { |
| static constexpr auto name = "set"; |
| static constexpr auto shortHelp = "set - Set value of a leaf."; |
| static constexpr auto longHelp = R"( |
| set <path_to_leaf> <value> |
| |
| Sets the leaf specified by <path_to_leaf> to <value>. |
| Values of type string must be enclosed in quotation marks (" or '). |
| |
| Usage: |
| /> set /module:someNumber 123 |
| /> set /module:someString 'abc' |
| /> set /module:someString "abc")"; |
| bool operator==(const set_& b) const; |
| dataPath_ m_path; |
| leaf_data_ m_data; |
| }; |
| |
| struct commit_ : x3::position_tagged { |
| static constexpr auto name = "commit"; |
| static constexpr auto shortHelp = "commit - Commit current changes."; |
| static constexpr auto longHelp = R"( |
| commit |
| |
| Commits the current changes. Accepts no arguments. |
| |
| Usage: |
| /> commit)"; |
| }; |
| |
| struct get_ : x3::position_tagged { |
| static constexpr auto name = "get"; |
| static constexpr auto shortHelp = "get - Retrieve configuration from the server."; |
| static constexpr auto longHelp = R"( |
| get [path] |
| |
| Prints out content of the current datastore at a specified [path], or below |
| the current context tree. |
| |
| Usage: |
| /> get |
| /> get /module:path)"; |
| bool operator==(const get_& b) const; |
| boost::optional<DatastoreTarget> m_dsTarget; |
| boost::optional<boost::variant<dataPath_, module_>> m_path; |
| }; |
| |
| struct describe_ : x3::position_tagged { |
| static constexpr auto name = "describe"; |
| static constexpr auto shortHelp = "describe - Print information about YANG tree path."; |
| static constexpr auto longHelp = R"( |
| describe <path> |
| |
| Shows documentation of YANG tree paths. In the YANG model, each item may |
| have an optional `description` which often explains the function of that |
| node to the end user. This command takes the description from the YANG model |
| and shows it to the user along with additional data, such as the type of the |
| node, units of leaf values, etc. |
| |
| Usage: |
| /> describe /module:node)"; |
| bool operator==(const describe_& b) const; |
| |
| boost::variant<schemaPath_, dataPath_> m_path; |
| }; |
| |
| struct copy_ : x3::position_tagged { |
| static constexpr auto name = "copy"; |
| static constexpr auto shortHelp = "copy - Copy configuration."; |
| static constexpr auto longHelp = R"( |
| copy <source> <destination> |
| |
| Copies configuration from <source> to <destination>. |
| |
| Usage: |
| /> copy running startup |
| /> copy startup running)"; |
| bool operator==(const copy_& b) const; |
| |
| Datastore m_source; |
| Datastore m_destination; |
| }; |
| |
| enum class MoveMode { |
| Begin, |
| End, |
| Before, |
| After |
| }; |
| |
| struct move_ : x3::position_tagged { |
| static constexpr auto name = "move"; |
| static constexpr auto shortHelp = "move - Move (leaf)list instances."; |
| static constexpr auto longHelp = R"( |
| move <path> begin |
| move <path> end |
| move <path> before <key> |
| move <path> after <key> |
| |
| Moves the instance specified by <path> to the position specified by the |
| specified by the second and third argument. |
| |
| Usage: |
| /> move mod:leaflist['abc'] begin |
| /> move mod:leaflist['def'] after 'abc' |
| /> move mod:interfaces[name='eth0'] after [name='eth1'])"; |
| bool operator==(const move_& b) const; |
| |
| dataPath_ m_source; |
| |
| std::variant<yang::move::Absolute, yang::move::Relative> m_destination; |
| }; |
| |
| struct dump_ : x3::position_tagged { |
| static constexpr auto name = "dump"; |
| static constexpr auto shortHelp = "dump - Print out datastore content as JSON or XML."; |
| static constexpr auto longHelp = R"( |
| dump xml|json |
| |
| Prints out the content of the datastore. Supports JSON and XML. |
| |
| Usage: |
| /> dump xml |
| /> dump json)"; |
| bool operator==(const dump_& other) const; |
| |
| DataFormat m_format; |
| }; |
| |
| struct prepare_ : x3::position_tagged { |
| static constexpr auto name = "prepare"; |
| static constexpr auto shortHelp = "prepare - Initiate RPC/action."; |
| static constexpr auto longHelp = R"( |
| prepare <path-to-rpc-or-action> |
| |
| This command enters a mode for entering input parameters for the RPC/action. |
| In this mode, you can use commands like `set` on nodes inside the RPC/action |
| to set the input. After setting the input, use `exec` to execute the |
| RPC/action. |
| |
| Usage: |
| /> prepare /mod:launch-nukes |
| /> set kilotons 1000 |
| /> exec)"; |
| bool operator==(const prepare_& other) const; |
| |
| dataPath_ m_path; |
| }; |
| |
| struct exec_ : x3::position_tagged { |
| static constexpr auto name = "exec"; |
| static constexpr auto shortHelp = "exec - Execute RPC/action."; |
| static constexpr auto longHelp = R"( |
| exec [path] |
| |
| This command executes the RPC/action you have previously initiated via the |
| `prepare` command. |
| |
| If the RPC/action has no input parameters, it can be directly execute via |
| `exec` without usgin `prepare`. |
| |
| Usage: |
| /> exec |
| /> exec /mod:myRpc)"; |
| bool operator==(const exec_& other) const; |
| |
| boost::optional<dataPath_> m_path; |
| }; |
| |
| struct cancel_ : x3::position_tagged { |
| static constexpr auto name = "cancel"; |
| static constexpr auto shortHelp = "cancel - Cancel an ongoing RPC/action input."; |
| static constexpr auto longHelp = R"( |
| cancel |
| |
| This command cancels a previously entered RPC/action context. |
| |
| Usage: |
| /> cancel)"; |
| bool operator==(const cancel_& other) const; |
| }; |
| |
| struct switch_ : x3::position_tagged { |
| static constexpr auto name = "switch"; |
| static constexpr auto shortHelp = "switch - Switch datastore target."; |
| static constexpr auto longHelp = R"( |
| switch <target> |
| |
| This command switches the datastore target. Available targets are: |
| |
| operational: |
| - reads from operational, writes to running |
| startup: |
| - reads from startup, writes to startup |
| running: |
| - reads from running, writes to running |
| |
| |
| Usage: |
| /> switch running |
| /> switch startup |
| /> switch operational)"; |
| bool operator==(const switch_& other) const; |
| DatastoreTarget m_target; |
| }; |
| |
| struct help_; |
| using CommandTypes = boost::mpl::vector<cancel_, cd_, commit_, copy_, create_, delete_, describe_, discard_, dump_, exec_, get_, help_, ls_, move_, prepare_, set_, switch_>; |
| struct help_ : x3::position_tagged { |
| static constexpr auto name = "help"; |
| static constexpr auto shortHelp = "help - Print help for commands."; |
| static constexpr auto longHelp = R"( |
| help [command_name] |
| |
| Print help for command_name. If used without an argument, |
| print short help for all commands. |
| |
| Usage: |
| /> help |
| /> help cd |
| /> help help)"; |
| bool operator==(const help_& b) const; |
| |
| // The help command has got one optional argument – a command name (type). |
| // All commands are saved in CommandTypes, so we could just use that, but |
| // that way, Spirit would be default constructing the command structs, |
| // which is undesirable, so firstly we use mpl::transform to wrap |
| // CommandTypes with boost::type: |
| using WrappedCommandTypes = boost::mpl::transform<CommandTypes, boost::type<boost::mpl::_>>::type; |
| // Next, we create a variant over the wrapped types: |
| using CommandTypesVariant = boost::make_variant_over<WrappedCommandTypes>::type; |
| // Finally, we wrap the variant with boost::optional: |
| boost::optional<CommandTypesVariant> m_cmd; |
| }; |
| |
| // TODO: The usage of MPL won't be necessary after std::variant support is added to Spirit |
| // https://github.com/boostorg/spirit/issues/270 |
| using command_ = boost::make_variant_over<CommandTypes>::type; |
| |
| BOOST_FUSION_ADAPT_STRUCT(ls_, m_options, m_path) |
| BOOST_FUSION_ADAPT_STRUCT(cd_, m_path) |
| BOOST_FUSION_ADAPT_STRUCT(create_, m_path) |
| BOOST_FUSION_ADAPT_STRUCT(delete_, m_path) |
| BOOST_FUSION_ADAPT_STRUCT(set_, m_path, m_data) |
| BOOST_FUSION_ADAPT_STRUCT(enum_, m_value) |
| BOOST_FUSION_ADAPT_STRUCT(bits_, m_bits) |
| BOOST_FUSION_ADAPT_STRUCT(binary_, m_value) |
| BOOST_FUSION_ADAPT_STRUCT(identityRef_, m_prefix, m_value) |
| BOOST_FUSION_ADAPT_STRUCT(commit_) |
| BOOST_FUSION_ADAPT_STRUCT(describe_, m_path) |
| BOOST_FUSION_ADAPT_STRUCT(help_, m_cmd) |
| BOOST_FUSION_ADAPT_STRUCT(discard_) |
| BOOST_FUSION_ADAPT_STRUCT(get_, m_dsTarget, m_path) |
| BOOST_FUSION_ADAPT_STRUCT(copy_, m_source, m_destination) |
| BOOST_FUSION_ADAPT_STRUCT(move_, m_source, m_destination) |
| BOOST_FUSION_ADAPT_STRUCT(dump_, m_format) |
| BOOST_FUSION_ADAPT_STRUCT(prepare_, m_path) |
| BOOST_FUSION_ADAPT_STRUCT(exec_, m_path) |
| BOOST_FUSION_ADAPT_STRUCT(switch_, m_target) |