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