blob: 0c02ecf6c8e76de455fc4f06f9155ce8991ada43 [file] [log] [blame]
Jan Kundrátdc2b0722018-03-02 14:13:37 +01001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
Jan Kundráta2740442018-03-22 16:56:43 +01003 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
Jan Kundrátdc2b0722018-03-02 14:13:37 +01004 *
Václav Kubernát624a8872018-03-02 17:28:47 +01005 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
Jan Kundrátdc2b0722018-03-02 14:13:37 +01006 *
7*/
8
Václav Kubernát624a8872018-03-02 17:28:47 +01009#include <boost/spirit/home/x3.hpp>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010010#include <docopt.h>
Václav Kubernát624a8872018-03-02 17:28:47 +010011#include <iostream>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010012#include <string>
13#include "NETCONF_CLI_VERSION.h"
Václav Kubernát96344a12018-05-28 16:33:39 +020014#include "interpreter.hpp"
Václav Kubernát48fc3832018-05-28 14:21:22 +020015#include "parser.hpp"
Václav Kubernátbddbb172018-06-13 16:27:39 +020016#include "static_schema.hpp"
Jan Kundrátdc2b0722018-03-02 14:13:37 +010017
Václav Kubernát624a8872018-03-02 17:28:47 +010018
Jan Kundrátdc2b0722018-03-02 14:13:37 +010019static const char usage[] =
Václav Kubernát624a8872018-03-02 17:28:47 +010020 R"(CLI interface to remote NETCONF hosts
Jan Kundrátdc2b0722018-03-02 14:13:37 +010021
22Usage:
Václav Kubernát624a8872018-03-02 17:28:47 +010023 netconf-cli
Jan Kundrátdc2b0722018-03-02 14:13:37 +010024 netconf-cli (-h | --help)
25 netconf-cli --version
26)";
27
Václav Kubernát624a8872018-03-02 17:28:47 +010028namespace x3 = boost::spirit::x3;
29namespace ascii = boost::spirit::x3::ascii;
30
31using Cmd = std::vector<std::string>;
Václav Kubernát624a8872018-03-02 17:28:47 +010032using ascii::space;
Václav Kubernát48fc3832018-05-28 14:21:22 +020033using x3::_attr;
34using x3::alpha;
35using x3::char_;
36using x3::lexeme;
37using x3::lit;
Václav Kubernát624a8872018-03-02 17:28:47 +010038
Václav Kubernát624a8872018-03-02 17:28:47 +010039
Jan Kundrátdc2b0722018-03-02 14:13:37 +010040int main(int argc, char* argv[])
41{
42 auto args = docopt::docopt(usage,
43 {argv + 1, argv + argc},
44 true,
45 "netconf-cli " NETCONF_CLI_VERSION,
46 true);
Václav Kubernát624a8872018-03-02 17:28:47 +010047 std::cout << "Welcome to netconf-cli" << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020048
Václav Kubernátbddbb172018-06-13 16:27:39 +020049 auto schema = std::make_shared<StaticSchema>();
Václav Kubernáta10d8b62018-06-13 17:42:46 +020050 schema->addContainer("", "a", yang::ContainerTraits::Presence);
51 schema->addContainer("", "b");
52 schema->addLeaf("", "leafString", yang::LeafDataTypes::String);
53 schema->addLeaf("", "leafDecimal", yang::LeafDataTypes::Decimal);
54 schema->addLeaf("", "leafBool", yang::LeafDataTypes::Bool);
55 schema->addLeaf("", "leafInt", yang::LeafDataTypes::Int);
56 schema->addLeaf("", "leafUint", yang::LeafDataTypes::Uint);
57 schema->addLeafEnum("", "leafEnum", {"lol", "data", "coze"});
58 schema->addContainer("a", "a2");
59 schema->addLeaf("a", "leafa", yang::LeafDataTypes::String);
60 schema->addContainer("b", "b2", yang::ContainerTraits::Presence);
61 schema->addContainer("a/a2", "a3", yang::ContainerTraits::Presence);
62 schema->addContainer("b/b2", "b3");
63 schema->addList("", "list", {"number"});
64 schema->addContainer("list", "contInList", yang::ContainerTraits::Presence);
65 schema->addList("", "twoKeyList", {"number", "name"});
Václav Kubernát48fc3832018-05-28 14:21:22 +020066 Parser parser(schema);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020067
68 while (true) {
Václav Kubernátf2e463f2018-05-28 15:51:08 +020069 std::cout << parser.currentNode() << "> ";
Václav Kubernátff2c9f62018-05-16 20:26:31 +020070 std::string input;
71 std::getline(std::cin, input);
72 if (std::cin.eof())
73 break;
74
75 try {
Václav Kubernátb61336d2018-05-28 17:35:03 +020076 command_ cmd = parser.parseCommand(input, std::cout);
Václav Kubernáta10d8b62018-06-13 17:42:46 +020077 boost::apply_visitor(Interpreter(parser, *schema), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +020078 } catch (InvalidCommandException& ex) {
79 std::cerr << ex.what() << std::endl;
80 }
81 }
82
Jan Kundrátdc2b0722018-03-02 14:13:37 +010083 return 0;
84}