Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 1 | #include <boost/algorithm/string/predicate.hpp> |
| 2 | #include <experimental/iterator> |
Václav Kubernát | 548cb19 | 2020-06-26 14:00:42 +0200 | [diff] [blame] | 3 | #include <fstream> |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 4 | #include <iostream> |
| 5 | #include <libyang/Tree_Data.hpp> |
| 6 | #include <libyang/libyang.h> |
| 7 | #include "UniqueResource.hpp" |
| 8 | #include "libyang_utils.hpp" |
| 9 | #include "utils.hpp" |
| 10 | #include "yang_access.hpp" |
| 11 | #include "yang_schema.hpp" |
| 12 | |
| 13 | namespace { |
| 14 | template <typename Type> using lyPtrDeleter_type = void (*)(Type*); |
| 15 | template <typename Type> const lyPtrDeleter_type<Type> lyPtrDeleter; |
| 16 | template <> const auto lyPtrDeleter<ly_set> = ly_set_free; |
| 17 | template <> const auto lyPtrDeleter<ly_ctx> = static_cast<lyPtrDeleter_type<ly_ctx>>([] (auto* ptr) {ly_ctx_destroy(ptr, nullptr);}); |
| 18 | template <> const auto lyPtrDeleter<lyd_node> = lyd_free_withsiblings; |
| 19 | |
| 20 | template <typename Type> |
| 21 | auto lyWrap(Type* ptr) |
| 22 | { |
| 23 | return std::unique_ptr<Type, lyPtrDeleter_type<Type>>{ptr, lyPtrDeleter<Type>}; |
| 24 | } |
| 25 | |
| 26 | // Convenient for functions that take m_datastore as an argument |
| 27 | using DatastoreType = std::unique_ptr<lyd_node, lyPtrDeleter_type<lyd_node>>; |
| 28 | } |
| 29 | |
| 30 | YangAccess::YangAccess() |
| 31 | : m_ctx(lyWrap(ly_ctx_new(nullptr, LY_CTX_DISABLE_SEARCHDIR_CWD))) |
| 32 | , m_datastore(lyWrap<lyd_node>(nullptr)) |
| 33 | , m_schema(std::make_shared<YangSchema>(libyang::create_new_Context(m_ctx.get()))) |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 34 | , m_validation_mode(LYD_OPT_DATA) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | YangAccess::YangAccess(std::shared_ptr<YangSchema> schema) |
| 39 | : m_ctx(schema->m_context->swig_ctx(), [](auto) {}) |
| 40 | , m_datastore(lyWrap<lyd_node>(nullptr)) |
| 41 | , m_schema(schema) |
| 42 | , m_validation_mode(LYD_OPT_RPC) |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Václav Kubernát | 51fa48e | 2020-07-08 17:17:34 +0200 | [diff] [blame] | 46 | YangAccess::~YangAccess() = default; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 47 | |
| 48 | [[noreturn]] void YangAccess::getErrorsAndThrow() const |
| 49 | { |
| 50 | auto errors = libyang::get_ly_errors(libyang::create_new_Context(m_ctx.get())); |
| 51 | std::vector<DatastoreError> errorsRes; |
| 52 | for (const auto& error : errors) { |
| 53 | using namespace std::string_view_literals; |
| 54 | errorsRes.emplace_back(error->errmsg(), error->errpath() != ""sv ? std::optional{error->errpath()} : std::nullopt); |
| 55 | } |
| 56 | |
| 57 | throw DatastoreException(errorsRes); |
| 58 | } |
| 59 | |
| 60 | void YangAccess::impl_newPath(const std::string& path, const std::optional<std::string>& value) |
| 61 | { |
| 62 | auto newNode = lyd_new_path(m_datastore.get(), m_ctx.get(), path.c_str(), value ? (void*)value->c_str() : nullptr, LYD_ANYDATA_CONSTSTRING, LYD_PATH_OPT_UPDATE); |
| 63 | if (!newNode) { |
| 64 | getErrorsAndThrow(); |
| 65 | } |
| 66 | if (!m_datastore) { |
| 67 | m_datastore = lyWrap(newNode); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | namespace { |
| 72 | void impl_unlink(DatastoreType& datastore, lyd_node* what) |
| 73 | { |
| 74 | // If the node to be unlinked is the one our datastore variable points to, we need to find a new one to point to (one of its siblings) |
| 75 | if (datastore.get() == what) { |
| 76 | auto oldDatastore = datastore.release(); |
| 77 | if (oldDatastore->prev != oldDatastore) { |
| 78 | datastore = lyWrap(oldDatastore->prev); |
| 79 | } else { |
| 80 | datastore = lyWrap(oldDatastore->next); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | lyd_unlink(what); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void YangAccess::impl_removeNode(const std::string& path) |
| 89 | { |
| 90 | auto set = lyWrap(lyd_find_path(m_datastore.get(), path.c_str())); |
| 91 | if (!set || set->number == 0) { |
| 92 | // Check if schema node exists - lyd_find_path first checks if the first argument is non-null before checking for path validity |
| 93 | if (!ly_ctx_get_node(m_ctx.get(), nullptr, path.c_str(), 0)) { |
| 94 | throw DatastoreException{{DatastoreError{"Schema node doesn't exist.", path}}}; |
| 95 | } |
| 96 | // Check if libyang found another error |
| 97 | if (ly_err_first(m_ctx.get())) { |
| 98 | getErrorsAndThrow(); |
| 99 | } |
| 100 | |
| 101 | // Otherwise the datastore just doesn't contain the wanted node. |
| 102 | throw DatastoreException{{DatastoreError{"Data node doesn't exist.", path}}}; |
| 103 | } |
| 104 | |
| 105 | auto toRemove = set->set.d[0]; |
| 106 | |
| 107 | impl_unlink(m_datastore, toRemove); |
| 108 | |
| 109 | lyd_free(toRemove); |
| 110 | } |
| 111 | |
| 112 | void YangAccess::validate() |
| 113 | { |
| 114 | auto datastore = m_datastore.release(); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 115 | |
| 116 | if (m_validation_mode == LYD_OPT_RPC) { |
| 117 | lyd_validate(&datastore, m_validation_mode, nullptr); |
| 118 | } else { |
| 119 | lyd_validate(&datastore, m_validation_mode | LYD_OPT_DATA_NO_YANGLIB, m_ctx.get()); |
| 120 | } |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 121 | m_datastore = lyWrap(datastore); |
| 122 | } |
| 123 | |
Václav Kubernát | d628291 | 2020-06-23 14:49:34 +0200 | [diff] [blame] | 124 | DatastoreAccess::Tree YangAccess::getItems(const std::string& path) const |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 125 | { |
| 126 | DatastoreAccess::Tree res; |
| 127 | if (!m_datastore) { |
| 128 | return res; |
| 129 | } |
| 130 | |
| 131 | auto set = lyWrap(lyd_find_path(m_datastore.get(), path == "/" ? "/*" : path.c_str())); |
| 132 | auto setWrapper = libyang::Set(set.get(), nullptr); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 133 | std::optional<std::string> ignoredXPathPrefix; |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 134 | lyNodesToTree(res, setWrapper.data()); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 135 | return res; |
| 136 | } |
| 137 | |
| 138 | void YangAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 139 | { |
| 140 | auto lyValue = value.type() == typeid(empty_) ? std::nullopt : std::optional(leafDataToString(value)); |
| 141 | impl_newPath(path, lyValue); |
| 142 | } |
| 143 | |
| 144 | void YangAccess::createItem(const std::string& path) |
| 145 | { |
| 146 | impl_newPath(path); |
| 147 | } |
| 148 | |
| 149 | void YangAccess::deleteItem(const std::string& path) |
| 150 | { |
| 151 | impl_removeNode(path); |
| 152 | } |
| 153 | |
| 154 | namespace { |
| 155 | struct impl_moveItem { |
| 156 | DatastoreType& m_datastore; |
| 157 | lyd_node* m_sourceNode; |
| 158 | |
| 159 | void operator()(yang::move::Absolute absolute) const |
| 160 | { |
| 161 | auto set = lyWrap(lyd_find_instance(m_sourceNode, m_sourceNode->schema)); |
| 162 | if (set->number == 1) { // m_sourceNode is the sole instance, do nothing |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | doUnlink(); |
| 167 | switch (absolute) { |
| 168 | case yang::move::Absolute::Begin: |
| 169 | if (set->set.d[0] == m_sourceNode) { // List is already at the beginning, do nothing |
| 170 | return; |
| 171 | } |
| 172 | lyd_insert_before(set->set.d[0], m_sourceNode); |
| 173 | return; |
| 174 | case yang::move::Absolute::End: |
| 175 | if (set->set.d[set->number - 1] == m_sourceNode) { // List is already at the end, do nothing |
| 176 | return; |
| 177 | } |
| 178 | lyd_insert_after(set->set.d[set->number - 1], m_sourceNode); |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void operator()(const yang::move::Relative& relative) const |
| 184 | { |
| 185 | auto keySuffix = m_sourceNode->schema->nodetype == LYS_LIST ? instanceToString(relative.m_path) |
| 186 | : leafDataToString(relative.m_path.at(".")); |
| 187 | lyd_node* destNode; |
| 188 | lyd_find_sibling_val(m_sourceNode, m_sourceNode->schema, keySuffix.c_str(), &destNode); |
| 189 | |
| 190 | doUnlink(); |
| 191 | if (relative.m_position == yang::move::Relative::Position::After) { |
| 192 | lyd_insert_after(destNode, m_sourceNode); |
| 193 | } else { |
| 194 | lyd_insert_before(destNode, m_sourceNode); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | private: |
| 199 | void doUnlink() const |
| 200 | { |
| 201 | impl_unlink(m_datastore, m_sourceNode); |
| 202 | } |
| 203 | }; |
| 204 | } |
| 205 | |
| 206 | void YangAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 207 | { |
| 208 | auto set = lyWrap(lyd_find_path(m_datastore.get(), source.c_str())); |
| 209 | if (!set) { // Error, the node probably doesn't exist in the schema |
| 210 | getErrorsAndThrow(); |
| 211 | } |
| 212 | if (set->number == 0) { |
| 213 | return; |
| 214 | } |
| 215 | auto sourceNode = set->set.d[0]; |
| 216 | std::visit(impl_moveItem{m_datastore, sourceNode}, move); |
| 217 | } |
| 218 | |
| 219 | void YangAccess::commitChanges() |
| 220 | { |
| 221 | validate(); |
| 222 | } |
| 223 | |
| 224 | void YangAccess::discardChanges() |
| 225 | { |
| 226 | } |
| 227 | |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 228 | [[noreturn]] DatastoreAccess::Tree YangAccess::execute(const std::string& path, const Tree& input) |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 229 | { |
| 230 | auto root = lyWrap(lyd_new_path(nullptr, m_ctx.get(), path.c_str(), nullptr, LYD_ANYDATA_CONSTSTRING, 0)); |
| 231 | if (!root) { |
| 232 | getErrorsAndThrow(); |
| 233 | } |
| 234 | for (const auto& [k, v] : input) { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 235 | if (v.type() == typeid(special_) && boost::get<special_>(v).m_value != SpecialValue::PresenceContainer) { |
| 236 | continue; |
| 237 | } |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 238 | |
| 239 | lyd_new_path(root.get(), m_ctx.get(), k.c_str(), (void*)leafDataToString(v).c_str(), LYD_ANYDATA_CONSTSTRING, LYD_PATH_OPT_UPDATE); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 240 | } |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 241 | throw std::logic_error("in-memory datastore doesn't support executing RPC/action"); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void YangAccess::copyConfig(const Datastore source, const Datastore dest) |
| 245 | { |
| 246 | if (source == Datastore::Startup && dest == Datastore::Running) { |
| 247 | m_datastore = nullptr; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | std::shared_ptr<Schema> YangAccess::schema() |
| 252 | { |
| 253 | return m_schema; |
| 254 | } |
| 255 | |
| 256 | std::vector<ListInstance> YangAccess::listInstances(const std::string& path) |
| 257 | { |
| 258 | std::vector<ListInstance> res; |
| 259 | if (!m_datastore) { |
| 260 | return res; |
| 261 | } |
| 262 | |
| 263 | auto instances = lyWrap(lyd_find_path(m_datastore.get(), path.c_str())); |
| 264 | auto instancesWrapper = libyang::Set(instances.get(), nullptr); |
| 265 | for (const auto& list : instancesWrapper.data()) { |
| 266 | ListInstance instance; |
| 267 | for (const auto& child : list->child()->tree_for()) { |
| 268 | if (child->schema()->nodetype() == LYS_LEAF) { |
| 269 | libyang::Schema_Node_Leaf leafSchema(child->schema()); |
| 270 | if (leafSchema.is_key()) { |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 271 | auto leafData = std::make_shared<libyang::Data_Node_Leaf_List>(child); |
| 272 | instance.insert({leafSchema.name(), leafValueFromNode(leafData)}); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 276 | res.emplace_back(instance); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 277 | } |
| 278 | return res; |
| 279 | } |
| 280 | |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 281 | std::string YangAccess::dump(const DataFormat format) const |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 282 | { |
| 283 | char* output; |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 284 | lyd_print_mem(&output, m_datastore.get(), format == DataFormat::Xml ? LYD_XML : LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT); |
Václav Kubernát | e3d282a | 2020-07-09 10:32:12 +0200 | [diff] [blame] | 285 | std::unique_ptr<char, decltype(&free)> deleter{output, free}; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 286 | |
| 287 | if (output) { |
| 288 | std::string res = output; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 289 | return res; |
| 290 | } |
| 291 | |
| 292 | return ""; |
| 293 | } |
| 294 | |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 295 | void YangAccess::loadModule(const std::string& name) |
| 296 | { |
| 297 | m_schema->loadModule(name); |
| 298 | } |
| 299 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 300 | void YangAccess::addSchemaFile(const std::string& path) |
| 301 | { |
| 302 | m_schema->addSchemaFile(path.c_str()); |
| 303 | } |
| 304 | |
| 305 | void YangAccess::addSchemaDir(const std::string& path) |
| 306 | { |
| 307 | m_schema->addSchemaDirectory(path.c_str()); |
| 308 | } |
| 309 | |
| 310 | void YangAccess::enableFeature(const std::string& module, const std::string& feature) |
| 311 | { |
| 312 | m_schema->enableFeature(module, feature); |
| 313 | } |
Václav Kubernát | 548cb19 | 2020-06-26 14:00:42 +0200 | [diff] [blame] | 314 | |
Václav Kubernát | 0c90dd4 | 2022-01-18 00:07:29 +0100 | [diff] [blame^] | 315 | void YangAccess::addDataFile(const std::string& path, const StrictDataParsing strict) |
Václav Kubernát | 548cb19 | 2020-06-26 14:00:42 +0200 | [diff] [blame] | 316 | { |
| 317 | std::ifstream fs(path); |
| 318 | char firstChar; |
| 319 | fs >> firstChar; |
| 320 | |
| 321 | std::cout << "Parsing \"" << path << "\" as " << (firstChar == '{' ? "JSON" : "XML") << "...\n"; |
Václav Kubernát | 0c90dd4 | 2022-01-18 00:07:29 +0100 | [diff] [blame^] | 322 | |
| 323 | auto parseFlags = LYD_OPT_DATA | LYD_OPT_DATA_NO_YANGLIB | LYD_OPT_TRUSTED; |
| 324 | if (strict == StrictDataParsing::Yes) { |
| 325 | parseFlags |= LYD_OPT_STRICT; |
| 326 | } |
| 327 | auto dataNode = lyd_parse_path(m_ctx.get(), path.c_str(), firstChar == '{' ? LYD_JSON : LYD_XML, parseFlags); |
Václav Kubernát | 548cb19 | 2020-06-26 14:00:42 +0200 | [diff] [blame] | 328 | |
| 329 | if (!dataNode) { |
| 330 | throw std::runtime_error("Supplied data file " + path + " couldn't be parsed."); |
| 331 | } |
| 332 | |
| 333 | if (!m_datastore) { |
| 334 | m_datastore = lyWrap(dataNode); |
| 335 | } else { |
| 336 | lyd_merge(m_datastore.get(), dataNode, LYD_OPT_DESTRUCT); |
| 337 | } |
| 338 | |
| 339 | validate(); |
| 340 | } |