Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 1 | /* |
| 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át | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 9 | #include <boost/algorithm/string/predicate.hpp> |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 10 | #include <boost/mpl/for_each.hpp> |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 11 | #include <iostream> |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 12 | #include <sstream> |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 13 | #include "datastore_access.hpp" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 14 | #include "interpreter.hpp" |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 15 | #include "utils.hpp" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 16 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 17 | struct 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 | |
| 33 | template <typename PathType> |
| 34 | std::string pathToString(const PathType& path) |
| 35 | { |
| 36 | return boost::apply_visitor(pathToStringVisitor(), path); |
| 37 | } |
| 38 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 39 | void Interpreter::operator()(const commit_&) const |
| 40 | { |
| 41 | m_datastore.commitChanges(); |
| 42 | } |
| 43 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 44 | void Interpreter::operator()(const discard_&) const |
| 45 | { |
| 46 | m_datastore.discardChanges(); |
| 47 | } |
| 48 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 49 | void Interpreter::operator()(const set_& set) const |
| 50 | { |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 51 | 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át | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 61 | m_datastore.setLeaf(pathToString(toCanonicalPath(set.m_path)), data); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 64 | void Interpreter::operator()(const get_& get) const |
| 65 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 66 | auto items = m_datastore.getItems(pathToString(toCanonicalPath(get.m_path))); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 67 | 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át | fb4bda6 | 2020-06-09 12:54:41 +0200 | [diff] [blame] | 72 | |
| 73 | while (it + 1 != items.end() && boost::starts_with((it + 1) ->first, leafListPrefix)) { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 74 | ++it; |
Václav Kubernát | fb4bda6 | 2020-06-09 12:54:41 +0200 | [diff] [blame] | 75 | std::cout << stripLeafListValueFromPath(it->first) << " = " << leafDataToString(it->second) << std::endl; |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 76 | } |
| 77 | } else { |
| 78 | std::cout << path << " = " << leafDataToString(value) << std::endl; |
| 79 | } |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 83 | void Interpreter::operator()(const cd_& cd) const |
| 84 | { |
| 85 | m_parser.changeNode(cd.m_path); |
| 86 | } |
| 87 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 88 | void Interpreter::operator()(const create_& create) const |
| 89 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 90 | m_datastore.createItem(pathToString(toCanonicalPath(create.m_path))); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void Interpreter::operator()(const delete_& delet) const |
| 94 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 95 | m_datastore.deleteItem(pathToString(toCanonicalPath(delet.m_path))); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 98 | void Interpreter::operator()(const ls_& ls) const |
| 99 | { |
| 100 | std::cout << "Possible nodes:" << std::endl; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 101 | auto recursion{Recursion::NonRecursive}; |
| 102 | for (auto it : ls.m_options) { |
| 103 | if (it == LsOption::Recursive) |
| 104 | recursion = Recursion::Recursive; |
| 105 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 106 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 107 | auto toPrint = m_datastore.schema()->availableNodes(toCanonicalPath(ls.m_path), recursion); |
Václav Kubernát | 8208687 | 2020-04-29 01:09:50 +0200 | [diff] [blame] | 108 | |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 109 | for (const auto& it : toPrint) { |
| 110 | std::cout << (it.first ? *it.first + ":" : "" ) + it.second << std::endl; |
| 111 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 114 | void Interpreter::operator()(const copy_& copy) const |
| 115 | { |
| 116 | m_datastore.copyConfig(copy.m_source, copy.m_destination); |
| 117 | } |
| 118 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 119 | std::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át | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 134 | if (std::holds_alternative<yang::LeafRef>(leafType.m_type)) { |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 135 | ss << "-> "; |
| 136 | ss << m_datastore.schema()->leafrefPath(path) << " "; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 137 | baseTypeStr = leafDataTypeToString(std::get<yang::LeafRef>(leafType.m_type).m_targetType->m_type); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 138 | } else { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 139 | baseTypeStr = leafDataTypeToString(leafType.m_type); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | if (typedefName) { |
| 143 | ss << *typedefName << " (" << baseTypeStr << ")"; |
| 144 | } else { |
| 145 | ss << baseTypeStr; |
| 146 | } |
| 147 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 148 | if (leafType.m_units) { |
| 149 | ss << " [" + *leafType.m_units + "]"; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (m_datastore.schema()->leafIsKey(path)) { |
| 153 | ss << " (key)"; |
| 154 | } |
Václav Kubernát | b1a75c6 | 2020-04-21 15:20:16 +0200 | [diff] [blame] | 155 | |
| 156 | if (auto defaultValue = m_datastore.schema()->defaultValue(path)) { |
| 157 | ss << " default: " << leafDataToString(*defaultValue); |
| 158 | } |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 159 | break; |
| 160 | } |
| 161 | case yang::NodeTypes::List: |
| 162 | ss << "list"; |
| 163 | break; |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 164 | 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át | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 170 | } |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 171 | |
| 172 | if (!m_datastore.schema()->isConfig(path)) { |
| 173 | ss << " (ro)"; |
| 174 | } |
| 175 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 176 | return ss.str(); |
| 177 | } |
| 178 | |
| 179 | void Interpreter::operator()(const describe_& describe) const |
| 180 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 181 | auto path = pathToString(toCanonicalPath(describe.m_path)); |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 182 | 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át | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 188 | if (auto description = m_datastore.schema()->description(path)) { |
| 189 | std::cout << std::endl << *description << std::endl; |
| 190 | } |
| 191 | } |
| 192 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 193 | void 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át | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 198 | void Interpreter::operator()(const dump_& dump) const |
| 199 | { |
| 200 | std::cout << m_datastore.dump(dump.m_format) << "\n"; |
| 201 | } |
| 202 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 203 | struct commandLongHelpVisitor : boost::static_visitor<const char*> { |
| 204 | template <typename T> |
| 205 | auto constexpr operator()(boost::type<T>) const |
| 206 | { |
| 207 | return T::longHelp; |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | struct commandShortHelpVisitor : boost::static_visitor<const char*> { |
| 212 | template <typename T> |
| 213 | auto constexpr operator()(boost::type<T>) const |
| 214 | { |
| 215 | return T::shortHelp; |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | void Interpreter::operator()(const help_& help) const |
| 220 | { |
| 221 | if (help.m_cmd) |
| 222 | std::cout << boost::apply_visitor(commandLongHelpVisitor(), help.m_cmd.get()) << std::endl; |
| 223 | else |
| 224 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([](auto cmd) { |
| 225 | std::cout << commandShortHelpVisitor()(cmd) << std::endl; |
| 226 | }); |
| 227 | } |
| 228 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 229 | template <typename PathType> |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 230 | boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const boost::optional<PathType>& optPath) const |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 231 | { |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 232 | if (!optPath) { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 233 | return m_parser.currentPath(); |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 234 | } |
| 235 | return toCanonicalPath(*optPath); |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 238 | struct impl_toCanonicalPath { |
| 239 | const dataPath_& m_parserPath; |
| 240 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 241 | using ReturnType = boost::variant<dataPath_, schemaPath_, module_>; |
| 242 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 243 | impl_toCanonicalPath(const dataPath_& parserPath) |
| 244 | : m_parserPath(parserPath) |
| 245 | { |
| 246 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 247 | ReturnType operator()(const module_& path) const |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 248 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 249 | return path; |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 250 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 251 | ReturnType operator()(const schemaPath_& path) const |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 252 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 253 | return impl(path); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 254 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 255 | ReturnType operator()(const dataPath_& path) const |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 256 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 257 | return impl(path); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 258 | } |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 259 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 260 | private: |
| 261 | template <typename PathType> |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 262 | ReturnType impl(const PathType& suffix) const |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 263 | { |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 264 | PathType res = [this] { |
| 265 | if constexpr (std::is_same<PathType, schemaPath_>()) { |
| 266 | return dataPathToSchemaPath(m_parserPath); |
| 267 | } else { |
| 268 | return m_parserPath; |
| 269 | } |
| 270 | }(); |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 271 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 272 | if (suffix.m_scope == Scope::Absolute) { |
Václav Kubernát | 59be0de | 2020-06-15 13:58:45 +0200 | [diff] [blame] | 273 | res = {Scope::Absolute, {}}; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 274 | } |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 275 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 276 | for (const auto& fragment : suffix.m_nodes) { |
Václav Kubernát | e781b90 | 2020-06-15 14:35:11 +0200 | [diff] [blame] | 277 | res.pushFragment(fragment); |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 278 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 279 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 280 | return res; |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | template <typename PathType> |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 285 | boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const PathType& path) const |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 286 | { |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 287 | if constexpr (std::is_same<PathType, dataPath_>()) { |
| 288 | return impl_toCanonicalPath(m_parser.currentPath())(path); |
| 289 | } else { |
| 290 | return boost::apply_visitor(impl_toCanonicalPath(m_parser.currentPath()), path); |
| 291 | } |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 294 | Interpreter::Interpreter(Parser& parser, DatastoreAccess& datastore) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 295 | : m_parser(parser) |
| 296 | , m_datastore(datastore) |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 297 | { |
| 298 | } |