blob: aae6b84a17e0c228e98fc0e7963bef33f4816be8 [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
Václav Kubernátbddbb172018-06-13 16:27:39 +020013StaticSchema::StaticSchema()
14{
Václav Kubernátefcac932020-01-10 15:26:32 +010015 m_nodes.emplace("/", std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020016}
17
18const std::unordered_map<std::string, NodeType>& StaticSchema::children(const std::string& name) const
19{
20 return m_nodes.at(name);
21}
22
Václav Kubernát744f57f2018-06-29 22:46:26 +020023bool StaticSchema::nodeExists(const std::string& location, const std::string& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020024{
Václav Kubernát744f57f2018-06-29 22:46:26 +020025 if (node.empty())
Václav Kubernátbddbb172018-06-13 16:27:39 +020026 return true;
27 const auto& childrenRef = children(location);
28
Václav Kubernát744f57f2018-06-29 22:46:26 +020029 return childrenRef.find(node) != childrenRef.end();
Václav Kubernátbddbb172018-06-13 16:27:39 +020030}
31
Václav Kubernát75877de2019-11-20 17:43:02 +010032bool StaticSchema::isModule(const std::string& name) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020033{
Václav Kubernát744f57f2018-06-29 22:46:26 +020034 return m_modules.find(name) != m_modules.end();
35}
36
Václav Kubernát2eaceb82018-10-08 19:56:30 +020037bool StaticSchema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát744f57f2018-06-29 22:46:26 +020038{
Václav Kubernátefcac932020-01-10 15:26:32 +010039 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +020040 auto fullName = fullNodeName(location, node);
41 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +020042 return false;
43
Václav Kubernát744f57f2018-06-29 22:46:26 +020044 return children(locationString).at(fullName).type() == typeid(yang::container);
Václav Kubernátbddbb172018-06-13 16:27:39 +020045}
46
47void StaticSchema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence)
48{
49 m_nodes.at(location).emplace(name, yang::container{isPresence});
50
51 //create a new set of children for the new node
52 std::string key = joinPaths(location, name);
53 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
54}
55
Václav Kubernát2eaceb82018-10-08 19:56:30 +020056bool StaticSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020057{
Václav Kubernátefcac932020-01-10 15:26:32 +010058 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +020059 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020060
Václav Kubernát744f57f2018-06-29 22:46:26 +020061 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020062 const auto& list = boost::get<yang::list>(child);
63 return list.m_keys.find(key) != list.m_keys.end();
64}
65
Václav Kubernát2eaceb82018-10-08 19:56:30 +020066const std::set<std::string> StaticSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020067{
Václav Kubernátefcac932020-01-10 15:26:32 +010068 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +020069 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020070
Václav Kubernát744f57f2018-06-29 22:46:26 +020071 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020072 const auto& list = boost::get<yang::list>(child);
73 return list.m_keys;
74}
75
Václav Kubernát2eaceb82018-10-08 19:56:30 +020076bool StaticSchema::isList(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020077{
Václav Kubernátefcac932020-01-10 15:26:32 +010078 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +020079 auto fullName = fullNodeName(location, node);
80 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +020081 return false;
Václav Kubernát744f57f2018-06-29 22:46:26 +020082 const auto& child = children(locationString).at(fullName);
Václav Kubernátbddbb172018-06-13 16:27:39 +020083 if (child.type() != typeid(yang::list))
84 return false;
85
86 return true;
87}
88
89void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
90{
91 m_nodes.at(location).emplace(name, yang::list{keys});
92
Václav Kubernát1446fe12019-10-02 19:32:51 +020093 std::string key = joinPaths(location, name);
94 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020095}
96
Václav Kubernát2eaceb82018-10-08 19:56:30 +020097bool StaticSchema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020098{
Václav Kubernát744f57f2018-06-29 22:46:26 +020099 if (!isContainer(location, node))
Václav Kubernátbddbb172018-06-13 16:27:39 +0200100 return false;
Václav Kubernátefcac932020-01-10 15:26:32 +0100101 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200102 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 +0200103}
104
105void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataTypes& type)
106{
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200107 m_nodes.at(location).emplace(name, yang::leaf{type, {}, {}, {}});
Václav Kubernáte69133a2019-11-01 19:01:34 +0100108 std::string key = joinPaths(location, name);
109 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
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);
Václav Kubernáte69133a2019-11-01 19:01:34 +0100118 std::string key = joinPaths(location, name);
119 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernáteeb38842019-03-20 19:46:05 +0100120}
121
122void StaticSchema::addLeafIdentityRef(const std::string& location, const std::string& name, const ModuleValuePair& base)
123{
124 assert(base.first); // base identity cannot have an empty module
125 yang::leaf toAdd;
126 toAdd.m_type = yang::LeafDataTypes::IdentityRef;
127 toAdd.m_identBase = base;
128 m_nodes.at(location).emplace(name, toAdd);
Václav Kubernáte69133a2019-11-01 19:01:34 +0100129 std::string key = joinPaths(location, name);
130 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +0200131}
132
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200133void StaticSchema::addLeafRef(const std::string& location, const std::string& name, const std::string& source)
134{
135 yang::leaf toAdd;
136 toAdd.m_type = yang::LeafDataTypes::LeafRef;
137 toAdd.m_leafRefSource = source;
138 m_nodes.at(location).emplace(name, toAdd);
Václav Kubernáte69133a2019-11-01 19:01:34 +0100139 std::string key = joinPaths(location, name);
140 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200141}
142
Václav Kubernát744f57f2018-06-29 22:46:26 +0200143void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +0200144{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200145 m_modules.emplace(name);
146}
Václav Kubernátbddbb172018-06-13 16:27:39 +0200147
Václav Kubernáteeb38842019-03-20 19:46:05 +0100148void StaticSchema::addIdentity(const std::optional<ModuleValuePair>& base, const ModuleValuePair& name)
149{
150 if (base)
151 m_identities.at(base.value()).emplace(name);
152
153 m_identities.emplace(name, std::set<ModuleValuePair>());
154}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200155
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200156bool StaticSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200157{
Václav Kubernát989b5de2019-02-20 16:28:35 +0100158 auto enums = enumValues(location, node);
159 return enums.find(value) != enums.end();
Václav Kubernátbddbb172018-06-13 16:27:39 +0200160}
161
Václav Kubernáteeb38842019-03-20 19:46:05 +0100162void StaticSchema::getIdentSet(const ModuleValuePair& ident, std::set<ModuleValuePair>& res) const
163{
164 res.insert(ident);
165 auto derivedIdentities = m_identities.at(ident);
166 for (auto it : derivedIdentities) {
167 getIdentSet(it, res);
168 }
169}
170
171const std::set<std::string> StaticSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const
172{
Václav Kubernátefcac932020-01-10 15:26:32 +0100173 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernáteeb38842019-03-20 19:46:05 +0100174 assert(isLeaf(location, node));
175
176 const auto& child = children(locationString).at(fullNodeName(location, node));
177 const auto& leaf = boost::get<yang::leaf>(child);
178
179 std::set<ModuleValuePair> identSet;
180 getIdentSet(leaf.m_identBase, identSet);
181
182 std::set<std::string> res;
183 std::transform(identSet.begin(), identSet.end(), std::inserter(res, res.end()), [location, node, prefixes](const auto& it) {
184 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
185 std::string stringIdent;
186 if (prefixes == Prefixes::Always || (it.first && it.first.value() != topLevelModule)) {
187 stringIdent += it.first ? it.first.value() : topLevelModule;
188 stringIdent += ":";
189 }
190 stringIdent += it.second;
191 return stringIdent;
192 });
193
194 return res;
195}
196
197bool StaticSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const
198{
199 auto identities = validIdentities(location, node, Prefixes::Always);
200
201 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
202 auto identModule = value.first ? value.first.value() : topLevelModule;
Václav Kubernát1bf704e2019-04-12 13:30:50 +0200203 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 +0100204}
205
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200206bool StaticSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200207{
Václav Kubernátefcac932020-01-10 15:26:32 +0100208 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200209 auto fullName = fullNodeName(location, node);
210 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +0200211 return false;
212
Václav Kubernát744f57f2018-06-29 22:46:26 +0200213 return children(locationString).at(fullName).type() == typeid(yang::leaf);
Václav Kubernátbddbb172018-06-13 16:27:39 +0200214}
215
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200216std::string lastNodeOfSchemaPath(const std::string& path)
217{
218 std::string res = path;
219 auto pos = res.find_last_of('/');
Václav Kubernátefcac932020-01-10 15:26:32 +0100220 if (pos == 0) { // path had only one path fragment - "/something:something"
221 res.erase(0, 1);
222 return res;
223 }
224 if (pos != res.npos) { // path had more fragments
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200225 res.erase(0, pos);
Václav Kubernátefcac932020-01-10 15:26:32 +0100226 return res;
227 }
228
229 // path was empty
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200230 return res;
231}
232
233yang::LeafDataTypes StaticSchema::leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const
234{
Václav Kubernátefcac932020-01-10 15:26:32 +0100235 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200236 auto leaf{boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node)))};
237 auto locationOfSource = stripLastNodeFromPath(leaf.m_leafRefSource);
238 auto nameOfSource = lastNodeOfSchemaPath(leaf.m_leafRefSource);
239 return boost::get<yang::leaf>(children(locationOfSource).at(nameOfSource)).m_type;
240}
241
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200242yang::LeafDataTypes StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200243{
Václav Kubernátefcac932020-01-10 15:26:32 +0100244 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200245 return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type;
246}
247
Václav Kubernát989b5de2019-02-20 16:28:35 +0100248const std::set<std::string> StaticSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const
249{
Václav Kubernátefcac932020-01-10 15:26:32 +0100250 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát989b5de2019-02-20 16:28:35 +0100251 assert(isLeaf(location, node));
252
253 const auto& child = children(locationString).at(fullNodeName(location, node));
254 const auto& leaf = boost::get<yang::leaf>(child);
255 return leaf.m_enumValues;
256}
257
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200258// We do not test StaticSchema, so we don't need to implement recursive childNodes
259// for this class.
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200260std::set<std::string> StaticSchema::childNodes(const schemaPath_& path, const Recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200261{
Václav Kubernátefcac932020-01-10 15:26:32 +0100262 std::string locationString = pathToSchemaString(path, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200263 std::set<std::string> res;
264
265 auto childrenRef = children(locationString);
266
Václav Kubernát90de9502019-11-20 17:19:44 +0100267 std::transform(childrenRef.begin(), childrenRef.end(), std::inserter(res, res.end()), [](auto it) { return it.first; });
Václav Kubernát744f57f2018-06-29 22:46:26 +0200268 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200269}
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200270
271// We do not test StaticSchema, so we don't need to implement recursive moduleNodes
272// for this class.
273std::set<std::string> StaticSchema::moduleNodes(const module_& module, const Recursion) const
274{
275 std::set<std::string> res;
276 auto topLevelNodes = m_nodes.at("");
277 auto modulePlusColon = module.m_name + ":";
278 for (const auto& it : topLevelNodes) {
279 if (boost::algorithm::starts_with(it.first, modulePlusColon)) {
280 res.insert(it.first);
281 }
282 }
283 return res;
284}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100285
286yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
287{
288 std::string locationString = pathToSchemaString(location, Prefixes::Always);
289 auto fullName = fullNodeName(location, node);
290 try {
291 auto targetNode = children(locationString).at(fullName);
292
293 if (targetNode.type() == typeid(yang::container)) {
294 if (boost::get<yang::container>(targetNode).m_presence == yang::ContainerTraits::Presence) {
295 return yang::NodeTypes::PresenceContainer;
296 }
297 return yang::NodeTypes::Container;
298 }
299
300 if (targetNode.type() == typeid(yang::list)) {
301 return yang::NodeTypes::List;
302 }
303
304 if (targetNode.type() == typeid(yang::leaf)) {
305 return yang::NodeTypes::Leaf;
306 }
307
308 throw std::runtime_error{"YangSchema::nodeType: unsupported type"};
309
310 } catch (std::out_of_range&) {
311 throw InvalidNodeException();
312 }
313}
314
315yang::NodeTypes StaticSchema::nodeType([[maybe_unused]] const std::string& path) const
316{
317 throw std::runtime_error{"Internal error: StaticSchema::nodeType(std::string) not implemented. The tests should not have called this overload."};
318}