blob: 8ee2b55d91c702f30f1940d605b2158ecf2fa9b4 [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
33template <typename PathType>
34std::string pathToString(const PathType& path)
35{
36 return boost::apply_visitor(pathToStringVisitor(), path);
37}
38
Václav Kubernát812ee282018-08-30 17:10:03 +020039void Interpreter::operator()(const commit_&) const
40{
41 m_datastore.commitChanges();
42}
43
Václav Kubernát6d791432018-10-25 16:00:35 +020044void Interpreter::operator()(const discard_&) const
45{
46 m_datastore.discardChanges();
47}
48
Václav Kubernát07204242018-06-04 18:12:09 +020049void Interpreter::operator()(const set_& set) const
50{
Václav Kubernáteeb38842019-03-20 19:46:05 +010051 auto data = set.m_data;
52
53 // If the user didn't supply a module prefix for identityref, we need to add it ourselves
54 if (data.type() == typeid(identityRef_)) {
55 auto identityRef = boost::get<identityRef_>(data);
56 if (!identityRef.m_prefix) {
57 identityRef.m_prefix = set.m_path.m_nodes.front().m_prefix.value();
58 data = identityRef;
59 }
60 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020061 m_datastore.setLeaf(pathToString(toCanonicalPath(set.m_path)), data);
Václav Kubernát07204242018-06-04 18:12:09 +020062}
63
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020064void Interpreter::operator()(const get_& get) const
65{
Václav Kubernát4c3d22f2020-06-12 16:05:01 +020066 auto items = m_datastore.getItems(pathToString(toCanonicalPath(get.m_path)));
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020067 for (auto it = items.begin(); it != items.end(); it++) {
68 auto [path, value] = *it;
69 if (value.type() == typeid(special_) && boost::get<special_>(value).m_value == SpecialValue::LeafList) {
70 auto leafListPrefix = path;
71 std::cout << path << " = " << leafDataToString(value) << std::endl;
Václav Kubernátfb4bda62020-06-09 12:54:41 +020072
73 while (it + 1 != items.end() && boost::starts_with((it + 1) ->first, leafListPrefix)) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020074 ++it;
Václav Kubernátfb4bda62020-06-09 12:54:41 +020075 std::cout << stripLeafListValueFromPath(it->first) << " = " << leafDataToString(it->second) << std::endl;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020076 }
77 } else {
78 std::cout << path << " = " << leafDataToString(value) << std::endl;
79 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020080 }
81}
82
Václav Kubernát96344a12018-05-28 16:33:39 +020083void Interpreter::operator()(const cd_& cd) const
84{
85 m_parser.changeNode(cd.m_path);
86}
87
Václav Kubernátb61336d2018-05-28 17:35:03 +020088void Interpreter::operator()(const create_& create) const
89{
Jan Kundrátcbf288b2020-06-18 20:44:39 +020090 m_datastore.createItem(pathToString(toCanonicalPath(create.m_path)));
Václav Kubernátb61336d2018-05-28 17:35:03 +020091}
92
93void Interpreter::operator()(const delete_& delet) const
94{
Jan Kundrátcbf288b2020-06-18 20:44:39 +020095 m_datastore.deleteItem(pathToString(toCanonicalPath(delet.m_path)));
Václav Kubernátb61336d2018-05-28 17:35:03 +020096}
97
Václav Kubernát11afac72018-07-18 14:59:53 +020098void Interpreter::operator()(const ls_& ls) const
99{
100 std::cout << "Possible nodes:" << std::endl;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200101 auto recursion{Recursion::NonRecursive};
102 for (auto it : ls.m_options) {
103 if (it == LsOption::Recursive)
104 recursion = Recursion::Recursive;
105 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200106
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200107 auto toPrint = m_datastore.schema()->availableNodes(toCanonicalPath(ls.m_path), recursion);
Václav Kubernát82086872020-04-29 01:09:50 +0200108
Václav Kubernát95b08872020-04-28 01:04:17 +0200109 for (const auto& it : toPrint) {
110 std::cout << (it.first ? *it.first + ":" : "" ) + it.second << std::endl;
111 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200112}
113
Václav Kubernát7160a132020-04-03 02:11:01 +0200114void Interpreter::operator()(const copy_& copy) const
115{
116 m_datastore.copyConfig(copy.m_source, copy.m_destination);
117}
118
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100119std::string Interpreter::buildTypeInfo(const std::string& path) const
120{
121 std::ostringstream ss;
122 switch (m_datastore.schema()->nodeType(path)) {
123 case yang::NodeTypes::Container:
124 ss << "container";
125 break;
126 case yang::NodeTypes::PresenceContainer:
127 ss << "presence container";
128 break;
129 case yang::NodeTypes::Leaf:
130 {
131 auto leafType = m_datastore.schema()->leafType(path);
132 auto typedefName = m_datastore.schema()->leafTypeName(path);
133 std::string baseTypeStr;
Václav Kubernát13b23d72020-04-16 21:49:51 +0200134 if (std::holds_alternative<yang::LeafRef>(leafType.m_type)) {
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100135 ss << "-> ";
136 ss << m_datastore.schema()->leafrefPath(path) << " ";
Václav Kubernát13b23d72020-04-16 21:49:51 +0200137 baseTypeStr = leafDataTypeToString(std::get<yang::LeafRef>(leafType.m_type).m_targetType->m_type);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100138 } else {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200139 baseTypeStr = leafDataTypeToString(leafType.m_type);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100140 }
141
142 if (typedefName) {
143 ss << *typedefName << " (" << baseTypeStr << ")";
144 } else {
145 ss << baseTypeStr;
146 }
147
Václav Kubernát13b23d72020-04-16 21:49:51 +0200148 if (leafType.m_units) {
149 ss << " [" + *leafType.m_units + "]";
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100150 }
151
152 if (m_datastore.schema()->leafIsKey(path)) {
153 ss << " (key)";
154 }
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200155
156 if (auto defaultValue = m_datastore.schema()->defaultValue(path)) {
157 ss << " default: " << leafDataToString(*defaultValue);
158 }
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100159 break;
160 }
161 case yang::NodeTypes::List:
162 ss << "list";
163 break;
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200164 case yang::NodeTypes::Action:
165 case yang::NodeTypes::AnyXml:
166 case yang::NodeTypes::LeafList:
167 case yang::NodeTypes::Notification:
168 case yang::NodeTypes::Rpc:
169 throw std::logic_error("describe got an rpc or an action: this should never happen, because their paths cannot be parsed");
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100170 }
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200171
172 if (!m_datastore.schema()->isConfig(path)) {
173 ss << " (ro)";
174 }
175
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100176 return ss.str();
177}
178
179void Interpreter::operator()(const describe_& describe) const
180{
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200181 auto path = pathToString(toCanonicalPath(describe.m_path));
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200182 auto status = m_datastore.schema()->status(path);
183 auto statusStr = status == yang::Status::Deprecated ? " (deprecated)" :
184 status == yang::Status::Obsolete ? " (obsolete)" :
185 "";
186
187 std::cout << path << ": " << buildTypeInfo(path) << statusStr << std::endl;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100188 if (auto description = m_datastore.schema()->description(path)) {
189 std::cout << std::endl << *description << std::endl;
190 }
191}
192
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200193void Interpreter::operator()(const move_& move) const
194{
195 m_datastore.moveItem(pathToDataString(move.m_source, Prefixes::WhenNeeded), move.m_destination);
196}
197
Václav Kubernát054cc992019-02-21 14:23:52 +0100198struct commandLongHelpVisitor : boost::static_visitor<const char*> {
199 template <typename T>
200 auto constexpr operator()(boost::type<T>) const
201 {
202 return T::longHelp;
203 }
204};
205
206struct commandShortHelpVisitor : boost::static_visitor<const char*> {
207 template <typename T>
208 auto constexpr operator()(boost::type<T>) const
209 {
210 return T::shortHelp;
211 }
212};
213
214void Interpreter::operator()(const help_& help) const
215{
216 if (help.m_cmd)
217 std::cout << boost::apply_visitor(commandLongHelpVisitor(), help.m_cmd.get()) << std::endl;
218 else
219 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([](auto cmd) {
220 std::cout << commandShortHelpVisitor()(cmd) << std::endl;
221 });
222}
223
Václav Kubernát7e167692020-06-12 09:53:01 +0200224template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200225boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const boost::optional<PathType>& optPath) const
Václav Kubernát6415b822018-08-22 17:40:01 +0200226{
Václav Kubernát7e167692020-06-12 09:53:01 +0200227 if (!optPath) {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200228 return m_parser.currentPath();
Václav Kubernát7e167692020-06-12 09:53:01 +0200229 }
230 return toCanonicalPath(*optPath);
Václav Kubernát6415b822018-08-22 17:40:01 +0200231}
232
Václav Kubernát7e167692020-06-12 09:53:01 +0200233struct impl_toCanonicalPath {
234 const dataPath_& m_parserPath;
235
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200236 using ReturnType = boost::variant<dataPath_, schemaPath_, module_>;
237
Václav Kubernát7e167692020-06-12 09:53:01 +0200238 impl_toCanonicalPath(const dataPath_& parserPath)
239 : m_parserPath(parserPath)
240 {
241 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200242 ReturnType operator()(const module_& path) const
Václav Kubernátd6247992020-05-27 00:17:56 +0200243 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200244 return path;
Václav Kubernátd6247992020-05-27 00:17:56 +0200245 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200246 ReturnType operator()(const schemaPath_& path) const
Václav Kubernát5c75b252018-10-10 18:33:47 +0200247 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200248 return impl(path);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200249 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200250 ReturnType operator()(const dataPath_& path) const
Václav Kubernát5c75b252018-10-10 18:33:47 +0200251 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200252 return impl(path);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200253 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200254
Václav Kubernát7e167692020-06-12 09:53:01 +0200255private:
256 template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200257 ReturnType impl(const PathType& suffix) const
Václav Kubernátd6247992020-05-27 00:17:56 +0200258 {
Václav Kubernát7e167692020-06-12 09:53:01 +0200259 PathType res = [this] {
260 if constexpr (std::is_same<PathType, schemaPath_>()) {
261 return dataPathToSchemaPath(m_parserPath);
262 } else {
263 return m_parserPath;
264 }
265 }();
Václav Kubernátd6247992020-05-27 00:17:56 +0200266
Václav Kubernát7e167692020-06-12 09:53:01 +0200267 if (suffix.m_scope == Scope::Absolute) {
Václav Kubernát59be0de2020-06-15 13:58:45 +0200268 res = {Scope::Absolute, {}};
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200269 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200270
Václav Kubernát7e167692020-06-12 09:53:01 +0200271 for (const auto& fragment : suffix.m_nodes) {
Václav Kubernáte781b902020-06-15 14:35:11 +0200272 res.pushFragment(fragment);
Václav Kubernát7e167692020-06-12 09:53:01 +0200273 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200274
Václav Kubernát7e167692020-06-12 09:53:01 +0200275 return res;
276 }
277};
278
279template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200280boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const PathType& path) const
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100281{
Václav Kubernát7e167692020-06-12 09:53:01 +0200282 if constexpr (std::is_same<PathType, dataPath_>()) {
283 return impl_toCanonicalPath(m_parser.currentPath())(path);
284 } else {
285 return boost::apply_visitor(impl_toCanonicalPath(m_parser.currentPath()), path);
286 }
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100287}
288
Václav Kubernát6415b822018-08-22 17:40:01 +0200289Interpreter::Interpreter(Parser& parser, DatastoreAccess& datastore)
Václav Kubernát812ee282018-08-30 17:10:03 +0200290 : m_parser(parser)
291 , m_datastore(datastore)
Václav Kubernát96344a12018-05-28 16:33:39 +0200292{
293}