Václav Kubernát | bddbb17 | 2018-06-13 16:27: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 | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 9 | #include <boost/algorithm/string/predicate.hpp> |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 10 | #include "static_schema.hpp" |
| 11 | #include "utils.hpp" |
| 12 | |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 13 | StaticSchema::StaticSchema() |
| 14 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 15 | m_nodes.emplace("/", std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | const std::unordered_map<std::string, NodeType>& StaticSchema::children(const std::string& name) const |
| 19 | { |
| 20 | return m_nodes.at(name); |
| 21 | } |
| 22 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 23 | bool StaticSchema::nodeExists(const std::string& location, const std::string& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 24 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 25 | if (node.empty()) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 26 | return true; |
| 27 | const auto& childrenRef = children(location); |
| 28 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 29 | return childrenRef.find(node) != childrenRef.end(); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 30 | } |
| 31 | |
Václav Kubernát | 75877de | 2019-11-20 17:43:02 +0100 | [diff] [blame] | 32 | bool StaticSchema::isModule(const std::string& name) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 33 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 34 | return m_modules.find(name) != m_modules.end(); |
| 35 | } |
| 36 | |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 37 | void StaticSchema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence) |
| 38 | { |
| 39 | m_nodes.at(location).emplace(name, yang::container{isPresence}); |
| 40 | |
| 41 | //create a new set of children for the new node |
| 42 | std::string key = joinPaths(location, name); |
| 43 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
| 44 | } |
| 45 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 46 | bool StaticSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 47 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 48 | std::string locationString = pathToSchemaString(location, Prefixes::Always); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 49 | assert(isList(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 50 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 51 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 52 | const auto& list = boost::get<yang::list>(child); |
| 53 | return list.m_keys.find(key) != list.m_keys.end(); |
| 54 | } |
| 55 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 56 | const std::set<std::string> StaticSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 57 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 58 | std::string locationString = pathToSchemaString(location, Prefixes::Always); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 59 | assert(isList(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 60 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 61 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 62 | const auto& list = boost::get<yang::list>(child); |
| 63 | return list.m_keys; |
| 64 | } |
| 65 | |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 66 | void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys) |
| 67 | { |
| 68 | m_nodes.at(location).emplace(name, yang::list{keys}); |
| 69 | |
Václav Kubernát | 1446fe1 | 2019-10-02 19:32:51 +0200 | [diff] [blame] | 70 | std::string key = joinPaths(location, name); |
| 71 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 74 | std::set<identityRef_> StaticSchema::validIdentities(std::string_view module, std::string_view value) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 75 | { |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 76 | std::set<ModuleValuePair> identities; |
| 77 | getIdentSet(ModuleNodePair{boost::optional<std::string>{module}, value}, identities); |
| 78 | std::set<identityRef_> res; |
| 79 | |
| 80 | std::transform(identities.begin(), identities.end(), std::inserter(res, res.end()), [](const auto& identity) { |
| 81 | return identityRef_{*identity.first, identity.second}; |
| 82 | }); |
| 83 | |
| 84 | return res; |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 87 | void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataType& type) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 88 | { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 89 | m_nodes.at(location).emplace(name, yang::leaf{yang::TypeInfo{type, std::nullopt}}); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 90 | std::string key = joinPaths(location, name); |
| 91 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 94 | void StaticSchema::addModule(const std::string& name) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 95 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 96 | m_modules.emplace(name); |
| 97 | } |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 98 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 99 | void StaticSchema::addIdentity(const std::optional<ModuleValuePair>& base, const ModuleValuePair& name) |
| 100 | { |
| 101 | if (base) |
| 102 | m_identities.at(base.value()).emplace(name); |
| 103 | |
| 104 | m_identities.emplace(name, std::set<ModuleValuePair>()); |
| 105 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 106 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 107 | void StaticSchema::getIdentSet(const ModuleValuePair& ident, std::set<ModuleValuePair>& res) const |
| 108 | { |
| 109 | res.insert(ident); |
| 110 | auto derivedIdentities = m_identities.at(ident); |
| 111 | for (auto it : derivedIdentities) { |
| 112 | getIdentSet(it, res); |
| 113 | } |
| 114 | } |
| 115 | |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 116 | std::string lastNodeOfSchemaPath(const std::string& path) |
| 117 | { |
| 118 | std::string res = path; |
Václav Kubernát | 2d7b05c | 2020-04-05 14:02:37 +0200 | [diff] [blame] | 119 | if (auto pos = res.find_last_of('/'); pos != res.npos) { |
| 120 | res.erase(0, pos + 1); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 121 | } |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 122 | return res; |
| 123 | } |
| 124 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 125 | yang::TypeInfo StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 126 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 127 | std::string locationString = pathToSchemaString(location, Prefixes::Always); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 128 | return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type; |
| 129 | } |
| 130 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 131 | yang::TypeInfo StaticSchema::leafType(const std::string& path) const |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 132 | { |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 133 | auto locationString = stripLastNodeFromPath(path); |
| 134 | auto node = lastNodeOfSchemaPath(path); |
| 135 | return boost::get<yang::leaf>(children(locationString).at(node)).m_type; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 138 | std::set<std::string> StaticSchema::availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 139 | { |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 140 | if (recursion == Recursion::Recursive) { |
| 141 | throw std::logic_error("Recursive StaticSchema::availableNodes is not implemented. It shouldn't be used in tests."); |
| 142 | } |
| 143 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 144 | std::set<std::string> res; |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 145 | if (path.type() == typeid(module_)) { |
| 146 | auto topLevelNodes = m_nodes.at(""); |
| 147 | auto modulePlusColon = boost::get<module_>(path).m_name + ":"; |
| 148 | for (const auto& it : topLevelNodes) { |
| 149 | if (boost::algorithm::starts_with(it.first, modulePlusColon)) { |
| 150 | res.insert(it.first); |
| 151 | } |
| 152 | } |
| 153 | return res; |
| 154 | } |
| 155 | |
| 156 | std::string locationString = |
| 157 | path.type() == typeid(schemaPath_) ? |
| 158 | pathToSchemaString(boost::get<schemaPath_>(path), Prefixes::Always) : |
| 159 | pathToSchemaString(boost::get<dataPath_>(path), Prefixes::Always); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 160 | |
| 161 | auto childrenRef = children(locationString); |
| 162 | |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 163 | std::transform(childrenRef.begin(), childrenRef.end(), std::inserter(res, res.end()), [](auto it) { return it.first; }); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 164 | return res; |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 165 | } |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 166 | |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 167 | yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const |
| 168 | { |
| 169 | std::string locationString = pathToSchemaString(location, Prefixes::Always); |
| 170 | auto fullName = fullNodeName(location, node); |
| 171 | try { |
| 172 | auto targetNode = children(locationString).at(fullName); |
| 173 | |
| 174 | if (targetNode.type() == typeid(yang::container)) { |
| 175 | if (boost::get<yang::container>(targetNode).m_presence == yang::ContainerTraits::Presence) { |
| 176 | return yang::NodeTypes::PresenceContainer; |
| 177 | } |
| 178 | return yang::NodeTypes::Container; |
| 179 | } |
| 180 | |
| 181 | if (targetNode.type() == typeid(yang::list)) { |
| 182 | return yang::NodeTypes::List; |
| 183 | } |
| 184 | |
| 185 | if (targetNode.type() == typeid(yang::leaf)) { |
| 186 | return yang::NodeTypes::Leaf; |
| 187 | } |
| 188 | |
| 189 | throw std::runtime_error{"YangSchema::nodeType: unsupported type"}; |
| 190 | |
| 191 | } catch (std::out_of_range&) { |
| 192 | throw InvalidNodeException(); |
| 193 | } |
| 194 | } |
| 195 | |
Václav Kubernát | 1e09bd6 | 2020-02-17 15:13:38 +0100 | [diff] [blame] | 196 | std::optional<std::string> StaticSchema::description([[maybe_unused]] const std::string& path) const |
| 197 | { |
| 198 | throw std::runtime_error{"StaticSchema::description not implemented"}; |
| 199 | } |
| 200 | |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 201 | yang::Status StaticSchema::status([[maybe_unused]] const std::string& location) const |
| 202 | { |
| 203 | throw std::runtime_error{"Internal error: StaticSchema::status(std::string) not implemented. The tests should not have called this overload."}; |
| 204 | } |
| 205 | |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 206 | yang::NodeTypes StaticSchema::nodeType([[maybe_unused]] const std::string& path) const |
| 207 | { |
| 208 | throw std::runtime_error{"Internal error: StaticSchema::nodeType(std::string) not implemented. The tests should not have called this overload."}; |
| 209 | } |
Václav Kubernát | ad87ece | 2020-02-19 15:20:56 +0100 | [diff] [blame] | 210 | |
Václav Kubernát | bd5e3c2 | 2020-02-19 15:22:00 +0100 | [diff] [blame] | 211 | std::string StaticSchema::leafrefPath([[maybe_unused]] const std::string& leafrefPath) const |
| 212 | { |
| 213 | throw std::runtime_error{"Internal error: StaticSchema::leafrefPath(std::string) not implemented. The tests should not have called this overload."}; |
| 214 | } |
Václav Kubernát | c386679 | 2020-02-20 14:12:56 +0100 | [diff] [blame] | 215 | |
| 216 | bool StaticSchema::leafIsKey([[maybe_unused]] const std::string& leafPath) const |
| 217 | { |
| 218 | throw std::runtime_error{"Internal error: StaticSchema::leafIsKey(std::string) not implemented. The tests should not have called this overload."}; |
| 219 | } |
Václav Kubernát | 6fcd028 | 2020-02-21 16:33:08 +0100 | [diff] [blame] | 220 | |
| 221 | std::optional<std::string> StaticSchema::leafTypeName([[maybe_unused]] const std::string& path) const |
| 222 | { |
| 223 | throw std::runtime_error{"Internal error: StaticSchema::leafTypeName(std::string) not implemented. The tests should not have called this overload."}; |
| 224 | } |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 225 | |
| 226 | bool StaticSchema::isConfig([[maybe_unused]] const std::string& leafPath) const |
| 227 | { |
| 228 | throw std::runtime_error{"Internal error: StaticSchema::isConfigLeaf(std::string) not implemented. The tests should not have called this overload."}; |
| 229 | } |
Václav Kubernát | b1a75c6 | 2020-04-21 15:20:16 +0200 | [diff] [blame] | 230 | |
| 231 | std::optional<std::string> StaticSchema::defaultValue([[maybe_unused]] const std::string& leafPath) const |
| 232 | { |
| 233 | throw std::runtime_error{"Internal error: StaticSchema::defaultValue(std::string) not implemented. The tests should not have called this overload."}; |
| 234 | } |