blob: a414dc9d2689754a995cb428355d2d7cb0dc84bc [file] [log] [blame]
Václav Kubernát624a8872018-03-02 17:28:47 +01001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
Václav Kubernátd6662962018-03-22 17:41:33 +01003 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
Václav Kubernát624a8872018-03-02 17:28:47 +01004 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
9#pragma once
10
Václav Kubernátb96eef72018-05-04 19:10:22 +020011#include <boost/variant.hpp>
12#include <set>
Václav Kubernát624a8872018-03-02 17:28:47 +010013#include <stdexcept>
14#include <unordered_map>
Václav Kubernátff2c9f62018-05-16 20:26:31 +020015#include "ast.hpp"
Václav Kubernátd6662962018-03-22 17:41:33 +010016
Václav Kubernát48fc3832018-05-28 14:21:22 +020017namespace yang {
18struct container {
19};
20struct list {
21 std::set<std::string> m_keys;
22};
Václav Kubernátb96eef72018-05-04 19:10:22 +020023}
Václav Kubernátd6662962018-03-22 17:41:33 +010024
Václav Kubernát814fa412018-05-25 19:47:18 +020025struct nodeToString : public boost::static_visitor<std::string> {
26 std::string operator()(const nodeup_&) const
27 {
28 return "..";
29 }
30 template <class T>
31 std::string operator()(const T& node) const
32 {
33 return node.m_name;
34 }
35};
Václav Kubernátb96eef72018-05-04 19:10:22 +020036
Václav Kubernát48fc3832018-05-28 14:21:22 +020037using NodeType = boost::variant<yang::container, yang::list>;
Václav Kubernátb96eef72018-05-04 19:10:22 +020038
Václav Kubernát624a8872018-03-02 17:28:47 +010039
40class InvalidNodeException : public std::invalid_argument {
41public:
42 using std::invalid_argument::invalid_argument;
43 ~InvalidNodeException() override;
Václav Kubernát624a8872018-03-02 17:28:47 +010044};
45
Václav Kubernát48fc3832018-05-28 14:21:22 +020046/*! \class Schema
47 * \brief The class representing the schema, that the user traverses.
Václav Kubernát624a8872018-03-02 17:28:47 +010048 * */
Václav Kubernát48fc3832018-05-28 14:21:22 +020049class Schema {
Václav Kubernát624a8872018-03-02 17:28:47 +010050public:
Václav Kubernát48fc3832018-05-28 14:21:22 +020051 Schema();
Václav Kubernátb96eef72018-05-04 19:10:22 +020052 bool nodeExists(const std::string& location, const std::string& name) const;
Václav Kubernátd6662962018-03-22 17:41:33 +010053
Václav Kubernát814fa412018-05-25 19:47:18 +020054 bool isContainer(const path_& location, const std::string& name) const;
Václav Kubernátb96eef72018-05-04 19:10:22 +020055 void addContainer(const std::string& location, const std::string& name);
Václav Kubernát814fa412018-05-25 19:47:18 +020056 const std::set<std::string>& listKeys(const path_& location, const std::string& name) const;
57 bool listHasKey(const path_& location, const std::string& name, const std::string& key) const;
58 bool isList(const path_& location, const std::string& name) const;
Václav Kubernátb96eef72018-05-04 19:10:22 +020059 void addList(const std::string& location, const std::string& name, const std::set<std::string>& keys);
Václav Kubernát624a8872018-03-02 17:28:47 +010060
61private:
Václav Kubernátb96eef72018-05-04 19:10:22 +020062 const std::unordered_map<std::string, NodeType>& children(const std::string& name) const;
Václav Kubernát624a8872018-03-02 17:28:47 +010063
Václav Kubernátb96eef72018-05-04 19:10:22 +020064 std::unordered_map<std::string, std::unordered_map<std::string, NodeType>> m_nodes;
Václav Kubernát624a8872018-03-02 17:28:47 +010065};