Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +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 | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 9 | #include "schema.hpp" |
| 10 | #include "utils.hpp" |
| 11 | |
| 12 | InvalidNodeException::~InvalidNodeException() = default; |
| 13 | |
Václav Kubernát | f2e463f | 2018-05-28 15:51:08 +0200 | [diff] [blame] | 14 | struct nodeToSchemaString : public boost::static_visitor<std::string> { |
| 15 | std::string operator()(const nodeup_&) const |
| 16 | { |
| 17 | return ".."; |
| 18 | } |
| 19 | template <class T> |
| 20 | std::string operator()(const T& node) const |
| 21 | { |
| 22 | return node.m_name; |
| 23 | } |
| 24 | }; |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 25 | |
| 26 | std::string pathToString(const path_& path) |
| 27 | { |
| 28 | std::string res; |
| 29 | for (const auto it : path.m_nodes) |
Václav Kubernát | f2e463f | 2018-05-28 15:51:08 +0200 | [diff] [blame] | 30 | res = joinPaths(res, boost::apply_visitor(nodeToSchemaString(), it)); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 31 | return res; |
| 32 | } |
| 33 | |
| 34 | Schema::Schema() |
| 35 | { |
| 36 | m_nodes.emplace("", std::unordered_map<std::string, NodeType>()); |
| 37 | } |
| 38 | |
| 39 | const std::unordered_map<std::string, NodeType>& Schema::children(const std::string& name) const |
| 40 | { |
| 41 | return m_nodes.at(name); |
| 42 | } |
| 43 | |
| 44 | bool Schema::nodeExists(const std::string& location, const std::string& name) const |
| 45 | { |
| 46 | if (name.empty()) |
| 47 | return true; |
| 48 | const auto& childrenRef = children(location); |
| 49 | |
| 50 | return childrenRef.find(name) != childrenRef.end(); |
| 51 | } |
| 52 | |
| 53 | bool Schema::isContainer(const path_& location, const std::string& name) const |
| 54 | { |
| 55 | std::string locationString = pathToString(location); |
| 56 | if (!nodeExists(locationString, name)) |
| 57 | return false; |
| 58 | |
| 59 | return children(locationString).at(name).type() == typeid(yang::container); |
| 60 | } |
| 61 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 62 | void Schema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence) |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 63 | { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 64 | m_nodes.at(location).emplace(name, yang::container{isPresence}); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 65 | |
| 66 | //create a new set of children for the new node |
| 67 | std::string key = joinPaths(location, name); |
| 68 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | bool Schema::listHasKey(const path_& location, const std::string& name, const std::string& key) const |
| 73 | { |
| 74 | std::string locationString = pathToString(location); |
| 75 | assert(isList(location, name)); |
| 76 | |
| 77 | const auto& child = children(locationString).at(name); |
| 78 | const auto& list = boost::get<yang::list>(child); |
| 79 | return list.m_keys.find(key) != list.m_keys.end(); |
| 80 | } |
| 81 | |
| 82 | const std::set<std::string>& Schema::listKeys(const path_& location, const std::string& name) const |
| 83 | { |
| 84 | std::string locationString = pathToString(location); |
| 85 | assert(isList(location, name)); |
| 86 | |
| 87 | const auto& child = children(locationString).at(name); |
| 88 | const auto& list = boost::get<yang::list>(child); |
| 89 | return list.m_keys; |
| 90 | } |
| 91 | |
| 92 | bool Schema::isList(const path_& location, const std::string& name) const |
| 93 | { |
| 94 | std::string locationString = pathToString(location); |
| 95 | if (!nodeExists(locationString, name)) |
| 96 | return false; |
| 97 | const auto& child = children(locationString).at(name); |
| 98 | if (child.type() != typeid(yang::list)) |
| 99 | return false; |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | void Schema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys) |
| 105 | { |
| 106 | m_nodes.at(location).emplace(name, yang::list{keys}); |
| 107 | |
| 108 | m_nodes.emplace(name, std::unordered_map<std::string, NodeType>()); |
| 109 | } |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 110 | |
| 111 | bool Schema::isPresenceContainer(const path_& location, const std::string& name) const |
| 112 | { |
| 113 | if (!isContainer(location, name)) |
| 114 | return false; |
| 115 | std::string locationString = pathToString(location); |
| 116 | return boost::get<yang::container>(children(locationString).at(name)).m_presence == yang::ContainerTraits::Presence; |
| 117 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame^] | 118 | |
| 119 | void Schema::addLeaf(const std::string& location, const std::string& name) |
| 120 | { |
| 121 | m_nodes.at(location).emplace(name, yang::leaf{}); |
| 122 | } |
| 123 | |
| 124 | bool Schema::isLeaf(const path_& location, const std::string& name) const |
| 125 | { |
| 126 | std::string locationString = pathToString(location); |
| 127 | if (!nodeExists(locationString, name)) |
| 128 | return false; |
| 129 | |
| 130 | return children(locationString).at(name).type() == typeid(yang::leaf); |
| 131 | } |