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() |
| 54 | { |
| 55 | m_context = std::make_shared<Context>(); |
| 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; |
| 109 | return Schema_Node_Container(getSchemaNode(location, node)).presence(); |
| 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 | |
| 117 | Schema_Node_Leaf leaf(getSchemaNode(location, node)); |
| 118 | const auto enm = std::unique_ptr<std::vector<S_Type_Enum>>(leaf.type()->info()->enums()->enm()); |
| 119 | |
| 120 | return std::any_of(enm->begin(), enm->end(), [=](const auto& x) { return x->name() == value; }); |
| 121 | } |
| 122 | |
| 123 | bool YangSchema::listHasKey(const path_& location, const ModuleNodePair& node, const std::string& key) const |
| 124 | { |
| 125 | if (!isList(location, node)) |
| 126 | return false; |
| 127 | const auto keys = listKeys(location, node); |
| 128 | return keys.find(key) != keys.end(); |
| 129 | } |
| 130 | |
| 131 | bool YangSchema::nodeExists(const std::string& location, const std::string& node) const |
| 132 | { |
| 133 | const auto absPath = location + "/" + node; |
| 134 | const auto set = m_context->find_path(absPath.c_str()); |
| 135 | return set->number() == 1; |
| 136 | } |
| 137 | |
| 138 | S_Set YangSchema::getNodeSet(const path_& location, const ModuleNodePair& node) const |
| 139 | { |
| 140 | std::string absPath = location.m_nodes.empty() ? "" : "/"; |
| 141 | absPath += pathToAbsoluteSchemaString(location) + "/" + fullNodeName(location, node); |
| 142 | return m_context->find_path(absPath.c_str()); |
| 143 | } |
| 144 | |
| 145 | std::shared_ptr<Schema_Node> YangSchema::getSchemaNode(const path_& location, const ModuleNodePair& node) const |
| 146 | { |
| 147 | const auto set = getNodeSet(location, node); |
| 148 | if (!set) |
| 149 | return nullptr; |
| 150 | const auto schemaSet = std::unique_ptr<std::vector<std::shared_ptr<Schema_Node>>>(set->schema()); |
| 151 | if (set->number() != 1) |
| 152 | return nullptr; |
| 153 | return (*(schemaSet->begin())); |
| 154 | } |
| 155 | |
| 156 | const std::set<std::string> YangSchema::listKeys(const path_& location, const ModuleNodePair& node) const |
| 157 | { |
| 158 | std::set<std::string> keys; |
| 159 | if (!isList(location, node)) |
| 160 | return keys; |
| 161 | Schema_Node_List list(getSchemaNode(location, node)); |
| 162 | const auto keysVec = std::unique_ptr<std::vector<S_Schema_Node_Leaf>>(list.keys()); |
| 163 | |
| 164 | std::transform(keysVec->begin(), keysVec->end(), std::inserter(keys, keys.begin()), |
| 165 | [] (const auto& it) {return it->name();}); |
| 166 | return keys; |
| 167 | } |
| 168 | |
| 169 | yang::LeafDataTypes YangSchema::leafType(const path_& location, const ModuleNodePair& node) const |
| 170 | { |
| 171 | using namespace std::string_literals; |
| 172 | if (!isLeaf(location, node)) |
| 173 | throw InvalidSchemaQueryException(fullNodeName(location, node) + " is not a leaf"); |
| 174 | |
| 175 | Schema_Node_Leaf leaf(getSchemaNode(location, node)); |
| 176 | switch (leaf.type()->base()) { |
| 177 | case LY_TYPE_STRING: |
| 178 | return yang::LeafDataTypes::String; |
| 179 | case LY_TYPE_DEC64: |
| 180 | return yang::LeafDataTypes::Decimal; |
| 181 | case LY_TYPE_BOOL: |
| 182 | return yang::LeafDataTypes::Bool; |
| 183 | case LY_TYPE_INT32: |
| 184 | return yang::LeafDataTypes::Int; |
| 185 | case LY_TYPE_UINT32: |
| 186 | return yang::LeafDataTypes::Uint; |
| 187 | case LY_TYPE_ENUM: |
| 188 | return yang::LeafDataTypes::Enum; |
| 189 | default: |
| 190 | throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported"); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | std::set<std::string> YangSchema::modules() const |
| 195 | { |
| 196 | const auto modules = std::unique_ptr<std::vector<S_Module>>(m_context->get_module_iter()); |
| 197 | |
| 198 | std::set<std::string> res; |
| 199 | std::transform(modules->begin(), modules->end(), |
| 200 | std::inserter(res, res.end()), |
| 201 | [] (const auto module) { return module->name(); }); |
| 202 | return res; |
| 203 | } |
| 204 | |
| 205 | std::set<std::string> YangSchema::childNodes(const path_& path) const |
| 206 | { |
| 207 | using namespace std::string_view_literals; |
| 208 | std::set<std::string> res; |
| 209 | if (path.m_nodes.empty()) { |
| 210 | const auto nodeVec = std::unique_ptr<std::vector<S_Schema_Node>>(m_context->data_instantiables(0)); |
| 211 | for (const auto it : *nodeVec) { |
| 212 | if (it->module()->name() == "ietf-yang-library"sv) |
| 213 | continue; |
| 214 | res.insert(std::string(it->module()->name()) + ":" + it->name()); |
| 215 | } |
| 216 | } else { |
| 217 | const auto absolutePath = "/" + pathToAbsoluteSchemaString(path); |
| 218 | const auto set = m_context->find_path(absolutePath.c_str()); |
| 219 | const auto schemaSet = std::unique_ptr<std::vector<std::shared_ptr<Schema_Node>>>(set->schema()); |
| 220 | for (auto it = (*(schemaSet->begin()))->child(); it; it = it->next()) { |
| 221 | res.insert(std::string(it->module()->name()) + ":" + it->name()); |
| 222 | } |
| 223 | } |
| 224 | return res; |
| 225 | } |