blob: 3249fa220c620b8877378bb8716874113577e82d [file] [log] [blame]
Mislav Novakovicedc6c082017-05-30 15:21:42 +02001/**
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 Novakovicedc6c082017-05-30 15:21:42 +020024int main() {
25
Mislav Novakovic6d741bb2017-11-15 17:01:16 +010026 S_Context ctx = nullptr;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020027 try {
28 ctx = S_Context(new Context("/etc/sysrepo/yang"));
29 } catch( const std::exception& e ) {
Mislav Novakovicb4535122018-05-24 17:19:18 +020030 std::cout << e.what() << std::endl;
Mislav Novakovic2fd11622018-02-14 15:59:19 +010031 auto errors = std::shared_ptr<std::vector<S_Error>>(get_ly_errors(ctx));
32 for(auto error = errors->begin() ; error != errors->end() ; ++error) {
Mislav Novakovicb4535122018-05-24 17:19:18 +020033 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 Novakovic2fd11622018-02-14 15:59:19 +010038 }
Mislav Novakovicedc6c082017-05-30 15:21:42 +020039 return -1;
40 }
41
42 auto module = ctx->get_module("turing-machine");
Mislav Novakovic6d741bb2017-11-15 17:01:16 +010043 if (module) {
Mislav Novakovicb4535122018-05-24 17:19:18 +020044 std::cout << module->name() << std::endl;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020045 } else {
46 module = ctx->load_module("turing-machine");
47 }
48
Mislav Novakovic6d741bb2017-11-15 17:01:16 +010049 S_Data_Node node = nullptr;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020050 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 Novakovicb4535122018-05-24 17:19:18 +020053 std::cout << e.what() << std::endl;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020054 }
55
Mislav Novakovic6d741bb2017-11-15 17:01:16 +010056 if (!node) {
Mislav Novakovicb4535122018-05-24 17:19:18 +020057 std::cout << "parse_path did not return any nodes" << std::endl;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020058 } else {
Mislav Novakovicb4535122018-05-24 17:19:18 +020059 std::cout << "tree_dfs\n" << std::endl;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020060 auto data_list = std::shared_ptr<std::vector<S_Data_Node>>(node->tree_dfs());
Mislav Novakovicf9aac052018-02-14 16:19:21 +010061 for(auto elem = data_list->begin() ; elem != data_list->end() ; ++elem) {
Mislav Novakovicb4535122018-05-24 17:19:18 +020062 std::cout << "name: " << (*elem)->schema()->name() << " type: " << (*elem)->schema()->nodetype() << std::endl;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020063 }
64
Mislav Novakovicb4535122018-05-24 17:19:18 +020065 std::cout << "\nChild of " << node->schema()->name() << " is: " << node->child()->schema()->name() << "\n" << std::endl;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020066
Mislav Novakovicb4535122018-05-24 17:19:18 +020067 std::cout << "tree_for\n" << std::endl;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020068
69 data_list = std::shared_ptr<std::vector<S_Data_Node>>(node->child()->child()->tree_dfs());
Mislav Novakovicf9aac052018-02-14 16:19:21 +010070 for(auto elem = data_list->begin() ; elem != data_list->end() ; ++elem) {
Mislav Novakovicb4535122018-05-24 17:19:18 +020071 std::cout << "child of " << node->child()->schema()->name() << " is: " << (*elem)->schema()->name() << " type: " << (*elem)->schema()->nodetype() << std::endl;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020072 }
73
Mislav Novakovicb4535122018-05-24 17:19:18 +020074 std::cout << "\n schema tree_dfs\n" << std::endl;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020075 auto schema_list = std::shared_ptr<std::vector<S_Schema_Node>>(node->schema()->tree_dfs());
Mislav Novakovicf9aac052018-02-14 16:19:21 +010076 for(auto elem = schema_list->begin() ; elem != schema_list->end() ; ++elem) {
Mislav Novakovicb4535122018-05-24 17:19:18 +020077 std::cout << "schema name " << (*elem)->name() << " type " << (*elem)->nodetype() << std::endl;
Mislav Novakovicedc6c082017-05-30 15:21:42 +020078 }
79 }
80
81 return 0;
82}