Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 1 | /* |
| 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 | #include <libyang/Libyang.hpp> |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 10 | #include <libyang/Tree_Data.hpp> |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 11 | #include <libyang/Tree_Schema.hpp> |
| 12 | #include <string_view> |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 13 | #include "UniqueResource.hpp" |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 14 | #include "utils.hpp" |
| 15 | #include "yang_schema.hpp" |
| 16 | |
| 17 | class YangLoadError : public std::runtime_error { |
| 18 | public: |
| 19 | using std::runtime_error::runtime_error; |
| 20 | ~YangLoadError() override = default; |
| 21 | }; |
| 22 | |
| 23 | class UnsupportedYangTypeException : public std::runtime_error { |
| 24 | public: |
| 25 | using std::runtime_error::runtime_error; |
| 26 | ~UnsupportedYangTypeException() override = default; |
| 27 | }; |
| 28 | |
| 29 | class InvalidSchemaQueryException : public std::runtime_error { |
| 30 | public: |
| 31 | using std::runtime_error::runtime_error; |
| 32 | ~InvalidSchemaQueryException() override = default; |
| 33 | }; |
| 34 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 35 | template <typename T> |
| 36 | std::string pathToYangAbsSchemPath(const T& path) |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 37 | { |
| 38 | std::string res = "/"; |
| 39 | std::string currentModule; |
| 40 | |
| 41 | for (const auto& it : path.m_nodes) { |
| 42 | const auto name = nodeToSchemaString(it); |
| 43 | |
| 44 | if (it.m_suffix.type() == typeid(module_)) { |
| 45 | currentModule = name; |
| 46 | continue; |
| 47 | } else { |
| 48 | res += currentModule + ":"; |
| 49 | res += name + "/"; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return res; |
| 54 | } |
| 55 | |
| 56 | YangSchema::YangSchema() |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 57 | : m_context(std::make_shared<libyang::Context>(nullptr, LY_CTX_DISABLE_SEARCHDIRS | LY_CTX_DISABLE_SEARCHDIR_CWD)) |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 58 | { |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 61 | YangSchema::YangSchema(std::shared_ptr<libyang::Context> lyCtx) |
| 62 | : m_context(lyCtx) |
| 63 | { |
| 64 | |
| 65 | } |
| 66 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 67 | YangSchema::~YangSchema() = default; |
| 68 | |
| 69 | void YangSchema::addSchemaString(const char* schema) |
| 70 | { |
| 71 | if (!m_context->parse_module_mem(schema, LYS_IN_YANG)) { |
| 72 | throw YangLoadError("Couldn't load schema"); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void YangSchema::addSchemaDirectory(const char* directoryName) |
| 77 | { |
| 78 | if (m_context->set_searchdir(directoryName)) { |
| 79 | throw YangLoadError("Couldn't add schema search directory"); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void YangSchema::addSchemaFile(const char* filename) |
| 84 | { |
| 85 | if (!m_context->parse_module_path(filename, LYS_IN_YANG)) { |
| 86 | throw YangLoadError("Couldn't load schema"); |
| 87 | } |
| 88 | } |
| 89 | |
Václav Kubernát | 75877de | 2019-11-20 17:43:02 +0100 | [diff] [blame] | 90 | bool YangSchema::isModule(const std::string& name) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 91 | { |
| 92 | const auto set = modules(); |
| 93 | return set.find(name) != set.end(); |
| 94 | } |
| 95 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 96 | bool YangSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 97 | { |
| 98 | if (!isList(location, node)) |
| 99 | return false; |
| 100 | const auto keys = listKeys(location, node); |
| 101 | return keys.find(key) != keys.end(); |
| 102 | } |
| 103 | |
Václav Kubernát | c386679 | 2020-02-20 14:12:56 +0100 | [diff] [blame] | 104 | bool YangSchema::leafIsKey(const std::string& leafPath) const |
| 105 | { |
| 106 | auto node = getSchemaNode(leafPath); |
| 107 | if (!node || node->nodetype() != LYS_LEAF) |
| 108 | return false; |
| 109 | |
| 110 | return libyang::Schema_Node_Leaf{node}.is_key().get(); |
| 111 | } |
| 112 | |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 113 | libyang::S_Schema_Node YangSchema::impl_getSchemaNode(const std::string& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 114 | { |
Václav Kubernát | f2b91e0 | 2019-04-11 15:36:48 +0200 | [diff] [blame] | 115 | // If no node is found find_path prints an error message, so we have to |
| 116 | // disable logging |
| 117 | // https://github.com/CESNET/libyang/issues/753 |
| 118 | { |
| 119 | int oldOptions; |
| 120 | auto logBlocker = make_unique_resource( |
| 121 | [&oldOptions]() { |
| 122 | oldOptions = libyang::set_log_options(0); |
| 123 | }, |
| 124 | [&oldOptions]() { |
| 125 | libyang::set_log_options(oldOptions); |
| 126 | }); |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 127 | return m_context->get_node(nullptr, node.c_str()); |
Václav Kubernát | f2b91e0 | 2019-04-11 15:36:48 +0200 | [diff] [blame] | 128 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 131 | |
| 132 | libyang::S_Schema_Node YangSchema::getSchemaNode(const std::string& node) const |
| 133 | { |
| 134 | return impl_getSchemaNode(node); |
| 135 | } |
| 136 | |
| 137 | libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const |
| 138 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 139 | std::string absPath = joinPaths(pathToSchemaString(location, Prefixes::Always), fullNodeName(location, node)); |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 140 | |
| 141 | return impl_getSchemaNode(absPath); |
| 142 | } |
| 143 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 144 | const std::set<std::string> YangSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 145 | { |
| 146 | std::set<std::string> keys; |
| 147 | if (!isList(location, node)) |
| 148 | return keys; |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 149 | libyang::Schema_Node_List list(getSchemaNode(location, node)); |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 150 | const auto& keysVec = list.keys(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 151 | |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 152 | std::transform(keysVec.begin(), keysVec.end(), std::inserter(keys, keys.begin()), [](const auto& it) { return it->name(); }); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 153 | return keys; |
| 154 | } |
| 155 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 156 | namespace { |
| 157 | std::set<enum_> enumValues(const libyang::S_Schema_Node_Leaf& leaf) |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 158 | { |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 159 | auto type = leaf->type(); |
| 160 | auto enm = type->info()->enums()->enm(); |
| 161 | // The enum can be a derived type and enm() only returns values, |
| 162 | // if that specific typedef changed the possible values. So we go |
| 163 | // up the hierarchy until we find a typedef that defined these values. |
| 164 | while (enm.empty()) { |
| 165 | type = type->der()->type(); |
| 166 | enm = type->info()->enums()->enm(); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 167 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 168 | |
| 169 | std::vector<libyang::S_Type_Enum> enabled; |
| 170 | std::copy_if(enm.begin(), enm.end(), std::back_inserter(enabled), [](const libyang::S_Type_Enum& it) { |
| 171 | auto iffeatures = it->iffeature(); |
| 172 | return std::all_of(iffeatures.begin(), iffeatures.end(), [](auto it) { return it->value(); }); |
| 173 | }); |
| 174 | |
| 175 | std::set<enum_> enumSet; |
| 176 | std::transform(enabled.begin(), enabled.end(), std::inserter(enumSet, enumSet.end()), [](auto it) { return enum_{it->name()}; }); |
| 177 | return enumSet; |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 180 | std::set<identityRef_> validIdentities(const libyang::S_Schema_Node_Leaf& leaf) |
| 181 | { |
| 182 | std::set<identityRef_> identSet; |
| 183 | |
| 184 | // auto topLevelModule = leaf->module(); |
| 185 | |
| 186 | auto info = leaf->type()->info(); |
| 187 | for (auto base : info->ident()->ref()) { // Iterate over all bases |
| 188 | identSet.emplace(base->module()->name(), base->name()); |
| 189 | // Iterate over derived identities (this is recursive!) |
| 190 | for (auto derived : base->der()->schema()) { |
| 191 | identSet.emplace(derived->module()->name(), derived->name()); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return identSet; |
| 196 | } |
| 197 | |
| 198 | std::string leafrefPath(const libyang::S_Schema_Node_Leaf& leaf) |
| 199 | { |
| 200 | return leaf->type()->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | yang::LeafDataType YangSchema::impl_leafType(const libyang::S_Schema_Node& node) const |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 205 | { |
| 206 | using namespace std::string_literals; |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 207 | auto leaf = std::make_shared<libyang::Schema_Node_Leaf>(node); |
| 208 | auto baseType{leaf->type()->base()}; |
| 209 | switch (baseType) { |
| 210 | case LY_TYPE_STRING: |
| 211 | return yang::String{}; |
| 212 | case LY_TYPE_DEC64: |
| 213 | return yang::Decimal{}; |
| 214 | case LY_TYPE_BOOL: |
| 215 | return yang::Bool{}; |
| 216 | case LY_TYPE_INT8: |
| 217 | return yang::Int8{}; |
| 218 | case LY_TYPE_INT16: |
| 219 | return yang::Int16{}; |
| 220 | case LY_TYPE_INT32: |
| 221 | return yang::Int32{}; |
| 222 | case LY_TYPE_INT64: |
| 223 | return yang::Int64{}; |
| 224 | case LY_TYPE_UINT8: |
| 225 | return yang::Uint8{}; |
| 226 | case LY_TYPE_UINT16: |
| 227 | return yang::Uint16{}; |
| 228 | case LY_TYPE_UINT32: |
| 229 | return yang::Uint32{}; |
| 230 | case LY_TYPE_UINT64: |
| 231 | return yang::Uint64{}; |
| 232 | case LY_TYPE_BINARY: |
| 233 | return yang::Binary{}; |
| 234 | case LY_TYPE_ENUM: |
| 235 | return yang::Enum{enumValues(leaf)}; |
| 236 | case LY_TYPE_IDENT: |
| 237 | return yang::IdentityRef{validIdentities(leaf)}; |
| 238 | case LY_TYPE_LEAFREF: |
| 239 | return yang::LeafRef{::leafrefPath(leaf), std::make_unique<yang::LeafDataType>(leafType(::leafrefPath(leaf)))}; |
| 240 | default: |
| 241 | using namespace std::string_literals; |
| 242 | throw UnsupportedYangTypeException("the type of "s + node->name() + " is not supported: " + std::to_string(baseType)); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 246 | yang::LeafDataType YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 247 | { |
| 248 | return impl_leafType(getSchemaNode(location, node)); |
| 249 | } |
| 250 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 251 | yang::LeafDataType YangSchema::leafType(const std::string& path) const |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 252 | { |
| 253 | return impl_leafType(getSchemaNode(path)); |
| 254 | } |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 255 | |
Václav Kubernát | 6fcd028 | 2020-02-21 16:33:08 +0100 | [diff] [blame] | 256 | std::optional<std::string> YangSchema::leafTypeName(const std::string& path) const |
| 257 | { |
| 258 | libyang::Schema_Node_Leaf leaf(getSchemaNode(path)); |
| 259 | return leaf.type()->der().get() ? std::optional{leaf.type()->der()->name()} : std::nullopt; |
| 260 | } |
| 261 | |
Václav Kubernát | bd5e3c2 | 2020-02-19 15:22:00 +0100 | [diff] [blame] | 262 | std::string YangSchema::leafrefPath(const std::string& leafrefPath) const |
| 263 | { |
| 264 | using namespace std::string_literals; |
| 265 | libyang::Schema_Node_Leaf leaf(getSchemaNode(leafrefPath)); |
| 266 | return leaf.type()->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX); |
| 267 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 268 | |
| 269 | std::set<std::string> YangSchema::modules() const |
| 270 | { |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 271 | const auto& modules = m_context->get_module_iter(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 272 | |
| 273 | std::set<std::string> res; |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 274 | std::transform(modules.begin(), modules.end(), std::inserter(res, res.end()), [](const auto module) { return module->name(); }); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 275 | return res; |
| 276 | } |
| 277 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 278 | std::set<std::string> YangSchema::childNodes(const schemaPath_& path, const Recursion recursion) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 279 | { |
| 280 | using namespace std::string_view_literals; |
| 281 | std::set<std::string> res; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 282 | std::vector<libyang::S_Schema_Node> nodes; |
| 283 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 284 | if (path.m_nodes.empty()) { |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 285 | nodes = m_context->data_instantiables(0); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 286 | } else { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 287 | const auto pathString = pathToSchemaString(path, Prefixes::Always); |
| 288 | const auto node = getSchemaNode(pathString); |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 289 | nodes = node->child_instantiables(0); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 290 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 291 | |
| 292 | for (const auto node : nodes) { |
| 293 | if (node->module()->name() == "ietf-yang-library"sv) |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 294 | continue; |
Václav Kubernát | fa81c8c | 2020-02-13 17:22:46 +0100 | [diff] [blame] | 295 | // FIXME: This is a temporary fix to filter out RPC nodes in |
| 296 | // tab-completion. The method will have to be changed/reworked when RPC |
| 297 | // support gets added. |
| 298 | if (node->nodetype() == LYS_RPC) |
| 299 | continue; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 300 | if (recursion == Recursion::Recursive) { |
| 301 | for (auto it : node->tree_dfs()) { |
| 302 | res.insert(it->path(LYS_PATH_FIRST_PREFIX)); |
| 303 | } |
| 304 | } else { |
Václav Kubernát | 4f77a25 | 2019-02-19 16:51:30 +0100 | [diff] [blame] | 305 | std::string toInsert; |
| 306 | if (path.m_nodes.empty() || path.m_nodes.front().m_prefix.get().m_name != node->module()->name()) { |
| 307 | toInsert += node->module()->name(); |
| 308 | toInsert += ":"; |
| 309 | } |
| 310 | toInsert += node->name(); |
| 311 | res.insert(toInsert); |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 315 | return res; |
| 316 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 317 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 318 | std::set<std::string> YangSchema::moduleNodes(const module_& module, const Recursion recursion) const |
| 319 | { |
| 320 | std::set<std::string> res; |
| 321 | const auto yangModule = m_context->get_module(module.m_name.c_str()); |
| 322 | |
| 323 | std::vector<libyang::S_Schema_Node> nodes; |
| 324 | |
| 325 | for (const auto node : yangModule->data_instantiables(0)) { |
| 326 | if (recursion == Recursion::Recursive) { |
| 327 | for (const auto it : node->tree_dfs()) { |
| 328 | res.insert(it->path(LYS_PATH_FIRST_PREFIX)); |
| 329 | } |
| 330 | } else { |
| 331 | res.insert(module.m_name + ":" + node->name()); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return res; |
| 336 | } |
| 337 | |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 338 | void YangSchema::loadModule(const std::string& moduleName) |
| 339 | { |
| 340 | m_context->load_module(moduleName.c_str()); |
| 341 | } |
| 342 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 343 | void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName) |
| 344 | { |
| 345 | m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str()); |
| 346 | } |
| 347 | |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 348 | void YangSchema::registerModuleCallback(const std::function<std::string(const char*, const char*, const char*, const char*)>& clb) |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 349 | { |
| 350 | auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) { |
| 351 | (void)submod_revision; |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 352 | auto moduleSource = clb(mod_name, mod_revision, submod_name, submod_revision); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 353 | if (moduleSource.empty()) { |
| 354 | return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr}; |
| 355 | } |
| 356 | return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())}; |
| 357 | }; |
| 358 | |
| 359 | auto deleter = [](void* data) { |
| 360 | free(data); |
| 361 | }; |
| 362 | m_context->add_missing_module_callback(lambda, deleter); |
| 363 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 364 | |
| 365 | std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const |
| 366 | { |
| 367 | return std::make_shared<libyang::Data_Node>(m_context, |
| 368 | path.c_str(), |
| 369 | value ? value.value().c_str() : nullptr, |
| 370 | LYD_ANYDATA_CONSTSTRING, |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 371 | LYD_PATH_OPT_EDIT); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name) |
| 375 | { |
| 376 | return m_context->get_module(name.c_str(), nullptr, 0); |
| 377 | } |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 378 | |
| 379 | namespace { |
| 380 | yang::NodeTypes impl_nodeType(const libyang::S_Schema_Node& node) |
| 381 | { |
| 382 | if (!node) { |
| 383 | throw InvalidNodeException(); |
| 384 | } |
| 385 | switch (node->nodetype()) { |
| 386 | case LYS_CONTAINER: |
| 387 | return libyang::Schema_Node_Container{node}.presence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container; |
| 388 | case LYS_LEAF: |
| 389 | return yang::NodeTypes::Leaf; |
| 390 | case LYS_LIST: |
| 391 | return yang::NodeTypes::List; |
| 392 | default: |
Václav Kubernát | 2a14139 | 2020-02-18 17:12:32 +0100 | [diff] [blame] | 393 | throw InvalidNodeException(); // FIXME: Implement all types. |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const |
| 399 | { |
| 400 | return impl_nodeType(getSchemaNode(location, node)); |
| 401 | } |
| 402 | |
| 403 | yang::NodeTypes YangSchema::nodeType(const std::string& path) const |
| 404 | { |
| 405 | return impl_nodeType(getSchemaNode(path)); |
| 406 | } |
Václav Kubernát | 1e09bd6 | 2020-02-17 15:13:38 +0100 | [diff] [blame] | 407 | |
| 408 | std::optional<std::string> YangSchema::description(const std::string& path) const |
| 409 | { |
| 410 | auto node = getSchemaNode(path.c_str()); |
| 411 | return node->dsc() ? std::optional{node->dsc()} : std::nullopt; |
| 412 | } |
| 413 | |
| 414 | std::optional<std::string> YangSchema::units(const std::string& path) const |
| 415 | { |
| 416 | auto node = getSchemaNode(path.c_str()); |
| 417 | if (node->nodetype() != LYS_LEAF) { |
| 418 | return std::nullopt; |
| 419 | } |
| 420 | libyang::Schema_Node_Leaf leaf{node}; |
| 421 | auto units = leaf.units(); |
| 422 | |
| 423 | // A leaf can specify units as part of its definition. |
| 424 | if (units) { |
| 425 | return units; |
| 426 | } |
| 427 | |
| 428 | // A typedef (or its parent typedefs) can specify units too. We'll use the first `units` we find. |
| 429 | for (auto parentTypedef = leaf.type()->der(); parentTypedef; parentTypedef = parentTypedef->type()->der()) { |
| 430 | units = parentTypedef->units(); |
| 431 | if (units) { |
| 432 | return units; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | return std::nullopt; |
| 437 | } |