blob: 8030f3f8eeb1b568f2acae17b713ea9362846c95 [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{
Václav Kubernát218ee7b2022-03-30 22:35:26 +020092 if (auto rpcInputPath = m_datastore.inputDatastorePath(); rpcInputPath && !pathToDataString(cd.m_path, Prefixes::WhenNeeded).starts_with(m_parser.currentNode())) {
93 throw std::runtime_error("Can't cd out of `prepare` context");
94 }
Václav Kubernát96344a12018-05-28 16:33:39 +020095 m_parser.changeNode(cd.m_path);
96}
97
Václav Kubernátb61336d2018-05-28 17:35:03 +020098void Interpreter::operator()(const create_& create) const
99{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200100 m_datastore.createItem(pathToString(toCanonicalPath(create.m_path)));
Václav Kubernátb61336d2018-05-28 17:35:03 +0200101}
102
103void Interpreter::operator()(const delete_& delet) const
104{
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200105 m_datastore.deleteItem(pathToString(toCanonicalPath(delet.m_path)));
Václav Kubernátb61336d2018-05-28 17:35:03 +0200106}
107
Václav Kubernát11afac72018-07-18 14:59:53 +0200108void Interpreter::operator()(const ls_& ls) const
109{
110 std::cout << "Possible nodes:" << std::endl;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200111 auto recursion{Recursion::NonRecursive};
112 for (auto it : ls.m_options) {
Václav Kubernát3a433232020-07-08 17:52:50 +0200113 if (it == LsOption::Recursive) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200114 recursion = Recursion::Recursive;
Václav Kubernát3a433232020-07-08 17:52:50 +0200115 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200116 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200117
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200118 auto toPrint = m_datastore.schema()->availableNodes(toCanonicalPath(ls.m_path), recursion);
Václav Kubernát82086872020-04-29 01:09:50 +0200119
Václav Kubernát95b08872020-04-28 01:04:17 +0200120 for (const auto& it : toPrint) {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100121 std::cout << (it.first ? *it.first + ":" : "") + it.second << std::endl;
Václav Kubernát95b08872020-04-28 01:04:17 +0200122 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200123}
124
Václav Kubernát7160a132020-04-03 02:11:01 +0200125void Interpreter::operator()(const copy_& copy) const
126{
127 m_datastore.copyConfig(copy.m_source, copy.m_destination);
128}
129
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100130std::string Interpreter::buildTypeInfo(const std::string& path) const
131{
132 std::ostringstream ss;
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100133 std::string typeDescription;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100134 switch (m_datastore.schema()->nodeType(path)) {
135 case yang::NodeTypes::Container:
136 ss << "container";
137 break;
138 case yang::NodeTypes::PresenceContainer:
139 ss << "presence container";
140 break;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100141 case yang::NodeTypes::Leaf: {
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100142 auto leafType = m_datastore.schema()->leafType(path);
143 auto typedefName = m_datastore.schema()->leafTypeName(path);
144 std::string baseTypeStr;
Václav Kubernát13b23d72020-04-16 21:49:51 +0200145 if (std::holds_alternative<yang::LeafRef>(leafType.m_type)) {
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100146 ss << "-> ";
147 ss << m_datastore.schema()->leafrefPath(path) << " ";
Václav Kubernát13b23d72020-04-16 21:49:51 +0200148 baseTypeStr = leafDataTypeToString(std::get<yang::LeafRef>(leafType.m_type).m_targetType->m_type);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100149 } else {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200150 baseTypeStr = leafDataTypeToString(leafType.m_type);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100151 }
152
153 if (typedefName) {
154 ss << *typedefName << " (" << baseTypeStr << ")";
155 } else {
156 ss << baseTypeStr;
157 }
158
Václav Kubernát13b23d72020-04-16 21:49:51 +0200159 if (leafType.m_units) {
160 ss << " [" + *leafType.m_units + "]";
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100161 }
162
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100163 if (leafType.m_description) {
164 typeDescription = "\nType description: " + *leafType.m_description;
165 }
166
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100167 if (m_datastore.schema()->leafIsKey(path)) {
168 ss << " (key)";
169 }
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200170
171 if (auto defaultValue = m_datastore.schema()->defaultValue(path)) {
172 ss << " default: " << leafDataToString(*defaultValue);
173 }
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100174 break;
175 }
176 case yang::NodeTypes::List:
177 ss << "list";
178 break;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200179 case yang::NodeTypes::Rpc:
180 ss << "RPC";
181 break;
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200182 case yang::NodeTypes::LeafList:
Václav Kubernát160e5a22020-12-01 01:11:28 +0100183 ss << "leaf-list";
184 break;
185 case yang::NodeTypes::Action:
186 ss << "action";
187 break;
188 case yang::NodeTypes::AnyXml:
189 throw std::logic_error("Sorry, anyxml isn't supported yet.");
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200190 case yang::NodeTypes::Notification:
Václav Kubernát160e5a22020-12-01 01:11:28 +0100191 throw std::logic_error("Sorry, notifications aren't supported yet.");
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100192 }
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200193
194 if (!m_datastore.schema()->isConfig(path)) {
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100195 ss << " (ro)\n";
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200196 }
197
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200198 auto status = m_datastore.schema()->status(path);
199 auto statusStr = status == yang::Status::Deprecated ? " (deprecated)" :
200 status == yang::Status::Obsolete ? " (obsolete)" :
201 "";
202
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100203 ss << statusStr;
204
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100205 if (auto description = m_datastore.schema()->description(path)) {
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100206 ss << std::endl << *description << std::endl;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100207 }
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100208
209 ss << typeDescription;
210 return ss.str();
211}
212
213void Interpreter::operator()(const describe_& describe) const
214{
215 auto fullPath = pathToString(toCanonicalPath(describe.m_path));
216
217 std::cout << pathToString(describe.m_path) << ": " << buildTypeInfo(fullPath) << std::endl;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100218}
219
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200220void Interpreter::operator()(const move_& move) const
221{
222 m_datastore.moveItem(pathToDataString(move.m_source, Prefixes::WhenNeeded), move.m_destination);
223}
224
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200225void Interpreter::operator()(const dump_& dump) const
226{
227 std::cout << m_datastore.dump(dump.m_format) << "\n";
228}
229
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200230void Interpreter::operator()(const prepare_& prepare) const
Václav Kubernáte7248b22020-06-26 15:38:59 +0200231{
Václav Kubernátb3960f82020-12-01 03:21:48 +0100232 m_datastore.initiate(pathToString(toCanonicalPath(prepare.m_path)));
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200233 m_parser.changeNode(prepare.m_path);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200234}
235
Václav Kubernátd8408e02020-12-02 05:13:27 +0100236void Interpreter::operator()(const exec_& exec) const
Václav Kubernáte7248b22020-06-26 15:38:59 +0200237{
238 m_parser.changeNode({Scope::Absolute, {}});
Václav Kubernátd8408e02020-12-02 05:13:27 +0100239 if (exec.m_path) {
240 m_datastore.initiate(pathToString(toCanonicalPath(*exec.m_path)));
241 }
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200242 auto output = m_datastore.execute();
243 std::cout << "RPC/action output:\n";
Václav Kubernáte7248b22020-06-26 15:38:59 +0200244 printTree(output);
245}
246
247void Interpreter::operator()(const cancel_&) const
248{
249 m_parser.changeNode({Scope::Absolute, {}});
Václav Kubernátaa4250a2020-07-22 00:02:23 +0200250 m_datastore.cancel();
Václav Kubernáte7248b22020-06-26 15:38:59 +0200251}
252
Václav Kubernát162165e2021-02-22 09:46:53 +0100253void Interpreter::operator()(const switch_& switch_cmd) const
254{
255 m_datastore.setTarget(switch_cmd.m_target);
256}
257
Václav Kubernát054cc992019-02-21 14:23:52 +0100258struct commandLongHelpVisitor : boost::static_visitor<const char*> {
259 template <typename T>
260 auto constexpr operator()(boost::type<T>) const
261 {
262 return T::longHelp;
263 }
264};
265
266struct commandShortHelpVisitor : boost::static_visitor<const char*> {
267 template <typename T>
268 auto constexpr operator()(boost::type<T>) const
269 {
270 return T::shortHelp;
271 }
272};
273
274void Interpreter::operator()(const help_& help) const
275{
Václav Kubernát3a433232020-07-08 17:52:50 +0200276 if (help.m_cmd) {
Václav Kubernát054cc992019-02-21 14:23:52 +0100277 std::cout << boost::apply_visitor(commandLongHelpVisitor(), help.m_cmd.get()) << std::endl;
Václav Kubernát3a433232020-07-08 17:52:50 +0200278 } else {
Václav Kubernát054cc992019-02-21 14:23:52 +0100279 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([](auto cmd) {
280 std::cout << commandShortHelpVisitor()(cmd) << std::endl;
281 });
Václav Kubernát3a433232020-07-08 17:52:50 +0200282 }
Václav Kubernát054cc992019-02-21 14:23:52 +0100283}
284
Václav Kubernát7e167692020-06-12 09:53:01 +0200285template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200286boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const boost::optional<PathType>& optPath) const
Václav Kubernát6415b822018-08-22 17:40:01 +0200287{
Václav Kubernát7e167692020-06-12 09:53:01 +0200288 if (!optPath) {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200289 return m_parser.currentPath();
Václav Kubernát7e167692020-06-12 09:53:01 +0200290 }
291 return toCanonicalPath(*optPath);
Václav Kubernát6415b822018-08-22 17:40:01 +0200292}
293
Václav Kubernát7e167692020-06-12 09:53:01 +0200294struct impl_toCanonicalPath {
295 const dataPath_& m_parserPath;
296
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200297 using ReturnType = boost::variant<dataPath_, schemaPath_, module_>;
298
Václav Kubernát7e167692020-06-12 09:53:01 +0200299 impl_toCanonicalPath(const dataPath_& parserPath)
300 : m_parserPath(parserPath)
301 {
302 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200303 ReturnType operator()(const module_& path) const
Václav Kubernátd6247992020-05-27 00:17:56 +0200304 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200305 return path;
Václav Kubernátd6247992020-05-27 00:17:56 +0200306 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200307 ReturnType operator()(const schemaPath_& path) const
Václav Kubernát5c75b252018-10-10 18:33:47 +0200308 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200309 return impl(path);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200310 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200311 ReturnType operator()(const dataPath_& path) const
Václav Kubernát5c75b252018-10-10 18:33:47 +0200312 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200313 return impl(path);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200314 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200315
Václav Kubernát7e167692020-06-12 09:53:01 +0200316private:
317 template <typename PathType>
Václav Kubernát59e4ee42020-07-08 17:32:45 +0200318 [[nodiscard]] ReturnType impl(const PathType& suffix) const
Václav Kubernátd6247992020-05-27 00:17:56 +0200319 {
Václav Kubernát7e167692020-06-12 09:53:01 +0200320 PathType res = [this] {
321 if constexpr (std::is_same<PathType, schemaPath_>()) {
322 return dataPathToSchemaPath(m_parserPath);
323 } else {
324 return m_parserPath;
325 }
326 }();
Václav Kubernátd6247992020-05-27 00:17:56 +0200327
Václav Kubernát7e167692020-06-12 09:53:01 +0200328 if (suffix.m_scope == Scope::Absolute) {
Václav Kubernát59be0de2020-06-15 13:58:45 +0200329 res = {Scope::Absolute, {}};
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200330 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200331
Václav Kubernát7e167692020-06-12 09:53:01 +0200332 for (const auto& fragment : suffix.m_nodes) {
Václav Kubernáte781b902020-06-15 14:35:11 +0200333 res.pushFragment(fragment);
Václav Kubernát7e167692020-06-12 09:53:01 +0200334 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200335
Václav Kubernát7e167692020-06-12 09:53:01 +0200336 return res;
337 }
338};
339
340template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200341boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const PathType& path) const
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100342{
Václav Kubernát7e167692020-06-12 09:53:01 +0200343 if constexpr (std::is_same<PathType, dataPath_>()) {
344 return impl_toCanonicalPath(m_parser.currentPath())(path);
345 } else {
346 return boost::apply_visitor(impl_toCanonicalPath(m_parser.currentPath()), path);
347 }
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100348}
349
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200350Interpreter::Interpreter(Parser& parser, ProxyDatastore& datastore)
Václav Kubernát812ee282018-08-30 17:10:03 +0200351 : m_parser(parser)
352 , m_datastore(datastore)
Václav Kubernát96344a12018-05-28 16:33:39 +0200353{
354}