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 | |
| 9 | #include <iostream> |
| 10 | #include "schema.hpp" |
| 11 | #include "utils.hpp" |
| 12 | |
| 13 | InvalidNodeException::~InvalidNodeException() = default; |
| 14 | |
| 15 | |
| 16 | std::string pathToString(const path_& path) |
| 17 | { |
| 18 | std::string res; |
| 19 | for (const auto it : path.m_nodes) |
| 20 | res = joinPaths(res, boost::apply_visitor(nodeToString(), it)); |
| 21 | return res; |
| 22 | } |
| 23 | |
| 24 | Schema::Schema() |
| 25 | { |
| 26 | m_nodes.emplace("", std::unordered_map<std::string, NodeType>()); |
| 27 | } |
| 28 | |
| 29 | const std::unordered_map<std::string, NodeType>& Schema::children(const std::string& name) const |
| 30 | { |
| 31 | return m_nodes.at(name); |
| 32 | } |
| 33 | |
| 34 | bool Schema::nodeExists(const std::string& location, const std::string& name) const |
| 35 | { |
| 36 | if (name.empty()) |
| 37 | return true; |
| 38 | const auto& childrenRef = children(location); |
| 39 | |
| 40 | return childrenRef.find(name) != childrenRef.end(); |
| 41 | } |
| 42 | |
| 43 | bool Schema::isContainer(const path_& location, const std::string& name) const |
| 44 | { |
| 45 | std::string locationString = pathToString(location); |
| 46 | if (!nodeExists(locationString, name)) |
| 47 | return false; |
| 48 | |
| 49 | return children(locationString).at(name).type() == typeid(yang::container); |
| 50 | } |
| 51 | |
| 52 | void Schema::addContainer(const std::string& location, const std::string& name) |
| 53 | { |
| 54 | m_nodes.at(location).emplace(name, yang::container{}); |
| 55 | |
| 56 | //create a new set of children for the new node |
| 57 | std::string key = joinPaths(location, name); |
| 58 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | bool Schema::listHasKey(const path_& location, const std::string& name, const std::string& key) const |
| 63 | { |
| 64 | std::string locationString = pathToString(location); |
| 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.find(key) != list.m_keys.end(); |
| 70 | } |
| 71 | |
| 72 | const std::set<std::string>& Schema::listKeys(const path_& location, const std::string& name) 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; |
| 80 | } |
| 81 | |
| 82 | bool Schema::isList(const path_& location, const std::string& name) const |
| 83 | { |
| 84 | std::string locationString = pathToString(location); |
| 85 | if (!nodeExists(locationString, name)) |
| 86 | return false; |
| 87 | const auto& child = children(locationString).at(name); |
| 88 | if (child.type() != typeid(yang::list)) |
| 89 | return false; |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | void Schema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys) |
| 95 | { |
| 96 | m_nodes.at(location).emplace(name, yang::list{keys}); |
| 97 | |
| 98 | m_nodes.emplace(name, std::unordered_map<std::string, NodeType>()); |
| 99 | } |