Skip to content

Commit 68416e9

Browse files
rkavitha-hclKAVITHA RAMALINGAM
authored and
KAVITHA RAMALINGAM
committed
Integrated Rebootbackend changes
1 parent 80a586a commit 68416e9

12 files changed

+1229
-0
lines changed
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/gnoi_reboot.py
3+
4+
XML generated on switch by executing:
5+
dbus-send --system --type=method_call --print-reply --dest=org.SONiC.HostService.gnoi_reboot /org/SONiC/HostService/gnoi_reboot org.freedesktop.DBus.Introspectable.Introspect
6+
7+
C++ header file generated by:
8+
sudo apt-get install libdbus-c++-dev
9+
dbusxx-xml2cpp ./gnoi_reboot.xml --proxy=gnoi_reboot_dbus.h
10+
?>
11+
12+
<node name="/org/SONiC/HostService/gnoi_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.gnoi_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,65 @@
1+
2+
/*
3+
* This file was automatically generated by dbusxx-xml2cpp; DO NOT EDIT!
4+
*/
5+
6+
#ifndef __dbusxx__rebootbackend_gnoi_reboot_dbus_h__PROXY_MARSHAL_H
7+
#define __dbusxx__rebootbackend_gnoi_reboot_dbus_h__PROXY_MARSHAL_H
8+
9+
#include <dbus-c++/dbus.h>
10+
11+
#include <cassert>
12+
13+
namespace org {
14+
namespace SONiC {
15+
namespace HostService {
16+
17+
class gnoi_reboot_proxy : public ::DBus::InterfaceProxy {
18+
public:
19+
gnoi_reboot_proxy()
20+
: ::DBus::InterfaceProxy("org.SONiC.HostService.gnoi_reboot") {}
21+
22+
public:
23+
/* properties exported by this interface */
24+
public:
25+
/* methods exported by this interface,
26+
* this functions will invoke the corresponding methods on the remote objects
27+
*/
28+
void issue_reboot(const std::vector<std::string>& options, int32_t& argout0,
29+
std::string& argout1) {
30+
::DBus::CallMessage call;
31+
::DBus::MessageIter wi = call.writer();
32+
33+
wi << options;
34+
call.member("issue_reboot");
35+
::DBus::Message ret = invoke_method(call);
36+
::DBus::MessageIter ri = ret.reader();
37+
38+
ri >> argout0;
39+
ri >> argout1;
40+
}
41+
42+
void get_reboot_status(int32_t& argout0, std::string& argout1) {
43+
::DBus::CallMessage call;
44+
call.member("get_reboot_status");
45+
::DBus::Message ret = invoke_method(call);
46+
::DBus::MessageIter ri = ret.reader();
47+
48+
ri >> argout0;
49+
ri >> argout1;
50+
}
51+
52+
public:
53+
/* signal handlers for this interface
54+
*/
55+
56+
private:
57+
/* unmarshalers (to unpack the DBus message before calling the actual signal
58+
* handler)
59+
*/
60+
};
61+
62+
} // namespace HostService
63+
} // namespace SONiC
64+
} // namespace org
65+
#endif //__dbusxx__rebootbackend_gnoi_reboot_dbus_h__PROXY_MARSHAL_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#pragma once
2+
3+
#include <mutex>
4+
#include <thread>
5+
#include <unordered_set>
6+
#include <vector>
7+
8+
#include "dbconnector.h"
9+
#include "notificationproducer.h"
10+
#include "reboot_common.h"
11+
#include "reboot_interfaces.h"
12+
#include "redis_utils.h"
13+
#include "select.h"
14+
#include "selectableevent.h"
15+
#include "selectabletimer.h"
16+
#include "subscriberstatetable.h"
17+
#include "system/system.pb.h"
18+
19+
namespace rebootbackend {
20+
21+
// Holds a thread safe representation of the InitThread internal state.
22+
// Thread-safe: the expectation is one thread will write and multiple threads
23+
// will read.
24+
class InitThreadStatus {
25+
public:
26+
enum ThreadStatus {
27+
NOT_STARTED = 0,
28+
PENDING = 1,
29+
WAITING_FOR_REGISTRATION = 2,
30+
WAITING_FOR_RECONCILIATION = 3,
31+
WAITING_FOR_STATE_VERIFICATION = 4,
32+
WAITING_FOR_UNFREEZE = 5,
33+
FINALIZE = 6,
34+
DONE = 7,
35+
ERROR = 8,
36+
};
37+
38+
enum ErrorCondition {
39+
NO_ERROR = 0,
40+
UNKNOWN = 1,
41+
INTERNAL_ERROR = 2,
42+
REGISTRATION_FAILED = 3,
43+
RECONCILIATION_FAILED = 4,
44+
STATE_VERIFICATION_FAILED = 5,
45+
UNFREEZE_FAILED = 6,
46+
DETECTED_CRITICAL_STATE = 7,
47+
};
48+
49+
struct DetailedStatus {
50+
gnoi::system::RebootStatusResponse thread_state;
51+
InitThreadStatus::ThreadStatus detailed_thread_status =
52+
InitThreadStatus::ThreadStatus::NOT_STARTED;
53+
InitThreadStatus::ErrorCondition detailed_thread_error_condition =
54+
InitThreadStatus::ErrorCondition::NO_ERROR;
55+
};
56+
57+
InitThreadStatus() {
58+
m_status.detailed_thread_status = ThreadStatus::NOT_STARTED;
59+
m_status.detailed_thread_error_condition = ErrorCondition::NO_ERROR;
60+
61+
m_status.thread_state.set_active(false);
62+
m_status.thread_state.set_method(gnoi::system::RebootMethod::COLD);
63+
m_status.thread_state.mutable_status()->set_status(
64+
gnoi::system::RebootStatus_Status::RebootStatus_Status_STATUS_SUCCESS);
65+
m_status.thread_state.mutable_status()->set_message("");
66+
}
67+
68+
void set_start_status() {
69+
const std::lock_guard<std::mutex> lock(m_mutex);
70+
m_status.detailed_thread_status = ThreadStatus::PENDING;
71+
m_status.detailed_thread_error_condition = ErrorCondition::NO_ERROR;
72+
73+
m_status.thread_state.set_active(true);
74+
m_status.thread_state.set_method(gnoi::system::RebootMethod::NSF);
75+
m_status.thread_state.mutable_status()->set_status(
76+
gnoi::system::RebootStatus_Status::RebootStatus_Status_STATUS_UNKNOWN);
77+
m_status.thread_state.mutable_status()->set_message("");
78+
}
79+
80+
bool get_active(void) {
81+
const std::lock_guard<std::mutex> lock(m_mutex);
82+
return m_status.thread_state.active();
83+
}
84+
85+
void set_detailed_thread_status(ThreadStatus new_status) {
86+
const std::lock_guard<std::mutex> lock(m_mutex);
87+
if (m_status.thread_state.active()) {
88+
m_status.detailed_thread_status = new_status;
89+
}
90+
}
91+
92+
void set_success() {
93+
const std::lock_guard<std::mutex> lock(m_mutex);
94+
if (m_status.thread_state.active()) {
95+
m_status.detailed_thread_status = ThreadStatus::DONE;
96+
m_status.thread_state.mutable_status()->set_status(
97+
gnoi::system::RebootStatus_Status::
98+
RebootStatus_Status_STATUS_SUCCESS);
99+
}
100+
}
101+
102+
void set_error(ErrorCondition error_condition,
103+
const std::string &error_message) {
104+
const std::lock_guard<std::mutex> lock(m_mutex);
105+
if (m_status.thread_state.active()) {
106+
m_status.detailed_thread_status = ThreadStatus::ERROR;
107+
m_status.detailed_thread_error_condition = error_condition;
108+
m_status.thread_state.mutable_status()->set_status(
109+
gnoi::system::RebootStatus_Status::
110+
RebootStatus_Status_STATUS_FAILURE);
111+
m_status.thread_state.mutable_status()->set_message(error_message);
112+
}
113+
}
114+
115+
void set_inactive() {
116+
const std::lock_guard<std::mutex> lock(m_mutex);
117+
m_status.thread_state.set_active(false);
118+
}
119+
120+
DetailedStatus get_detailed_thread_status() {
121+
const std::lock_guard<std::mutex> lock(m_mutex);
122+
return m_status;
123+
}
124+
125+
gnoi::system::RebootStatusResponse get_response() {
126+
const std::lock_guard<std::mutex> lock(m_mutex);
127+
return m_status.thread_state;
128+
}
129+
130+
private:
131+
std::mutex m_mutex;
132+
DetailedStatus m_status;
133+
};
134+
135+
136+
} // namespace rebootbackend
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.gnoi_reboot";
8+
constexpr char kRebootPath[] = "/org/SONiC/HostService/gnoi_reboot";
9+
10+
constexpr char kContainerShutdownBusName[] =
11+
"org.SONiC.HostService.gnoi_container_shutdown";
12+
constexpr char kContainerShutdownPath[] =
13+
"/org/SONiC/HostService/gnoi_container_shutdown";
14+
15+
// DBus::BusDispatcher dispatcher;
16+
DBus::Connection& HostServiceDbus::getConnection(void) {
17+
static DBus::Connection* connPtr = nullptr;
18+
if (connPtr == nullptr) {
19+
static DBus::BusDispatcher dispatcher;
20+
DBus::default_dispatcher = &dispatcher;
21+
22+
static DBus::Connection conn = DBus::Connection::SystemBus();
23+
connPtr = &conn;
24+
}
25+
return *connPtr;
26+
}
27+
28+
DbusInterface::DbusResponse HostServiceDbus::Reboot(
29+
const std::string& jsonRebootRequest) {
30+
int32_t status;
31+
32+
GnoiDbusReboot reboot_client(getConnection(), kRebootBusName, kRebootPath);
33+
std::string retString;
34+
std::vector<std::string> options;
35+
options.push_back(jsonRebootRequest);
36+
try {
37+
reboot_client.issue_reboot(options, status, retString);
38+
} catch (DBus::Error& ex) {
39+
return DbusResponse{
40+
DbusStatus::DBUS_FAIL,
41+
"HostServiceDbus::Reboot: failed to call reboot host service"};
42+
}
43+
44+
// gnoi_reboot.py returns 0 for success, 1 for failure
45+
if (status == 0) {
46+
// Successful reboot response is an empty string.
47+
return DbusResponse{DbusStatus::DBUS_SUCCESS, ""};
48+
}
49+
return DbusResponse{DbusStatus::DBUS_FAIL, retString};
50+
}
51+
52+
DbusInterface::DbusResponse HostServiceDbus::RebootStatus(
53+
const std::string& jsonStatusRequest) {
54+
GnoiDbusReboot reboot_client(getConnection(), kRebootBusName, kRebootPath);
55+
int32_t status;
56+
std::string retString;
57+
58+
try {
59+
reboot_client.get_reboot_status(status, retString);
60+
} catch (DBus::Error& ex) {
61+
return DbusResponse{
62+
DbusStatus::DBUS_FAIL,
63+
"HostServiceDbus::RebootStatus: failed to call reboot status "
64+
"host service"};
65+
}
66+
67+
// gnoi_reboot.py returns 0 for success, 1 for failure
68+
if (status == 0) {
69+
return DbusResponse{DbusStatus::DBUS_SUCCESS, retString};
70+
}
71+
return DbusResponse{DbusStatus::DBUS_FAIL, retString};
72+
}
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 "gnoi_reboot_dbus.h" // auto generated gnoi_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 GnoiDbusReboot : public org::SONiC::HostService::gnoi_reboot_proxy,
16+
public DBus::IntrospectableProxy,
17+
public DBus::ObjectProxy {
18+
public:
19+
GnoiDbusReboot(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,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)