blob: d58928d982bba7de87b5efe4f379be8ec014dca4 [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
Václav Kubernát7efd6d52021-11-09 01:31:11 +010048 sysrepo::Connection srConn;
49 auto srSess = srConn.sessionStart();
Tomáš Pecka55f64f82020-12-10 19:03:30 +010050
Václav Kubernát7efd6d52021-11-09 01:31:11 +010051 std::optional<sysrepo::Subscription> srSub;
Tomáš Pecka55f64f82020-12-10 19:03:30 +010052
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át7efd6d52021-11-09 01:31:11 +010061 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áš Pecka55f64f82020-12-10 19:03:30 +010065
Václav Kubernát7efd6d52021-11-09 01:31:11 +010066
67 srSub = srSess.onOperGet(
Jan Kundrátb3e99982022-03-18 17:38:20 +010068 MODULE_NAME,
Václav Kubernát7efd6d52021-11-09 01:31:11 +010069 cb,
Jan Kundrátb3e99982022-03-18 17:38:20 +010070 MODULE_PREFIX + "/*",
Václav Kubernát7efd6d52021-11-09 01:31:11 +010071 sysrepo::SubscribeOptions::Passive | sysrepo::SubscribeOptions::OperMerge);
Tomáš Pecka55f64f82020-12-10 19:03:30 +010072 } 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át7efd6d52021-11-09 01:31:11 +010078 srSess.switchDatastore(sysrepo::Datastore::Operational);
Tomáš Pecka55f64f82020-12-10 19:03:30 +010079 for (const auto& [k, v] : data) {
Jan Kundrátb3e99982022-03-18 17:38:20 +010080 srSess.setItem(MODULE_PREFIX + k, v);
Tomáš Pecka55f64f82020-12-10 19:03:30 +010081 }
Václav Kubernát7efd6d52021-11-09 01:31:11 +010082 srSess.applyChanges();
83 srSess.switchDatastore(sysrepo::Datastore::Running);
Tomáš Pecka55f64f82020-12-10 19:03:30 +010084 }
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át7efd6d52021-11-09 01:31:11 +010093 signal(SIGTERM, [] (int){});
94 pause();
Tomáš Pecka55f64f82020-12-10 19:03:30 +010095 return 0;
96}