Skip to content

Commit 7c5edad

Browse files
rkavitha-hclVSuryaprasad-HCL
authored andcommitted
Integrated Rebootbackend changes
1 parent 9179ab0 commit 7c5edad

10 files changed

+1031
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "interfaces.h"
2+
3+
#include <dbus-c++/dbus.h> // DBus
4+
5+
// #include "component_state_helper.h"
6+
#include "reboot_interfaces.h"
7+
8+
constexpr char kRebootBusName[] = "org.SONiC.HostService.reboot";
9+
constexpr char kRebootPath[] = "/org/SONiC/HostService/reboot";
10+
11+
constexpr char kContainerShutdownBusName[] =
12+
"org.SONiC.HostService.container_shutdown";
13+
constexpr char kContainerShutdownPath[] =
14+
"/org/SONiC/HostService/container_shutdown";
15+
16+
// DBus::BusDispatcher dispatcher;
17+
DBus::Connection& HostServiceDbus::getConnection(void) {
18+
static DBus::Connection* connPtr = nullptr;
19+
if (connPtr == nullptr) {
20+
static DBus::BusDispatcher dispatcher;
21+
DBus::default_dispatcher = &dispatcher;
22+
23+
static DBus::Connection conn = DBus::Connection::SystemBus();
24+
connPtr = &conn;
25+
}
26+
return *connPtr;
27+
}
28+
29+
DbusInterface::DbusResponse HostServiceDbus::Reboot(
30+
const std::string& jsonRebootRequest) {
31+
int32_t status;
32+
33+
DbusReboot reboot_client(getConnection(), kRebootBusName, kRebootPath);
34+
std::string retString;
35+
std::vector<std::string> options;
36+
options.push_back(jsonRebootRequest);
37+
try {
38+
reboot_client.issue_reboot(options, status, retString);
39+
} catch (DBus::Error& ex) {
40+
return DbusResponse{
41+
DbusStatus::DBUS_FAIL,
42+
"HostServiceDbus::Reboot: failed to call reboot host service"};
43+
}
44+
45+
// reboot.py returns 0 for success, 1 for failure
46+
if (status == 0) {
47+
// Successful reboot response is an empty string.
48+
return DbusResponse{DbusStatus::DBUS_SUCCESS, ""};
49+
}
50+
return DbusResponse{DbusStatus::DBUS_FAIL, retString};
51+
}
52+
53+
DbusInterface::DbusResponse HostServiceDbus::RebootStatus(
54+
const std::string& jsonStatusRequest) {
55+
DbusReboot reboot_client(getConnection(), kRebootBusName, kRebootPath);
56+
int32_t status;
57+
std::string retString;
58+
59+
try {
60+
reboot_client.get_reboot_status(status, retString);
61+
} catch (DBus::Error& ex) {
62+
return DbusResponse{
63+
DbusStatus::DBUS_FAIL,
64+
"HostServiceDbus::RebootStatus: failed to call reboot status "
65+
"host service"};
66+
}
67+
68+
// reboot.py returns 0 for success, 1 for failure
69+
if (status == 0) {
70+
return DbusResponse{DbusStatus::DBUS_SUCCESS, retString};
71+
}
72+
return DbusResponse{DbusStatus::DBUS_FAIL, retString};
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
/* Reboot is a request to the reboot sonic host service to request a reboot
10+
from the platform. This takes as an argument a string based json formatted
11+
Reboot request from
12+
system.proto.’https://github.com/openconfig/gnoi/blob/73a1e7675c5f963e7810bd3828203f2758eb47e8/system/system.proto#L107
13+
*/
14+
15+
class DbusReboot : public org::SONiC::HostService::reboot_proxy,
16+
public DBus::IntrospectableProxy,
17+
public DBus::ObjectProxy {
18+
public:
19+
DbusReboot(DBus::Connection& connection, const char* dbus_bus_name_p,
20+
const char* dbus_obj_name_p)
21+
: DBus::ObjectProxy(connection, dbus_obj_name_p, dbus_bus_name_p) {}
22+
};
23+
24+
/* DbusResponse consists of STATUS: success/fail: i.e. was the dbus request
25+
successful DbusResponse.json_string: string based json formatted RebootResponse
26+
defined here:
27+
https://github.com/openconfig/gnoi/blob/73a1e7675c5f963e7810bd3828203f2758eb47e8/system/system.proto#L119
28+
*/
29+
30+
class HostServiceDbus : public DbusInterface {
31+
public:
32+
DbusInterface::DbusResponse Reboot(
33+
const std::string& json_reboot_request) override;
34+
DbusInterface::DbusResponse RebootStatus(
35+
const std::string& json_status_request) override;
36+
37+
private:
38+
static DBus::Connection& getConnection(void);
39+
};
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)