blob: ebb8d4c0519db363773d6c1bbead17835ec86831 [file] [log] [blame]
Tomáš Pecka55f64f82020-12-10 19:03:30 +01001#include <csignal>
2#include <cstring>
3#include <fstream>
Václav Kubernát7efd6d52021-11-09 01:31:11 +01004#include <iostream>
Tomáš Pecka55f64f82020-12-10 19:03:30 +01005#include <map>
Václav Kubernát7efd6d52021-11-09 01:31:11 +01006#include <sysrepo-cpp/Connection.hpp>
Tomáš Pecka55f64f82020-12-10 19:03:30 +01007#include <sysrepo-cpp/Session.hpp>
8#include <unistd.h>
9
10using namespace std::string_literals;
11
12volatile sig_atomic_t g_exit_application = 0;
13
14static const std::string MODULE_NAME = "ietf-hardware";
15static const std::string MODULE_PREFIX = "/" + MODULE_NAME + ":hardware";
16
Václav Kubernát7efd6d52021-11-09 01:31:11 +010017void valuesToYang(const std::map<std::string, std::string>& values, sysrepo::Session session, std::optional<libyang::DataNode>& parent, const std::string& prefix)
Tomáš Pecka55f64f82020-12-10 19:03:30 +010018{
19 for (const auto& [propertyName, value] : values) {
20 if (!parent) {
Jan Kundrátb3e99982022-03-18 17:38:20 +010021 parent = session.getContext().newPath(prefix + propertyName, value, libyang::CreationOptions::Output);
Tomáš Pecka55f64f82020-12-10 19:03:30 +010022 } else {
Jan Kundrátb3e99982022-03-18 17:38:20 +010023 parent->newPath(prefix + propertyName, value, libyang::CreationOptions::Output);
Tomáš Pecka55f64f82020-12-10 19:03:30 +010024 }
25 }
26}
27
28void usage(const char* progName)
29{
30 std::cout << "Usage: " << progName << "--subscribe|--setitem" << std::endl;
31}
32
33int 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
Tomáš Pecka566fa712023-01-18 17:59:03 +010048 std::map<std::string, std::string> data;
Václav Kubernát7efd6d52021-11-09 01:31:11 +010049 sysrepo::Connection srConn;
50 auto srSess = srConn.sessionStart();
Václav Kubernát7efd6d52021-11-09 01:31:11 +010051 std::optional<sysrepo::Subscription> srSub;
Tomáš Pecka55f64f82020-12-10 19:03:30 +010052
Tomáš Pecka55f64f82020-12-10 19:03:30 +010053 if (isDaemonSubscribe) {
54 data = {
55 {"/component[name='ne']/description", "This data was brought to you by process 2 (subscr)."},
56 {"/component[name='ne:ctrl']/class", "iana-hardware:module"},
57 };
58
Václav Kubernát7efd6d52021-11-09 01:31:11 +010059 sysrepo::OperGetCb cb = [&](sysrepo::Session session, auto, auto, auto, auto, auto, auto& parent) {
60 valuesToYang(data, session, parent, MODULE_PREFIX);
61 return sysrepo::ErrorCode::Ok;
62 };
Tomáš Pecka55f64f82020-12-10 19:03:30 +010063
Václav Kubernát7efd6d52021-11-09 01:31:11 +010064
65 srSub = srSess.onOperGet(
Jan Kundrátb3e99982022-03-18 17:38:20 +010066 MODULE_NAME,
Václav Kubernát7efd6d52021-11-09 01:31:11 +010067 cb,
Jan Kundrátb3e99982022-03-18 17:38:20 +010068 MODULE_PREFIX + "/*",
Václav Kubernát7efd6d52021-11-09 01:31:11 +010069 sysrepo::SubscribeOptions::Passive | sysrepo::SubscribeOptions::OperMerge);
Tomáš Pecka55f64f82020-12-10 19:03:30 +010070 } else if (isDaemonSetItem) {
71 data = {
72 {"/component[name='ne']/class", "iana-hardware:module"},
73 {"/component[name='ne:edfa']/class", "iana-hardware:module"},
74 };
75
Václav Kubernát7efd6d52021-11-09 01:31:11 +010076 srSess.switchDatastore(sysrepo::Datastore::Operational);
Tomáš Pecka55f64f82020-12-10 19:03:30 +010077 for (const auto& [k, v] : data) {
Jan Kundrátb3e99982022-03-18 17:38:20 +010078 srSess.setItem(MODULE_PREFIX + k, v);
Tomáš Pecka55f64f82020-12-10 19:03:30 +010079 }
Václav Kubernát7efd6d52021-11-09 01:31:11 +010080 srSess.applyChanges();
81 srSess.switchDatastore(sysrepo::Datastore::Running);
Tomáš Pecka55f64f82020-12-10 19:03:30 +010082 }
83
84 // touch a file so somebody can read that sysrepo things are initialised
85 {
86 std::string filename = std::to_string(getpid()) + ".sysrepo";
87 std::ofstream ofs(filename);
88 ofs << "";
89 }
90
Václav Kubernát7efd6d52021-11-09 01:31:11 +010091 signal(SIGTERM, [] (int){});
92 pause();
Tomáš Pecka55f64f82020-12-10 19:03:30 +010093 return 0;
94}