blob: 0fe96da1fd3217d641045a6761dd091468dd1a40 [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áte066fe22022-04-06 00:32:26 +020013#include "UniqueResource.hpp"
Václav Kubernát6415b822018-08-22 17:40:01 +020014#include "datastore_access.hpp"
Václav Kubernát96344a12018-05-28 16:33:39 +020015#include "interpreter.hpp"
Václav Kubernát509ce652019-05-29 19:46:44 +020016#include "utils.hpp"
Václav Kubernát96344a12018-05-28 16:33:39 +020017
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020018struct pathToStringVisitor : boost::static_visitor<std::string> {
19 std::string operator()(const module_& path) const
20 {
21 using namespace std::string_literals;
22 return "/"s + boost::get<module_>(path).m_name + ":*";
23 }
24 std::string operator()(const schemaPath_& path) const
25 {
26 return pathToSchemaString(path, Prefixes::WhenNeeded);
27 }
28 std::string operator()(const dataPath_& path) const
29 {
30 return pathToDataString(path, Prefixes::WhenNeeded);
31 }
32};
33
Václav Kubernáte7248b22020-06-26 15:38:59 +020034namespace {
35void printTree(const DatastoreAccess::Tree tree)
36{
37 for (auto it = tree.begin(); it != tree.end(); it++) {
38 auto [path, value] = *it;
39 if (value.type() == typeid(special_) && boost::get<special_>(value).m_value == SpecialValue::LeafList) {
40 auto leafListPrefix = path;
41 std::cout << path << " = " << leafDataToString(value) << std::endl;
42
43 while (it + 1 != tree.end() && boost::starts_with((it + 1)->first, leafListPrefix)) {
44 ++it;
45 std::cout << stripLeafListValueFromPath(it->first) << " = " << leafDataToString(it->second) << std::endl;
46 }
47 } else {
48 std::cout << path << " = " << leafDataToString(value) << std::endl;
49 }
50 }
51}
52}
53
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020054template <typename PathType>
55std::string pathToString(const PathType& path)
56{
57 return boost::apply_visitor(pathToStringVisitor(), path);
58}
59
Václav Kubernát812ee282018-08-30 17:10:03 +020060void Interpreter::operator()(const commit_&) const
61{
62 m_datastore.commitChanges();
63}
64
Václav Kubernát6d791432018-10-25 16:00:35 +020065void Interpreter::operator()(const discard_&) const
66{
67 m_datastore.discardChanges();
68}
69
Václav Kubernát07204242018-06-04 18:12:09 +020070void Interpreter::operator()(const set_& set) const
71{
Václav Kubernáteeb38842019-03-20 19:46:05 +010072 auto data = set.m_data;
73
74 // If the user didn't supply a module prefix for identityref, we need to add it ourselves
75 if (data.type() == typeid(identityRef_)) {
76 auto identityRef = boost::get<identityRef_>(data);
77 if (!identityRef.m_prefix) {
78 identityRef.m_prefix = set.m_path.m_nodes.front().m_prefix.value();
79 data = identityRef;
80 }
81 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020082 m_datastore.setLeaf(pathToString(toCanonicalPath(set.m_path)), data);
Václav Kubernát07204242018-06-04 18:12:09 +020083}
84
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020085void Interpreter::operator()(const get_& get) const
86{
Václav Kubernáte066fe22022-04-06 00:32:26 +020087 auto targetSwitcher = make_unique_resource([] {}, [this, oldTarget = m_datastore.target()] {
88 m_datastore.setTarget(oldTarget);
89 });
90
91 if (get.m_dsTarget) {
92 m_datastore.setTarget(*get.m_dsTarget);
93 }
94
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020095 auto items = m_datastore.getItems(pathToString(toCanonicalPath(get.m_path)));
Václav Kubernáte7248b22020-06-26 15:38:59 +020096 printTree(items);
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020097}
98
Václav Kubernát96344a12018-05-28 16:33:39 +020099void Interpreter::operator()(const cd_& cd) const
100{
Václav Kubernát218ee7b2022-03-30 22:35:26 +0200101 if (auto rpcInputPath = m_datastore.inputDatastorePath(); rpcInputPath && !pathToDataString(cd.m_path, Prefixes::WhenNeeded).starts_with(m_parser.currentNode())) {
102 throw std::runtime_error("Can't cd out of `prepare` context");
103 }
Václav Kubernát96344a12018-05-28 16:33:39 +0200104 m_parser.changeNode(cd.m_path);
105}
106
Václav Kubernátb61336d2018-05-28 17:35:03 +0200107void Interpreter::operator()(const create_& create) const
108{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200109 m_datastore.createItem(pathToString(toCanonicalPath(create.m_path)));
Václav Kubernátb61336d2018-05-28 17:35:03 +0200110}
111
112void Interpreter::operator()(const delete_& delet) const
113{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200114 m_datastore.deleteItem(pathToString(toCanonicalPath(delet.m_path)));
Václav Kubernátb61336d2018-05-28 17:35:03 +0200115}
116
Václav Kubernát11afac72018-07-18 14:59:53 +0200117void Interpreter::operator()(const ls_& ls) const
118{
119 std::cout << "Possible nodes:" << std::endl;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200120 auto recursion{Recursion::NonRecursive};
121 for (auto it : ls.m_options) {
Václav Kubernát3a433232020-07-08 17:52:50 +0200122 if (it == LsOption::Recursive) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200123 recursion = Recursion::Recursive;
Václav Kubernát3a433232020-07-08 17:52:50 +0200124 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200125 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200126
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200127 auto toPrint = m_datastore.schema()->availableNodes(toCanonicalPath(ls.m_path), recursion);
Václav Kubernát82086872020-04-29 01:09:50 +0200128
Václav Kubernát95b08872020-04-28 01:04:17 +0200129 for (const auto& it : toPrint) {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100130 std::cout << (it.first ? *it.first + ":" : "") + it.second << std::endl;
Václav Kubernát95b08872020-04-28 01:04:17 +0200131 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200132}
133
Václav Kubernát7160a132020-04-03 02:11:01 +0200134void Interpreter::operator()(const copy_& copy) const
135{
136 m_datastore.copyConfig(copy.m_source, copy.m_destination);
137}
138
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100139std::string Interpreter::buildTypeInfo(const std::string& path) const
140{
141 std::ostringstream ss;
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100142 std::string typeDescription;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100143 switch (m_datastore.schema()->nodeType(path)) {
144 case yang::NodeTypes::Container:
145 ss << "container";
146 break;
147 case yang::NodeTypes::PresenceContainer:
148 ss << "presence container";
149 break;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100150 case yang::NodeTypes::Leaf: {
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100151 auto leafType = m_datastore.schema()->leafType(path);
152 auto typedefName = m_datastore.schema()->leafTypeName(path);
153 std::string baseTypeStr;
Václav Kubernát13b23d72020-04-16 21:49:51 +0200154 if (std::holds_alternative<yang::LeafRef>(leafType.m_type)) {
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100155 ss << "-> ";
156 ss << m_datastore.schema()->leafrefPath(path) << " ";
Václav Kubernát13b23d72020-04-16 21:49:51 +0200157 baseTypeStr = leafDataTypeToString(std::get<yang::LeafRef>(leafType.m_type).m_targetType->m_type);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100158 } else {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200159 baseTypeStr = leafDataTypeToString(leafType.m_type);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100160 }
161
162 if (typedefName) {
163 ss << *typedefName << " (" << baseTypeStr << ")";
164 } else {
165 ss << baseTypeStr;
166 }
167
Václav Kubernát13b23d72020-04-16 21:49:51 +0200168 if (leafType.m_units) {
169 ss << " [" + *leafType.m_units + "]";
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100170 }
171
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100172 if (leafType.m_description) {
173 typeDescription = "\nType description: " + *leafType.m_description;
174 }
175
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100176 if (m_datastore.schema()->leafIsKey(path)) {
177 ss << " (key)";
178 }
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200179
180 if (auto defaultValue = m_datastore.schema()->defaultValue(path)) {
181 ss << " default: " << leafDataToString(*defaultValue);
182 }
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100183 break;
184 }
185 case yang::NodeTypes::List:
186 ss << "list";
187 break;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200188 case yang::NodeTypes::Rpc:
189 ss << "RPC";
190 break;
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200191 case yang::NodeTypes::LeafList:
Václav Kubernát160e5a22020-12-01 01:11:28 +0100192 ss << "leaf-list";
193 break;
194 case yang::NodeTypes::Action:
195 ss << "action";
196 break;
197 case yang::NodeTypes::AnyXml:
198 throw std::logic_error("Sorry, anyxml isn't supported yet.");
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200199 case yang::NodeTypes::Notification:
Václav Kubernát160e5a22020-12-01 01:11:28 +0100200 throw std::logic_error("Sorry, notifications aren't supported yet.");
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100201 }
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200202
203 if (!m_datastore.schema()->isConfig(path)) {
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100204 ss << " (ro)\n";
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200205 }
206
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200207 auto status = m_datastore.schema()->status(path);
208 auto statusStr = status == yang::Status::Deprecated ? " (deprecated)" :
209 status == yang::Status::Obsolete ? " (obsolete)" :
210 "";
211
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100212 ss << statusStr;
213
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100214 if (auto description = m_datastore.schema()->description(path)) {
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100215 ss << std::endl << *description << std::endl;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100216 }
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100217
218 ss << typeDescription;
219 return ss.str();
220}
221
222void Interpreter::operator()(const describe_& describe) const
223{
224 auto fullPath = pathToString(toCanonicalPath(describe.m_path));
225
226 std::cout << pathToString(describe.m_path) << ": " << buildTypeInfo(fullPath) << std::endl;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100227}
228
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200229void Interpreter::operator()(const move_& move) const
230{
231 m_datastore.moveItem(pathToDataString(move.m_source, Prefixes::WhenNeeded), move.m_destination);
232}
233
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200234void Interpreter::operator()(const dump_& dump) const
235{
236 std::cout << m_datastore.dump(dump.m_format) << "\n";
237}
238
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200239void Interpreter::operator()(const prepare_& prepare) const
Václav Kubernáte7248b22020-06-26 15:38:59 +0200240{
Václav Kubernátb3960f82020-12-01 03:21:48 +0100241 m_datastore.initiate(pathToString(toCanonicalPath(prepare.m_path)));
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200242 m_parser.changeNode(prepare.m_path);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200243}
244
Václav Kubernátd8408e02020-12-02 05:13:27 +0100245void Interpreter::operator()(const exec_& exec) const
Václav Kubernáte7248b22020-06-26 15:38:59 +0200246{
247 m_parser.changeNode({Scope::Absolute, {}});
Václav Kubernátd8408e02020-12-02 05:13:27 +0100248 if (exec.m_path) {
249 m_datastore.initiate(pathToString(toCanonicalPath(*exec.m_path)));
250 }
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200251 auto output = m_datastore.execute();
252 std::cout << "RPC/action output:\n";
Václav Kubernáte7248b22020-06-26 15:38:59 +0200253 printTree(output);
254}
255
256void Interpreter::operator()(const cancel_&) const
257{
258 m_parser.changeNode({Scope::Absolute, {}});
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200259 m_datastore.cancel();
Václav Kubernáte7248b22020-06-26 15:38:59 +0200260}
261
Václav Kubernát162165e2021-02-22 09:46:53 +0100262void Interpreter::operator()(const switch_& switch_cmd) const
263{
264 m_datastore.setTarget(switch_cmd.m_target);
265}
266
Václav Kubernát054cc992019-02-21 14:23:52 +0100267struct commandLongHelpVisitor : boost::static_visitor<const char*> {
268 template <typename T>
269 auto constexpr operator()(boost::type<T>) const
270 {
271 return T::longHelp;
272 }
273};
274
275struct commandShortHelpVisitor : boost::static_visitor<const char*> {
276 template <typename T>
277 auto constexpr operator()(boost::type<T>) const
278 {
279 return T::shortHelp;
280 }
281};
282
283void Interpreter::operator()(const help_& help) const
284{
Václav Kubernát3a433232020-07-08 17:52:50 +0200285 if (help.m_cmd) {
Václav Kubernát054cc992019-02-21 14:23:52 +0100286 std::cout << boost::apply_visitor(commandLongHelpVisitor(), help.m_cmd.get()) << std::endl;
Václav Kubernát3a433232020-07-08 17:52:50 +0200287 } else {
Václav Kubernát054cc992019-02-21 14:23:52 +0100288 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([](auto cmd) {
289 std::cout << commandShortHelpVisitor()(cmd) << std::endl;
290 });
Václav Kubernát3a433232020-07-08 17:52:50 +0200291 }
Václav Kubernát054cc992019-02-21 14:23:52 +0100292}
293
Václav Kubernát7e167692020-06-12 09:53:01 +0200294template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200295boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const boost::optional<PathType>& optPath) const
Václav Kubernát6415b822018-08-22 17:40:01 +0200296{
Václav Kubernát7e167692020-06-12 09:53:01 +0200297 if (!optPath) {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200298 return m_parser.currentPath();
Václav Kubernát7e167692020-06-12 09:53:01 +0200299 }
300 return toCanonicalPath(*optPath);
Václav Kubernát6415b822018-08-22 17:40:01 +0200301}
302
Václav Kubernát7e167692020-06-12 09:53:01 +0200303struct impl_toCanonicalPath {
304 const dataPath_& m_parserPath;
305
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200306 using ReturnType = boost::variant<dataPath_, schemaPath_, module_>;
307
Václav Kubernát7e167692020-06-12 09:53:01 +0200308 impl_toCanonicalPath(const dataPath_& parserPath)
309 : m_parserPath(parserPath)
310 {
311 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200312 ReturnType operator()(const module_& path) const
Václav Kubernátd6247992020-05-27 00:17:56 +0200313 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200314 return path;
Václav Kubernátd6247992020-05-27 00:17:56 +0200315 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200316 ReturnType operator()(const schemaPath_& path) const
Václav Kubernát5c75b252018-10-10 18:33:47 +0200317 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200318 return impl(path);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200319 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200320 ReturnType operator()(const dataPath_& path) const
Václav Kubernát5c75b252018-10-10 18:33:47 +0200321 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200322 return impl(path);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200323 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200324
Václav Kubernát7e167692020-06-12 09:53:01 +0200325private:
326 template <typename PathType>
Václav Kubernát59e4ee42020-07-08 17:32:45 +0200327 [[nodiscard]] ReturnType impl(const PathType& suffix) const
Václav Kubernátd6247992020-05-27 00:17:56 +0200328 {
Václav Kubernát7e167692020-06-12 09:53:01 +0200329 PathType res = [this] {
330 if constexpr (std::is_same<PathType, schemaPath_>()) {
331 return dataPathToSchemaPath(m_parserPath);
332 } else {
333 return m_parserPath;
334 }
335 }();
Václav Kubernátd6247992020-05-27 00:17:56 +0200336
Václav Kubernát7e167692020-06-12 09:53:01 +0200337 if (suffix.m_scope == Scope::Absolute) {
Václav Kubernát59be0de2020-06-15 13:58:45 +0200338 res = {Scope::Absolute, {}};
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200339 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200340
Václav Kubernát7e167692020-06-12 09:53:01 +0200341 for (const auto& fragment : suffix.m_nodes) {
Václav Kubernáte781b902020-06-15 14:35:11 +0200342 res.pushFragment(fragment);
Václav Kubernát7e167692020-06-12 09:53:01 +0200343 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200344
Václav Kubernát7e167692020-06-12 09:53:01 +0200345 return res;
346 }
347};
348
349template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200350boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const PathType& path) const
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100351{
Václav Kubernát7e167692020-06-12 09:53:01 +0200352 if constexpr (std::is_same<PathType, dataPath_>()) {
353 return impl_toCanonicalPath(m_parser.currentPath())(path);
354 } else {
355 return boost::apply_visitor(impl_toCanonicalPath(m_parser.currentPath()), path);
356 }
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100357}
358
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200359Interpreter::Interpreter(Parser& parser, ProxyDatastore& datastore)
Václav Kubernát812ee282018-08-30 17:10:03 +0200360 : m_parser(parser)
361 , m_datastore(datastore)
Václav Kubernát96344a12018-05-28 16:33:39 +0200362{
363}