blob: dc4509778bfabf4596ff00b425d6736aba3435ef [file] [log] [blame]
Václav Kubernát0d4db442018-07-18 17:18:43 +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#pragma once
10
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020011#include <functional>
Václav Kubernátc31bd602019-03-07 11:44:48 +010012#include <optional>
Václav Kubernát0d4db442018-07-18 17:18:43 +020013#include <set>
Václav Kubernát0d4db442018-07-18 17:18:43 +020014#include "ast_path.hpp"
15#include "schema.hpp"
16
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +020017namespace libyang {
Václav Kubernát0d4db442018-07-18 17:18:43 +020018class Context;
Václav Kubernát0d4db442018-07-18 17:18:43 +020019class Schema_Node;
Václav Kubernát3a99f002020-03-31 02:27:41 +020020class Schema_Node_Leaf;
Václav Kubernátc31bd602019-03-07 11:44:48 +010021class Data_Node;
22class Module;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +020023}
Václav Kubernát0d4db442018-07-18 17:18:43 +020024
25/*! \class YangSchema
26 * \brief A schema class, which uses libyang for queries.
27 * */
28class YangSchema : public Schema {
29public:
30 YangSchema();
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010031 YangSchema(std::shared_ptr<libyang::Context> lyCtx);
Václav Kubernát0d4db442018-07-18 17:18:43 +020032 ~YangSchema() override;
33
Václav Kubernát59e4ee42020-07-08 17:32:45 +020034 [[nodiscard]] yang::NodeTypes nodeType(const std::string& path) const override;
35 [[nodiscard]] yang::NodeTypes nodeType(const schemaPath_& location, const ModuleNodePair& node) const override;
36 [[nodiscard]] bool isModule(const std::string& name) const override;
37 [[nodiscard]] bool listHasKey(const schemaPath_& listPath, const std::string& key) const override;
38 [[nodiscard]] bool leafIsKey(const std::string& leafPath) const override;
39 [[nodiscard]] bool isConfig(const std::string& path) const override;
40 [[nodiscard]] std::optional<std::string> defaultValue(const std::string& leafPath) const override;
41 [[nodiscard]] const std::set<std::string> listKeys(const schemaPath_& listPath) const override;
42 [[nodiscard]] yang::TypeInfo leafType(const schemaPath_& location, const ModuleNodePair& node) const override;
43 [[nodiscard]] yang::TypeInfo leafType(const std::string& path) const override;
Václav Kubernát76ba4ec2020-05-18 13:26:56 +020044 /** @brief If the leaf type is a typedef, returns the typedef name. */
Václav Kubernát59e4ee42020-07-08 17:32:45 +020045 [[nodiscard]] std::optional<std::string> leafTypeName(const std::string& path) const override;
46 [[nodiscard]] std::string leafrefPath(const std::string& leafrefPath) const override;
47 [[nodiscard]] std::set<ModuleNodePair> availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const override;
48 [[nodiscard]] std::optional<std::string> description(const std::string& path) const override;
49 [[nodiscard]] yang::Status status(const std::string& location) const override;
Václav Kubernátd8408e02020-12-02 05:13:27 +010050 [[nodiscard]] bool hasInputNodes(const std::string& path) const override;
Václav Kubernát0d4db442018-07-18 17:18:43 +020051
Václav Kubernátb52dc252019-12-04 13:03:39 +010052 void registerModuleCallback(const std::function<std::string(const char*, const char*, const char*, const char*)>& clb);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020053
54 /** @short Loads a module called moduleName. */
55 void loadModule(const std::string& moduleName);
56
Václav Kubernátbf9c6112019-11-04 16:03:35 +010057 /** @short Enables a feature called featureName on a module called moduleName. */
58 void enableFeature(const std::string& moduleName, const std::string& featureName);
59
Václav Kubernát0d4db442018-07-18 17:18:43 +020060 /** @short Adds a new module passed as a YANG string. */
61 void addSchemaString(const char* schema);
62
63 /** @short Adds a new module from a file. */
64 void addSchemaFile(const char* filename);
65
66 /** @short Adds a new directory for schema lookup. */
67 void addSchemaDirectory(const char* directoryName);
68
Václav Kubernátc31bd602019-03-07 11:44:48 +010069 /** @short Creates a new data node from a path (to be used with NETCONF edit-config) */
Václav Kubernát59e4ee42020-07-08 17:32:45 +020070 [[nodiscard]] std::shared_ptr<libyang::Data_Node> dataNodeFromPath(const std::string& path, const std::optional<const std::string> value = std::nullopt) const;
Václav Kubernátc31bd602019-03-07 11:44:48 +010071 std::shared_ptr<libyang::Module> getYangModule(const std::string& name);
72
Václav Kubernáta8789602020-07-20 15:18:19 +020073 [[nodiscard]] std::string dataPathToSchemaPath(const std::string& path);
Václav Kubernátb4e5b182020-11-16 19:55:09 +010074
Václav Kubernát0d4db442018-07-18 17:18:43 +020075private:
Václav Kubernáte7248b22020-06-26 15:38:59 +020076 friend class YangAccess;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020077 template <typename NodeType>
Václav Kubernát59e4ee42020-07-08 17:32:45 +020078 [[nodiscard]] yang::TypeInfo impl_leafType(const std::shared_ptr<libyang::Schema_Node>& node) const;
79 [[nodiscard]] std::set<std::string> modules() const;
Václav Kubernát0d4db442018-07-18 17:18:43 +020080
Václav Kubernát0d4db442018-07-18 17:18:43 +020081
82 /** @short Returns a single Schema_Node if the criteria matches only one, otherwise nullptr. */
Václav Kubernát59e4ee42020-07-08 17:32:45 +020083 [[nodiscard]] std::shared_ptr<libyang::Schema_Node> getSchemaNode(const std::string& node) const;
Václav Kubernát2db124c2020-05-28 21:58:36 +020084 /** @short Returns a single Schema_Node if the criteria matches only one, otherwise nullptr. */
Václav Kubernát59e4ee42020-07-08 17:32:45 +020085 [[nodiscard]] std::shared_ptr<libyang::Schema_Node> getSchemaNode(const schemaPath_& listPath) const;
Václav Kubernát47a3f672019-11-08 15:42:43 +010086
87 /** @short Returns a single Schema_Node if the criteria matches only one, otherwise nullptr. */
Václav Kubernát59e4ee42020-07-08 17:32:45 +020088 [[nodiscard]] std::shared_ptr<libyang::Schema_Node> getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +020089 std::shared_ptr<libyang::Context> m_context;
Václav Kubernát47a3f672019-11-08 15:42:43 +010090
Václav Kubernát59e4ee42020-07-08 17:32:45 +020091 [[nodiscard]] std::shared_ptr<libyang::Schema_Node> impl_getSchemaNode(const std::string& node) const;
Václav Kubernát0d4db442018-07-18 17:18:43 +020092};