blob: 4964e8e337eedeb898c14b2fdcddf71233168677 [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átbddbb172018-06-13 16:27:39 +020037void StaticSchema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence)
38{
39 m_nodes.at(location).emplace(name, yang::container{isPresence});
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, NodeType>());
44}
45
Václav Kubernát2eaceb82018-10-08 19:56:30 +020046bool StaticSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020047{
Václav Kubernátefcac932020-01-10 15:26:32 +010048 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +020049 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020050
Václav Kubernát744f57f2018-06-29 22:46:26 +020051 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020052 const auto& list = boost::get<yang::list>(child);
53 return list.m_keys.find(key) != list.m_keys.end();
54}
55
Václav Kubernát2eaceb82018-10-08 19:56:30 +020056const std::set<std::string> StaticSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) 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;
64}
65
Václav Kubernátbddbb172018-06-13 16:27:39 +020066void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
67{
68 m_nodes.at(location).emplace(name, yang::list{keys});
69
Václav Kubernát1446fe12019-10-02 19:32:51 +020070 std::string key = joinPaths(location, name);
71 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020072}
73
Václav Kubernát3a99f002020-03-31 02:27:41 +020074std::set<identityRef_> StaticSchema::validIdentities(std::string_view module, std::string_view value)
Václav Kubernátbddbb172018-06-13 16:27:39 +020075{
Václav Kubernát3a99f002020-03-31 02:27:41 +020076 std::set<ModuleValuePair> identities;
77 getIdentSet(ModuleNodePair{boost::optional<std::string>{module}, value}, identities);
78 std::set<identityRef_> res;
79
80 std::transform(identities.begin(), identities.end(), std::inserter(res, res.end()), [](const auto& identity) {
81 return identityRef_{*identity.first, identity.second};
82 });
83
84 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +020085}
86
Václav Kubernát3a99f002020-03-31 02:27:41 +020087void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataType& type)
Václav Kubernátbddbb172018-06-13 16:27:39 +020088{
Václav Kubernát3a99f002020-03-31 02:27:41 +020089 m_nodes.at(location).emplace(name, yang::leaf{type});
Václav Kubernáte69133a2019-11-01 19:01:34 +010090 std::string key = joinPaths(location, name);
91 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +020092}
93
Václav Kubernát744f57f2018-06-29 22:46:26 +020094void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +020095{
Václav Kubernát744f57f2018-06-29 22:46:26 +020096 m_modules.emplace(name);
97}
Václav Kubernátbddbb172018-06-13 16:27:39 +020098
Václav Kubernáteeb38842019-03-20 19:46:05 +010099void StaticSchema::addIdentity(const std::optional<ModuleValuePair>& base, const ModuleValuePair& name)
100{
101 if (base)
102 m_identities.at(base.value()).emplace(name);
103
104 m_identities.emplace(name, std::set<ModuleValuePair>());
105}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200106
Václav Kubernáteeb38842019-03-20 19:46:05 +0100107void StaticSchema::getIdentSet(const ModuleValuePair& ident, std::set<ModuleValuePair>& res) const
108{
109 res.insert(ident);
110 auto derivedIdentities = m_identities.at(ident);
111 for (auto it : derivedIdentities) {
112 getIdentSet(it, res);
113 }
114}
115
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200116std::string lastNodeOfSchemaPath(const std::string& path)
117{
118 std::string res = path;
119 auto pos = res.find_last_of('/');
Václav Kubernátefcac932020-01-10 15:26:32 +0100120 if (pos == 0) { // path had only one path fragment - "/something:something"
121 res.erase(0, 1);
122 return res;
123 }
124 if (pos != res.npos) { // path had more fragments
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200125 res.erase(0, pos);
Václav Kubernátefcac932020-01-10 15:26:32 +0100126 return res;
127 }
128
129 // path was empty
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200130 return res;
131}
132
Václav Kubernát3a99f002020-03-31 02:27:41 +0200133yang::LeafDataType StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200134{
Václav Kubernátefcac932020-01-10 15:26:32 +0100135 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200136 return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type;
137}
138
Václav Kubernát3a99f002020-03-31 02:27:41 +0200139yang::LeafDataType StaticSchema::leafType(const std::string& path) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100140{
Václav Kubernát3a99f002020-03-31 02:27:41 +0200141 auto locationString = stripLastNodeFromPath(path);
142 auto node = lastNodeOfSchemaPath(path);
143 return boost::get<yang::leaf>(children(locationString).at(node)).m_type;
Václav Kubernát989b5de2019-02-20 16:28:35 +0100144}
145
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200146// We do not test StaticSchema, so we don't need to implement recursive childNodes
147// for this class.
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200148std::set<std::string> StaticSchema::childNodes(const schemaPath_& path, const Recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200149{
Václav Kubernátefcac932020-01-10 15:26:32 +0100150 std::string locationString = pathToSchemaString(path, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200151 std::set<std::string> res;
152
153 auto childrenRef = children(locationString);
154
Václav Kubernát90de9502019-11-20 17:19:44 +0100155 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 +0200156 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200157}
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200158
159// We do not test StaticSchema, so we don't need to implement recursive moduleNodes
160// for this class.
161std::set<std::string> StaticSchema::moduleNodes(const module_& module, const Recursion) const
162{
163 std::set<std::string> res;
164 auto topLevelNodes = m_nodes.at("");
165 auto modulePlusColon = module.m_name + ":";
166 for (const auto& it : topLevelNodes) {
167 if (boost::algorithm::starts_with(it.first, modulePlusColon)) {
168 res.insert(it.first);
169 }
170 }
171 return res;
172}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100173
174yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
175{
176 std::string locationString = pathToSchemaString(location, Prefixes::Always);
177 auto fullName = fullNodeName(location, node);
178 try {
179 auto targetNode = children(locationString).at(fullName);
180
181 if (targetNode.type() == typeid(yang::container)) {
182 if (boost::get<yang::container>(targetNode).m_presence == yang::ContainerTraits::Presence) {
183 return yang::NodeTypes::PresenceContainer;
184 }
185 return yang::NodeTypes::Container;
186 }
187
188 if (targetNode.type() == typeid(yang::list)) {
189 return yang::NodeTypes::List;
190 }
191
192 if (targetNode.type() == typeid(yang::leaf)) {
193 return yang::NodeTypes::Leaf;
194 }
195
196 throw std::runtime_error{"YangSchema::nodeType: unsupported type"};
197
198 } catch (std::out_of_range&) {
199 throw InvalidNodeException();
200 }
201}
202
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100203std::optional<std::string> StaticSchema::description([[maybe_unused]] const std::string& path) const
204{
205 throw std::runtime_error{"StaticSchema::description not implemented"};
206}
207
208std::optional<std::string> StaticSchema::units([[maybe_unused]] const std::string& path) const
209{
210 throw std::runtime_error{"StaticSchema::units not implemented"};
211}
212
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100213yang::NodeTypes StaticSchema::nodeType([[maybe_unused]] const std::string& path) const
214{
215 throw std::runtime_error{"Internal error: StaticSchema::nodeType(std::string) not implemented. The tests should not have called this overload."};
216}
Václav Kubernátad87ece2020-02-19 15:20:56 +0100217
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100218std::string StaticSchema::leafrefPath([[maybe_unused]] const std::string& leafrefPath) const
219{
220 throw std::runtime_error{"Internal error: StaticSchema::leafrefPath(std::string) not implemented. The tests should not have called this overload."};
221}
Václav Kubernátc3866792020-02-20 14:12:56 +0100222
223bool StaticSchema::leafIsKey([[maybe_unused]] const std::string& leafPath) const
224{
225 throw std::runtime_error{"Internal error: StaticSchema::leafIsKey(std::string) not implemented. The tests should not have called this overload."};
226}
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100227
228std::optional<std::string> StaticSchema::leafTypeName([[maybe_unused]] const std::string& path) const
229{
230 throw std::runtime_error{"Internal error: StaticSchema::leafTypeName(std::string) not implemented. The tests should not have called this overload."};
231}