blob: 75ccb4fb5bf0147d4ba4c137589b191fdad2f4d5 [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átabf52802020-05-19 01:31:17 +020015 m_nodes.emplace("/", std::unordered_map<std::string, NodeInfo>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020016}
17
Václav Kubernátabf52802020-05-19 01:31:17 +020018const std::unordered_map<std::string, NodeInfo>& StaticSchema::children(const std::string& name) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020019{
20 return m_nodes.at(name);
21}
22
Václav Kubernát75877de2019-11-20 17:43:02 +010023bool StaticSchema::isModule(const std::string& name) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020024{
Václav Kubernát744f57f2018-06-29 22:46:26 +020025 return m_modules.find(name) != m_modules.end();
26}
27
Václav Kubernátbddbb172018-06-13 16:27:39 +020028void StaticSchema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence)
29{
Václav Kubernátabf52802020-05-19 01:31:17 +020030 m_nodes.at(location).emplace(name, NodeInfo{yang::container{isPresence}, yang::AccessType::Writable});
Václav Kubernátbddbb172018-06-13 16:27:39 +020031
32 //create a new set of children for the new node
33 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020034 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020035}
36
Václav Kubernát2db124c2020-05-28 21:58:36 +020037bool StaticSchema::listHasKey(const schemaPath_& listPath, const std::string& key) const
38{
39 return listKeys(listPath).count(key);
40}
41
Václav Kubernát2db124c2020-05-28 21:58:36 +020042std::string lastNodeOfSchemaPath(const std::string& path)
43{
44 std::string res = path;
45 if (auto pos = res.find_last_of('/'); pos != res.npos) {
46 res.erase(0, pos + 1);
47 }
48 return res;
49}
50
51const std::set<std::string> StaticSchema::listKeys(const schemaPath_& listPath) const
52{
53 auto listPathString = pathToSchemaString(listPath, Prefixes::Always);
54 const auto& child = children(stripLastNodeFromPath(listPathString)).at(lastNodeOfSchemaPath(listPathString));
55 const auto& list = std::get<yang::list>(child.m_nodeType);
56 return list.m_keys;
57}
58
Václav Kubernátbddbb172018-06-13 16:27:39 +020059void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
60{
Václav Kubernátabf52802020-05-19 01:31:17 +020061 m_nodes.at(location).emplace(name, NodeInfo{yang::list{keys}, yang::AccessType::Writable});
Václav Kubernátbddbb172018-06-13 16:27:39 +020062
Václav Kubernát1446fe12019-10-02 19:32:51 +020063 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020064 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020065}
66
Václav Kubernát3a99f002020-03-31 02:27:41 +020067std::set<identityRef_> StaticSchema::validIdentities(std::string_view module, std::string_view value)
Václav Kubernátbddbb172018-06-13 16:27:39 +020068{
Václav Kubernát222ecff2020-05-14 23:14:35 +020069 std::set<identityRef_> identities;
70 getIdentSet(identityRef_{std::string{module}, std::string{value}}, identities);
Václav Kubernát3a99f002020-03-31 02:27:41 +020071
Václav Kubernát222ecff2020-05-14 23:14:35 +020072 return identities;
Václav Kubernátbddbb172018-06-13 16:27:39 +020073}
74
Václav Kubernátabf52802020-05-19 01:31:17 +020075void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataType& type, const yang::AccessType accessType)
Václav Kubernátbddbb172018-06-13 16:27:39 +020076{
Václav Kubernátabf52802020-05-19 01:31:17 +020077 m_nodes.at(location).emplace(name, NodeInfo{yang::leaf{yang::TypeInfo{type, std::nullopt}}, accessType});
Václav Kubernáte69133a2019-11-01 19:01:34 +010078 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020079 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +020080}
81
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020082void StaticSchema::addLeafList(const std::string& location, const std::string& name, const yang::LeafDataType& type)
83{
84 m_nodes.at(location).emplace(name, NodeInfo{yang::leaflist{yang::TypeInfo{type, std::nullopt}}, yang::AccessType::Writable});
85 std::string key = joinPaths(location, name);
86 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
87}
88
Václav Kubernát744f57f2018-06-29 22:46:26 +020089void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +020090{
Václav Kubernát744f57f2018-06-29 22:46:26 +020091 m_modules.emplace(name);
92}
Václav Kubernátbddbb172018-06-13 16:27:39 +020093
Václav Kubernát222ecff2020-05-14 23:14:35 +020094void StaticSchema::addIdentity(const std::optional<identityRef_>& base, const identityRef_& name)
Václav Kubernáteeb38842019-03-20 19:46:05 +010095{
Václav Kubernát3a433232020-07-08 17:52:50 +020096 if (base) {
Václav Kubernáteeb38842019-03-20 19:46:05 +010097 m_identities.at(base.value()).emplace(name);
Václav Kubernát3a433232020-07-08 17:52:50 +020098 }
Václav Kubernáteeb38842019-03-20 19:46:05 +010099
Václav Kubernát222ecff2020-05-14 23:14:35 +0200100 m_identities.emplace(name, std::set<identityRef_>());
Václav Kubernáteeb38842019-03-20 19:46:05 +0100101}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200102
Václav Kubernát222ecff2020-05-14 23:14:35 +0200103void StaticSchema::getIdentSet(const identityRef_& ident, std::set<identityRef_>& res) const
Václav Kubernáteeb38842019-03-20 19:46:05 +0100104{
105 res.insert(ident);
106 auto derivedIdentities = m_identities.at(ident);
107 for (auto it : derivedIdentities) {
108 getIdentSet(it, res);
109 }
110}
111
Václav Kubernát13b23d72020-04-16 21:49:51 +0200112yang::TypeInfo StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200113{
Václav Kubernátefcac932020-01-10 15:26:32 +0100114 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200115 auto nodeType = children(locationString).at(fullNodeName(location, node)).m_nodeType;
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200116 if (std::holds_alternative<yang::leaf>(nodeType)) {
117 return std::get<yang::leaf>(nodeType).m_type;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200118 }
119
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200120 if (std::holds_alternative<yang::leaflist>(nodeType)) {
121 return std::get<yang::leaflist>(nodeType).m_type;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200122 }
123
124 throw std::logic_error("StaticSchema::leafType: Path is not a leaf or a leaflist");
Václav Kubernát744f57f2018-06-29 22:46:26 +0200125}
126
Václav Kubernát13b23d72020-04-16 21:49:51 +0200127yang::TypeInfo StaticSchema::leafType(const std::string& path) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100128{
Václav Kubernát3a99f002020-03-31 02:27:41 +0200129 auto locationString = stripLastNodeFromPath(path);
130 auto node = lastNodeOfSchemaPath(path);
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200131 return std::get<yang::leaf>(children(locationString).at(node).m_nodeType).m_type;
Václav Kubernát989b5de2019-02-20 16:28:35 +0100132}
133
Václav Kubernát95b08872020-04-28 01:04:17 +0200134std::set<ModuleNodePair> StaticSchema::availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200135{
Václav Kubernát3a823f42020-04-29 23:40:21 +0200136 if (recursion == Recursion::Recursive) {
137 throw std::logic_error("Recursive StaticSchema::availableNodes is not implemented. It shouldn't be used in tests.");
138 }
139
Václav Kubernát95b08872020-04-28 01:04:17 +0200140 std::set<ModuleNodePair> res;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200141 if (path.type() == typeid(module_)) {
142 auto topLevelNodes = m_nodes.at("");
143 auto modulePlusColon = boost::get<module_>(path).m_name + ":";
144 for (const auto& it : topLevelNodes) {
145 if (boost::algorithm::starts_with(it.first, modulePlusColon)) {
Václav Kubernát95b08872020-04-28 01:04:17 +0200146 res.insert(splitModuleNode(it.first));
Václav Kubernát3a823f42020-04-29 23:40:21 +0200147 }
148 }
149 return res;
150 }
151
Václav Kubernáte2d629f2020-04-28 11:01:54 +0200152 auto getTopLevelModule = [] (const auto& path) -> boost::optional<std::string> {
153 if (!path.m_nodes.empty()) {
154 return path.m_nodes.begin()->m_prefix.flat_map([] (const auto& module) {return boost::optional<std::string>(module.m_name);});
155 }
156
157 return boost::none;
158 };
159
160 std::string locationString;
161 boost::optional<std::string> topLevelModule;
162 if (path.type() == typeid(schemaPath_)) {
163 locationString = pathToSchemaString(boost::get<schemaPath_>(path), Prefixes::Always);
164 topLevelModule = getTopLevelModule(boost::get<schemaPath_>(path));
165 } else {
166 locationString = pathToSchemaString(boost::get<dataPath_>(path), Prefixes::Always);
167 topLevelModule = getTopLevelModule(boost::get<dataPath_>(path));
168 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200169
170 auto childrenRef = children(locationString);
171
Václav Kubernáte2d629f2020-04-28 11:01:54 +0200172 std::transform(childrenRef.begin(), childrenRef.end(), std::inserter(res, res.end()), [path, topLevelModule](const auto& it) {
173 auto res = splitModuleNode(it.first);
174 if (topLevelModule == res.first) {
175 res.first = boost::none;
176 }
177 return res;
Václav Kubernát95b08872020-04-28 01:04:17 +0200178 });
Václav Kubernát744f57f2018-06-29 22:46:26 +0200179 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200180}
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200181
Václav Kubernát19a368b2020-05-27 00:05:23 +0200182struct impl_nodeType {
183
184 yang::NodeTypes operator()(const yang::container& cont)
185 {
186 if (cont.m_presence == yang::ContainerTraits::Presence) {
187 return yang::NodeTypes::PresenceContainer;
188 }
189 return yang::NodeTypes::Container;
190 }
191 yang::NodeTypes operator()(const yang::list&)
192 {
193 return yang::NodeTypes::List;
194 }
195 yang::NodeTypes operator()(const yang::leaf&)
196 {
197 return yang::NodeTypes::Leaf;
198
199 }
200 yang::NodeTypes operator()(const yang::leaflist&)
201 {
202 return yang::NodeTypes::LeafList;
203 }
204};
205
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100206yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
207{
208 std::string locationString = pathToSchemaString(location, Prefixes::Always);
209 auto fullName = fullNodeName(location, node);
210 try {
211 auto targetNode = children(locationString).at(fullName);
212
Václav Kubernát19a368b2020-05-27 00:05:23 +0200213 return std::visit(impl_nodeType{}, targetNode.m_nodeType);
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100214 } catch (std::out_of_range&) {
215 throw InvalidNodeException();
216 }
217}
218
Václav Kubernátabf52802020-05-19 01:31:17 +0200219std::string fullNodeName(const std::string& location, const std::string& node)
220{
221 // If the node already contains a module name, just return it.
222 if (node.find_first_of(':') != std::string::npos) {
223 return node;
224 }
225
226 // Otherwise take the module name from the first node of location.
227 return location.substr(location.find_first_not_of('/'), location.find_first_of(':') - 1) + ":" + node;
228}
229
230bool StaticSchema::isConfig(const std::string& leafPath) const
231{
232 auto locationString = stripLastNodeFromPath(leafPath);
233
234 auto node = fullNodeName(locationString, lastNodeOfSchemaPath(leafPath));
235 return children(locationString).at(node).m_configType == yang::AccessType::Writable;
236}
237
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100238std::optional<std::string> StaticSchema::description([[maybe_unused]] const std::string& path) const
239{
240 throw std::runtime_error{"StaticSchema::description not implemented"};
241}
242
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200243yang::Status StaticSchema::status([[maybe_unused]] const std::string& location) const
244{
245 throw std::runtime_error{"Internal error: StaticSchema::status(std::string) not implemented. The tests should not have called this overload."};
246}
247
Václav Kubernáte811bfa2020-05-29 02:25:20 +0200248yang::NodeTypes StaticSchema::nodeType(const std::string& path) const
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100249{
Václav Kubernáte811bfa2020-05-29 02:25:20 +0200250 auto locationString = stripLastNodeFromPath(path);
251
252 auto node = fullNodeName(locationString, lastNodeOfSchemaPath(path));
253 return std::visit(impl_nodeType{}, children(locationString).at(node).m_nodeType);
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100254}
Václav Kubernátad87ece2020-02-19 15:20:56 +0100255
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100256std::string StaticSchema::leafrefPath([[maybe_unused]] const std::string& leafrefPath) const
257{
258 throw std::runtime_error{"Internal error: StaticSchema::leafrefPath(std::string) not implemented. The tests should not have called this overload."};
259}
Václav Kubernátc3866792020-02-20 14:12:56 +0100260
261bool StaticSchema::leafIsKey([[maybe_unused]] const std::string& leafPath) const
262{
263 throw std::runtime_error{"Internal error: StaticSchema::leafIsKey(std::string) not implemented. The tests should not have called this overload."};
264}
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100265
266std::optional<std::string> StaticSchema::leafTypeName([[maybe_unused]] const std::string& path) const
267{
268 throw std::runtime_error{"Internal error: StaticSchema::leafTypeName(std::string) not implemented. The tests should not have called this overload."};
269}
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200270
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200271std::optional<std::string> StaticSchema::defaultValue([[maybe_unused]] const std::string& leafPath) const
272{
273 throw std::runtime_error{"Internal error: StaticSchema::defaultValue(std::string) not implemented. The tests should not have called this overload."};
274}