blob: d80ac7bd29f5c953cbc0f0358a8ad85107b29418 [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
9#include "static_schema.hpp"
10#include "utils.hpp"
11
12InvalidNodeException::~InvalidNodeException() = default;
13
14StaticSchema::StaticSchema()
15{
16 m_nodes.emplace("", std::unordered_map<std::string, NodeType>());
17}
18
Václav Kubernát744f57f2018-06-29 22:46:26 +020019
Václav Kubernátbddbb172018-06-13 16:27:39 +020020const std::unordered_map<std::string, NodeType>& StaticSchema::children(const std::string& name) const
21{
22 return m_nodes.at(name);
23}
24
Václav Kubernát744f57f2018-06-29 22:46:26 +020025bool StaticSchema::nodeExists(const std::string& location, const std::string& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020026{
Václav Kubernát744f57f2018-06-29 22:46:26 +020027 if (node.empty())
Václav Kubernátbddbb172018-06-13 16:27:39 +020028 return true;
29 const auto& childrenRef = children(location);
30
Václav Kubernát744f57f2018-06-29 22:46:26 +020031 return childrenRef.find(node) != childrenRef.end();
Václav Kubernátbddbb172018-06-13 16:27:39 +020032}
33
Václav Kubernát2eaceb82018-10-08 19:56:30 +020034bool StaticSchema::isModule(const schemaPath_&, const std::string& name) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020035{
Václav Kubernát744f57f2018-06-29 22:46:26 +020036 return m_modules.find(name) != m_modules.end();
37}
38
Václav Kubernát2eaceb82018-10-08 19:56:30 +020039bool StaticSchema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát744f57f2018-06-29 22:46:26 +020040{
41 std::string locationString = pathToAbsoluteSchemaString(location);
42 auto fullName = fullNodeName(location, node);
43 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +020044 return false;
45
Václav Kubernát744f57f2018-06-29 22:46:26 +020046 return children(locationString).at(fullName).type() == typeid(yang::container);
Václav Kubernátbddbb172018-06-13 16:27:39 +020047}
48
49void StaticSchema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence)
50{
51 m_nodes.at(location).emplace(name, yang::container{isPresence});
52
53 //create a new set of children for the new node
54 std::string key = joinPaths(location, name);
55 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
56}
57
Václav Kubernát2eaceb82018-10-08 19:56:30 +020058bool StaticSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020059{
Václav Kubernát744f57f2018-06-29 22:46:26 +020060 std::string locationString = pathToAbsoluteSchemaString(location);
61 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020062
Václav Kubernát744f57f2018-06-29 22:46:26 +020063 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020064 const auto& list = boost::get<yang::list>(child);
65 return list.m_keys.find(key) != list.m_keys.end();
66}
67
Václav Kubernát2eaceb82018-10-08 19:56:30 +020068const std::set<std::string> StaticSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020069{
Václav Kubernát744f57f2018-06-29 22:46:26 +020070 std::string locationString = pathToAbsoluteSchemaString(location);
71 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020072
Václav Kubernát744f57f2018-06-29 22:46:26 +020073 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020074 const auto& list = boost::get<yang::list>(child);
75 return list.m_keys;
76}
77
Václav Kubernát2eaceb82018-10-08 19:56:30 +020078bool StaticSchema::isList(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020079{
Václav Kubernát744f57f2018-06-29 22:46:26 +020080 std::string locationString = pathToAbsoluteSchemaString(location);
81 auto fullName = fullNodeName(location, node);
82 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +020083 return false;
Václav Kubernát744f57f2018-06-29 22:46:26 +020084 const auto& child = children(locationString).at(fullName);
Václav Kubernátbddbb172018-06-13 16:27:39 +020085 if (child.type() != typeid(yang::list))
86 return false;
87
88 return true;
89}
90
91void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
92{
93 m_nodes.at(location).emplace(name, yang::list{keys});
94
95 m_nodes.emplace(name, std::unordered_map<std::string, NodeType>());
96}
97
Václav Kubernát2eaceb82018-10-08 19:56:30 +020098bool StaticSchema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020099{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200100 if (!isContainer(location, node))
Václav Kubernátbddbb172018-06-13 16:27:39 +0200101 return false;
Václav Kubernát744f57f2018-06-29 22:46:26 +0200102 std::string locationString = pathToAbsoluteSchemaString(location);
103 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 +0200104}
105
106void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataTypes& type)
107{
108 m_nodes.at(location).emplace(name, yang::leaf{type, {}});
109}
110
111void StaticSchema::addLeafEnum(const std::string& location, const std::string& name, std::set<std::string> enumValues)
112{
113 m_nodes.at(location).emplace(name, yang::leaf{yang::LeafDataTypes::Enum, enumValues});
114}
115
Václav Kubernát744f57f2018-06-29 22:46:26 +0200116void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +0200117{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200118 m_modules.emplace(name);
119}
Václav Kubernátbddbb172018-06-13 16:27:39 +0200120
Václav Kubernát744f57f2018-06-29 22:46:26 +0200121
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200122bool StaticSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200123{
Václav Kubernát989b5de2019-02-20 16:28:35 +0100124 auto enums = enumValues(location, node);
125 return enums.find(value) != enums.end();
Václav Kubernátbddbb172018-06-13 16:27:39 +0200126}
127
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200128bool StaticSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200129{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200130 std::string locationString = pathToAbsoluteSchemaString(location);
131 auto fullName = fullNodeName(location, node);
132 if (!nodeExists(locationString, fullName))
Václav Kubernátbddbb172018-06-13 16:27:39 +0200133 return false;
134
Václav Kubernát744f57f2018-06-29 22:46:26 +0200135 return children(locationString).at(fullName).type() == typeid(yang::leaf);
Václav Kubernátbddbb172018-06-13 16:27:39 +0200136}
137
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200138yang::LeafDataTypes StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200139{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200140 std::string locationString = pathToAbsoluteSchemaString(location);
141 return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type;
142}
143
Václav Kubernát989b5de2019-02-20 16:28:35 +0100144const std::set<std::string> StaticSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const
145{
146 std::string locationString = pathToAbsoluteSchemaString(location);
147 assert(isLeaf(location, node));
148
149 const auto& child = children(locationString).at(fullNodeName(location, node));
150 const auto& leaf = boost::get<yang::leaf>(child);
151 return leaf.m_enumValues;
152}
153
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200154// We do not test StaticSchema, so we don't need to implement recursive childNodes
155// for this class.
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200156std::set<std::string> StaticSchema::childNodes(const schemaPath_& path, const Recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200157{
158 std::string locationString = pathToAbsoluteSchemaString(path);
159 std::set<std::string> res;
160
161 auto childrenRef = children(locationString);
162
163 std::transform(childrenRef.begin(), childrenRef.end(),
164 std::inserter(res, res.end()),
165 [] (auto it) { return it.first; });
166 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200167}