blob: 26e2be4dbe8faad0b1b61b72db499a449a11a019 [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át5b8a8f32020-05-20 00:57:22 +02009#include <boost/algorithm/string/predicate.hpp>
Václav Kubernát509ce652019-05-29 19:46:44 +020010#include <boost/mpl/for_each.hpp>
Václav Kubernátb61336d2018-05-28 17:35:03 +020011#include <iostream>
Václav Kubernát9cfcd872020-02-18 12:34:02 +010012#include <sstream>
Václav Kubernát6415b822018-08-22 17:40:01 +020013#include "datastore_access.hpp"
Václav Kubernát96344a12018-05-28 16:33:39 +020014#include "interpreter.hpp"
Václav Kubernát509ce652019-05-29 19:46:44 +020015#include "utils.hpp"
Václav Kubernát96344a12018-05-28 16:33:39 +020016
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020017struct pathToStringVisitor : boost::static_visitor<std::string> {
18 std::string operator()(const module_& path) const
19 {
20 using namespace std::string_literals;
21 return "/"s + boost::get<module_>(path).m_name + ":*";
22 }
23 std::string operator()(const schemaPath_& path) const
24 {
25 return pathToSchemaString(path, Prefixes::WhenNeeded);
26 }
27 std::string operator()(const dataPath_& path) const
28 {
29 return pathToDataString(path, Prefixes::WhenNeeded);
30 }
31};
32
Václav Kubernáte7248b22020-06-26 15:38:59 +020033namespace {
34void printTree(const DatastoreAccess::Tree tree)
35{
36 for (auto it = tree.begin(); it != tree.end(); it++) {
37 auto [path, value] = *it;
38 if (value.type() == typeid(special_) && boost::get<special_>(value).m_value == SpecialValue::LeafList) {
39 auto leafListPrefix = path;
40 std::cout << path << " = " << leafDataToString(value) << std::endl;
41
42 while (it + 1 != tree.end() && boost::starts_with((it + 1)->first, leafListPrefix)) {
43 ++it;
44 std::cout << stripLeafListValueFromPath(it->first) << " = " << leafDataToString(it->second) << std::endl;
45 }
46 } else {
47 std::cout << path << " = " << leafDataToString(value) << std::endl;
48 }
49 }
50}
51}
52
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020053template <typename PathType>
54std::string pathToString(const PathType& path)
55{
56 return boost::apply_visitor(pathToStringVisitor(), path);
57}
58
Václav Kubernát812ee282018-08-30 17:10:03 +020059void Interpreter::operator()(const commit_&) const
60{
61 m_datastore.commitChanges();
62}
63
Václav Kubernát6d791432018-10-25 16:00:35 +020064void Interpreter::operator()(const discard_&) const
65{
66 m_datastore.discardChanges();
67}
68
Václav Kubernát07204242018-06-04 18:12:09 +020069void Interpreter::operator()(const set_& set) const
70{
Václav Kubernáteeb38842019-03-20 19:46:05 +010071 auto data = set.m_data;
72
73 // If the user didn't supply a module prefix for identityref, we need to add it ourselves
74 if (data.type() == typeid(identityRef_)) {
75 auto identityRef = boost::get<identityRef_>(data);
76 if (!identityRef.m_prefix) {
77 identityRef.m_prefix = set.m_path.m_nodes.front().m_prefix.value();
78 data = identityRef;
79 }
80 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020081 m_datastore.setLeaf(pathToString(toCanonicalPath(set.m_path)), data);
Václav Kubernát07204242018-06-04 18:12:09 +020082}
83
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020084void Interpreter::operator()(const get_& get) const
85{
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020086 auto items = m_datastore.getItems(pathToString(toCanonicalPath(get.m_path)));
Václav Kubernáte7248b22020-06-26 15:38:59 +020087 printTree(items);
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020088}
89
Václav Kubernát96344a12018-05-28 16:33:39 +020090void Interpreter::operator()(const cd_& cd) const
91{
92 m_parser.changeNode(cd.m_path);
93}
94
Václav Kubernátb61336d2018-05-28 17:35:03 +020095void Interpreter::operator()(const create_& create) const
96{
Jan Kundrátcbf288b2020-06-18 20:44:39 +020097 m_datastore.createItem(pathToString(toCanonicalPath(create.m_path)));
Václav Kubernátb61336d2018-05-28 17:35:03 +020098}
99
100void Interpreter::operator()(const delete_& delet) const
101{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200102 m_datastore.deleteItem(pathToString(toCanonicalPath(delet.m_path)));
Václav Kubernátb61336d2018-05-28 17:35:03 +0200103}
104
Václav Kubernát11afac72018-07-18 14:59:53 +0200105void Interpreter::operator()(const ls_& ls) const
106{
107 std::cout << "Possible nodes:" << std::endl;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200108 auto recursion{Recursion::NonRecursive};
109 for (auto it : ls.m_options) {
Václav Kubernát3a433232020-07-08 17:52:50 +0200110 if (it == LsOption::Recursive) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200111 recursion = Recursion::Recursive;
Václav Kubernát3a433232020-07-08 17:52:50 +0200112 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200113 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200114
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200115 auto toPrint = m_datastore.schema()->availableNodes(toCanonicalPath(ls.m_path), recursion);
Václav Kubernát82086872020-04-29 01:09:50 +0200116
Václav Kubernát95b08872020-04-28 01:04:17 +0200117 for (const auto& it : toPrint) {
118 std::cout << (it.first ? *it.first + ":" : "" ) + it.second << std::endl;
119 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200120}
121
Václav Kubernát7160a132020-04-03 02:11:01 +0200122void Interpreter::operator()(const copy_& copy) const
123{
124 m_datastore.copyConfig(copy.m_source, copy.m_destination);
125}
126
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100127std::string Interpreter::buildTypeInfo(const std::string& path) const
128{
129 std::ostringstream ss;
130 switch (m_datastore.schema()->nodeType(path)) {
131 case yang::NodeTypes::Container:
132 ss << "container";
133 break;
134 case yang::NodeTypes::PresenceContainer:
135 ss << "presence container";
136 break;
137 case yang::NodeTypes::Leaf:
138 {
139 auto leafType = m_datastore.schema()->leafType(path);
140 auto typedefName = m_datastore.schema()->leafTypeName(path);
141 std::string baseTypeStr;
Václav Kubernát13b23d72020-04-16 21:49:51 +0200142 if (std::holds_alternative<yang::LeafRef>(leafType.m_type)) {
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100143 ss << "-> ";
144 ss << m_datastore.schema()->leafrefPath(path) << " ";
Václav Kubernát13b23d72020-04-16 21:49:51 +0200145 baseTypeStr = leafDataTypeToString(std::get<yang::LeafRef>(leafType.m_type).m_targetType->m_type);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100146 } else {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200147 baseTypeStr = leafDataTypeToString(leafType.m_type);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100148 }
149
150 if (typedefName) {
151 ss << *typedefName << " (" << baseTypeStr << ")";
152 } else {
153 ss << baseTypeStr;
154 }
155
Václav Kubernát13b23d72020-04-16 21:49:51 +0200156 if (leafType.m_units) {
157 ss << " [" + *leafType.m_units + "]";
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100158 }
159
160 if (m_datastore.schema()->leafIsKey(path)) {
161 ss << " (key)";
162 }
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200163
164 if (auto defaultValue = m_datastore.schema()->defaultValue(path)) {
165 ss << " default: " << leafDataToString(*defaultValue);
166 }
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100167 break;
168 }
169 case yang::NodeTypes::List:
170 ss << "list";
171 break;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200172 case yang::NodeTypes::Rpc:
173 ss << "RPC";
174 break;
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200175 case yang::NodeTypes::Action:
176 case yang::NodeTypes::AnyXml:
177 case yang::NodeTypes::LeafList:
178 case yang::NodeTypes::Notification:
Václav Kubernáte7248b22020-06-26 15:38:59 +0200179 throw std::logic_error("describe can't handle the type of " + path);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100180 }
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200181
182 if (!m_datastore.schema()->isConfig(path)) {
183 ss << " (ro)";
184 }
185
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100186 return ss.str();
187}
188
189void Interpreter::operator()(const describe_& describe) const
190{
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200191 auto path = pathToString(toCanonicalPath(describe.m_path));
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200192 auto status = m_datastore.schema()->status(path);
193 auto statusStr = status == yang::Status::Deprecated ? " (deprecated)" :
194 status == yang::Status::Obsolete ? " (obsolete)" :
195 "";
196
197 std::cout << path << ": " << buildTypeInfo(path) << statusStr << std::endl;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100198 if (auto description = m_datastore.schema()->description(path)) {
199 std::cout << std::endl << *description << std::endl;
200 }
201}
202
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200203void Interpreter::operator()(const move_& move) const
204{
205 m_datastore.moveItem(pathToDataString(move.m_source, Prefixes::WhenNeeded), move.m_destination);
206}
207
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200208void Interpreter::operator()(const dump_& dump) const
209{
210 std::cout << m_datastore.dump(dump.m_format) << "\n";
211}
212
Václav Kubernáte7248b22020-06-26 15:38:59 +0200213void Interpreter::operator()(const rpc_& rpc) const
214{
215 m_datastore.initiateRpc(pathToString(toCanonicalPath(rpc.m_path)));
216 m_parser.changeNode(rpc.m_path);
217}
218
219void Interpreter::operator()(const exec_&) const
220{
221 m_parser.changeNode({Scope::Absolute, {}});
222 auto output = m_datastore.executeRpc();
223 std::cout << "RPC output:\n";
224 printTree(output);
225}
226
227void Interpreter::operator()(const cancel_&) const
228{
229 m_parser.changeNode({Scope::Absolute, {}});
230 m_datastore.cancelRpc();
231}
232
Václav Kubernát054cc992019-02-21 14:23:52 +0100233struct commandLongHelpVisitor : boost::static_visitor<const char*> {
234 template <typename T>
235 auto constexpr operator()(boost::type<T>) const
236 {
237 return T::longHelp;
238 }
239};
240
241struct commandShortHelpVisitor : boost::static_visitor<const char*> {
242 template <typename T>
243 auto constexpr operator()(boost::type<T>) const
244 {
245 return T::shortHelp;
246 }
247};
248
249void Interpreter::operator()(const help_& help) const
250{
Václav Kubernát3a433232020-07-08 17:52:50 +0200251 if (help.m_cmd) {
Václav Kubernát054cc992019-02-21 14:23:52 +0100252 std::cout << boost::apply_visitor(commandLongHelpVisitor(), help.m_cmd.get()) << std::endl;
Václav Kubernát3a433232020-07-08 17:52:50 +0200253 } else {
Václav Kubernát054cc992019-02-21 14:23:52 +0100254 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([](auto cmd) {
255 std::cout << commandShortHelpVisitor()(cmd) << std::endl;
256 });
Václav Kubernát3a433232020-07-08 17:52:50 +0200257 }
Václav Kubernát054cc992019-02-21 14:23:52 +0100258}
259
Václav Kubernát7e167692020-06-12 09:53:01 +0200260template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200261boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const boost::optional<PathType>& optPath) const
Václav Kubernát6415b822018-08-22 17:40:01 +0200262{
Václav Kubernát7e167692020-06-12 09:53:01 +0200263 if (!optPath) {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200264 return m_parser.currentPath();
Václav Kubernát7e167692020-06-12 09:53:01 +0200265 }
266 return toCanonicalPath(*optPath);
Václav Kubernát6415b822018-08-22 17:40:01 +0200267}
268
Václav Kubernát7e167692020-06-12 09:53:01 +0200269struct impl_toCanonicalPath {
270 const dataPath_& m_parserPath;
271
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200272 using ReturnType = boost::variant<dataPath_, schemaPath_, module_>;
273
Václav Kubernát7e167692020-06-12 09:53:01 +0200274 impl_toCanonicalPath(const dataPath_& parserPath)
275 : m_parserPath(parserPath)
276 {
277 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200278 ReturnType operator()(const module_& path) const
Václav Kubernátd6247992020-05-27 00:17:56 +0200279 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200280 return path;
Václav Kubernátd6247992020-05-27 00:17:56 +0200281 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200282 ReturnType operator()(const schemaPath_& path) const
Václav Kubernát5c75b252018-10-10 18:33:47 +0200283 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200284 return impl(path);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200285 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200286 ReturnType operator()(const dataPath_& path) const
Václav Kubernát5c75b252018-10-10 18:33:47 +0200287 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200288 return impl(path);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200289 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200290
Václav Kubernát7e167692020-06-12 09:53:01 +0200291private:
292 template <typename PathType>
Václav Kubernát59e4ee42020-07-08 17:32:45 +0200293 [[nodiscard]] ReturnType impl(const PathType& suffix) const
Václav Kubernátd6247992020-05-27 00:17:56 +0200294 {
Václav Kubernát7e167692020-06-12 09:53:01 +0200295 PathType res = [this] {
296 if constexpr (std::is_same<PathType, schemaPath_>()) {
297 return dataPathToSchemaPath(m_parserPath);
298 } else {
299 return m_parserPath;
300 }
301 }();
Václav Kubernátd6247992020-05-27 00:17:56 +0200302
Václav Kubernát7e167692020-06-12 09:53:01 +0200303 if (suffix.m_scope == Scope::Absolute) {
Václav Kubernát59be0de2020-06-15 13:58:45 +0200304 res = {Scope::Absolute, {}};
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200305 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200306
Václav Kubernát7e167692020-06-12 09:53:01 +0200307 for (const auto& fragment : suffix.m_nodes) {
Václav Kubernáte781b902020-06-15 14:35:11 +0200308 res.pushFragment(fragment);
Václav Kubernát7e167692020-06-12 09:53:01 +0200309 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200310
Václav Kubernát7e167692020-06-12 09:53:01 +0200311 return res;
312 }
313};
314
315template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200316boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const PathType& path) const
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100317{
Václav Kubernát7e167692020-06-12 09:53:01 +0200318 if constexpr (std::is_same<PathType, dataPath_>()) {
319 return impl_toCanonicalPath(m_parser.currentPath())(path);
320 } else {
321 return boost::apply_visitor(impl_toCanonicalPath(m_parser.currentPath()), path);
322 }
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100323}
324
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200325Interpreter::Interpreter(Parser& parser, ProxyDatastore& datastore)
Václav Kubernát812ee282018-08-30 17:10:03 +0200326 : m_parser(parser)
327 , m_datastore(datastore)
Václav Kubernát96344a12018-05-28 16:33:39 +0200328{
329}