| #include <csignal> |
| #include <cstring> |
| #include <fstream> |
| #include <iostream> |
| #include <map> |
| #include <sysrepo-cpp/Connection.hpp> |
| #include <sysrepo-cpp/Session.hpp> |
| #include <unistd.h> |
| |
| using namespace std::string_literals; |
| |
| volatile sig_atomic_t g_exit_application = 0; |
| |
| static const std::string MODULE_NAME = "ietf-hardware"; |
| static const std::string MODULE_PREFIX = "/" + MODULE_NAME + ":hardware"; |
| |
| void valuesToYang(const std::map<std::string, std::string>& values, sysrepo::Session session, std::optional<libyang::DataNode>& parent, const std::string& prefix) |
| { |
| for (const auto& [propertyName, value] : values) { |
| if (!parent) { |
| parent = session.getContext().newPath(prefix + propertyName, value, libyang::CreationOptions::Output); |
| } else { |
| parent->newPath(prefix + propertyName, value, libyang::CreationOptions::Output); |
| } |
| } |
| } |
| |
| void usage(const char* progName) |
| { |
| std::cout << "Usage: " << progName << "--subscribe|--setitem" << std::endl; |
| } |
| |
| int main(int argc, char* argv[]) |
| { |
| if (argc != 2) { |
| usage(argv[0]); |
| return 1; |
| } |
| |
| bool isDaemonSubscribe = argv[1] == "--subscribe"s; |
| bool isDaemonSetItem = argv[1] == "--set-item"s; |
| |
| if (isDaemonSubscribe == isDaemonSetItem) { |
| usage(argv[0]); |
| return 1; |
| } |
| |
| std::map<std::string, std::string> data; |
| sysrepo::Connection srConn; |
| auto srSess = srConn.sessionStart(); |
| std::optional<sysrepo::Subscription> srSub; |
| |
| if (isDaemonSubscribe) { |
| data = { |
| {"/component[name='ne']/description", "This data was brought to you by process 2 (subscr)."}, |
| {"/component[name='ne:ctrl']/class", "iana-hardware:module"}, |
| }; |
| |
| sysrepo::OperGetCb cb = [&](sysrepo::Session session, auto, auto, auto, auto, auto, auto& parent) { |
| valuesToYang(data, session, parent, MODULE_PREFIX); |
| return sysrepo::ErrorCode::Ok; |
| }; |
| |
| |
| srSub = srSess.onOperGet( |
| MODULE_NAME, |
| cb, |
| MODULE_PREFIX + "/*", |
| sysrepo::SubscribeOptions::Passive | sysrepo::SubscribeOptions::OperMerge); |
| } else if (isDaemonSetItem) { |
| data = { |
| {"/component[name='ne']/class", "iana-hardware:module"}, |
| {"/component[name='ne:edfa']/class", "iana-hardware:module"}, |
| }; |
| |
| srSess.switchDatastore(sysrepo::Datastore::Operational); |
| for (const auto& [k, v] : data) { |
| srSess.setItem(MODULE_PREFIX + k, v); |
| } |
| srSess.applyChanges(); |
| srSess.switchDatastore(sysrepo::Datastore::Running); |
| } |
| |
| // touch a file so somebody can read that sysrepo things are initialised |
| { |
| std::string filename = std::to_string(getpid()) + ".sysrepo"; |
| std::ofstream ofs(filename); |
| ofs << ""; |
| } |
| |
| signal(SIGTERM, [] (int){}); |
| pause(); |
| return 0; |
| } |