Skip to content

Commit

Permalink
Merge branch 'master' into improve_full_config
Browse files Browse the repository at this point in the history
  • Loading branch information
ganglyu authored Nov 6, 2024
2 parents abfa863 + e844925 commit ef1910a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
24 changes: 20 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
# Build directory
build/

# Debian packaging files
debian/.debhelper/
debian/files
debian/sonic-telemetry.debhelper.log
debian/sonic-telemetry.substvars
debian/sonic-telemetry/
vendor
src
cvl
translib
debian/sonic-gnmi.debhelper.log
debian/sonic-gnmi.substvars
debian/sonic-gnmi/

# Vendor directory
vendor/

# Source directories
src/
cvl/
translib/

# SWSS common generated files
swsscommon/swsscommon.go
swsscommon/swsscommon.i
swsscommon/swsscommon_wrap.cxx
swsscommon/swsscommon_wrap.h
3 changes: 3 additions & 0 deletions common_utils/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const (
DBUS_RESTART_SERVICE
DBUS_FILE_STAT
DBUS_VALIDATE_YANG
DBUS_HALT_SYSTEM
COUNTER_SIZE
)

Expand Down Expand Up @@ -92,6 +93,8 @@ func (c CounterType) String() string {
return "DBUS restart service"
case DBUS_FILE_STAT:
return "DBUS file stat"
case DBUS_HALT_SYSTEM:
return "DBUS halt system"
default:
return ""
}
Expand Down
14 changes: 14 additions & 0 deletions gnmi_server/gnoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ func (srv *SystemServer) KillProcess(ctx context.Context, req *gnoi_system_pb.Ki
return &resp, nil
}

func HaltSystem() error {
sc,err := ssc.NewDbusClient()
if err != nil {
return err
}

log.V(2).Infof("Halting the system..")
err = sc.HaltSystem()
if err != nil {
log.V(2).Infof("Failed to Halt the system %v", err);
}
return err
}

// TODO: Support GNOI Reboot
func (srv *SystemServer) Reboot(ctx context.Context, req *gnoi_system_pb.RebootRequest) (*gnoi_system_pb.RebootResponse, error) {
_, err := authenticate(srv.config, ctx)
Expand Down
19 changes: 19 additions & 0 deletions sonic_service_client/dbus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Service interface {
RestartService(service string) error
GetFileStat(path string) (map[string]string, error)
ValidateYang(config string) error
HaltSystem() error
}

type DbusClient struct {
Expand Down Expand Up @@ -213,3 +214,21 @@ func (c *DbusClient) ValidateYang(config string) error {
_, err := DbusApi(busName, busPath, intName, 60, config)
return err
}

func (c *DbusClient) HaltSystem() error {
// Increment the counter for the DBUS_HALT_SYSTEM event
common_utils.IncCounter(common_utils.DBUS_HALT_SYSTEM)

// Set the module name and update the D-Bus properties
modName := "systemd"
busName := c.busNamePrefix + modName
busPath := c.busPathPrefix + modName
intName := c.intNamePrefix + modName + ".execute_reboot"

//Set the method to HALT(3) the system
const RebootMethod_HALT = 3

// Invoke the D-Bus API to execute the halt command
_, err := DbusApi(busName, busPath, intName, 10, RebootMethod_HALT)
return err
}
11 changes: 11 additions & 0 deletions test/test_gnoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ def test_gnoi_reboot(self):
assert ret != 0, 'Reboot should fail' + msg
assert 'Unimplemented' in msg

def test_gnoi_reboot_halt(self):
ret, old_cnt = gnmi_dump('DBUS halt system')
assert ret == 0, 'Fail to read counter'

ret, msg = gnoi_reboot(3, 0, 'Test halt system')
assert ret == 0, msg

ret, new_cnt = gnmi_dump('DBUS halt system')
assert ret == 0, 'Fail to read counter'
assert new_cnt == old_cnt+1, 'DBUS API is not invoked'

def test_gnoi_rebootstatus(self):
ret, msg = gnoi_rebootstatus()
assert ret != 0, 'RebootStatus should fail' + msg
Expand Down

0 comments on commit ef1910a

Please sign in to comment.