blob: 65debf0297c0e391b86f9f4705ad708933c12bb9 [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
19const std::unordered_map<std::string, NodeType>& StaticSchema::children(const std::string& name) const
20{
21 return m_nodes.at(name);
22}
23
24bool StaticSchema::nodeExists(const std::string& location, const std::string& name) const
25{
26 if (name.empty())
27 return true;
28 const auto& childrenRef = children(location);
29
30 return childrenRef.find(name) != childrenRef.end();
31}
32
33bool StaticSchema::isContainer(const path_& location, const std::string& name) const
34{
35 std::string locationString = pathToSchemaString(location);
36 if (!nodeExists(locationString, name))
37 return false;
38
39 return children(locationString).at(name).type() == typeid(yang::container);
40}
41
42void StaticSchema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence)
43{
44 m_nodes.at(location).emplace(name, yang::container{isPresence});
45
46 //create a new set of children for the new node
47 std::string key = joinPaths(location, name);
48 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
49}
50
51
52bool StaticSchema::listHasKey(const path_& location, const std::string& name, const std::string& key) const
53{
54 std::string locationString = pathToSchemaString(location);
55 assert(isList(location, name));
56
57 const auto& child = children(locationString).at(name);
58 const auto& list = boost::get<yang::list>(child);
59 return list.m_keys.find(key) != list.m_keys.end();
60}
61
62const std::set<std::string>& StaticSchema::listKeys(const path_& location, const std::string& name) const
63{
64 std::string locationString = pathToSchemaString(location);
65 assert(isList(location, name));
66
67 const auto& child = children(locationString).at(name);
68 const auto& list = boost::get<yang::list>(child);
69 return list.m_keys;
70}
71
72bool StaticSchema::isList(const path_& location, const std::string& name) const
73{
74 std::string locationString = pathToSchemaString(location);
75 if (!nodeExists(locationString, name))
76 return false;
77 const auto& child = children(locationString).at(name);
78 if (child.type() != typeid(yang::list))
79 return false;
80
81 return true;
82}
83
84void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
85{
86 m_nodes.at(location).emplace(name, yang::list{keys});
87
88 m_nodes.emplace(name, std::unordered_map<std::string, NodeType>());
89}
90
91bool StaticSchema::isPresenceContainer(const path_& location, const std::string& name) const
92{
93 if (!isContainer(location, name))
94 return false;
95 std::string locationString = pathToSchemaString(location);
96 return boost::get<yang::container>(children(locationString).at(name)).m_presence == yang::ContainerTraits::Presence;
97}
98
99void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataTypes& type)
100{
101 m_nodes.at(location).emplace(name, yang::leaf{type, {}});
102}
103
104void StaticSchema::addLeafEnum(const std::string& location, const std::string& name, std::set<std::string> enumValues)
105{
106 m_nodes.at(location).emplace(name, yang::leaf{yang::LeafDataTypes::Enum, enumValues});
107}
108
109bool StaticSchema::leafEnumHasValue(const path_& location, const std::string& name, const std::string& value) const
110{
111 std::string locationString = pathToSchemaString(location);
112 assert(isLeaf(location, name));
113
114 const auto& child = children(locationString).at(name);
115 const auto& list = boost::get<yang::leaf>(child);
116 return list.m_enumValues.find(value) != list.m_enumValues.end();
117}
118
119bool StaticSchema::isLeaf(const path_& location, const std::string& name) const
120{
121 std::string locationString = pathToSchemaString(location);
122 if (!nodeExists(locationString, name))
123 return false;
124
125 return children(locationString).at(name).type() == typeid(yang::leaf);
126}
127
128yang::LeafDataTypes StaticSchema::leafType(const path_& location, const std::string& name) const
129{
130 std::string locationString = pathToSchemaString(location);
131 return boost::get<yang::leaf>(children(locationString).at(name)).m_type;
132}