Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file process_tree.cpp |
| 3 | * @author Mislav Novakovic <mislav.novakovic@sartura.hr> |
| 4 | * @brief Example of the libyang C++ bindings |
| 5 | * |
| 6 | * Copyright (c) 2017 Deutsche Telekom AG. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include <iostream> |
| 16 | #include <memory> |
| 17 | #include <stdexcept> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include <Libyang.hpp> |
| 21 | #include <Tree_Data.hpp> |
| 22 | #include <Tree_Schema.hpp> |
| 23 | |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 24 | int main() { |
| 25 | |
Mislav Novakovic | 6d741bb | 2017-11-15 17:01:16 +0100 | [diff] [blame] | 26 | S_Context ctx = nullptr; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 27 | try { |
| 28 | ctx = S_Context(new Context("/etc/sysrepo/yang")); |
| 29 | } catch( const std::exception& e ) { |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 30 | std::cout << e.what() << std::endl; |
Mislav Novakovic | 2fd1162 | 2018-02-14 15:59:19 +0100 | [diff] [blame] | 31 | auto errors = std::shared_ptr<std::vector<S_Error>>(get_ly_errors(ctx)); |
| 32 | for(auto error = errors->begin() ; error != errors->end() ; ++error) { |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 33 | std::cout << "err: " << (*error)->err() << std::endl; |
| 34 | std::cout << "vecode: " << (*error)->vecode() << std::endl; |
| 35 | std::cout << "errmsg: " << (*error)->errmsg() << std::endl; |
| 36 | std::cout << "errpath: " << (*error)->errpath() << std::endl; |
| 37 | std::cout << "errapptag: " << (*error)->errapptag() << std::endl; |
Mislav Novakovic | 2fd1162 | 2018-02-14 15:59:19 +0100 | [diff] [blame] | 38 | } |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | auto module = ctx->get_module("turing-machine"); |
Mislav Novakovic | 6d741bb | 2017-11-15 17:01:16 +0100 | [diff] [blame] | 43 | if (module) { |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 44 | std::cout << module->name() << std::endl; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 45 | } else { |
| 46 | module = ctx->load_module("turing-machine"); |
| 47 | } |
| 48 | |
Mislav Novakovic | 6d741bb | 2017-11-15 17:01:16 +0100 | [diff] [blame] | 49 | S_Data_Node node = nullptr; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 50 | try { |
| 51 | node = ctx->parse_data_path("/etc/sysrepo/data/turing-machine.startup", LYD_XML, LYD_OPT_CONFIG); |
| 52 | } catch( const std::exception& e ) { |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 53 | std::cout << e.what() << std::endl; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Mislav Novakovic | 6d741bb | 2017-11-15 17:01:16 +0100 | [diff] [blame] | 56 | if (!node) { |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 57 | std::cout << "parse_path did not return any nodes" << std::endl; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 58 | } else { |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 59 | std::cout << "tree_dfs\n" << std::endl; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 60 | auto data_list = std::shared_ptr<std::vector<S_Data_Node>>(node->tree_dfs()); |
Mislav Novakovic | f9aac05 | 2018-02-14 16:19:21 +0100 | [diff] [blame] | 61 | for(auto elem = data_list->begin() ; elem != data_list->end() ; ++elem) { |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 62 | std::cout << "name: " << (*elem)->schema()->name() << " type: " << (*elem)->schema()->nodetype() << std::endl; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 65 | std::cout << "\nChild of " << node->schema()->name() << " is: " << node->child()->schema()->name() << "\n" << std::endl; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 66 | |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 67 | std::cout << "tree_for\n" << std::endl; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 68 | |
| 69 | data_list = std::shared_ptr<std::vector<S_Data_Node>>(node->child()->child()->tree_dfs()); |
Mislav Novakovic | f9aac05 | 2018-02-14 16:19:21 +0100 | [diff] [blame] | 70 | for(auto elem = data_list->begin() ; elem != data_list->end() ; ++elem) { |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 71 | std::cout << "child of " << node->child()->schema()->name() << " is: " << (*elem)->schema()->name() << " type: " << (*elem)->schema()->nodetype() << std::endl; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 74 | std::cout << "\n schema tree_dfs\n" << std::endl; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 75 | auto schema_list = std::shared_ptr<std::vector<S_Schema_Node>>(node->schema()->tree_dfs()); |
Mislav Novakovic | f9aac05 | 2018-02-14 16:19:21 +0100 | [diff] [blame] | 76 | for(auto elem = schema_list->begin() ; elem != schema_list->end() ; ++elem) { |
Mislav Novakovic | b453512 | 2018-05-24 17:19:18 +0200 | [diff] [blame^] | 77 | std::cout << "schema name " << (*elem)->name() << " type " << (*elem)->nodetype() << std::endl; |
Mislav Novakovic | edc6c08 | 2017-05-30 15:21:42 +0200 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |