blob: 0eb564a6e7b0280a2d2189125d6da82c76e82e9a [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átbddbb172018-06-13 16:27:39 +020074void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataTypes& type)
75{
Václav Kubernát6a8d1d92019-04-24 20:30:36 +020076 m_nodes.at(location).emplace(name, yang::leaf{type, {}, {}, {}});
Václav Kubernáte69133a2019-11-01 19:01:34 +010077 std::string key = joinPaths(location, name);
78 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020079}
80
81void StaticSchema::addLeafEnum(const std::string& location, const std::string& name, std::set<std::string> enumValues)
82{
Václav Kubernáteeb38842019-03-20 19:46:05 +010083 yang::leaf toAdd;
84 toAdd.m_type = yang::LeafDataTypes::Enum;
85 toAdd.m_enumValues = enumValues;
86 m_nodes.at(location).emplace(name, toAdd);
Václav Kubernáte69133a2019-11-01 19:01:34 +010087 std::string key = joinPaths(location, name);
88 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernáteeb38842019-03-20 19:46:05 +010089}
90
91void StaticSchema::addLeafIdentityRef(const std::string& location, const std::string& name, const ModuleValuePair& base)
92{
93 assert(base.first); // base identity cannot have an empty module
94 yang::leaf toAdd;
95 toAdd.m_type = yang::LeafDataTypes::IdentityRef;
96 toAdd.m_identBase = base;
97 m_nodes.at(location).emplace(name, toAdd);
Václav Kubernáte69133a2019-11-01 19:01:34 +010098 std::string key = joinPaths(location, name);
99 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +0200100}
101
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200102void StaticSchema::addLeafRef(const std::string& location, const std::string& name, const std::string& source)
103{
104 yang::leaf toAdd;
105 toAdd.m_type = yang::LeafDataTypes::LeafRef;
106 toAdd.m_leafRefSource = source;
107 m_nodes.at(location).emplace(name, toAdd);
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át6a8d1d92019-04-24 20:30:36 +0200110}
111
Václav Kubernát744f57f2018-06-29 22:46:26 +0200112void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +0200113{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200114 m_modules.emplace(name);
115}
Václav Kubernátbddbb172018-06-13 16:27:39 +0200116
Václav Kubernáteeb38842019-03-20 19:46:05 +0100117void StaticSchema::addIdentity(const std::optional<ModuleValuePair>& base, const ModuleValuePair& name)
118{
119 if (base)
120 m_identities.at(base.value()).emplace(name);
121
122 m_identities.emplace(name, std::set<ModuleValuePair>());
123}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200124
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200125bool StaticSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200126{
Václav Kubernát989b5de2019-02-20 16:28:35 +0100127 auto enums = enumValues(location, node);
128 return enums.find(value) != enums.end();
Václav Kubernátbddbb172018-06-13 16:27:39 +0200129}
130
Václav Kubernáteeb38842019-03-20 19:46:05 +0100131void StaticSchema::getIdentSet(const ModuleValuePair& ident, std::set<ModuleValuePair>& res) const
132{
133 res.insert(ident);
134 auto derivedIdentities = m_identities.at(ident);
135 for (auto it : derivedIdentities) {
136 getIdentSet(it, res);
137 }
138}
139
140const std::set<std::string> StaticSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const
141{
Václav Kubernátefcac932020-01-10 15:26:32 +0100142 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernáteeb38842019-03-20 19:46:05 +0100143 assert(isLeaf(location, node));
144
145 const auto& child = children(locationString).at(fullNodeName(location, node));
146 const auto& leaf = boost::get<yang::leaf>(child);
147
148 std::set<ModuleValuePair> identSet;
149 getIdentSet(leaf.m_identBase, identSet);
150
151 std::set<std::string> res;
152 std::transform(identSet.begin(), identSet.end(), std::inserter(res, res.end()), [location, node, prefixes](const auto& it) {
153 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
154 std::string stringIdent;
155 if (prefixes == Prefixes::Always || (it.first && it.first.value() != topLevelModule)) {
156 stringIdent += it.first ? it.first.value() : topLevelModule;
157 stringIdent += ":";
158 }
159 stringIdent += it.second;
160 return stringIdent;
161 });
162
163 return res;
164}
165
166bool StaticSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const
167{
168 auto identities = validIdentities(location, node, Prefixes::Always);
169
170 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
171 auto identModule = value.first ? value.first.value() : topLevelModule;
Václav Kubernát1bf704e2019-04-12 13:30:50 +0200172 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 +0100173}
174
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200175std::string lastNodeOfSchemaPath(const std::string& path)
176{
177 std::string res = path;
178 auto pos = res.find_last_of('/');
Václav Kubernátefcac932020-01-10 15:26:32 +0100179 if (pos == 0) { // path had only one path fragment - "/something:something"
180 res.erase(0, 1);
181 return res;
182 }
183 if (pos != res.npos) { // path had more fragments
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200184 res.erase(0, pos);
Václav Kubernátefcac932020-01-10 15:26:32 +0100185 return res;
186 }
187
188 // path was empty
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200189 return res;
190}
191
Václav Kubernátf0fe7692020-02-19 14:39:47 +0100192yang::LeafDataTypes StaticSchema::leafrefBaseType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200193{
Václav Kubernátefcac932020-01-10 15:26:32 +0100194 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200195 auto leaf{boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node)))};
196 auto locationOfSource = stripLastNodeFromPath(leaf.m_leafRefSource);
197 auto nameOfSource = lastNodeOfSchemaPath(leaf.m_leafRefSource);
198 return boost::get<yang::leaf>(children(locationOfSource).at(nameOfSource)).m_type;
199}
200
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200201yang::LeafDataTypes StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200202{
Václav Kubernátefcac932020-01-10 15:26:32 +0100203 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200204 return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type;
205}
206
Václav Kubernát9bf36852020-02-18 17:47:56 +0100207yang::LeafDataTypes StaticSchema::leafType([[maybe_unused]] const std::string& path) const
208{
209 throw std::runtime_error{"StaticSchema::leafType not implemented"};
210}
211
Václav Kubernát989b5de2019-02-20 16:28:35 +0100212const std::set<std::string> StaticSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const
213{
Václav Kubernátefcac932020-01-10 15:26:32 +0100214 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát989b5de2019-02-20 16:28:35 +0100215 assert(isLeaf(location, node));
216
217 const auto& child = children(locationString).at(fullNodeName(location, node));
218 const auto& leaf = boost::get<yang::leaf>(child);
219 return leaf.m_enumValues;
220}
221
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200222// We do not test StaticSchema, so we don't need to implement recursive childNodes
223// for this class.
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200224std::set<std::string> StaticSchema::childNodes(const schemaPath_& path, const Recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200225{
Václav Kubernátefcac932020-01-10 15:26:32 +0100226 std::string locationString = pathToSchemaString(path, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200227 std::set<std::string> res;
228
229 auto childrenRef = children(locationString);
230
Václav Kubernát90de9502019-11-20 17:19:44 +0100231 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 +0200232 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200233}
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200234
235// We do not test StaticSchema, so we don't need to implement recursive moduleNodes
236// for this class.
237std::set<std::string> StaticSchema::moduleNodes(const module_& module, const Recursion) const
238{
239 std::set<std::string> res;
240 auto topLevelNodes = m_nodes.at("");
241 auto modulePlusColon = module.m_name + ":";
242 for (const auto& it : topLevelNodes) {
243 if (boost::algorithm::starts_with(it.first, modulePlusColon)) {
244 res.insert(it.first);
245 }
246 }
247 return res;
248}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100249
250yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
251{
252 std::string locationString = pathToSchemaString(location, Prefixes::Always);
253 auto fullName = fullNodeName(location, node);
254 try {
255 auto targetNode = children(locationString).at(fullName);
256
257 if (targetNode.type() == typeid(yang::container)) {
258 if (boost::get<yang::container>(targetNode).m_presence == yang::ContainerTraits::Presence) {
259 return yang::NodeTypes::PresenceContainer;
260 }
261 return yang::NodeTypes::Container;
262 }
263
264 if (targetNode.type() == typeid(yang::list)) {
265 return yang::NodeTypes::List;
266 }
267
268 if (targetNode.type() == typeid(yang::leaf)) {
269 return yang::NodeTypes::Leaf;
270 }
271
272 throw std::runtime_error{"YangSchema::nodeType: unsupported type"};
273
274 } catch (std::out_of_range&) {
275 throw InvalidNodeException();
276 }
277}
278
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100279std::optional<std::string> StaticSchema::description([[maybe_unused]] const std::string& path) const
280{
281 throw std::runtime_error{"StaticSchema::description not implemented"};
282}
283
284std::optional<std::string> StaticSchema::units([[maybe_unused]] const std::string& path) const
285{
286 throw std::runtime_error{"StaticSchema::units not implemented"};
287}
288
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100289yang::NodeTypes StaticSchema::nodeType([[maybe_unused]] const std::string& path) const
290{
291 throw std::runtime_error{"Internal error: StaticSchema::nodeType(std::string) not implemented. The tests should not have called this overload."};
292}
Václav Kubernátad87ece2020-02-19 15:20:56 +0100293
294yang::LeafDataTypes StaticSchema::leafrefBaseType([[maybe_unused]] const std::string& path) const
295{
296 throw std::runtime_error{"Internal error: StaticSchema::leafrefBaseType(std::string) not implemented. The tests should not have called this overload."};
297}
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100298
299std::string StaticSchema::leafrefPath([[maybe_unused]] const std::string& leafrefPath) const
300{
301 throw std::runtime_error{"Internal error: StaticSchema::leafrefPath(std::string) not implemented. The tests should not have called this overload."};
302}
Václav Kubernátc3866792020-02-20 14:12:56 +0100303
304bool StaticSchema::leafIsKey([[maybe_unused]] const std::string& leafPath) const
305{
306 throw std::runtime_error{"Internal error: StaticSchema::leafIsKey(std::string) not implemented. The tests should not have called this overload."};
307}