blob: af1004a1b555b84403178d049ce7dd156e2caad0 [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áte7248b22020-06-26 15:38:59 +020037void StaticSchema::addRpc(const std::string& location, const std::string& name)
38{
39 m_nodes.at(location).emplace(name, NodeInfo{yang::rpc{}, yang::AccessType::Writable});
40
41 //create a new set of children for the new node
42 std::string key = joinPaths(location, name);
43 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
44}
45
Václav Kubernát3d787b12020-10-29 09:11:26 +010046void StaticSchema::addAction(const std::string& location, const std::string& name)
47{
48 m_nodes.at(location).emplace(name, NodeInfo{yang::action{}, yang::AccessType::Writable});
49
50 //create a new set of children for the new node
51 std::string key = joinPaths(location, name);
52 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
53}
54
Václav Kubernát2db124c2020-05-28 21:58:36 +020055bool StaticSchema::listHasKey(const schemaPath_& listPath, const std::string& key) const
56{
57 return listKeys(listPath).count(key);
58}
59
Václav Kubernát2db124c2020-05-28 21:58:36 +020060std::string lastNodeOfSchemaPath(const std::string& path)
61{
62 std::string res = path;
63 if (auto pos = res.find_last_of('/'); pos != res.npos) {
64 res.erase(0, pos + 1);
65 }
66 return res;
67}
68
69const std::set<std::string> StaticSchema::listKeys(const schemaPath_& listPath) const
70{
71 auto listPathString = pathToSchemaString(listPath, Prefixes::Always);
72 const auto& child = children(stripLastNodeFromPath(listPathString)).at(lastNodeOfSchemaPath(listPathString));
73 const auto& list = std::get<yang::list>(child.m_nodeType);
74 return list.m_keys;
75}
76
Václav Kubernátbddbb172018-06-13 16:27:39 +020077void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
78{
Václav Kubernátabf52802020-05-19 01:31:17 +020079 m_nodes.at(location).emplace(name, NodeInfo{yang::list{keys}, yang::AccessType::Writable});
Václav Kubernátbddbb172018-06-13 16:27:39 +020080
Václav Kubernát1446fe12019-10-02 19:32:51 +020081 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020082 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020083}
84
Václav Kubernát3a99f002020-03-31 02:27:41 +020085std::set<identityRef_> StaticSchema::validIdentities(std::string_view module, std::string_view value)
Václav Kubernátbddbb172018-06-13 16:27:39 +020086{
Václav Kubernát222ecff2020-05-14 23:14:35 +020087 std::set<identityRef_> identities;
88 getIdentSet(identityRef_{std::string{module}, std::string{value}}, identities);
Václav Kubernát3a99f002020-03-31 02:27:41 +020089
Václav Kubernát222ecff2020-05-14 23:14:35 +020090 return identities;
Václav Kubernátbddbb172018-06-13 16:27:39 +020091}
92
Václav Kubernátabf52802020-05-19 01:31:17 +020093void 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 +020094{
Václav Kubernátabf52802020-05-19 01:31:17 +020095 m_nodes.at(location).emplace(name, NodeInfo{yang::leaf{yang::TypeInfo{type, std::nullopt}}, accessType});
Václav Kubernáte69133a2019-11-01 19:01:34 +010096 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020097 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +020098}
99
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200100void StaticSchema::addLeafList(const std::string& location, const std::string& name, const yang::LeafDataType& type)
101{
102 m_nodes.at(location).emplace(name, NodeInfo{yang::leaflist{yang::TypeInfo{type, std::nullopt}}, yang::AccessType::Writable});
103 std::string key = joinPaths(location, name);
104 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
105}
106
Václav Kubernát744f57f2018-06-29 22:46:26 +0200107void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +0200108{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200109 m_modules.emplace(name);
110}
Václav Kubernátbddbb172018-06-13 16:27:39 +0200111
Václav Kubernát222ecff2020-05-14 23:14:35 +0200112void StaticSchema::addIdentity(const std::optional<identityRef_>& base, const identityRef_& name)
Václav Kubernáteeb38842019-03-20 19:46:05 +0100113{
Václav Kubernát3a433232020-07-08 17:52:50 +0200114 if (base) {
Václav Kubernáteeb38842019-03-20 19:46:05 +0100115 m_identities.at(base.value()).emplace(name);
Václav Kubernát3a433232020-07-08 17:52:50 +0200116 }
Václav Kubernáteeb38842019-03-20 19:46:05 +0100117
Václav Kubernát222ecff2020-05-14 23:14:35 +0200118 m_identities.emplace(name, std::set<identityRef_>());
Václav Kubernáteeb38842019-03-20 19:46:05 +0100119}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200120
Václav Kubernát222ecff2020-05-14 23:14:35 +0200121void StaticSchema::getIdentSet(const identityRef_& ident, std::set<identityRef_>& res) const
Václav Kubernáteeb38842019-03-20 19:46:05 +0100122{
123 res.insert(ident);
124 auto derivedIdentities = m_identities.at(ident);
125 for (auto it : derivedIdentities) {
126 getIdentSet(it, res);
127 }
128}
129
Václav Kubernát13b23d72020-04-16 21:49:51 +0200130yang::TypeInfo StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200131{
Václav Kubernátefcac932020-01-10 15:26:32 +0100132 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200133 auto nodeType = children(locationString).at(fullNodeName(location, node)).m_nodeType;
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200134 if (std::holds_alternative<yang::leaf>(nodeType)) {
135 return std::get<yang::leaf>(nodeType).m_type;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200136 }
137
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200138 if (std::holds_alternative<yang::leaflist>(nodeType)) {
139 return std::get<yang::leaflist>(nodeType).m_type;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200140 }
141
142 throw std::logic_error("StaticSchema::leafType: Path is not a leaf or a leaflist");
Václav Kubernát744f57f2018-06-29 22:46:26 +0200143}
144
Václav Kubernát13b23d72020-04-16 21:49:51 +0200145yang::TypeInfo StaticSchema::leafType(const std::string& path) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100146{
Václav Kubernát3a99f002020-03-31 02:27:41 +0200147 auto locationString = stripLastNodeFromPath(path);
148 auto node = lastNodeOfSchemaPath(path);
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200149 return std::get<yang::leaf>(children(locationString).at(node).m_nodeType).m_type;
Václav Kubernát989b5de2019-02-20 16:28:35 +0100150}
151
Václav Kubernát95b08872020-04-28 01:04:17 +0200152std::set<ModuleNodePair> StaticSchema::availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200153{
Václav Kubernát3a823f42020-04-29 23:40:21 +0200154 if (recursion == Recursion::Recursive) {
155 throw std::logic_error("Recursive StaticSchema::availableNodes is not implemented. It shouldn't be used in tests.");
156 }
157
Václav Kubernát95b08872020-04-28 01:04:17 +0200158 std::set<ModuleNodePair> res;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200159 if (path.type() == typeid(module_)) {
160 auto topLevelNodes = m_nodes.at("");
161 auto modulePlusColon = boost::get<module_>(path).m_name + ":";
162 for (const auto& it : topLevelNodes) {
163 if (boost::algorithm::starts_with(it.first, modulePlusColon)) {
Václav Kubernát95b08872020-04-28 01:04:17 +0200164 res.insert(splitModuleNode(it.first));
Václav Kubernát3a823f42020-04-29 23:40:21 +0200165 }
166 }
167 return res;
168 }
169
Václav Kubernáte2d629f2020-04-28 11:01:54 +0200170 auto getTopLevelModule = [] (const auto& path) -> boost::optional<std::string> {
171 if (!path.m_nodes.empty()) {
172 return path.m_nodes.begin()->m_prefix.flat_map([] (const auto& module) {return boost::optional<std::string>(module.m_name);});
173 }
174
175 return boost::none;
176 };
177
178 std::string locationString;
179 boost::optional<std::string> topLevelModule;
180 if (path.type() == typeid(schemaPath_)) {
181 locationString = pathToSchemaString(boost::get<schemaPath_>(path), Prefixes::Always);
182 topLevelModule = getTopLevelModule(boost::get<schemaPath_>(path));
183 } else {
184 locationString = pathToSchemaString(boost::get<dataPath_>(path), Prefixes::Always);
185 topLevelModule = getTopLevelModule(boost::get<dataPath_>(path));
186 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200187
188 auto childrenRef = children(locationString);
189
Václav Kubernáte2d629f2020-04-28 11:01:54 +0200190 std::transform(childrenRef.begin(), childrenRef.end(), std::inserter(res, res.end()), [path, topLevelModule](const auto& it) {
191 auto res = splitModuleNode(it.first);
192 if (topLevelModule == res.first) {
193 res.first = boost::none;
194 }
195 return res;
Václav Kubernát95b08872020-04-28 01:04:17 +0200196 });
Václav Kubernát744f57f2018-06-29 22:46:26 +0200197 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200198}
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200199
Václav Kubernát19a368b2020-05-27 00:05:23 +0200200struct impl_nodeType {
201
202 yang::NodeTypes operator()(const yang::container& cont)
203 {
204 if (cont.m_presence == yang::ContainerTraits::Presence) {
205 return yang::NodeTypes::PresenceContainer;
206 }
207 return yang::NodeTypes::Container;
208 }
209 yang::NodeTypes operator()(const yang::list&)
210 {
211 return yang::NodeTypes::List;
212 }
213 yang::NodeTypes operator()(const yang::leaf&)
214 {
215 return yang::NodeTypes::Leaf;
216
217 }
218 yang::NodeTypes operator()(const yang::leaflist&)
219 {
220 return yang::NodeTypes::LeafList;
221 }
Václav Kubernáte7248b22020-06-26 15:38:59 +0200222
223 yang::NodeTypes operator()(const yang::rpc)
224 {
225 return yang::NodeTypes::Rpc;
226 }
Václav Kubernát3d787b12020-10-29 09:11:26 +0100227
228 yang::NodeTypes operator()(const yang::action)
229 {
230 return yang::NodeTypes::Action;
231 }
Václav Kubernát19a368b2020-05-27 00:05:23 +0200232};
233
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100234yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
235{
236 std::string locationString = pathToSchemaString(location, Prefixes::Always);
237 auto fullName = fullNodeName(location, node);
238 try {
239 auto targetNode = children(locationString).at(fullName);
240
Václav Kubernát19a368b2020-05-27 00:05:23 +0200241 return std::visit(impl_nodeType{}, targetNode.m_nodeType);
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100242 } catch (std::out_of_range&) {
243 throw InvalidNodeException();
244 }
245}
246
Václav Kubernátabf52802020-05-19 01:31:17 +0200247std::string fullNodeName(const std::string& location, const std::string& node)
248{
249 // If the node already contains a module name, just return it.
250 if (node.find_first_of(':') != std::string::npos) {
251 return node;
252 }
253
254 // Otherwise take the module name from the first node of location.
255 return location.substr(location.find_first_not_of('/'), location.find_first_of(':') - 1) + ":" + node;
256}
257
258bool StaticSchema::isConfig(const std::string& leafPath) const
259{
260 auto locationString = stripLastNodeFromPath(leafPath);
261
262 auto node = fullNodeName(locationString, lastNodeOfSchemaPath(leafPath));
263 return children(locationString).at(node).m_configType == yang::AccessType::Writable;
264}
265
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100266std::optional<std::string> StaticSchema::description([[maybe_unused]] const std::string& path) const
267{
268 throw std::runtime_error{"StaticSchema::description not implemented"};
269}
270
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200271yang::Status StaticSchema::status([[maybe_unused]] const std::string& location) const
272{
273 throw std::runtime_error{"Internal error: StaticSchema::status(std::string) not implemented. The tests should not have called this overload."};
274}
275
Václav Kubernáte811bfa2020-05-29 02:25:20 +0200276yang::NodeTypes StaticSchema::nodeType(const std::string& path) const
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100277{
Václav Kubernáte811bfa2020-05-29 02:25:20 +0200278 auto locationString = stripLastNodeFromPath(path);
279
280 auto node = fullNodeName(locationString, lastNodeOfSchemaPath(path));
281 return std::visit(impl_nodeType{}, children(locationString).at(node).m_nodeType);
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100282}
Václav Kubernátad87ece2020-02-19 15:20:56 +0100283
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100284std::string StaticSchema::leafrefPath([[maybe_unused]] const std::string& leafrefPath) const
285{
286 throw std::runtime_error{"Internal error: StaticSchema::leafrefPath(std::string) not implemented. The tests should not have called this overload."};
287}
Václav Kubernátc3866792020-02-20 14:12:56 +0100288
289bool StaticSchema::leafIsKey([[maybe_unused]] const std::string& leafPath) const
290{
291 throw std::runtime_error{"Internal error: StaticSchema::leafIsKey(std::string) not implemented. The tests should not have called this overload."};
292}
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100293
294std::optional<std::string> StaticSchema::leafTypeName([[maybe_unused]] const std::string& path) const
295{
296 throw std::runtime_error{"Internal error: StaticSchema::leafTypeName(std::string) not implemented. The tests should not have called this overload."};
297}
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200298
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200299std::optional<std::string> StaticSchema::defaultValue([[maybe_unused]] const std::string& leafPath) const
300{
301 throw std::runtime_error{"Internal error: StaticSchema::defaultValue(std::string) not implemented. The tests should not have called this overload."};
302}