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 | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 14 | Schema::Schema() |
| 15 | { |
| 16 | m_nodes.emplace("", std::unordered_map<std::string, NodeType>()); |
| 17 | } |
| 18 | |
| 19 | const std::unordered_map<std::string, NodeType>& Schema::children(const std::string& name) const |
| 20 | { |
| 21 | return m_nodes.at(name); |
| 22 | } |
| 23 | |
| 24 | bool Schema::nodeExists(const std::string& location, const std::string& name) const |
| 25 | { |
| 26 | if (name.empty()) |
| 27 | return true; |
| 28 | const auto& childrenRef = children(location); |
| 29 | |
| 30 | return childrenRef.find(name) != childrenRef.end(); |
| 31 | } |
| 32 | |
| 33 | bool Schema::isContainer(const path_& location, const std::string& name) const |
| 34 | { |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame^] | 35 | std::string locationString = pathToSchemaString(location); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 36 | if (!nodeExists(locationString, name)) |
| 37 | return false; |
| 38 | |
| 39 | return children(locationString).at(name).type() == typeid(yang::container); |
| 40 | } |
| 41 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 42 | 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] | 43 | { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 44 | m_nodes.at(location).emplace(name, yang::container{isPresence}); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 45 | |
| 46 | //create a new set of children for the new node |
| 47 | std::string key = joinPaths(location, name); |
| 48 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | bool Schema::listHasKey(const path_& location, const std::string& name, const std::string& key) const |
| 53 | { |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame^] | 54 | std::string locationString = pathToSchemaString(location); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 55 | assert(isList(location, name)); |
| 56 | |
| 57 | const auto& child = children(locationString).at(name); |
| 58 | const auto& list = boost::get<yang::list>(child); |
| 59 | return list.m_keys.find(key) != list.m_keys.end(); |
| 60 | } |
| 61 | |
| 62 | const std::set<std::string>& Schema::listKeys(const path_& location, const std::string& name) const |
| 63 | { |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame^] | 64 | std::string locationString = pathToSchemaString(location); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 65 | assert(isList(location, name)); |
| 66 | |
| 67 | const auto& child = children(locationString).at(name); |
| 68 | const auto& list = boost::get<yang::list>(child); |
| 69 | return list.m_keys; |
| 70 | } |
| 71 | |
| 72 | bool Schema::isList(const path_& location, const std::string& name) const |
| 73 | { |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame^] | 74 | std::string locationString = pathToSchemaString(location); |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 75 | if (!nodeExists(locationString, name)) |
| 76 | return false; |
| 77 | const auto& child = children(locationString).at(name); |
| 78 | if (child.type() != typeid(yang::list)) |
| 79 | return false; |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | void Schema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys) |
| 85 | { |
| 86 | m_nodes.at(location).emplace(name, yang::list{keys}); |
| 87 | |
| 88 | m_nodes.emplace(name, std::unordered_map<std::string, NodeType>()); |
| 89 | } |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 90 | |
| 91 | bool Schema::isPresenceContainer(const path_& location, const std::string& name) const |
| 92 | { |
| 93 | if (!isContainer(location, name)) |
| 94 | return false; |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame^] | 95 | std::string locationString = pathToSchemaString(location); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 96 | return boost::get<yang::container>(children(locationString).at(name)).m_presence == yang::ContainerTraits::Presence; |
| 97 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 98 | |
| 99 | void Schema::addLeaf(const std::string& location, const std::string& name) |
| 100 | { |
| 101 | m_nodes.at(location).emplace(name, yang::leaf{}); |
| 102 | } |
| 103 | |
| 104 | bool Schema::isLeaf(const path_& location, const std::string& name) const |
| 105 | { |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame^] | 106 | std::string locationString = pathToSchemaString(location); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 107 | if (!nodeExists(locationString, name)) |
| 108 | return false; |
| 109 | |
| 110 | return children(locationString).at(name).type() == typeid(yang::leaf); |
| 111 | } |