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 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 19 | |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 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 | |
| 95 | m_nodes.emplace(name, std::unordered_map<std::string, NodeType>()); |
| 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 | { |
| 108 | m_nodes.at(location).emplace(name, yang::leaf{type, {}}); |
| 109 | } |
| 110 | |
| 111 | void StaticSchema::addLeafEnum(const std::string& location, const std::string& name, std::set<std::string> enumValues) |
| 112 | { |
| 113 | m_nodes.at(location).emplace(name, yang::leaf{yang::LeafDataTypes::Enum, enumValues}); |
| 114 | } |
| 115 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 116 | void StaticSchema::addModule(const std::string& name) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 117 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 118 | m_modules.emplace(name); |
| 119 | } |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 120 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 121 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame^] | 122 | 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] | 123 | { |
| 124 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 125 | assert(isLeaf(location, node)); |
| 126 | |
| 127 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 128 | const auto& list = boost::get<yang::leaf>(child); |
| 129 | return list.m_enumValues.find(value) != list.m_enumValues.end(); |
| 130 | } |
| 131 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame^] | 132 | bool StaticSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 133 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 134 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 135 | auto fullName = fullNodeName(location, node); |
| 136 | if (!nodeExists(locationString, fullName)) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 137 | return false; |
| 138 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 139 | return children(locationString).at(fullName).type() == typeid(yang::leaf); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame^] | 142 | 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] | 143 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 144 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 145 | return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type; |
| 146 | } |
| 147 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 148 | // We do not test StaticSchema, so we don't need to implement recursive childNodes |
| 149 | // for this class. |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame^] | 150 | 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] | 151 | { |
| 152 | std::string locationString = pathToAbsoluteSchemaString(path); |
| 153 | std::set<std::string> res; |
| 154 | |
| 155 | auto childrenRef = children(locationString); |
| 156 | |
| 157 | std::transform(childrenRef.begin(), childrenRef.end(), |
| 158 | std::inserter(res, res.end()), |
| 159 | [] (auto it) { return it.first; }); |
| 160 | return res; |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 161 | } |