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 | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 74 | void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataTypes& type) |
| 75 | { |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 76 | m_nodes.at(location).emplace(name, yang::leaf{type, {}, {}, {}}); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 77 | std::string key = joinPaths(location, name); |
| 78 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void StaticSchema::addLeafEnum(const std::string& location, const std::string& name, std::set<std::string> enumValues) |
| 82 | { |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 83 | yang::leaf toAdd; |
| 84 | toAdd.m_type = yang::LeafDataTypes::Enum; |
| 85 | toAdd.m_enumValues = enumValues; |
| 86 | m_nodes.at(location).emplace(name, toAdd); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 87 | std::string key = joinPaths(location, name); |
| 88 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void StaticSchema::addLeafIdentityRef(const std::string& location, const std::string& name, const ModuleValuePair& base) |
| 92 | { |
| 93 | assert(base.first); // base identity cannot have an empty module |
| 94 | yang::leaf toAdd; |
| 95 | toAdd.m_type = yang::LeafDataTypes::IdentityRef; |
| 96 | toAdd.m_identBase = base; |
| 97 | m_nodes.at(location).emplace(name, toAdd); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 98 | std::string key = joinPaths(location, name); |
| 99 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 102 | void StaticSchema::addLeafRef(const std::string& location, const std::string& name, const std::string& source) |
| 103 | { |
| 104 | yang::leaf toAdd; |
| 105 | toAdd.m_type = yang::LeafDataTypes::LeafRef; |
| 106 | toAdd.m_leafRefSource = source; |
| 107 | m_nodes.at(location).emplace(name, toAdd); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 108 | std::string key = joinPaths(location, name); |
| 109 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 112 | void StaticSchema::addModule(const std::string& name) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 113 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 114 | m_modules.emplace(name); |
| 115 | } |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 116 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 117 | void StaticSchema::addIdentity(const std::optional<ModuleValuePair>& base, const ModuleValuePair& name) |
| 118 | { |
| 119 | if (base) |
| 120 | m_identities.at(base.value()).emplace(name); |
| 121 | |
| 122 | m_identities.emplace(name, std::set<ModuleValuePair>()); |
| 123 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 124 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 125 | bool StaticSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 126 | { |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 127 | auto enums = enumValues(location, node); |
| 128 | return enums.find(value) != enums.end(); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 131 | void StaticSchema::getIdentSet(const ModuleValuePair& ident, std::set<ModuleValuePair>& res) const |
| 132 | { |
| 133 | res.insert(ident); |
| 134 | auto derivedIdentities = m_identities.at(ident); |
| 135 | for (auto it : derivedIdentities) { |
| 136 | getIdentSet(it, res); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | const std::set<std::string> StaticSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const |
| 141 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 142 | std::string locationString = pathToSchemaString(location, Prefixes::Always); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 143 | assert(isLeaf(location, node)); |
| 144 | |
| 145 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
| 146 | const auto& leaf = boost::get<yang::leaf>(child); |
| 147 | |
| 148 | std::set<ModuleValuePair> identSet; |
| 149 | getIdentSet(leaf.m_identBase, identSet); |
| 150 | |
| 151 | std::set<std::string> res; |
| 152 | std::transform(identSet.begin(), identSet.end(), std::inserter(res, res.end()), [location, node, prefixes](const auto& it) { |
| 153 | auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name; |
| 154 | std::string stringIdent; |
| 155 | if (prefixes == Prefixes::Always || (it.first && it.first.value() != topLevelModule)) { |
| 156 | stringIdent += it.first ? it.first.value() : topLevelModule; |
| 157 | stringIdent += ":"; |
| 158 | } |
| 159 | stringIdent += it.second; |
| 160 | return stringIdent; |
| 161 | }); |
| 162 | |
| 163 | return res; |
| 164 | } |
| 165 | |
| 166 | bool StaticSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const |
| 167 | { |
| 168 | auto identities = validIdentities(location, node, Prefixes::Always); |
| 169 | |
| 170 | auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name; |
| 171 | auto identModule = value.first ? value.first.value() : topLevelModule; |
Václav Kubernát | 1bf704e | 2019-04-12 13:30:50 +0200 | [diff] [blame] | 172 | return std::any_of(identities.begin(), identities.end(), [toFind = identModule + ":" + value.second](const auto& x) { return x == toFind; }); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 175 | std::string lastNodeOfSchemaPath(const std::string& path) |
| 176 | { |
| 177 | std::string res = path; |
| 178 | auto pos = res.find_last_of('/'); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 179 | if (pos == 0) { // path had only one path fragment - "/something:something" |
| 180 | res.erase(0, 1); |
| 181 | return res; |
| 182 | } |
| 183 | if (pos != res.npos) { // path had more fragments |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 184 | res.erase(0, pos); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 185 | return res; |
| 186 | } |
| 187 | |
| 188 | // path was empty |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 189 | return res; |
| 190 | } |
| 191 | |
Václav Kubernát | f0fe769 | 2020-02-19 14:39:47 +0100 | [diff] [blame] | 192 | yang::LeafDataTypes StaticSchema::leafrefBaseType(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 193 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 194 | std::string locationString = pathToSchemaString(location, Prefixes::Always); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 195 | auto leaf{boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node)))}; |
| 196 | auto locationOfSource = stripLastNodeFromPath(leaf.m_leafRefSource); |
| 197 | auto nameOfSource = lastNodeOfSchemaPath(leaf.m_leafRefSource); |
| 198 | return boost::get<yang::leaf>(children(locationOfSource).at(nameOfSource)).m_type; |
| 199 | } |
| 200 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 201 | yang::LeafDataTypes StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 202 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 203 | std::string locationString = pathToSchemaString(location, Prefixes::Always); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 204 | return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type; |
| 205 | } |
| 206 | |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 207 | yang::LeafDataTypes StaticSchema::leafType([[maybe_unused]] const std::string& path) const |
| 208 | { |
| 209 | throw std::runtime_error{"StaticSchema::leafType not implemented"}; |
| 210 | } |
| 211 | |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 212 | const std::set<std::string> StaticSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const |
| 213 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 214 | std::string locationString = pathToSchemaString(location, Prefixes::Always); |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 215 | assert(isLeaf(location, node)); |
| 216 | |
| 217 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
| 218 | const auto& leaf = boost::get<yang::leaf>(child); |
| 219 | return leaf.m_enumValues; |
| 220 | } |
| 221 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 222 | // We do not test StaticSchema, so we don't need to implement recursive childNodes |
| 223 | // for this class. |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 224 | std::set<std::string> StaticSchema::childNodes(const schemaPath_& path, const Recursion) const |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 225 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 226 | std::string locationString = pathToSchemaString(path, Prefixes::Always); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 227 | std::set<std::string> res; |
| 228 | |
| 229 | auto childrenRef = children(locationString); |
| 230 | |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 231 | 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] | 232 | return res; |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 233 | } |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 234 | |
| 235 | // We do not test StaticSchema, so we don't need to implement recursive moduleNodes |
| 236 | // for this class. |
| 237 | std::set<std::string> StaticSchema::moduleNodes(const module_& module, const Recursion) const |
| 238 | { |
| 239 | std::set<std::string> res; |
| 240 | auto topLevelNodes = m_nodes.at(""); |
| 241 | auto modulePlusColon = module.m_name + ":"; |
| 242 | for (const auto& it : topLevelNodes) { |
| 243 | if (boost::algorithm::starts_with(it.first, modulePlusColon)) { |
| 244 | res.insert(it.first); |
| 245 | } |
| 246 | } |
| 247 | return res; |
| 248 | } |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 249 | |
| 250 | yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const |
| 251 | { |
| 252 | std::string locationString = pathToSchemaString(location, Prefixes::Always); |
| 253 | auto fullName = fullNodeName(location, node); |
| 254 | try { |
| 255 | auto targetNode = children(locationString).at(fullName); |
| 256 | |
| 257 | if (targetNode.type() == typeid(yang::container)) { |
| 258 | if (boost::get<yang::container>(targetNode).m_presence == yang::ContainerTraits::Presence) { |
| 259 | return yang::NodeTypes::PresenceContainer; |
| 260 | } |
| 261 | return yang::NodeTypes::Container; |
| 262 | } |
| 263 | |
| 264 | if (targetNode.type() == typeid(yang::list)) { |
| 265 | return yang::NodeTypes::List; |
| 266 | } |
| 267 | |
| 268 | if (targetNode.type() == typeid(yang::leaf)) { |
| 269 | return yang::NodeTypes::Leaf; |
| 270 | } |
| 271 | |
| 272 | throw std::runtime_error{"YangSchema::nodeType: unsupported type"}; |
| 273 | |
| 274 | } catch (std::out_of_range&) { |
| 275 | throw InvalidNodeException(); |
| 276 | } |
| 277 | } |
| 278 | |
Václav Kubernát | 1e09bd6 | 2020-02-17 15:13:38 +0100 | [diff] [blame] | 279 | std::optional<std::string> StaticSchema::description([[maybe_unused]] const std::string& path) const |
| 280 | { |
| 281 | throw std::runtime_error{"StaticSchema::description not implemented"}; |
| 282 | } |
| 283 | |
| 284 | std::optional<std::string> StaticSchema::units([[maybe_unused]] const std::string& path) const |
| 285 | { |
| 286 | throw std::runtime_error{"StaticSchema::units not implemented"}; |
| 287 | } |
| 288 | |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 289 | yang::NodeTypes StaticSchema::nodeType([[maybe_unused]] const std::string& path) const |
| 290 | { |
| 291 | throw std::runtime_error{"Internal error: StaticSchema::nodeType(std::string) not implemented. The tests should not have called this overload."}; |
| 292 | } |
Václav Kubernát | ad87ece | 2020-02-19 15:20:56 +0100 | [diff] [blame^] | 293 | |
| 294 | yang::LeafDataTypes StaticSchema::leafrefBaseType([[maybe_unused]] const std::string& path) const |
| 295 | { |
| 296 | throw std::runtime_error{"Internal error: StaticSchema::leafrefBaseType(std::string) not implemented. The tests should not have called this overload."}; |
| 297 | } |