blob: 36766d6ff1ce5a192b72b36b836ab3a5fe060907 [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
Jan Kundrát6e9f97a2023-12-20 14:18:52 +010011#include <filesystem>
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020012#include <functional>
Václav Kubernátcfdb9222021-07-07 22:36:24 +020013#include <libyang-cpp/Context.hpp>
Václav Kubernátc31bd602019-03-07 11:44:48 +010014#include <optional>
Václav Kubernát0d4db442018-07-18 17:18:43 +020015#include <set>
Václav Kubernát0d4db442018-07-18 17:18:43 +020016#include "ast_path.hpp"
17#include "schema.hpp"
18
Václav Kubernát0d4db442018-07-18 17:18:43 +020019/*! \class YangSchema
20 * \brief A schema class, which uses libyang for queries.
21 * */
22class YangSchema : public Schema {
23public:
24 YangSchema();
Václav Kubernátcfdb9222021-07-07 22:36:24 +020025 YangSchema(libyang::Context lyCtx);
Václav Kubernát0d4db442018-07-18 17:18:43 +020026 ~YangSchema() override;
27
Václav Kubernát59e4ee42020-07-08 17:32:45 +020028 [[nodiscard]] yang::NodeTypes nodeType(const std::string& path) const override;
29 [[nodiscard]] yang::NodeTypes nodeType(const schemaPath_& location, const ModuleNodePair& node) const override;
30 [[nodiscard]] bool isModule(const std::string& name) const override;
31 [[nodiscard]] bool listHasKey(const schemaPath_& listPath, const std::string& key) const override;
32 [[nodiscard]] bool leafIsKey(const std::string& leafPath) const override;
33 [[nodiscard]] bool isConfig(const std::string& path) const override;
34 [[nodiscard]] std::optional<std::string> defaultValue(const std::string& leafPath) const override;
35 [[nodiscard]] const std::set<std::string> listKeys(const schemaPath_& listPath) const override;
36 [[nodiscard]] yang::TypeInfo leafType(const schemaPath_& location, const ModuleNodePair& node) const override;
37 [[nodiscard]] yang::TypeInfo leafType(const std::string& path) const override;
Václav Kubernát76ba4ec2020-05-18 13:26:56 +020038 /** @brief If the leaf type is a typedef, returns the typedef name. */
Václav Kubernát59e4ee42020-07-08 17:32:45 +020039 [[nodiscard]] std::optional<std::string> leafTypeName(const std::string& path) const override;
40 [[nodiscard]] std::string leafrefPath(const std::string& leafrefPath) const override;
41 [[nodiscard]] std::set<ModuleNodePair> availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const override;
42 [[nodiscard]] std::optional<std::string> description(const std::string& path) const override;
43 [[nodiscard]] yang::Status status(const std::string& location) const override;
Václav Kubernátd8408e02020-12-02 05:13:27 +010044 [[nodiscard]] bool hasInputNodes(const std::string& path) const override;
Václav Kubernát0d4db442018-07-18 17:18:43 +020045
Jan Kundrátf59b83c2022-03-18 18:12:08 +010046 void registerModuleCallback(const std::function<std::string(const std::string_view, const std::optional<std::string_view>, const std::optional<std::string_view>, const std::optional<std::string_view>)>& clb);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020047
48 /** @short Loads a module called moduleName. */
49 void loadModule(const std::string& moduleName);
50
Václav Kubernátcfdb9222021-07-07 22:36:24 +020051 /** @short Sets enabled features. */
52 void setEnabledFeatures(const std::string& moduleName, const std::vector<std::string>& features);
Václav Kubernátbf9c6112019-11-04 16:03:35 +010053
Václav Kubernát0d4db442018-07-18 17:18:43 +020054 /** @short Adds a new module passed as a YANG string. */
55 void addSchemaString(const char* schema);
56
57 /** @short Adds a new module from a file. */
Jan Kundrát6e9f97a2023-12-20 14:18:52 +010058 void addSchemaFile(const std::filesystem::path& filename);
Václav Kubernát0d4db442018-07-18 17:18:43 +020059
60 /** @short Adds a new directory for schema lookup. */
Jan Kundrát6e9f97a2023-12-20 14:18:52 +010061 void addSchemaDirectory(const std::filesystem::path& directory);
Václav Kubernát0d4db442018-07-18 17:18:43 +020062
Václav Kubernátc31bd602019-03-07 11:44:48 +010063 /** @short Creates a new data node from a path (to be used with NETCONF edit-config) */
Václav Kubernátcfdb9222021-07-07 22:36:24 +020064 [[nodiscard]] libyang::CreatedNodes dataNodeFromPath(const std::string& path, const std::optional<const std::string> value = std::nullopt) const;
65 std::optional<libyang::Module> getYangModule(const std::string& name);
Václav Kubernátc31bd602019-03-07 11:44:48 +010066
Václav Kubernáta8789602020-07-20 15:18:19 +020067 [[nodiscard]] std::string dataPathToSchemaPath(const std::string& path);
Václav Kubernátb4e5b182020-11-16 19:55:09 +010068
Václav Kubernát0d4db442018-07-18 17:18:43 +020069private:
Václav Kubernáte7248b22020-06-26 15:38:59 +020070 friend class YangAccess;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020071 template <typename NodeType>
Václav Kubernátcfdb9222021-07-07 22:36:24 +020072 [[nodiscard]] yang::TypeInfo impl_leafType(const NodeType& node) const;
Václav Kubernát59e4ee42020-07-08 17:32:45 +020073 [[nodiscard]] std::set<std::string> modules() const;
Václav Kubernát0d4db442018-07-18 17:18:43 +020074
Václav Kubernát0d4db442018-07-18 17:18:43 +020075
Václav Kubernátcfdb9222021-07-07 22:36:24 +020076 /** @short Returns a single SchemaNode if the criteria matches only one, otherwise nullopt. */
77 [[nodiscard]] std::optional<libyang::SchemaNode> getSchemaNode(const std::string& node) const;
78 /** @short Returns a single Schema_Node if the criteria matches only one, otherwise nullopt. */
79 [[nodiscard]] std::optional<libyang::SchemaNode> getSchemaNode(const schemaPath_& listPath) const;
Václav Kubernát47a3f672019-11-08 15:42:43 +010080
81 /** @short Returns a single Schema_Node if the criteria matches only one, otherwise nullptr. */
Václav Kubernátcfdb9222021-07-07 22:36:24 +020082 [[nodiscard]] std::optional<libyang::SchemaNode> getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const;
83 libyang::Context m_context;
Václav Kubernát47a3f672019-11-08 15:42:43 +010084
Václav Kubernátcfdb9222021-07-07 22:36:24 +020085 [[nodiscard]] std::optional<libyang::SchemaNode> impl_getSchemaNode(const std::string& node) const;
Václav Kubernát0d4db442018-07-18 17:18:43 +020086};