blob: 86dc6fabc6b8a0b0ecd24de7f97eba164ebfb802 [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át744f57f2018-06-29 22:46:26 +020034bool StaticSchema::isModule(const path_&, 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
39bool 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á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
58
Václav Kubernát744f57f2018-06-29 22:46:26 +020059bool StaticSchema::listHasKey(const path_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020060{
Václav Kubernát744f57f2018-06-29 22:46:26 +020061 std::string locationString = pathToAbsoluteSchemaString(location);
62 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020063
Václav Kubernát744f57f2018-06-29 22:46:26 +020064 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020065 const auto& list = boost::get<yang::list>(child);
66 return list.m_keys.find(key) != list.m_keys.end();
67}
68
Václav Kubernát744f57f2018-06-29 22:46:26 +020069const std::set<std::string>& StaticSchema::listKeys(const path_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020070{
Václav Kubernát744f57f2018-06-29 22:46:26 +020071 std::string locationString = pathToAbsoluteSchemaString(location);
72 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020073
Václav Kubernát744f57f2018-06-29 22:46:26 +020074 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020075 const auto& list = boost::get<yang::list>(child);
76 return list.m_keys;
77}
78
Václav Kubernát744f57f2018-06-29 22:46:26 +020079bool StaticSchema::isList(const path_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020080{
Václav Kubernát744f57f2018-06-29 22:46:26 +020081 std::string locationString = pathToAbsoluteSchemaString(location);
82 auto fullName = fullNodeName(location, node);
83 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +020084 return false;
Václav Kubernát744f57f2018-06-29 22:46:26 +020085 const auto& child = children(locationString).at(fullName);
Václav Kubernátbddbb172018-06-13 16:27:39 +020086 if (child.type() != typeid(yang::list))
87 return false;
88
89 return true;
90}
91
92void 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át744f57f2018-06-29 22:46:26 +020099bool StaticSchema::isPresenceContainer(const path_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200100{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200101 if (!isContainer(location, node))
Václav Kubernátbddbb172018-06-13 16:27:39 +0200102 return false;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200103 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átbddbb172018-06-13 16:27:39 +0200105}
106
107void 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
112void 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át744f57f2018-06-29 22:46:26 +0200117void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +0200118{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200119 m_modules.emplace(name);
120}
Václav Kubernátbddbb172018-06-13 16:27:39 +0200121
Václav Kubernát744f57f2018-06-29 22:46:26 +0200122
123bool 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átbddbb172018-06-13 16:27:39 +0200129 const auto& list = boost::get<yang::leaf>(child);
130 return list.m_enumValues.find(value) != list.m_enumValues.end();
131}
132
Václav Kubernát744f57f2018-06-29 22:46:26 +0200133bool StaticSchema::isLeaf(const path_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200134{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200135 std::string locationString = pathToAbsoluteSchemaString(location);
136 auto fullName = fullNodeName(location, node);
137 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +0200138 return false;
139
Václav Kubernát744f57f2018-06-29 22:46:26 +0200140 return children(locationString).at(fullName).type() == typeid(yang::leaf);
Václav Kubernátbddbb172018-06-13 16:27:39 +0200141}
142
Václav Kubernát744f57f2018-06-29 22:46:26 +0200143yang::LeafDataTypes StaticSchema::leafType(const path_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200144{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200145 std::string locationString = pathToAbsoluteSchemaString(location);
146 return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type;
147}
148
149std::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átbddbb172018-06-13 16:27:39 +0200160}