Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 1 | #include <csignal> |
| 2 | #include <cstring> |
| 3 | #include <fstream> |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 4 | #include <iostream> |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 5 | #include <map> |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 6 | #include <sysrepo-cpp/Connection.hpp> |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 7 | #include <sysrepo-cpp/Session.hpp> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | using namespace std::string_literals; |
| 11 | |
| 12 | volatile sig_atomic_t g_exit_application = 0; |
| 13 | |
| 14 | static const std::string MODULE_NAME = "ietf-hardware"; |
| 15 | static const std::string MODULE_PREFIX = "/" + MODULE_NAME + ":hardware"; |
| 16 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 17 | void valuesToYang(const std::map<std::string, std::string>& values, sysrepo::Session session, std::optional<libyang::DataNode>& parent, const std::string& prefix) |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 18 | { |
| 19 | for (const auto& [propertyName, value] : values) { |
| 20 | if (!parent) { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 21 | parent = session.getContext().newPath((prefix + propertyName).c_str(), value.c_str(), libyang::CreationOptions::Output); |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 22 | } else { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 23 | parent->newPath((prefix + propertyName).c_str(), value.c_str(), libyang::CreationOptions::Output); |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | void usage(const char* progName) |
| 29 | { |
| 30 | std::cout << "Usage: " << progName << "--subscribe|--setitem" << std::endl; |
| 31 | } |
| 32 | |
| 33 | int main(int argc, char* argv[]) |
| 34 | { |
| 35 | if (argc != 2) { |
| 36 | usage(argv[0]); |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | bool isDaemonSubscribe = argv[1] == "--subscribe"s; |
| 41 | bool isDaemonSetItem = argv[1] == "--set-item"s; |
| 42 | |
| 43 | if (isDaemonSubscribe == isDaemonSetItem) { |
| 44 | usage(argv[0]); |
| 45 | return 1; |
| 46 | } |
| 47 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 48 | sysrepo::Connection srConn; |
| 49 | auto srSess = srConn.sessionStart(); |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 50 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 51 | std::optional<sysrepo::Subscription> srSub; |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 52 | |
| 53 | std::map<std::string, std::string> data; |
| 54 | |
| 55 | if (isDaemonSubscribe) { |
| 56 | data = { |
| 57 | {"/component[name='ne']/description", "This data was brought to you by process 2 (subscr)."}, |
| 58 | {"/component[name='ne:ctrl']/class", "iana-hardware:module"}, |
| 59 | }; |
| 60 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 61 | sysrepo::OperGetCb cb = [&](sysrepo::Session session, auto, auto, auto, auto, auto, auto& parent) { |
| 62 | valuesToYang(data, session, parent, MODULE_PREFIX); |
| 63 | return sysrepo::ErrorCode::Ok; |
| 64 | }; |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 65 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 66 | |
| 67 | srSub = srSess.onOperGet( |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 68 | MODULE_NAME.c_str(), |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 69 | cb, |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 70 | (MODULE_PREFIX + "/*").c_str(), |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 71 | sysrepo::SubscribeOptions::Passive | sysrepo::SubscribeOptions::OperMerge); |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 72 | } else if (isDaemonSetItem) { |
| 73 | data = { |
| 74 | {"/component[name='ne']/class", "iana-hardware:module"}, |
| 75 | {"/component[name='ne:edfa']/class", "iana-hardware:module"}, |
| 76 | }; |
| 77 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 78 | srSess.switchDatastore(sysrepo::Datastore::Operational); |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 79 | for (const auto& [k, v] : data) { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 80 | srSess.setItem((MODULE_PREFIX + k).c_str(), v.c_str()); |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 81 | } |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 82 | srSess.applyChanges(); |
| 83 | srSess.switchDatastore(sysrepo::Datastore::Running); |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // touch a file so somebody can read that sysrepo things are initialised |
| 87 | { |
| 88 | std::string filename = std::to_string(getpid()) + ".sysrepo"; |
| 89 | std::ofstream ofs(filename); |
| 90 | ofs << ""; |
| 91 | } |
| 92 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 93 | signal(SIGTERM, [] (int){}); |
| 94 | pause(); |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 95 | return 0; |
| 96 | } |