Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 1 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 4 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 5 | * |
| 6 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #pragma once |
| 11 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 12 | #include <boost/variant.hpp> |
| 13 | #include <set> |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 14 | #include <stdexcept> |
| 15 | #include <unordered_map> |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 16 | #include "ast_path.hpp" |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 17 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 18 | using ModuleValuePair = std::pair<boost::optional<std::string>, std::string>; |
| 19 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 20 | namespace yang { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 21 | enum class ContainerTraits { |
| 22 | Presence, |
| 23 | None, |
| 24 | }; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 25 | |
| 26 | enum class LeafDataTypes { |
| 27 | String, |
| 28 | Decimal, |
| 29 | Bool, |
| 30 | Int, |
| 31 | Uint, |
| 32 | Enum, |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 33 | Binary, |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 34 | IdentityRef, |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 37 | struct container { |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 38 | yang::ContainerTraits m_presence; |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 39 | }; |
| 40 | struct list { |
| 41 | std::set<std::string> m_keys; |
| 42 | }; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 43 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 44 | struct leaf { |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 45 | yang::LeafDataTypes m_type; |
| 46 | std::set<std::string> m_enumValues; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 47 | ModuleValuePair m_identBase; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 48 | }; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 49 | |
| 50 | struct module { |
| 51 | }; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 52 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 53 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 54 | enum class Recursion { |
| 55 | NonRecursive, |
| 56 | Recursive |
| 57 | }; |
Václav Kubernát | f2e463f | 2018-05-28 15:51:08 +0200 | [diff] [blame] | 58 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 59 | enum class Prefixes { |
| 60 | Always, |
| 61 | WhenNeeded |
| 62 | }; |
| 63 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 64 | using NodeType = boost::variant<yang::container, yang::list, yang::leaf, yang::module>; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 65 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 66 | |
| 67 | class InvalidNodeException : public std::invalid_argument { |
| 68 | public: |
| 69 | using std::invalid_argument::invalid_argument; |
| 70 | ~InvalidNodeException() override; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 73 | /*! \class Schema |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 74 | * \brief A base schema class for schemas |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 75 | * */ |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 76 | |
| 77 | using ModuleNodePair = std::pair<boost::optional<std::string>, std::string>; |
| 78 | |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 79 | class Schema { |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 80 | public: |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 81 | virtual ~Schema(); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 82 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 83 | 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át | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 89 | virtual bool leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const = 0; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 90 | virtual bool listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const = 0; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 91 | 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át | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 93 | virtual const std::set<std::string> validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const = 0; |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 94 | virtual const std::set<std::string> enumValues(const schemaPath_& location, const ModuleNodePair& node) const = 0; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 95 | virtual std::set<std::string> childNodes(const schemaPath_& path, const Recursion recursion) const = 0; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 96 | |
| 97 | private: |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 98 | const std::unordered_map<std::string, NodeType>& children(const std::string& name) const; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 99 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 100 | std::unordered_map<std::string, std::unordered_map<std::string, NodeType>> m_nodes; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 101 | }; |