blob: a3f59aa64cfda9ed2a22e8bf4234f8af650bf01a [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
Václav Kubernát9456b5c2019-10-02 21:14:52 +02009#include <boost/algorithm/string/predicate.hpp>
Václav Kubernátbddbb172018-06-13 16:27:39 +020010#include "static_schema.hpp"
11#include "utils.hpp"
12
13InvalidNodeException::~InvalidNodeException() = default;
14
15StaticSchema::StaticSchema()
16{
17 m_nodes.emplace("", std::unordered_map<std::string, NodeType>());
18}
19
20const 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
Václav Kubernát1446fe12019-10-02 19:32:51 +020095 std::string key = joinPaths(location, name);
96 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020097}
98
Václav Kubernát2eaceb82018-10-08 19:56:30 +020099bool StaticSchema::isPresenceContainer(const schemaPath_& 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{
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200109 m_nodes.at(location).emplace(name, yang::leaf{type, {}, {}, {}});
Václav Kubernátbddbb172018-06-13 16:27:39 +0200110}
111
112void StaticSchema::addLeafEnum(const std::string& location, const std::string& name, std::set<std::string> enumValues)
113{
Václav Kubernáteeb38842019-03-20 19:46:05 +0100114 yang::leaf toAdd;
115 toAdd.m_type = yang::LeafDataTypes::Enum;
116 toAdd.m_enumValues = enumValues;
117 m_nodes.at(location).emplace(name, toAdd);
118}
119
120void StaticSchema::addLeafIdentityRef(const std::string& location, const std::string& name, const ModuleValuePair& base)
121{
122 assert(base.first); // base identity cannot have an empty module
123 yang::leaf toAdd;
124 toAdd.m_type = yang::LeafDataTypes::IdentityRef;
125 toAdd.m_identBase = base;
126 m_nodes.at(location).emplace(name, toAdd);
Václav Kubernátbddbb172018-06-13 16:27:39 +0200127}
128
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200129void StaticSchema::addLeafRef(const std::string& location, const std::string& name, const std::string& source)
130{
131 yang::leaf toAdd;
132 toAdd.m_type = yang::LeafDataTypes::LeafRef;
133 toAdd.m_leafRefSource = source;
134 m_nodes.at(location).emplace(name, toAdd);
135}
136
Václav Kubernát744f57f2018-06-29 22:46:26 +0200137void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +0200138{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200139 m_modules.emplace(name);
140}
Václav Kubernátbddbb172018-06-13 16:27:39 +0200141
Václav Kubernáteeb38842019-03-20 19:46:05 +0100142void StaticSchema::addIdentity(const std::optional<ModuleValuePair>& base, const ModuleValuePair& name)
143{
144 if (base)
145 m_identities.at(base.value()).emplace(name);
146
147 m_identities.emplace(name, std::set<ModuleValuePair>());
148}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200149
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200150bool StaticSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200151{
Václav Kubernát989b5de2019-02-20 16:28:35 +0100152 auto enums = enumValues(location, node);
153 return enums.find(value) != enums.end();
Václav Kubernátbddbb172018-06-13 16:27:39 +0200154}
155
Václav Kubernáteeb38842019-03-20 19:46:05 +0100156void StaticSchema::getIdentSet(const ModuleValuePair& ident, std::set<ModuleValuePair>& res) const
157{
158 res.insert(ident);
159 auto derivedIdentities = m_identities.at(ident);
160 for (auto it : derivedIdentities) {
161 getIdentSet(it, res);
162 }
163}
164
165const std::set<std::string> StaticSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const
166{
167 std::string locationString = pathToAbsoluteSchemaString(location);
168 assert(isLeaf(location, node));
169
170 const auto& child = children(locationString).at(fullNodeName(location, node));
171 const auto& leaf = boost::get<yang::leaf>(child);
172
173 std::set<ModuleValuePair> identSet;
174 getIdentSet(leaf.m_identBase, identSet);
175
176 std::set<std::string> res;
177 std::transform(identSet.begin(), identSet.end(), std::inserter(res, res.end()), [location, node, prefixes](const auto& it) {
178 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
179 std::string stringIdent;
180 if (prefixes == Prefixes::Always || (it.first && it.first.value() != topLevelModule)) {
181 stringIdent += it.first ? it.first.value() : topLevelModule;
182 stringIdent += ":";
183 }
184 stringIdent += it.second;
185 return stringIdent;
186 });
187
188 return res;
189}
190
191bool StaticSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const
192{
193 auto identities = validIdentities(location, node, Prefixes::Always);
194
195 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
196 auto identModule = value.first ? value.first.value() : topLevelModule;
Václav Kubernát1bf704e2019-04-12 13:30:50 +0200197 return std::any_of(identities.begin(), identities.end(), [toFind = identModule + ":" + value.second](const auto& x) { return x == toFind; });
Václav Kubernáteeb38842019-03-20 19:46:05 +0100198}
199
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200200bool StaticSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200201{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200202 std::string locationString = pathToAbsoluteSchemaString(location);
203 auto fullName = fullNodeName(location, node);
204 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +0200205 return false;
206
Václav Kubernát744f57f2018-06-29 22:46:26 +0200207 return children(locationString).at(fullName).type() == typeid(yang::leaf);
Václav Kubernátbddbb172018-06-13 16:27:39 +0200208}
209
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200210std::string lastNodeOfSchemaPath(const std::string& path)
211{
212 std::string res = path;
213 auto pos = res.find_last_of('/');
214 if (pos != res.npos)
215 res.erase(0, pos);
216 return res;
217}
218
219yang::LeafDataTypes StaticSchema::leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const
220{
221 std::string locationString = pathToAbsoluteSchemaString(location);
222 auto leaf{boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node)))};
223 auto locationOfSource = stripLastNodeFromPath(leaf.m_leafRefSource);
224 auto nameOfSource = lastNodeOfSchemaPath(leaf.m_leafRefSource);
225 return boost::get<yang::leaf>(children(locationOfSource).at(nameOfSource)).m_type;
226}
227
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200228yang::LeafDataTypes StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200229{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200230 std::string locationString = pathToAbsoluteSchemaString(location);
231 return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type;
232}
233
Václav Kubernát989b5de2019-02-20 16:28:35 +0100234const std::set<std::string> StaticSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const
235{
236 std::string locationString = pathToAbsoluteSchemaString(location);
237 assert(isLeaf(location, node));
238
239 const auto& child = children(locationString).at(fullNodeName(location, node));
240 const auto& leaf = boost::get<yang::leaf>(child);
241 return leaf.m_enumValues;
242}
243
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200244// We do not test StaticSchema, so we don't need to implement recursive childNodes
245// for this class.
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200246std::set<std::string> StaticSchema::childNodes(const schemaPath_& path, const Recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200247{
248 std::string locationString = pathToAbsoluteSchemaString(path);
249 std::set<std::string> res;
250
251 auto childrenRef = children(locationString);
252
253 std::transform(childrenRef.begin(), childrenRef.end(),
254 std::inserter(res, res.end()),
255 [] (auto it) { return it.first; });
256 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200257}
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200258
259// We do not test StaticSchema, so we don't need to implement recursive moduleNodes
260// for this class.
261std::set<std::string> StaticSchema::moduleNodes(const module_& module, const Recursion) const
262{
263 std::set<std::string> res;
264 auto topLevelNodes = m_nodes.at("");
265 auto modulePlusColon = module.m_name + ":";
266 for (const auto& it : topLevelNodes) {
267 if (boost::algorithm::starts_with(it.first, modulePlusColon)) {
268 res.insert(it.first);
269 }
270 }
271 return res;
272}