blob: 9be7751d70e4813bf7d8639738c75ec9c029b507 [file] [log] [blame]
Václav Kubernátbddbb172018-06-13 16:27:39 +02001/*
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
12InvalidNodeException::~InvalidNodeException() = default;
13
14StaticSchema::StaticSchema()
15{
16 m_nodes.emplace("", std::unordered_map<std::string, NodeType>());
17}
18
Václav Kubernát744f57f2018-06-29 22:46:26 +020019
Václav Kubernátbddbb172018-06-13 16:27:39 +020020const 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át744f57f2018-06-29 22:46:26 +020025bool StaticSchema::nodeExists(const std::string& location, const std::string& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020026{
Václav Kubernát744f57f2018-06-29 22:46:26 +020027 if (node.empty())
Václav Kubernátbddbb172018-06-13 16:27:39 +020028 return true;
29 const auto& childrenRef = children(location);
30
Václav Kubernát744f57f2018-06-29 22:46:26 +020031 return childrenRef.find(node) != childrenRef.end();
Václav Kubernátbddbb172018-06-13 16:27:39 +020032}
33
Václav Kubernát2eaceb82018-10-08 19:56:30 +020034bool StaticSchema::isModule(const schemaPath_&, const std::string& name) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020035{
Václav Kubernát744f57f2018-06-29 22:46:26 +020036 return m_modules.find(name) != m_modules.end();
37}
38
Václav Kubernát2eaceb82018-10-08 19:56:30 +020039bool StaticSchema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát744f57f2018-06-29 22:46:26 +020040{
41 std::string locationString = pathToAbsoluteSchemaString(location);
42 auto fullName = fullNodeName(location, node);
43 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +020044 return false;
45
Václav Kubernát744f57f2018-06-29 22:46:26 +020046 return children(locationString).at(fullName).type() == typeid(yang::container);
Václav Kubernátbddbb172018-06-13 16:27:39 +020047}
48
49void 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át2eaceb82018-10-08 19:56:30 +020058bool StaticSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020059{
Václav Kubernát744f57f2018-06-29 22:46:26 +020060 std::string locationString = pathToAbsoluteSchemaString(location);
61 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020062
Václav Kubernát744f57f2018-06-29 22:46:26 +020063 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020064 const auto& list = boost::get<yang::list>(child);
65 return list.m_keys.find(key) != list.m_keys.end();
66}
67
Václav Kubernát2eaceb82018-10-08 19:56:30 +020068const std::set<std::string> StaticSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020069{
Václav Kubernát744f57f2018-06-29 22:46:26 +020070 std::string locationString = pathToAbsoluteSchemaString(location);
71 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020072
Václav Kubernát744f57f2018-06-29 22:46:26 +020073 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020074 const auto& list = boost::get<yang::list>(child);
75 return list.m_keys;
76}
77
Václav Kubernát2eaceb82018-10-08 19:56:30 +020078bool StaticSchema::isList(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020079{
Václav Kubernát744f57f2018-06-29 22:46:26 +020080 std::string locationString = pathToAbsoluteSchemaString(location);
81 auto fullName = fullNodeName(location, node);
82 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +020083 return false;
Václav Kubernát744f57f2018-06-29 22:46:26 +020084 const auto& child = children(locationString).at(fullName);
Václav Kubernátbddbb172018-06-13 16:27:39 +020085 if (child.type() != typeid(yang::list))
86 return false;
87
88 return true;
89}
90
91void 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át2eaceb82018-10-08 19:56:30 +020098bool StaticSchema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020099{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200100 if (!isContainer(location, node))
Václav Kubernátbddbb172018-06-13 16:27:39 +0200101 return false;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200102 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átbddbb172018-06-13 16:27:39 +0200104}
105
106void 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
111void 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át744f57f2018-06-29 22:46:26 +0200116void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +0200117{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200118 m_modules.emplace(name);
119}
Václav Kubernátbddbb172018-06-13 16:27:39 +0200120
Václav Kubernát744f57f2018-06-29 22:46:26 +0200121
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200122bool StaticSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200123{
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átbddbb172018-06-13 16:27:39 +0200128 const auto& list = boost::get<yang::leaf>(child);
129 return list.m_enumValues.find(value) != list.m_enumValues.end();
130}
131
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200132bool StaticSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200133{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200134 std::string locationString = pathToAbsoluteSchemaString(location);
135 auto fullName = fullNodeName(location, node);
136 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +0200137 return false;
138
Václav Kubernát744f57f2018-06-29 22:46:26 +0200139 return children(locationString).at(fullName).type() == typeid(yang::leaf);
Václav Kubernátbddbb172018-06-13 16:27:39 +0200140}
141
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200142yang::LeafDataTypes StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200143{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200144 std::string locationString = pathToAbsoluteSchemaString(location);
145 return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type;
146}
147
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200148// We do not test StaticSchema, so we don't need to implement recursive childNodes
149// for this class.
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200150std::set<std::string> StaticSchema::childNodes(const schemaPath_& path, const Recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200151{
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átbddbb172018-06-13 16:27:39 +0200161}