blob: aff5bcc007e60f3d21bf3f27da55e4d8609cbcc6 [file] [log] [blame]
Václav Kubernát96344a12018-05-28 16:33:39 +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
Václav Kubernátb61336d2018-05-28 17:35:03 +02009#include <iostream>
Václav Kubernát6415b822018-08-22 17:40:01 +020010#include "datastore_access.hpp"
Václav Kubernát96344a12018-05-28 16:33:39 +020011#include "interpreter.hpp"
12
Václav Kubernátebca2552018-06-08 19:06:02 +020013struct leafDataToString : boost::static_visitor<std::string> {
14 std::string operator()(const enum_& data) const
15 {
16 return data.m_value;
17 }
Václav Kubernátab538992019-03-06 15:30:50 +010018
19 std::string operator()(const binary_& data) const
20 {
21 return data.m_value;
22 }
23
Václav Kubernátebca2552018-06-08 19:06:02 +020024 template <typename T>
25 std::string operator()(const T& data) const
26 {
27 std::stringstream stream;
28 stream << data;
29 return stream.str();
30 }
31};
32
Václav Kubernát812ee282018-08-30 17:10:03 +020033void Interpreter::operator()(const commit_&) const
34{
35 m_datastore.commitChanges();
36}
37
Václav Kubernát6d791432018-10-25 16:00:35 +020038void Interpreter::operator()(const discard_&) const
39{
40 m_datastore.discardChanges();
41}
42
Václav Kubernát07204242018-06-04 18:12:09 +020043void Interpreter::operator()(const set_& set) const
44{
Václav Kubernát6415b822018-08-22 17:40:01 +020045 m_datastore.setLeaf(absolutePathFromCommand(set), set.m_data);
Václav Kubernát07204242018-06-04 18:12:09 +020046}
47
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020048void Interpreter::operator()(const get_& get) const
49{
50 auto items = m_datastore.getItems(absolutePathFromCommand(get));
51 for (auto it : items) {
52 std::cout << it.first << " = " << boost::apply_visitor(leafDataToString(), it.second) << std::endl;
53 }
54}
55
Václav Kubernát96344a12018-05-28 16:33:39 +020056void Interpreter::operator()(const cd_& cd) const
57{
58 m_parser.changeNode(cd.m_path);
59}
60
Václav Kubernátb61336d2018-05-28 17:35:03 +020061void Interpreter::operator()(const create_& create) const
62{
Václav Kubernát6415b822018-08-22 17:40:01 +020063 m_datastore.createPresenceContainer(absolutePathFromCommand(create));
Václav Kubernátb61336d2018-05-28 17:35:03 +020064}
65
66void Interpreter::operator()(const delete_& delet) const
67{
Václav Kubernát6415b822018-08-22 17:40:01 +020068 m_datastore.deletePresenceContainer(absolutePathFromCommand(delet));
Václav Kubernátb61336d2018-05-28 17:35:03 +020069}
70
Václav Kubernát11afac72018-07-18 14:59:53 +020071void Interpreter::operator()(const ls_& ls) const
72{
73 std::cout << "Possible nodes:" << std::endl;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020074 auto recursion{Recursion::NonRecursive};
75 for (auto it : ls.m_options) {
76 if (it == LsOption::Recursive)
77 recursion = Recursion::Recursive;
78 }
Václav Kubernát11afac72018-07-18 14:59:53 +020079
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020080 for (const auto& it : m_parser.availableNodes(ls.m_path, recursion))
Václav Kubernát11afac72018-07-18 14:59:53 +020081 std::cout << it << std::endl;
82}
83
Václav Kubernát054cc992019-02-21 14:23:52 +010084struct commandLongHelpVisitor : boost::static_visitor<const char*> {
85 template <typename T>
86 auto constexpr operator()(boost::type<T>) const
87 {
88 return T::longHelp;
89 }
90};
91
92struct commandShortHelpVisitor : boost::static_visitor<const char*> {
93 template <typename T>
94 auto constexpr operator()(boost::type<T>) const
95 {
96 return T::shortHelp;
97 }
98};
99
100void Interpreter::operator()(const help_& help) const
101{
102 if (help.m_cmd)
103 std::cout << boost::apply_visitor(commandLongHelpVisitor(), help.m_cmd.get()) << std::endl;
104 else
105 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([](auto cmd) {
106 std::cout << commandShortHelpVisitor()(cmd) << std::endl;
107 });
108}
109
Václav Kubernát6415b822018-08-22 17:40:01 +0200110template <typename T>
111std::string Interpreter::absolutePathFromCommand(const T& command) const
112{
Václav Kubernát37171a12018-08-31 17:01:48 +0200113 if (command.m_path.m_scope == Scope::Absolute)
114 return "/" + pathToDataString(command.m_path);
115 else
116 return joinPaths(m_parser.currentNode(), pathToDataString(command.m_path));
Václav Kubernát6415b822018-08-22 17:40:01 +0200117}
118
Václav Kubernát5c75b252018-10-10 18:33:47 +0200119struct pathToStringVisitor : boost::static_visitor<std::string> {
120 std::string operator()(const schemaPath_& path) const
121 {
122 return pathToSchemaString(path);
123 }
124 std::string operator()(const dataPath_& path) const
125 {
126 return pathToDataString(path);
127 }
128};
129
130struct getPathScopeVisitor : boost::static_visitor<Scope> {
131 template <typename T>
132 Scope operator()(const T& path) const
133 {
134 return path.m_scope;
135 }
136};
137
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200138std::string Interpreter::absolutePathFromCommand(const get_& get) const
139{
140 if (!get.m_path) {
141 return m_parser.currentNode();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200142 }
143
144 const auto path = *get.m_path;
145 std::string pathString = boost::apply_visitor(pathToStringVisitor(), path);
146 auto pathScope{boost::apply_visitor(getPathScopeVisitor(), path)};
147
148 if (pathScope == Scope::Absolute) {
149 return "/" + pathString;
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200150 } else {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200151 return joinPaths(m_parser.currentNode(), pathString);
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200152 }
153}
154
Václav Kubernát6415b822018-08-22 17:40:01 +0200155Interpreter::Interpreter(Parser& parser, DatastoreAccess& datastore)
Václav Kubernát812ee282018-08-30 17:10:03 +0200156 : m_parser(parser)
157 , m_datastore(datastore)
Václav Kubernát96344a12018-05-28 16:33:39 +0200158{
159}