blob: 87feeaa9725d6e094a70e2c4c7bea2026d665667 [file] [log] [blame]
Václav Kubernátbddbb172018-06-13 16:27:39 +02001
Václav Kubernát624a8872018-03-02 17:28:47 +01002/*
3 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
Václav Kubernátd6662962018-03-22 17:41:33 +01004 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
Václav Kubernát624a8872018-03-02 17:28:47 +01005 *
6 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
7 *
8*/
9
10#pragma once
11
Václav Kubernátb96eef72018-05-04 19:10:22 +020012#include <boost/variant.hpp>
13#include <set>
Václav Kubernát624a8872018-03-02 17:28:47 +010014#include <stdexcept>
15#include <unordered_map>
Václav Kubernát24df80e2018-06-06 15:18:03 +020016#include "ast_path.hpp"
Václav Kubernátd6662962018-03-22 17:41:33 +010017
Václav Kubernáteeb38842019-03-20 19:46:05 +010018using ModuleValuePair = std::pair<boost::optional<std::string>, std::string>;
19
Václav Kubernát48fc3832018-05-28 14:21:22 +020020namespace yang {
Václav Kubernátb61336d2018-05-28 17:35:03 +020021enum class ContainerTraits {
22 Presence,
23 None,
24};
Václav Kubernátebca2552018-06-08 19:06:02 +020025
26enum class LeafDataTypes {
27 String,
28 Decimal,
29 Bool,
30 Int,
31 Uint,
32 Enum,
Václav Kubernátab538992019-03-06 15:30:50 +010033 Binary,
Václav Kubernáteeb38842019-03-20 19:46:05 +010034 IdentityRef,
Václav Kubernátebca2552018-06-08 19:06:02 +020035};
36
Václav Kubernát48fc3832018-05-28 14:21:22 +020037struct container {
Václav Kubernátb61336d2018-05-28 17:35:03 +020038 yang::ContainerTraits m_presence;
Václav Kubernát48fc3832018-05-28 14:21:22 +020039};
40struct list {
41 std::set<std::string> m_keys;
42};
Václav Kubernáteeb38842019-03-20 19:46:05 +010043
Václav Kubernát07204242018-06-04 18:12:09 +020044struct leaf {
Václav Kubernátebca2552018-06-08 19:06:02 +020045 yang::LeafDataTypes m_type;
46 std::set<std::string> m_enumValues;
Václav Kubernáteeb38842019-03-20 19:46:05 +010047 ModuleValuePair m_identBase;
Václav Kubernát07204242018-06-04 18:12:09 +020048};
Václav Kubernát744f57f2018-06-29 22:46:26 +020049
50struct module {
51};
Václav Kubernátb96eef72018-05-04 19:10:22 +020052}
Václav Kubernátd6662962018-03-22 17:41:33 +010053
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020054enum class Recursion {
55 NonRecursive,
56 Recursive
57};
Václav Kubernátf2e463f2018-05-28 15:51:08 +020058
Václav Kubernáteeb38842019-03-20 19:46:05 +010059enum class Prefixes {
60 Always,
61 WhenNeeded
62};
63
Václav Kubernát744f57f2018-06-29 22:46:26 +020064using NodeType = boost::variant<yang::container, yang::list, yang::leaf, yang::module>;
Václav Kubernátb96eef72018-05-04 19:10:22 +020065
Václav Kubernát624a8872018-03-02 17:28:47 +010066
67class InvalidNodeException : public std::invalid_argument {
68public:
69 using std::invalid_argument::invalid_argument;
70 ~InvalidNodeException() override;
Václav Kubernát624a8872018-03-02 17:28:47 +010071};
72
Václav Kubernát48fc3832018-05-28 14:21:22 +020073/*! \class Schema
Václav Kubernátbddbb172018-06-13 16:27:39 +020074 * \brief A base schema class for schemas
Václav Kubernát624a8872018-03-02 17:28:47 +010075 * */
Václav Kubernát744f57f2018-06-29 22:46:26 +020076
77using ModuleNodePair = std::pair<boost::optional<std::string>, std::string>;
78
Václav Kubernát48fc3832018-05-28 14:21:22 +020079class Schema {
Václav Kubernát624a8872018-03-02 17:28:47 +010080public:
Václav Kubernátbddbb172018-06-13 16:27:39 +020081 virtual ~Schema();
Václav Kubernátd6662962018-03-22 17:41:33 +010082
Václav Kubernát2eaceb82018-10-08 19:56:30 +020083 virtual bool isContainer(const schemaPath_& location, const ModuleNodePair& node) const = 0;
84 virtual bool isLeaf(const schemaPath_& location, const ModuleNodePair& node) const = 0;
85 virtual bool isModule(const schemaPath_& location, const std::string& name) const = 0;
86 virtual bool isList(const schemaPath_& location, const ModuleNodePair& node) const = 0;
87 virtual bool isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const = 0;
88 virtual bool leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const = 0;
Václav Kubernáteeb38842019-03-20 19:46:05 +010089 virtual bool leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const = 0;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020090 virtual bool listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const = 0;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020091 virtual const std::set<std::string> listKeys(const schemaPath_& location, const ModuleNodePair& node) const = 0;
92 virtual yang::LeafDataTypes leafType(const schemaPath_& location, const ModuleNodePair& node) const = 0;
Václav Kubernáteeb38842019-03-20 19:46:05 +010093 virtual const std::set<std::string> validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const = 0;
Václav Kubernát989b5de2019-02-20 16:28:35 +010094 virtual const std::set<std::string> enumValues(const schemaPath_& location, const ModuleNodePair& node) const = 0;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020095 virtual std::set<std::string> childNodes(const schemaPath_& path, const Recursion recursion) const = 0;
Václav Kubernát624a8872018-03-02 17:28:47 +010096
97private:
Václav Kubernátb96eef72018-05-04 19:10:22 +020098 const std::unordered_map<std::string, NodeType>& children(const std::string& name) const;
Václav Kubernát624a8872018-03-02 17:28:47 +010099
Václav Kubernátb96eef72018-05-04 19:10:22 +0200100 std::unordered_map<std::string, std::unordered_map<std::string, NodeType>> m_nodes;
Václav Kubernát624a8872018-03-02 17:28:47 +0100101};