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 | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 34 | bool StaticSchema::isModule(const path_&, 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 | |
| 39 | bool StaticSchema::isContainer(const path_& location, const ModuleNodePair& node) const |
| 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 | |
| 58 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 59 | bool StaticSchema::listHasKey(const path_& location, const ModuleNodePair& node, const std::string& key) const |
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 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 62 | assert(isList(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 63 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 64 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 65 | const auto& list = boost::get<yang::list>(child); |
| 66 | return list.m_keys.find(key) != list.m_keys.end(); |
| 67 | } |
| 68 | |
Václav Kubernát | 76e983c | 2018-08-06 13:56:03 +0200 | [diff] [blame] | 69 | const std::set<std::string> StaticSchema::listKeys(const path_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 70 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 71 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 72 | assert(isList(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 73 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 74 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 75 | const auto& list = boost::get<yang::list>(child); |
| 76 | return list.m_keys; |
| 77 | } |
| 78 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 79 | bool StaticSchema::isList(const path_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 80 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 81 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 82 | auto fullName = fullNodeName(location, node); |
| 83 | if (!nodeExists(locationString, fullName)) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 84 | return false; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 85 | const auto& child = children(locationString).at(fullName); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 86 | if (child.type() != typeid(yang::list)) |
| 87 | return false; |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys) |
| 93 | { |
| 94 | m_nodes.at(location).emplace(name, yang::list{keys}); |
| 95 | |
| 96 | m_nodes.emplace(name, std::unordered_map<std::string, NodeType>()); |
| 97 | } |
| 98 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 99 | bool StaticSchema::isPresenceContainer(const path_& 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 | { |
| 109 | m_nodes.at(location).emplace(name, yang::leaf{type, {}}); |
| 110 | } |
| 111 | |
| 112 | void StaticSchema::addLeafEnum(const std::string& location, const std::string& name, std::set<std::string> enumValues) |
| 113 | { |
| 114 | m_nodes.at(location).emplace(name, yang::leaf{yang::LeafDataTypes::Enum, enumValues}); |
| 115 | } |
| 116 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 117 | void StaticSchema::addModule(const std::string& name) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 118 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 119 | m_modules.emplace(name); |
| 120 | } |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 121 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 122 | |
| 123 | bool StaticSchema::leafEnumHasValue(const path_& location, const ModuleNodePair& node, const std::string& value) const |
| 124 | { |
| 125 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 126 | assert(isLeaf(location, node)); |
| 127 | |
| 128 | const auto& child = children(locationString).at(fullNodeName(location, node)); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 129 | const auto& list = boost::get<yang::leaf>(child); |
| 130 | return list.m_enumValues.find(value) != list.m_enumValues.end(); |
| 131 | } |
| 132 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 133 | bool StaticSchema::isLeaf(const path_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 134 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 135 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 136 | auto fullName = fullNodeName(location, node); |
| 137 | if (!nodeExists(locationString, fullName)) |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 138 | return false; |
| 139 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 140 | return children(locationString).at(fullName).type() == typeid(yang::leaf); |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 143 | yang::LeafDataTypes StaticSchema::leafType(const path_& location, const ModuleNodePair& node) const |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 144 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 145 | std::string locationString = pathToAbsoluteSchemaString(location); |
| 146 | return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type; |
| 147 | } |
| 148 | |
| 149 | std::set<std::string> StaticSchema::childNodes(const path_& path) const |
| 150 | { |
| 151 | std::string locationString = pathToAbsoluteSchemaString(path); |
| 152 | std::set<std::string> res; |
| 153 | |
| 154 | auto childrenRef = children(locationString); |
| 155 | |
| 156 | std::transform(childrenRef.begin(), childrenRef.end(), |
| 157 | std::inserter(res, res.end()), |
| 158 | [] (auto it) { return it.first; }); |
| 159 | return res; |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 160 | } |