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 | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 17 | void Interpreter::operator()(const commit_&) const |
| 18 | { |
| 19 | m_datastore.commitChanges(); |
| 20 | } |
| 21 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 22 | void Interpreter::operator()(const discard_&) const |
| 23 | { |
| 24 | m_datastore.discardChanges(); |
| 25 | } |
| 26 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 27 | void Interpreter::operator()(const set_& set) const |
| 28 | { |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 29 | auto data = set.m_data; |
| 30 | |
| 31 | // If the user didn't supply a module prefix for identityref, we need to add it ourselves |
| 32 | if (data.type() == typeid(identityRef_)) { |
| 33 | auto identityRef = boost::get<identityRef_>(data); |
| 34 | if (!identityRef.m_prefix) { |
| 35 | identityRef.m_prefix = set.m_path.m_nodes.front().m_prefix.value(); |
| 36 | data = identityRef; |
| 37 | } |
| 38 | } |
| 39 | m_datastore.setLeaf(absolutePathFromCommand(set), data); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 42 | void Interpreter::operator()(const get_& get) const |
| 43 | { |
| 44 | auto items = m_datastore.getItems(absolutePathFromCommand(get)); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 45 | for (auto it = items.begin(); it != items.end(); it++) { |
| 46 | auto [path, value] = *it; |
| 47 | if (value.type() == typeid(special_) && boost::get<special_>(value).m_value == SpecialValue::LeafList) { |
| 48 | auto leafListPrefix = path; |
| 49 | std::cout << path << " = " << leafDataToString(value) << std::endl; |
| 50 | ++it; |
| 51 | while (boost::starts_with(it->first, leafListPrefix)) { |
| 52 | std::cout << stripLeafListValueFromPath(it->first) << " = " << leafDataToString(it->second) << std::endl; |
| 53 | ++it; |
| 54 | } |
| 55 | } else { |
| 56 | std::cout << path << " = " << leafDataToString(value) << std::endl; |
| 57 | } |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 61 | void Interpreter::operator()(const cd_& cd) const |
| 62 | { |
| 63 | m_parser.changeNode(cd.m_path); |
| 64 | } |
| 65 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 66 | void Interpreter::operator()(const create_& create) const |
| 67 | { |
Václav Kubernát | be22862 | 2019-05-23 14:44:12 +0200 | [diff] [blame] | 68 | if (create.m_path.m_nodes.back().m_suffix.type() == typeid(listElement_)) |
| 69 | m_datastore.createListInstance(absolutePathFromCommand(create)); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 70 | else if (create.m_path.m_nodes.back().m_suffix.type() == typeid(leafListElement_)) |
| 71 | m_datastore.createLeafListInstance(absolutePathFromCommand(create)); |
Václav Kubernát | be22862 | 2019-05-23 14:44:12 +0200 | [diff] [blame] | 72 | else |
| 73 | m_datastore.createPresenceContainer(absolutePathFromCommand(create)); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void Interpreter::operator()(const delete_& delet) const |
| 77 | { |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 78 | if (delet.m_path.m_nodes.back().m_suffix.type() == typeid(container_)) |
| 79 | m_datastore.deletePresenceContainer(absolutePathFromCommand(delet)); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 80 | else if (delet.m_path.m_nodes.back().m_suffix.type() == typeid(leafListElement_)) |
| 81 | m_datastore.deleteLeafListInstance(absolutePathFromCommand(delet)); |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 82 | else |
| 83 | m_datastore.deleteListInstance(absolutePathFromCommand(delet)); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 86 | void Interpreter::operator()(const ls_& ls) const |
| 87 | { |
| 88 | std::cout << "Possible nodes:" << std::endl; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 89 | auto recursion{Recursion::NonRecursive}; |
| 90 | for (auto it : ls.m_options) { |
| 91 | if (it == LsOption::Recursive) |
| 92 | recursion = Recursion::Recursive; |
| 93 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 94 | |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 95 | std::set<ModuleNodePair> toPrint; |
Václav Kubernát | 8208687 | 2020-04-29 01:09:50 +0200 | [diff] [blame] | 96 | |
| 97 | auto pathArg = dataPathToSchemaPath(m_parser.currentPath()); |
| 98 | if (ls.m_path) { |
| 99 | if (ls.m_path->type() == typeid(module_)) { |
| 100 | toPrint = m_datastore.schema()->availableNodes(*ls.m_path, recursion); |
| 101 | } else { |
| 102 | auto schemaPath = anyPathToSchemaPath(*ls.m_path); |
| 103 | if (schemaPath.m_scope == Scope::Absolute) { |
| 104 | pathArg = schemaPath; |
| 105 | } else { |
| 106 | pathArg.m_nodes.insert(pathArg.m_nodes.end(), schemaPath.m_nodes.begin(), schemaPath.m_nodes.end()); |
| 107 | } |
| 108 | toPrint = m_datastore.schema()->availableNodes(pathArg, recursion); |
| 109 | } |
| 110 | } else { |
| 111 | toPrint = m_datastore.schema()->availableNodes(pathArg, recursion); |
| 112 | } |
| 113 | |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 114 | for (const auto& it : toPrint) { |
| 115 | std::cout << (it.first ? *it.first + ":" : "" ) + it.second << std::endl; |
| 116 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 119 | void Interpreter::operator()(const copy_& copy) const |
| 120 | { |
| 121 | m_datastore.copyConfig(copy.m_source, copy.m_destination); |
| 122 | } |
| 123 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 124 | std::string Interpreter::buildTypeInfo(const std::string& path) const |
| 125 | { |
| 126 | std::ostringstream ss; |
| 127 | switch (m_datastore.schema()->nodeType(path)) { |
| 128 | case yang::NodeTypes::Container: |
| 129 | ss << "container"; |
| 130 | break; |
| 131 | case yang::NodeTypes::PresenceContainer: |
| 132 | ss << "presence container"; |
| 133 | break; |
| 134 | case yang::NodeTypes::Leaf: |
| 135 | { |
| 136 | auto leafType = m_datastore.schema()->leafType(path); |
| 137 | auto typedefName = m_datastore.schema()->leafTypeName(path); |
| 138 | std::string baseTypeStr; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 139 | if (std::holds_alternative<yang::LeafRef>(leafType.m_type)) { |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 140 | ss << "-> "; |
| 141 | ss << m_datastore.schema()->leafrefPath(path) << " "; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 142 | 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] | 143 | } else { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 144 | baseTypeStr = leafDataTypeToString(leafType.m_type); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | if (typedefName) { |
| 148 | ss << *typedefName << " (" << baseTypeStr << ")"; |
| 149 | } else { |
| 150 | ss << baseTypeStr; |
| 151 | } |
| 152 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 153 | if (leafType.m_units) { |
| 154 | ss << " [" + *leafType.m_units + "]"; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (m_datastore.schema()->leafIsKey(path)) { |
| 158 | ss << " (key)"; |
| 159 | } |
Václav Kubernát | b1a75c6 | 2020-04-21 15:20:16 +0200 | [diff] [blame] | 160 | |
| 161 | if (auto defaultValue = m_datastore.schema()->defaultValue(path)) { |
| 162 | ss << " default: " << leafDataToString(*defaultValue); |
| 163 | } |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | case yang::NodeTypes::List: |
| 167 | ss << "list"; |
| 168 | break; |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 169 | case yang::NodeTypes::Action: |
| 170 | case yang::NodeTypes::AnyXml: |
| 171 | case yang::NodeTypes::LeafList: |
| 172 | case yang::NodeTypes::Notification: |
| 173 | case yang::NodeTypes::Rpc: |
| 174 | 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] | 175 | } |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 176 | |
| 177 | if (!m_datastore.schema()->isConfig(path)) { |
| 178 | ss << " (ro)"; |
| 179 | } |
| 180 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 181 | return ss.str(); |
| 182 | } |
| 183 | |
| 184 | void Interpreter::operator()(const describe_& describe) const |
| 185 | { |
| 186 | auto path = absolutePathFromCommand(describe); |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 187 | auto status = m_datastore.schema()->status(path); |
| 188 | auto statusStr = status == yang::Status::Deprecated ? " (deprecated)" : |
| 189 | status == yang::Status::Obsolete ? " (obsolete)" : |
| 190 | ""; |
| 191 | |
| 192 | std::cout << path << ": " << buildTypeInfo(path) << statusStr << std::endl; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 193 | if (auto description = m_datastore.schema()->description(path)) { |
| 194 | std::cout << std::endl << *description << std::endl; |
| 195 | } |
| 196 | } |
| 197 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 198 | struct 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 | |
| 206 | struct 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 | |
| 214 | void 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át | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 224 | template <typename T> |
| 225 | std::string Interpreter::absolutePathFromCommand(const T& command) const |
| 226 | { |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 227 | if (command.m_path.m_scope == Scope::Absolute) |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 228 | return pathToDataString(command.m_path, Prefixes::WhenNeeded); |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 229 | else |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 230 | return joinPaths(m_parser.currentNode(), pathToDataString(command.m_path, Prefixes::WhenNeeded)); |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 233 | struct pathToStringVisitor : boost::static_visitor<std::string> { |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame^] | 234 | std::string operator()(const module_& path) const |
| 235 | { |
| 236 | using namespace std::string_literals; |
| 237 | return "/"s + boost::get<module_>(path).m_name + ":*"; |
| 238 | } |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 239 | std::string operator()(const schemaPath_& path) const |
| 240 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 241 | return pathToSchemaString(path, Prefixes::WhenNeeded); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 242 | } |
| 243 | std::string operator()(const dataPath_& path) const |
| 244 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 245 | return pathToDataString(path, Prefixes::WhenNeeded); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 246 | } |
| 247 | }; |
| 248 | |
| 249 | struct getPathScopeVisitor : boost::static_visitor<Scope> { |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame^] | 250 | Scope operator()(const module_&) const |
| 251 | { |
| 252 | throw std::logic_error("Interpreter: a top-level module has no scope."); |
| 253 | } |
| 254 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 255 | template <typename T> |
| 256 | Scope operator()(const T& path) const |
| 257 | { |
| 258 | return path.m_scope; |
| 259 | } |
| 260 | }; |
| 261 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 262 | std::string Interpreter::absolutePathFromCommand(const get_& get) const |
| 263 | { |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 264 | using namespace std::string_literals; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 265 | if (!get.m_path) { |
| 266 | return m_parser.currentNode(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | const auto path = *get.m_path; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 270 | if (path.type() == typeid(module_)) { |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame^] | 271 | return boost::apply_visitor(pathToStringVisitor(), path); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 272 | } else { |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame^] | 273 | std::string pathString = boost::apply_visitor(pathToStringVisitor(), path); |
| 274 | auto pathScope{boost::apply_visitor(getPathScopeVisitor(), path)}; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 275 | |
| 276 | if (pathScope == Scope::Absolute) { |
Václav Kubernát | 82684cf | 2020-01-06 13:30:37 +0100 | [diff] [blame] | 277 | return pathString; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 278 | } else { |
| 279 | return joinPaths(m_parser.currentNode(), pathString); |
| 280 | } |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 284 | std::string Interpreter::absolutePathFromCommand(const describe_& describe) const |
| 285 | { |
| 286 | auto pathStr = boost::apply_visitor(pathToStringVisitor(), describe.m_path); |
| 287 | if (boost::apply_visitor(getPathScopeVisitor(), describe.m_path) == Scope::Absolute) |
| 288 | return pathStr; |
| 289 | else |
| 290 | return joinPaths(m_parser.currentNode(), pathStr); |
| 291 | } |
| 292 | |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 293 | Interpreter::Interpreter(Parser& parser, DatastoreAccess& datastore) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 294 | : m_parser(parser) |
| 295 | , m_datastore(datastore) |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 296 | { |
| 297 | } |