blob: 1760b5d25c56fdcd33b20b1b1fc3711d7f60b0d9 [file] [log] [blame]
Václav Kubernát48fc3832018-05-28 14:21:22 +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 <iostream>
10#include "schema.hpp"
11#include "utils.hpp"
12
13InvalidNodeException::~InvalidNodeException() = default;
14
Václav Kubernátf2e463f2018-05-28 15:51:08 +020015struct nodeToSchemaString : public boost::static_visitor<std::string> {
16 std::string operator()(const nodeup_&) const
17 {
18 return "..";
19 }
20 template <class T>
21 std::string operator()(const T& node) const
22 {
23 return node.m_name;
24 }
25};
Václav Kubernát48fc3832018-05-28 14:21:22 +020026
27std::string pathToString(const path_& path)
28{
29 std::string res;
30 for (const auto it : path.m_nodes)
Václav Kubernátf2e463f2018-05-28 15:51:08 +020031 res = joinPaths(res, boost::apply_visitor(nodeToSchemaString(), it));
Václav Kubernát48fc3832018-05-28 14:21:22 +020032 return res;
33}
34
35Schema::Schema()
36{
37 m_nodes.emplace("", std::unordered_map<std::string, NodeType>());
38}
39
40const std::unordered_map<std::string, NodeType>& Schema::children(const std::string& name) const
41{
42 return m_nodes.at(name);
43}
44
45bool Schema::nodeExists(const std::string& location, const std::string& name) const
46{
47 if (name.empty())
48 return true;
49 const auto& childrenRef = children(location);
50
51 return childrenRef.find(name) != childrenRef.end();
52}
53
54bool Schema::isContainer(const path_& location, const std::string& name) const
55{
56 std::string locationString = pathToString(location);
57 if (!nodeExists(locationString, name))
58 return false;
59
60 return children(locationString).at(name).type() == typeid(yang::container);
61}
62
63void Schema::addContainer(const std::string& location, const std::string& name)
64{
65 m_nodes.at(location).emplace(name, yang::container{});
66
67 //create a new set of children for the new node
68 std::string key = joinPaths(location, name);
69 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
70}
71
72
73bool Schema::listHasKey(const path_& location, const std::string& name, const std::string& key) const
74{
75 std::string locationString = pathToString(location);
76 assert(isList(location, name));
77
78 const auto& child = children(locationString).at(name);
79 const auto& list = boost::get<yang::list>(child);
80 return list.m_keys.find(key) != list.m_keys.end();
81}
82
83const std::set<std::string>& Schema::listKeys(const path_& location, const std::string& name) const
84{
85 std::string locationString = pathToString(location);
86 assert(isList(location, name));
87
88 const auto& child = children(locationString).at(name);
89 const auto& list = boost::get<yang::list>(child);
90 return list.m_keys;
91}
92
93bool Schema::isList(const path_& location, const std::string& name) const
94{
95 std::string locationString = pathToString(location);
96 if (!nodeExists(locationString, name))
97 return false;
98 const auto& child = children(locationString).at(name);
99 if (child.type() != typeid(yang::list))
100 return false;
101
102 return true;
103}
104
105void Schema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
106{
107 m_nodes.at(location).emplace(name, yang::list{keys});
108
109 m_nodes.emplace(name, std::unordered_map<std::string, NodeType>());
110}