blob: 5ff73e7679c20b06988a141ac675a1c88e66dc04 [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) {
Václav Kubernát3a433232020-07-08 17:52:50 +0200103 if (it == LsOption::Recursive) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200104 recursion = Recursion::Recursive;
Václav Kubernát3a433232020-07-08 17:52:50 +0200105 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200106 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200107
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200108 auto toPrint = m_datastore.schema()->availableNodes(toCanonicalPath(ls.m_path), recursion);
Václav Kubernát82086872020-04-29 01:09:50 +0200109
Václav Kubernát95b08872020-04-28 01:04:17 +0200110 for (const auto& it : toPrint) {
111 std::cout << (it.first ? *it.first + ":" : "" ) + it.second << std::endl;
112 }
Václav Kubernát11afac72018-07-18 14:59:53 +0200113}
114
Václav Kubernát7160a132020-04-03 02:11:01 +0200115void Interpreter::operator()(const copy_& copy) const
116{
117 m_datastore.copyConfig(copy.m_source, copy.m_destination);
118}
119
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100120std::string Interpreter::buildTypeInfo(const std::string& path) const
121{
122 std::ostringstream ss;
123 switch (m_datastore.schema()->nodeType(path)) {
124 case yang::NodeTypes::Container:
125 ss << "container";
126 break;
127 case yang::NodeTypes::PresenceContainer:
128 ss << "presence container";
129 break;
130 case yang::NodeTypes::Leaf:
131 {
132 auto leafType = m_datastore.schema()->leafType(path);
133 auto typedefName = m_datastore.schema()->leafTypeName(path);
134 std::string baseTypeStr;
Václav Kubernát13b23d72020-04-16 21:49:51 +0200135 if (std::holds_alternative<yang::LeafRef>(leafType.m_type)) {
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100136 ss << "-> ";
137 ss << m_datastore.schema()->leafrefPath(path) << " ";
Václav Kubernát13b23d72020-04-16 21:49:51 +0200138 baseTypeStr = leafDataTypeToString(std::get<yang::LeafRef>(leafType.m_type).m_targetType->m_type);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100139 } else {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200140 baseTypeStr = leafDataTypeToString(leafType.m_type);
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100141 }
142
143 if (typedefName) {
144 ss << *typedefName << " (" << baseTypeStr << ")";
145 } else {
146 ss << baseTypeStr;
147 }
148
Václav Kubernát13b23d72020-04-16 21:49:51 +0200149 if (leafType.m_units) {
150 ss << " [" + *leafType.m_units + "]";
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100151 }
152
153 if (m_datastore.schema()->leafIsKey(path)) {
154 ss << " (key)";
155 }
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200156
157 if (auto defaultValue = m_datastore.schema()->defaultValue(path)) {
158 ss << " default: " << leafDataToString(*defaultValue);
159 }
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100160 break;
161 }
162 case yang::NodeTypes::List:
163 ss << "list";
164 break;
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200165 case yang::NodeTypes::Action:
166 case yang::NodeTypes::AnyXml:
167 case yang::NodeTypes::LeafList:
168 case yang::NodeTypes::Notification:
169 case yang::NodeTypes::Rpc:
170 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 +0100171 }
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200172
173 if (!m_datastore.schema()->isConfig(path)) {
174 ss << " (ro)";
175 }
176
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100177 return ss.str();
178}
179
180void Interpreter::operator()(const describe_& describe) const
181{
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200182 auto path = pathToString(toCanonicalPath(describe.m_path));
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200183 auto status = m_datastore.schema()->status(path);
184 auto statusStr = status == yang::Status::Deprecated ? " (deprecated)" :
185 status == yang::Status::Obsolete ? " (obsolete)" :
186 "";
187
188 std::cout << path << ": " << buildTypeInfo(path) << statusStr << std::endl;
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100189 if (auto description = m_datastore.schema()->description(path)) {
190 std::cout << std::endl << *description << std::endl;
191 }
192}
193
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200194void Interpreter::operator()(const move_& move) const
195{
196 m_datastore.moveItem(pathToDataString(move.m_source, Prefixes::WhenNeeded), move.m_destination);
197}
198
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200199void Interpreter::operator()(const dump_& dump) const
200{
201 std::cout << m_datastore.dump(dump.m_format) << "\n";
202}
203
Václav Kubernát054cc992019-02-21 14:23:52 +0100204struct commandLongHelpVisitor : boost::static_visitor<const char*> {
205 template <typename T>
206 auto constexpr operator()(boost::type<T>) const
207 {
208 return T::longHelp;
209 }
210};
211
212struct commandShortHelpVisitor : boost::static_visitor<const char*> {
213 template <typename T>
214 auto constexpr operator()(boost::type<T>) const
215 {
216 return T::shortHelp;
217 }
218};
219
220void Interpreter::operator()(const help_& help) const
221{
Václav Kubernát3a433232020-07-08 17:52:50 +0200222 if (help.m_cmd) {
Václav Kubernát054cc992019-02-21 14:23:52 +0100223 std::cout << boost::apply_visitor(commandLongHelpVisitor(), help.m_cmd.get()) << std::endl;
Václav Kubernát3a433232020-07-08 17:52:50 +0200224 } else {
Václav Kubernát054cc992019-02-21 14:23:52 +0100225 boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([](auto cmd) {
226 std::cout << commandShortHelpVisitor()(cmd) << std::endl;
227 });
Václav Kubernát3a433232020-07-08 17:52:50 +0200228 }
Václav Kubernát054cc992019-02-21 14:23:52 +0100229}
230
Václav Kubernát7e167692020-06-12 09:53:01 +0200231template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200232boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const boost::optional<PathType>& optPath) const
Václav Kubernát6415b822018-08-22 17:40:01 +0200233{
Václav Kubernát7e167692020-06-12 09:53:01 +0200234 if (!optPath) {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200235 return m_parser.currentPath();
Václav Kubernát7e167692020-06-12 09:53:01 +0200236 }
237 return toCanonicalPath(*optPath);
Václav Kubernát6415b822018-08-22 17:40:01 +0200238}
239
Václav Kubernát7e167692020-06-12 09:53:01 +0200240struct impl_toCanonicalPath {
241 const dataPath_& m_parserPath;
242
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200243 using ReturnType = boost::variant<dataPath_, schemaPath_, module_>;
244
Václav Kubernát7e167692020-06-12 09:53:01 +0200245 impl_toCanonicalPath(const dataPath_& parserPath)
246 : m_parserPath(parserPath)
247 {
248 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200249 ReturnType operator()(const module_& path) const
Václav Kubernátd6247992020-05-27 00:17:56 +0200250 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200251 return path;
Václav Kubernátd6247992020-05-27 00:17:56 +0200252 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200253 ReturnType operator()(const schemaPath_& path) const
Václav Kubernát5c75b252018-10-10 18:33:47 +0200254 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200255 return impl(path);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200256 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200257 ReturnType operator()(const dataPath_& path) const
Václav Kubernát5c75b252018-10-10 18:33:47 +0200258 {
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200259 return impl(path);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200260 }
Václav Kubernát5c75b252018-10-10 18:33:47 +0200261
Václav Kubernát7e167692020-06-12 09:53:01 +0200262private:
263 template <typename PathType>
Václav Kubernát59e4ee42020-07-08 17:32:45 +0200264 [[nodiscard]] ReturnType impl(const PathType& suffix) const
Václav Kubernátd6247992020-05-27 00:17:56 +0200265 {
Václav Kubernát7e167692020-06-12 09:53:01 +0200266 PathType res = [this] {
267 if constexpr (std::is_same<PathType, schemaPath_>()) {
268 return dataPathToSchemaPath(m_parserPath);
269 } else {
270 return m_parserPath;
271 }
272 }();
Václav Kubernátd6247992020-05-27 00:17:56 +0200273
Václav Kubernát7e167692020-06-12 09:53:01 +0200274 if (suffix.m_scope == Scope::Absolute) {
Václav Kubernát59be0de2020-06-15 13:58:45 +0200275 res = {Scope::Absolute, {}};
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200276 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200277
Václav Kubernát7e167692020-06-12 09:53:01 +0200278 for (const auto& fragment : suffix.m_nodes) {
Václav Kubernáte781b902020-06-15 14:35:11 +0200279 res.pushFragment(fragment);
Václav Kubernát7e167692020-06-12 09:53:01 +0200280 }
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200281
Václav Kubernát7e167692020-06-12 09:53:01 +0200282 return res;
283 }
284};
285
286template <typename PathType>
Václav Kubernát4c3d22f2020-06-12 16:05:01 +0200287boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const PathType& path) const
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100288{
Václav Kubernát7e167692020-06-12 09:53:01 +0200289 if constexpr (std::is_same<PathType, dataPath_>()) {
290 return impl_toCanonicalPath(m_parser.currentPath())(path);
291 } else {
292 return boost::apply_visitor(impl_toCanonicalPath(m_parser.currentPath()), path);
293 }
Václav Kubernát9cfcd872020-02-18 12:34:02 +0100294}
295
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200296Interpreter::Interpreter(Parser& parser, ProxyDatastore& datastore)
Václav Kubernát812ee282018-08-30 17:10:03 +0200297 : m_parser(parser)
298 , m_datastore(datastore)
Václav Kubernát96344a12018-05-28 16:33:39 +0200299{
300}