blob: 199cb9603715104b25e01c341ec023da7bfbeb56 [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át509ce652019-05-29 19:46:44 +020012#include <boost/variant/variant.hpp>
Václav Kubernátb96eef72018-05-04 19:10:22 +020013#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,
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020030 Int8,
31 Uint8,
32 Int16,
33 Uint16,
34 Int32,
35 Uint32,
36 Int64,
37 Uint64,
Václav Kubernátebca2552018-06-08 19:06:02 +020038 Enum,
Václav Kubernátab538992019-03-06 15:30:50 +010039 Binary,
Václav Kubernáteeb38842019-03-20 19:46:05 +010040 IdentityRef,
Václav Kubernátebca2552018-06-08 19:06:02 +020041};
42
Václav Kubernát48fc3832018-05-28 14:21:22 +020043struct container {
Václav Kubernátb61336d2018-05-28 17:35:03 +020044 yang::ContainerTraits m_presence;
Václav Kubernát48fc3832018-05-28 14:21:22 +020045};
46struct list {
47 std::set<std::string> m_keys;
48};
Václav Kubernáteeb38842019-03-20 19:46:05 +010049
Václav Kubernát07204242018-06-04 18:12:09 +020050struct leaf {
Václav Kubernátebca2552018-06-08 19:06:02 +020051 yang::LeafDataTypes m_type;
52 std::set<std::string> m_enumValues;
Václav Kubernáteeb38842019-03-20 19:46:05 +010053 ModuleValuePair m_identBase;
Václav Kubernát07204242018-06-04 18:12:09 +020054};
Václav Kubernát744f57f2018-06-29 22:46:26 +020055
56struct module {
57};
Václav Kubernátb96eef72018-05-04 19:10:22 +020058}
Václav Kubernátd6662962018-03-22 17:41:33 +010059
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020060enum class Recursion {
61 NonRecursive,
62 Recursive
63};
Václav Kubernátf2e463f2018-05-28 15:51:08 +020064
Václav Kubernáteeb38842019-03-20 19:46:05 +010065enum class Prefixes {
66 Always,
67 WhenNeeded
68};
69
Václav Kubernát744f57f2018-06-29 22:46:26 +020070using NodeType = boost::variant<yang::container, yang::list, yang::leaf, yang::module>;
Václav Kubernátb96eef72018-05-04 19:10:22 +020071
Václav Kubernát624a8872018-03-02 17:28:47 +010072
73class InvalidNodeException : public std::invalid_argument {
74public:
75 using std::invalid_argument::invalid_argument;
76 ~InvalidNodeException() override;
Václav Kubernát624a8872018-03-02 17:28:47 +010077};
78
Václav Kubernát48fc3832018-05-28 14:21:22 +020079/*! \class Schema
Václav Kubernátbddbb172018-06-13 16:27:39 +020080 * \brief A base schema class for schemas
Václav Kubernát624a8872018-03-02 17:28:47 +010081 * */
Václav Kubernát744f57f2018-06-29 22:46:26 +020082
83using ModuleNodePair = std::pair<boost::optional<std::string>, std::string>;
84
Václav Kubernát48fc3832018-05-28 14:21:22 +020085class Schema {
Václav Kubernát624a8872018-03-02 17:28:47 +010086public:
Václav Kubernátbddbb172018-06-13 16:27:39 +020087 virtual ~Schema();
Václav Kubernátd6662962018-03-22 17:41:33 +010088
Václav Kubernát2eaceb82018-10-08 19:56:30 +020089 virtual bool isContainer(const schemaPath_& location, const ModuleNodePair& node) const = 0;
90 virtual bool isLeaf(const schemaPath_& location, const ModuleNodePair& node) const = 0;
91 virtual bool isModule(const schemaPath_& location, const std::string& name) const = 0;
92 virtual bool isList(const schemaPath_& location, const ModuleNodePair& node) const = 0;
93 virtual bool isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const = 0;
94 virtual bool leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const = 0;
Václav Kubernáteeb38842019-03-20 19:46:05 +010095 virtual bool leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const = 0;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020096 virtual bool listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const = 0;
Václav Kubernát2eaceb82018-10-08 19:56:30 +020097 virtual const std::set<std::string> listKeys(const schemaPath_& location, const ModuleNodePair& node) const = 0;
98 virtual yang::LeafDataTypes leafType(const schemaPath_& location, const ModuleNodePair& node) const = 0;
Václav Kubernáteeb38842019-03-20 19:46:05 +010099 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 +0100100 virtual const std::set<std::string> enumValues(const schemaPath_& location, const ModuleNodePair& node) const = 0;
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200101 virtual std::set<std::string> childNodes(const schemaPath_& path, const Recursion recursion) const = 0;
Václav Kubernát624a8872018-03-02 17:28:47 +0100102
103private:
Václav Kubernátb96eef72018-05-04 19:10:22 +0200104 const std::unordered_map<std::string, NodeType>& children(const std::string& name) const;
Václav Kubernát624a8872018-03-02 17:28:47 +0100105
Václav Kubernátb96eef72018-05-04 19:10:22 +0200106 std::unordered_map<std::string, std::unordered_map<std::string, NodeType>> m_nodes;
Václav Kubernát624a8872018-03-02 17:28:47 +0100107};