Skip to content

Commit 76fa407

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

12 files changed

+1238
-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,72 @@
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+
#include <cassert>
11+
12+
namespace org {
13+
namespace SONiC {
14+
namespace HostService {
15+
16+
class gnoi_reboot_proxy
17+
: public ::DBus::InterfaceProxy
18+
{
19+
public:
20+
21+
gnoi_reboot_proxy()
22+
: ::DBus::InterfaceProxy("org.SONiC.HostService.gnoi_reboot")
23+
{
24+
}
25+
26+
public:
27+
28+
/* properties exported by this interface */
29+
public:
30+
31+
/* methods exported by this interface,
32+
* this functions will invoke the corresponding methods on the remote objects
33+
*/
34+
void issue_reboot(const std::vector< std::string >& options, int32_t& argout0, std::string& argout1)
35+
{
36+
::DBus::CallMessage call;
37+
::DBus::MessageIter wi = call.writer();
38+
39+
wi << options;
40+
call.member("issue_reboot");
41+
::DBus::Message ret = invoke_method (call);
42+
::DBus::MessageIter ri = ret.reader();
43+
44+
ri >> argout0;
45+
ri >> argout1;
46+
}
47+
48+
void get_reboot_status(int32_t& argout0, std::string& argout1)
49+
{
50+
::DBus::CallMessage call;
51+
call.member("get_reboot_status");
52+
::DBus::Message ret = invoke_method (call);
53+
::DBus::MessageIter ri = ret.reader();
54+
55+
ri >> argout0;
56+
ri >> argout1;
57+
}
58+
59+
60+
public:
61+
62+
/* signal handlers for this interface
63+
*/
64+
65+
private:
66+
67+
/* unmarshalers (to unpack the DBus message before calling the actual signal handler)
68+
*/
69+
};
70+
71+
} } }
72+
#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,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.gnoi_reboot";
9+
constexpr char kRebootPath[] = "/org/SONiC/HostService/gnoi_reboot";
10+
11+
constexpr char kContainerShutdownBusName[] = "org.SONiC.HostService.gnoi_container_shutdown";
12+
constexpr char kContainerShutdownPath[] = "/org/SONiC/HostService/gnoi_container_shutdown";
13+
14+
// DBus::BusDispatcher dispatcher;
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+
GnoiDbusReboot 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+
// gnoi_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+
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+
}
73+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 system.proto.’https://github.com/openconfig/gnoi/blob/73a1e7675c5f963e7810bd3828203f2758eb47e8/system/system.proto#L107 */
12+
13+
class GnoiDbusReboot : public org::SONiC::HostService::gnoi_reboot_proxy,
14+
public DBus::IntrospectableProxy,
15+
public DBus::ObjectProxy {
16+
public:
17+
GnoiDbusReboot(DBus::Connection& connection, const char* dbus_bus_name_p,
18+
const char* dbus_obj_name_p)
19+
: DBus::ObjectProxy(connection, dbus_obj_name_p, dbus_bus_name_p) {}
20+
};
21+
22+
/* DbusResponse consists of STATUS: success/fail: i.e. was the dbus request
23+
successful DbusResponse.json_string: string based json formatted RebootResponse
24+
defined here:
25+
https://github.com/openconfig/gnoi/blob/73a1e7675c5f963e7810bd3828203f2758eb47e8/system/system.proto#L119 */
26+
27+
28+
class HostServiceDbus : public DbusInterface {
29+
public:
30+
DbusInterface::DbusResponse Reboot(
31+
const std::string& json_reboot_request) override;
32+
DbusInterface::DbusResponse RebootStatus(
33+
const std::string& json_status_request) override;
34+
35+
private:
36+
static DBus::Connection& getConnection(void);
37+
};
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,21 @@
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+
};
21+

0 commit comments

Comments
 (0)