Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 1 | #include <csignal> |
| 2 | #include <cstring> |
| 3 | #include <fstream> |
| 4 | #include <map> |
| 5 | #include <sysrepo-cpp/Session.hpp> |
| 6 | #include <unistd.h> |
| 7 | |
| 8 | using namespace std::string_literals; |
| 9 | |
| 10 | volatile sig_atomic_t g_exit_application = 0; |
| 11 | |
| 12 | static const std::string MODULE_NAME = "ietf-hardware"; |
| 13 | static const std::string MODULE_PREFIX = "/" + MODULE_NAME + ":hardware"; |
| 14 | |
| 15 | void valuesToYang(const std::map<std::string, std::string>& values, std::shared_ptr<::sysrepo::Session> session, std::shared_ptr<libyang::Data_Node>& parent, const std::string& prefix) |
| 16 | { |
| 17 | for (const auto& [propertyName, value] : values) { |
| 18 | if (!parent) { |
| 19 | parent = std::make_shared<libyang::Data_Node>( |
| 20 | session->get_context(), |
| 21 | (prefix + propertyName).c_str(), |
| 22 | value.c_str(), |
| 23 | LYD_ANYDATA_CONSTSTRING, |
| 24 | LYD_PATH_OPT_OUTPUT); |
| 25 | } else { |
| 26 | parent->new_path( |
| 27 | session->get_context(), |
| 28 | (prefix + propertyName).c_str(), |
| 29 | value.c_str(), |
| 30 | LYD_ANYDATA_CONSTSTRING, |
| 31 | LYD_PATH_OPT_OUTPUT); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void usage(const char* progName) |
| 37 | { |
| 38 | std::cout << "Usage: " << progName << "--subscribe|--setitem" << std::endl; |
| 39 | } |
| 40 | |
| 41 | int main(int argc, char* argv[]) |
| 42 | { |
| 43 | if (argc != 2) { |
| 44 | usage(argv[0]); |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | bool isDaemonSubscribe = argv[1] == "--subscribe"s; |
| 49 | bool isDaemonSetItem = argv[1] == "--set-item"s; |
| 50 | |
| 51 | if (isDaemonSubscribe == isDaemonSetItem) { |
| 52 | usage(argv[0]); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | auto srConn = std::make_shared<sysrepo::Connection>(); |
| 57 | auto srSess = std::make_shared<sysrepo::Session>(srConn); |
| 58 | |
| 59 | std::shared_ptr<sysrepo::Subscribe> srSubs; |
| 60 | uint32_t srLastRequestId = 0; // for subscribe part |
| 61 | |
| 62 | std::map<std::string, std::string> data; |
| 63 | |
| 64 | if (isDaemonSubscribe) { |
| 65 | data = { |
| 66 | {"/component[name='ne']/description", "This data was brought to you by process 2 (subscr)."}, |
| 67 | {"/component[name='ne:ctrl']/class", "iana-hardware:module"}, |
| 68 | }; |
| 69 | |
| 70 | srSubs = std::make_shared<sysrepo::Subscribe>(srSess); |
| 71 | |
| 72 | srSubs->oper_get_items_subscribe( |
| 73 | MODULE_NAME.c_str(), |
| 74 | [&](std::shared_ptr<::sysrepo::Session> session, [[maybe_unused]] const char* module_name, [[maybe_unused]] const char* xpath, [[maybe_unused]] const char* request_xpath, uint32_t request_id, std::shared_ptr<libyang::Data_Node>& parent) { |
| 75 | if (srLastRequestId == request_id) { |
| 76 | return SR_ERR_OK; |
| 77 | } |
| 78 | srLastRequestId = request_id; |
| 79 | |
| 80 | valuesToYang(data, session, parent, MODULE_PREFIX); |
| 81 | return SR_ERR_OK; |
| 82 | }, |
| 83 | (MODULE_PREFIX + "/*").c_str(), |
| 84 | SR_SUBSCR_PASSIVE | SR_SUBSCR_OPER_MERGE | SR_SUBSCR_CTX_REUSE); |
| 85 | } else if (isDaemonSetItem) { |
| 86 | data = { |
| 87 | {"/component[name='ne']/class", "iana-hardware:module"}, |
| 88 | {"/component[name='ne:edfa']/class", "iana-hardware:module"}, |
| 89 | }; |
| 90 | |
| 91 | srSess->session_switch_ds(SR_DS_OPERATIONAL); |
| 92 | for (const auto& [k, v] : data) { |
| 93 | srSess->set_item_str((MODULE_PREFIX + k).c_str(), v.c_str()); |
| 94 | } |
| 95 | srSess->apply_changes(); |
| 96 | srSess->session_switch_ds(SR_DS_RUNNING); |
| 97 | } |
| 98 | |
| 99 | // touch a file so somebody can read that sysrepo things are initialised |
| 100 | { |
| 101 | std::string filename = std::to_string(getpid()) + ".sysrepo"; |
| 102 | std::ofstream ofs(filename); |
| 103 | ofs << ""; |
| 104 | } |
| 105 | |
| 106 | sleep(1000); // I guess, this is plenty of seconds, right? |
| 107 | |
| 108 | return 0; |
| 109 | } |