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 | |
| 13 | InvalidNodeException::~InvalidNodeException() = default; |
| 14 | |
| 15 | StaticSchema::StaticSchema() |
| 16 | { |
| 17 | m_nodes.emplace("", std::unordered_map<std::string, NodeType>()); |
| 18 | } |
| 19 | |
| 20 | const std::unordered_map<std::string, NodeType>& StaticSchema::children(const std::string& name) const |
| 21 | { |
| 22 | return m_nodes.at(name); |
| 23 | } |
| 24 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 25 | 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] | 26 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 27 | if (node.empty()) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 28 | return true; |
| 29 | const auto& childrenRef = children(location); |
| 30 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 31 | return childrenRef.find(node) != childrenRef.end(); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 34 | bool StaticSchema::isModule(const schemaPath_&, const std::string& name) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 35 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 36 | return m_modules.find(name) != m_modules.end(); |
| 37 | } |
| 38 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 39 | bool StaticSchema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 40 | { |
| 41 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 42 | auto fullName = fullNodeName(location, node); |
| 43 | if (!nodeExists(locationString, fullName)) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 44 | return false; |
| 45 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 46 | return children(locationString).at(fullName).type() == typeid(yang::container); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void StaticSchema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence) |
| 50 | { |
| 51 | m_nodes.at(location).emplace(name, yang::container{isPresence}); |
| 52 | |
| 53 | //create a new set of children for the new node |
| 54 | std::string key = joinPaths(location, name); |
| 55 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
| 56 | } |
| 57 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 58 | 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] | 59 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 60 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 61 | assert(isList(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 62 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 63 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 64 | const auto& list = boost::get<yang::list>(child); |
| 65 | return list.m_keys.find(key) != list.m_keys.end(); |
| 66 | } |
| 67 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 68 | 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] | 69 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 70 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 71 | assert(isList(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 72 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 73 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 74 | const auto& list = boost::get<yang::list>(child); |
| 75 | return list.m_keys; |
| 76 | } |
| 77 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 78 | bool StaticSchema::isList(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 79 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 80 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 81 | auto fullName = fullNodeName(location, node); |
| 82 | if (!nodeExists(locationString, fullName)) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 83 | return false; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 84 | const auto& child = children(locationString).at(fullName); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 85 | if (child.type() != typeid(yang::list)) |
| 86 | return false; |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys) |
| 92 | { |
| 93 | m_nodes.at(location).emplace(name, yang::list{keys}); |
| 94 | |
Václav Kubernát | 1446fe1 | 2019-10-02 19:32:51 +0200 | [diff] [blame] | 95 | std::string key = joinPaths(location, name); |
| 96 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 99 | bool StaticSchema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 100 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 101 | if (!isContainer(location, node)) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 102 | return false; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 103 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 104 | return boost::get<yang::container>(children(locationString).at(fullNodeName(location, node))).m_presence == yang::ContainerTraits::Presence; |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataTypes& type) |
| 108 | { |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 109 | m_nodes.at(location).emplace(name, yang::leaf{type, {}, {}, {}}); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 110 | std::string key = joinPaths(location, name); |
| 111 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void StaticSchema::addLeafEnum(const std::string& location, const std::string& name, std::set<std::string> enumValues) |
| 115 | { |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 116 | yang::leaf toAdd; |
| 117 | toAdd.m_type = yang::LeafDataTypes::Enum; |
| 118 | toAdd.m_enumValues = enumValues; |
| 119 | m_nodes.at(location).emplace(name, toAdd); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 120 | std::string key = joinPaths(location, name); |
| 121 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void StaticSchema::addLeafIdentityRef(const std::string& location, const std::string& name, const ModuleValuePair& base) |
| 125 | { |
| 126 | assert(base.first); // base identity cannot have an empty module |
| 127 | yang::leaf toAdd; |
| 128 | toAdd.m_type = yang::LeafDataTypes::IdentityRef; |
| 129 | toAdd.m_identBase = base; |
| 130 | m_nodes.at(location).emplace(name, toAdd); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 131 | std::string key = joinPaths(location, name); |
| 132 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 135 | void StaticSchema::addLeafRef(const std::string& location, const std::string& name, const std::string& source) |
| 136 | { |
| 137 | yang::leaf toAdd; |
| 138 | toAdd.m_type = yang::LeafDataTypes::LeafRef; |
| 139 | toAdd.m_leafRefSource = source; |
| 140 | m_nodes.at(location).emplace(name, toAdd); |
Václav Kubernát | e69133a | 2019-11-01 19:01:34 +0100 | [diff] [blame] | 141 | std::string key = joinPaths(location, name); |
| 142 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 145 | void StaticSchema::addModule(const std::string& name) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 146 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 147 | m_modules.emplace(name); |
| 148 | } |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 149 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 150 | void StaticSchema::addIdentity(const std::optional<ModuleValuePair>& base, const ModuleValuePair& name) |
| 151 | { |
| 152 | if (base) |
| 153 | m_identities.at(base.value()).emplace(name); |
| 154 | |
| 155 | m_identities.emplace(name, std::set<ModuleValuePair>()); |
| 156 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 157 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 158 | 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] | 159 | { |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 160 | auto enums = enumValues(location, node); |
| 161 | return enums.find(value) != enums.end(); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 164 | void StaticSchema::getIdentSet(const ModuleValuePair& ident, std::set<ModuleValuePair>& res) const |
| 165 | { |
| 166 | res.insert(ident); |
| 167 | auto derivedIdentities = m_identities.at(ident); |
| 168 | for (auto it : derivedIdentities) { |
| 169 | getIdentSet(it, res); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | const std::set<std::string> StaticSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const |
| 174 | { |
| 175 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 176 | assert(isLeaf(location, node)); |
| 177 | |
| 178 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
| 179 | const auto& leaf = boost::get<yang::leaf>(child); |
| 180 | |
| 181 | std::set<ModuleValuePair> identSet; |
| 182 | getIdentSet(leaf.m_identBase, identSet); |
| 183 | |
| 184 | std::set<std::string> res; |
| 185 | std::transform(identSet.begin(), identSet.end(), std::inserter(res, res.end()), [location, node, prefixes](const auto& it) { |
| 186 | auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name; |
| 187 | std::string stringIdent; |
| 188 | if (prefixes == Prefixes::Always || (it.first && it.first.value() != topLevelModule)) { |
| 189 | stringIdent += it.first ? it.first.value() : topLevelModule; |
| 190 | stringIdent += ":"; |
| 191 | } |
| 192 | stringIdent += it.second; |
| 193 | return stringIdent; |
| 194 | }); |
| 195 | |
| 196 | return res; |
| 197 | } |
| 198 | |
| 199 | bool StaticSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const |
| 200 | { |
| 201 | auto identities = validIdentities(location, node, Prefixes::Always); |
| 202 | |
| 203 | auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name; |
| 204 | auto identModule = value.first ? value.first.value() : topLevelModule; |
Václav Kubernát | 1bf704e | 2019-04-12 13:30:50 +0200 | [diff] [blame] | 205 | 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] | 206 | } |
| 207 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 208 | bool StaticSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 209 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 210 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 211 | auto fullName = fullNodeName(location, node); |
| 212 | if (!nodeExists(locationString, fullName)) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 213 | return false; |
| 214 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 215 | return children(locationString).at(fullName).type() == typeid(yang::leaf); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 218 | std::string lastNodeOfSchemaPath(const std::string& path) |
| 219 | { |
| 220 | std::string res = path; |
| 221 | auto pos = res.find_last_of('/'); |
| 222 | if (pos != res.npos) |
| 223 | res.erase(0, pos); |
| 224 | return res; |
| 225 | } |
| 226 | |
| 227 | yang::LeafDataTypes StaticSchema::leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const |
| 228 | { |
| 229 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 230 | auto leaf{boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node)))}; |
| 231 | auto locationOfSource = stripLastNodeFromPath(leaf.m_leafRefSource); |
| 232 | auto nameOfSource = lastNodeOfSchemaPath(leaf.m_leafRefSource); |
| 233 | return boost::get<yang::leaf>(children(locationOfSource).at(nameOfSource)).m_type; |
| 234 | } |
| 235 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 236 | 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] | 237 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 238 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 239 | return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type; |
| 240 | } |
| 241 | |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 242 | const std::set<std::string> StaticSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const |
| 243 | { |
| 244 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 245 | assert(isLeaf(location, node)); |
| 246 | |
| 247 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
| 248 | const auto& leaf = boost::get<yang::leaf>(child); |
| 249 | return leaf.m_enumValues; |
| 250 | } |
| 251 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 252 | // We do not test StaticSchema, so we don't need to implement recursive childNodes |
| 253 | // for this class. |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 254 | 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] | 255 | { |
| 256 | std::string locationString = pathToAbsoluteSchemaString(path); |
| 257 | std::set<std::string> res; |
| 258 | |
| 259 | auto childrenRef = children(locationString); |
| 260 | |
| 261 | std::transform(childrenRef.begin(), childrenRef.end(), |
| 262 | std::inserter(res, res.end()), |
| 263 | [] (auto it) { return it.first; }); |
| 264 | return res; |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 265 | } |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 266 | |
| 267 | // We do not test StaticSchema, so we don't need to implement recursive moduleNodes |
| 268 | // for this class. |
| 269 | std::set<std::string> StaticSchema::moduleNodes(const module_& module, const Recursion) const |
| 270 | { |
| 271 | std::set<std::string> res; |
| 272 | auto topLevelNodes = m_nodes.at(""); |
| 273 | auto modulePlusColon = module.m_name + ":"; |
| 274 | for (const auto& it : topLevelNodes) { |
| 275 | if (boost::algorithm::starts_with(it.first, modulePlusColon)) { |
| 276 | res.insert(it.first); |
| 277 | } |
| 278 | } |
| 279 | return res; |
| 280 | } |