Skip to content

Commit 7d2eb45

Browse files
rkavitha-hclBibhuprasad Singh
authored and
Bibhuprasad Singh
committed
gNOI Cold Reboot - Integrated Rebootbackend changes
1 parent f6c62d8 commit 7d2eb45

10 files changed

+1015
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "interfaces.h"
2+
3+
#include <dbus-c++/dbus.h> // DBus
4+
5+
#include "reboot_interfaces.h"
6+
7+
constexpr char kRebootBusName[] = "org.SONiC.HostService.reboot";
8+
constexpr char kRebootPath[] = "/org/SONiC/HostService/reboot";
9+
10+
constexpr char kContainerShutdownBusName[] =
11+
"org.SONiC.HostService.container_shutdown";
12+
constexpr char kContainerShutdownPath[] =
13+
"/org/SONiC/HostService/container_shutdown";
14+
15+
DBus::Connection& HostServiceDbus::getConnection(void) {
16+
static DBus::Connection* connPtr = nullptr;
17+
if (connPtr == nullptr) {
18+
static DBus::BusDispatcher dispatcher;
19+
DBus::default_dispatcher = &dispatcher;
20+
21+
static DBus::Connection conn = DBus::Connection::SystemBus();
22+
connPtr = &conn;
23+
}
24+
return *connPtr;
25+
}
26+
27+
DbusInterface::DbusResponse HostServiceDbus::Reboot(
28+
const std::string& jsonRebootRequest) {
29+
int32_t status;
30+
31+
DbusReboot reboot_client(getConnection(), kRebootBusName, kRebootPath);
32+
std::string retString;
33+
std::vector<std::string> options;
34+
options.push_back(jsonRebootRequest);
35+
try {
36+
reboot_client.issue_reboot(options, status, retString);
37+
} catch (DBus::Error& ex) {
38+
return DbusResponse{
39+
DbusStatus::DBUS_FAIL,
40+
"HostServiceDbus::Reboot: failed to call reboot host service"};
41+
}
42+
43+
// reboot.py returns 0 for success, 1 for failure
44+
if (status == 0) {
45+
// Successful reboot response is an empty string.
46+
return DbusResponse{DbusStatus::DBUS_SUCCESS, ""};
47+
}
48+
return DbusResponse{DbusStatus::DBUS_FAIL, retString};
49+
}
50+
51+
DbusInterface::DbusResponse HostServiceDbus::RebootStatus(
52+
const std::string& jsonStatusRequest) {
53+
DbusReboot reboot_client(getConnection(), kRebootBusName, kRebootPath);
54+
int32_t status;
55+
std::string retString;
56+
57+
try {
58+
reboot_client.get_reboot_status(status, retString);
59+
} catch (DBus::Error& ex) {
60+
return DbusResponse{
61+
DbusStatus::DBUS_FAIL,
62+
"HostServiceDbus::RebootStatus: failed to call reboot status "
63+
"host service"};
64+
}
65+
66+
// reboot.py returns 0 for success, 1 for failure
67+
if (status == 0) {
68+
return DbusResponse{DbusStatus::DBUS_SUCCESS, retString};
69+
}
70+
return DbusResponse{DbusStatus::DBUS_FAIL, retString};
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
#include <dbus-c++/dbus.h>
3+
4+
#include <string>
5+
6+
#include "reboot_dbus.h" // auto generated reboot_proxy
7+
#include "reboot_interfaces.h"
8+
9+
class DbusReboot : public org::SONiC::HostService::reboot_proxy,
10+
public DBus::IntrospectableProxy,
11+
public DBus::ObjectProxy {
12+
public:
13+
DbusReboot(DBus::Connection& connection, const char* dbus_bus_name_p,
14+
const char* dbus_obj_name_p)
15+
: DBus::ObjectProxy(connection, dbus_obj_name_p, dbus_bus_name_p) {}
16+
};
17+
18+
class HostServiceDbus : public DbusInterface {
19+
public:
20+
DbusInterface::DbusResponse Reboot(
21+
const std::string& json_reboot_request) override;
22+
DbusInterface::DbusResponse RebootStatus(
23+
const std::string& json_status_request) override;
24+
25+
private:
26+
static DBus::Connection& getConnection(void);
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?ignore
2+
XML representation of dbus interface created by: sonic-host-services/host_modules/reboot.py
3+
4+
XML generated on switch by executing:
5+
dbus-send --system --type=method_call --print-reply --dest=org.SONiC.HostService.reboot /org/SONiC/HostService/reboot org.freedesktop.DBus.Introspectable.Introspect
6+
7+
C++ header file generated by:
8+
sudo apt-get install libdbus-c++-dev
9+
dbusxx-xml2cpp ./reboot.xml --proxy=reboot_dbus.h
10+
?>
11+
12+
<node name="/org/SONiC/HostService/reboot">
13+
<interface name="org.freedesktop.DBus.Introspectable">
14+
<method name="Introspect">
15+
<arg direction="out" type="s" />
16+
</method>
17+
</interface>
18+
<interface name="org.SONiC.HostService.reboot">
19+
<method name="issue_reboot">
20+
<arg direction="in" type="as" name="options" />
21+
<arg direction="out" type="i" />
22+
<arg direction="out" type="s" />
23+
</method>
24+
<method name="get_reboot_status">
25+
<arg direction="out" type="i" />
26+
<arg direction="out" type="s" />
27+
</method>
28+
</interface>
29+
</node>
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <time.h>
4+
5+
#include "status_code_util.h"
6+
7+
namespace rebootbackend {
8+
9+
extern bool sigterm_requested;
10+
struct NotificationResponse {
11+
swss::StatusCode status;
12+
std::string json_string;
13+
};
14+
15+
} // namespace rebootbackend
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
class DbusInterface {
6+
public:
7+
enum class DbusStatus {
8+
DBUS_SUCCESS,
9+
DBUS_FAIL,
10+
};
11+
12+
struct DbusResponse {
13+
DbusStatus status;
14+
std::string json_string;
15+
};
16+
17+
virtual ~DbusInterface() = default;
18+
virtual DbusResponse Reboot(const std::string& jsonRebootRequest) = 0;
19+
virtual DbusResponse RebootStatus(const std::string& jsonStatusRequest) = 0;
20+
};

0 commit comments

Comments
 (0)