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> |
| 10 | #include <libyang/Tree_Schema.hpp> |
| 11 | #include <string_view> |
| 12 | #include "utils.hpp" |
| 13 | #include "yang_schema.hpp" |
| 14 | |
| 15 | class YangLoadError : public std::runtime_error { |
| 16 | public: |
| 17 | using std::runtime_error::runtime_error; |
| 18 | ~YangLoadError() override = default; |
| 19 | }; |
| 20 | |
| 21 | class UnsupportedYangTypeException : public std::runtime_error { |
| 22 | public: |
| 23 | using std::runtime_error::runtime_error; |
| 24 | ~UnsupportedYangTypeException() override = default; |
| 25 | }; |
| 26 | |
| 27 | class InvalidSchemaQueryException : public std::runtime_error { |
| 28 | public: |
| 29 | using std::runtime_error::runtime_error; |
| 30 | ~InvalidSchemaQueryException() override = default; |
| 31 | }; |
| 32 | |
| 33 | std::string pathToYangAbsSchemPath(const path_& path) |
| 34 | { |
| 35 | std::string res = "/"; |
| 36 | std::string currentModule; |
| 37 | |
| 38 | for (const auto& it : path.m_nodes) { |
| 39 | const auto name = nodeToSchemaString(it); |
| 40 | |
| 41 | if (it.m_suffix.type() == typeid(module_)) { |
| 42 | currentModule = name; |
| 43 | continue; |
| 44 | } else { |
| 45 | res += currentModule + ":"; |
| 46 | res += name + "/"; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return res; |
| 51 | } |
| 52 | |
| 53 | YangSchema::YangSchema() |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 54 | : 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] | 55 | { |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | YangSchema::~YangSchema() = default; |
| 59 | |
| 60 | void YangSchema::addSchemaString(const char* schema) |
| 61 | { |
| 62 | if (!m_context->parse_module_mem(schema, LYS_IN_YANG)) { |
| 63 | throw YangLoadError("Couldn't load schema"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void YangSchema::addSchemaDirectory(const char* directoryName) |
| 68 | { |
| 69 | if (m_context->set_searchdir(directoryName)) { |
| 70 | throw YangLoadError("Couldn't add schema search directory"); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void YangSchema::addSchemaFile(const char* filename) |
| 75 | { |
| 76 | if (!m_context->parse_module_path(filename, LYS_IN_YANG)) { |
| 77 | throw YangLoadError("Couldn't load schema"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool YangSchema::isModule(const path_&, const std::string& name) const |
| 82 | { |
| 83 | const auto set = modules(); |
| 84 | return set.find(name) != set.end(); |
| 85 | } |
| 86 | |
| 87 | bool YangSchema::isContainer(const path_& location, const ModuleNodePair& node) const |
| 88 | { |
| 89 | const auto schemaNode = getSchemaNode(location, node); |
| 90 | return schemaNode && schemaNode->nodetype() == LYS_CONTAINER; |
| 91 | } |
| 92 | |
| 93 | bool YangSchema::isLeaf(const path_& location, const ModuleNodePair& node) const |
| 94 | { |
| 95 | const auto schemaNode = getSchemaNode(location, node); |
| 96 | return schemaNode && schemaNode->nodetype() == LYS_LEAF; |
| 97 | } |
| 98 | |
| 99 | bool YangSchema::isList(const path_& location, const ModuleNodePair& node) const |
| 100 | { |
| 101 | const auto schemaNode = getSchemaNode(location, node); |
| 102 | return schemaNode && schemaNode->nodetype() == LYS_LIST; |
| 103 | } |
| 104 | |
| 105 | bool YangSchema::isPresenceContainer(const path_& location, const ModuleNodePair& node) const |
| 106 | { |
| 107 | if (!isContainer(location, node)) |
| 108 | return false; |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 109 | return libyang::Schema_Node_Container(getSchemaNode(location, node)).presence(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool YangSchema::leafEnumHasValue(const path_& location, const ModuleNodePair& node, const std::string& value) const |
| 113 | { |
| 114 | if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::Enum) |
| 115 | return false; |
| 116 | |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 117 | libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node)); |
Václav Kubernát | 6a713d6 | 2018-10-03 18:47:34 +0200 | [diff] [blame^] | 118 | auto type = leaf.type(); |
| 119 | auto enm = type->info()->enums()->enm(); |
| 120 | // The enum can be a derived type and enm() only returns values, |
| 121 | // if that specific typedef changed the possible values. So we go |
| 122 | // up the hierarchy until we find a typedef that defined these values. |
| 123 | while (enm.empty()) { |
| 124 | type = type->der()->type(); |
| 125 | enm = type->info()->enums()->enm(); |
| 126 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 127 | |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 128 | return std::any_of(enm.begin(), enm.end(), [=](const auto& x) { return x->name() == value; }); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | bool YangSchema::listHasKey(const path_& location, const ModuleNodePair& node, const std::string& key) const |
| 132 | { |
| 133 | if (!isList(location, node)) |
| 134 | return false; |
| 135 | const auto keys = listKeys(location, node); |
| 136 | return keys.find(key) != keys.end(); |
| 137 | } |
| 138 | |
| 139 | bool YangSchema::nodeExists(const std::string& location, const std::string& node) const |
| 140 | { |
| 141 | const auto absPath = location + "/" + node; |
| 142 | const auto set = m_context->find_path(absPath.c_str()); |
| 143 | return set->number() == 1; |
| 144 | } |
| 145 | |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 146 | libyang::S_Set YangSchema::getNodeSet(const path_& location, const ModuleNodePair& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 147 | { |
| 148 | std::string absPath = location.m_nodes.empty() ? "" : "/"; |
| 149 | absPath += pathToAbsoluteSchemaString(location) + "/" + fullNodeName(location, node); |
| 150 | return m_context->find_path(absPath.c_str()); |
| 151 | } |
| 152 | |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 153 | libyang::S_Schema_Node YangSchema::getSchemaNode(const path_& location, const ModuleNodePair& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 154 | { |
| 155 | const auto set = getNodeSet(location, node); |
| 156 | if (!set) |
| 157 | return nullptr; |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 158 | const auto& schemaSet = set->schema(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 159 | if (set->number() != 1) |
| 160 | return nullptr; |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 161 | return *schemaSet.begin(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | const std::set<std::string> YangSchema::listKeys(const path_& location, const ModuleNodePair& node) const |
| 165 | { |
| 166 | std::set<std::string> keys; |
| 167 | if (!isList(location, node)) |
| 168 | return keys; |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 169 | libyang::Schema_Node_List list(getSchemaNode(location, node)); |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 170 | const auto& keysVec = list.keys(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 171 | |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 172 | std::transform(keysVec.begin(), keysVec.end(), std::inserter(keys, keys.begin()), |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 173 | [] (const auto& it) {return it->name();}); |
| 174 | return keys; |
| 175 | } |
| 176 | |
| 177 | yang::LeafDataTypes YangSchema::leafType(const path_& location, const ModuleNodePair& node) const |
| 178 | { |
| 179 | using namespace std::string_literals; |
| 180 | if (!isLeaf(location, node)) |
| 181 | throw InvalidSchemaQueryException(fullNodeName(location, node) + " is not a leaf"); |
| 182 | |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 183 | libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node)); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 184 | switch (leaf.type()->base()) { |
| 185 | case LY_TYPE_STRING: |
| 186 | return yang::LeafDataTypes::String; |
| 187 | case LY_TYPE_DEC64: |
| 188 | return yang::LeafDataTypes::Decimal; |
| 189 | case LY_TYPE_BOOL: |
| 190 | return yang::LeafDataTypes::Bool; |
| 191 | case LY_TYPE_INT32: |
| 192 | return yang::LeafDataTypes::Int; |
| 193 | case LY_TYPE_UINT32: |
| 194 | return yang::LeafDataTypes::Uint; |
| 195 | case LY_TYPE_ENUM: |
| 196 | return yang::LeafDataTypes::Enum; |
| 197 | default: |
| 198 | throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported"); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | std::set<std::string> YangSchema::modules() const |
| 203 | { |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 204 | const auto& modules = m_context->get_module_iter(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 205 | |
| 206 | std::set<std::string> res; |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 207 | std::transform(modules.begin(), modules.end(), |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 208 | std::inserter(res, res.end()), |
| 209 | [] (const auto module) { return module->name(); }); |
| 210 | return res; |
| 211 | } |
| 212 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 213 | std::set<std::string> YangSchema::childNodes(const path_& path, const Recursion recursion) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 214 | { |
| 215 | using namespace std::string_view_literals; |
| 216 | std::set<std::string> res; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 217 | std::vector<libyang::S_Schema_Node> nodes; |
| 218 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 219 | if (path.m_nodes.empty()) { |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 220 | nodes = m_context->data_instantiables(0); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 221 | } else { |
| 222 | const auto absolutePath = "/" + pathToAbsoluteSchemaString(path); |
| 223 | const auto set = m_context->find_path(absolutePath.c_str()); |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 224 | const auto schemaSet = set->schema(); |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 225 | for (auto it = (*schemaSet.begin())->child(); it; it = it->next()) { |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 226 | nodes.push_back(it); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 227 | } |
| 228 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 229 | |
| 230 | for (const auto node : nodes) { |
| 231 | if (node->module()->name() == "ietf-yang-library"sv) |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 232 | continue; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 233 | if (recursion == Recursion::Recursive) { |
| 234 | for (auto it : node->tree_dfs()) { |
| 235 | res.insert(it->path(LYS_PATH_FIRST_PREFIX)); |
| 236 | } |
| 237 | } else { |
| 238 | res.insert(std::string(node->module()->name()) + ":" + node->name()); |
| 239 | } |
| 240 | } |
| 241 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 242 | return res; |
| 243 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 244 | |
| 245 | void YangSchema::loadModule(const std::string& moduleName) |
| 246 | { |
| 247 | m_context->load_module(moduleName.c_str()); |
| 248 | } |
| 249 | |
| 250 | void YangSchema::registerModuleCallback(const std::function<std::string(const char*, const char*, const char*)>& clb) |
| 251 | { |
| 252 | auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) { |
| 253 | (void)submod_revision; |
| 254 | auto moduleSource = clb(mod_name, mod_revision, submod_name); |
| 255 | if (moduleSource.empty()) { |
| 256 | return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr}; |
| 257 | } |
| 258 | return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())}; |
| 259 | }; |
| 260 | |
| 261 | auto deleter = [](void* data) { |
| 262 | free(data); |
| 263 | }; |
| 264 | m_context->add_missing_module_callback(lambda, deleter); |
| 265 | } |