@@ -3,8 +3,6 @@ package gnmi
33import (
44 "context"
55 "encoding/json"
6- "errors"
7- io "io/ioutil"
86 "os"
97 "os/user"
108 "strconv"
@@ -15,8 +13,6 @@ import (
1513 log "github.com/golang/glog"
1614 gnoi_file_pb "github.com/openconfig/gnoi/file"
1715 gnoi_os_pb "github.com/openconfig/gnoi/os"
18- gnoi_system_pb "github.com/openconfig/gnoi/system"
19- "github.com/sonic-net/sonic-gnmi/common_utils"
2016 spb "github.com/sonic-net/sonic-gnmi/proto/gnoi"
2117 spb_jwt "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt"
2218 ssc "github.com/sonic-net/sonic-gnmi/sonic_service_client"
@@ -25,6 +21,10 @@ import (
2521 "google.golang.org/grpc/status"
2622)
2723
24+ const (
25+ stateDB string = "STATE_DB"
26+ )
27+
2828func ReadFileStat (path string ) (* gnoi_file_pb.StatInfo , error ) {
2929 sc , err := ssc .NewDbusClient ()
3030 if err != nil {
@@ -97,29 +97,6 @@ func (srv *FileServer) Get(req *gnoi_file_pb.GetRequest, stream gnoi_file_pb.Fil
9797 return status .Errorf (codes .Unimplemented , "" )
9898}
9999
100- func KillOrRestartProcess (restart bool , serviceName string ) error {
101- sc , err := ssc .NewDbusClient ()
102- if err != nil {
103- return err
104- }
105- defer sc .Close ()
106-
107- if restart {
108- log .V (2 ).Infof ("Restarting service %s..." , serviceName )
109- err = sc .RestartService (serviceName )
110- if err != nil {
111- log .V (2 ).Infof ("Failed to restart service %s: %v" , serviceName , err )
112- }
113- } else {
114- log .V (2 ).Infof ("Stopping service %s..." , serviceName )
115- err = sc .StopService (serviceName )
116- if err != nil {
117- log .V (2 ).Infof ("Failed to stop service %s: %v" , serviceName , err )
118- }
119- }
120- return err
121- }
122-
123100func (srv * OSServer ) Verify (ctx context.Context , req * gnoi_os_pb.VerifyRequest ) (* gnoi_os_pb.VerifyResponse , error ) {
124101 _ , err := authenticate (srv .config , ctx , false )
125102 if err != nil {
@@ -212,159 +189,6 @@ func (srv *OSServer) Activate(ctx context.Context, req *gnoi_os_pb.ActivateReque
212189 return & resp , nil
213190}
214191
215- func (srv * SystemServer ) KillProcess (ctx context.Context , req * gnoi_system_pb.KillProcessRequest ) (* gnoi_system_pb.KillProcessResponse , error ) {
216- _ , err := authenticate (srv .config , ctx , true )
217- if err != nil {
218- return nil , err
219- }
220-
221- serviceName := req .GetName ()
222- restart := req .GetRestart ()
223- if req .GetPid () != 0 {
224- return nil , status .Errorf (codes .Unimplemented , "Pid option is not implemented" )
225- }
226- if req .GetSignal () != gnoi_system_pb .KillProcessRequest_SIGNAL_TERM {
227- return nil , status .Errorf (codes .Unimplemented , "KillProcess only supports SIGNAL_TERM (option 1) for graceful process termination. Please specify SIGNAL_TERM" )
228- }
229- log .V (1 ).Info ("gNOI: KillProcess with optional restart" )
230- log .V (1 ).Info ("Request: " , req )
231- err = KillOrRestartProcess (restart , serviceName )
232- if err != nil {
233- return nil , err
234- }
235- var resp gnoi_system_pb.KillProcessResponse
236- return & resp , nil
237- }
238-
239- func HaltSystem () error {
240- sc , err := ssc .NewDbusClient ()
241- if err != nil {
242- return err
243- }
244- defer sc .Close ()
245-
246- log .V (2 ).Infof ("Halting the system.." )
247- err = sc .HaltSystem ()
248- if err != nil {
249- log .V (2 ).Infof ("Failed to Halt the system %v" , err )
250- }
251- return err
252- }
253-
254- func RebootSystem (fileName string ) error {
255- log .V (2 ).Infof ("Rebooting with %s..." , fileName )
256- sc , err := ssc .NewDbusClient ()
257- if err != nil {
258- return err
259- }
260- defer sc .Close ()
261-
262- err = sc .ConfigReload (fileName )
263- return err
264- }
265-
266- func (srv * SystemServer ) Reboot (ctx context.Context , req * gnoi_system_pb.RebootRequest ) (* gnoi_system_pb.RebootResponse , error ) {
267- fileName := common_utils .GNMI_WORK_PATH + "/config_db.json.tmp"
268-
269- _ , err := authenticate (srv .config , ctx , true )
270- if err != nil {
271- return nil , err
272- }
273- log .V (1 ).Info ("gNOI: Reboot" )
274- log .V (1 ).Info ("Request:" , req )
275-
276- // Check the reboot type
277- switch req .GetMethod () {
278- case gnoi_system_pb .RebootMethod_HALT :
279- log .V (1 ).Info ("Reboot method is HALT. Halting the system..." )
280- err = HaltSystem ()
281- if err != nil {
282- return nil , err
283- }
284- default :
285- log .V (1 ).Info ("Reboot system now, delay is ignored..." )
286- // TODO: Support GNOI reboot delay
287- // Delay in nanoseconds before issuing reboot.
288- // https://github.com/openconfig/gnoi/blob/master/system/system.proto#L102-L115
289- config_db_json , err := io .ReadFile (fileName )
290- if errors .Is (err , os .ErrNotExist ) {
291- fileName = ""
292- }
293- err = RebootSystem (string (config_db_json ))
294- if err != nil {
295- return nil , err
296- }
297- }
298-
299- var resp gnoi_system_pb.RebootResponse
300- return & resp , nil
301- }
302-
303- // TODO: Support GNOI RebootStatus
304- func (srv * SystemServer ) RebootStatus (ctx context.Context , req * gnoi_system_pb.RebootStatusRequest ) (* gnoi_system_pb.RebootStatusResponse , error ) {
305- _ , err := authenticate (srv .config , ctx , false )
306- if err != nil {
307- return nil , err
308- }
309- log .V (1 ).Info ("gNOI: RebootStatus" )
310- return nil , status .Errorf (codes .Unimplemented , "" )
311- }
312-
313- // TODO: Support GNOI CancelReboot
314- func (srv * SystemServer ) CancelReboot (ctx context.Context , req * gnoi_system_pb.CancelRebootRequest ) (* gnoi_system_pb.CancelRebootResponse , error ) {
315- _ , err := authenticate (srv .config , ctx , true )
316- if err != nil {
317- return nil , err
318- }
319- log .V (1 ).Info ("gNOI: CancelReboot" )
320- return nil , status .Errorf (codes .Unimplemented , "" )
321- }
322- func (srv * SystemServer ) Ping (req * gnoi_system_pb.PingRequest , rs gnoi_system_pb.System_PingServer ) error {
323- ctx := rs .Context ()
324- _ , err := authenticate (srv .config , ctx , true )
325- if err != nil {
326- return err
327- }
328- log .V (1 ).Info ("gNOI: Ping" )
329- return status .Errorf (codes .Unimplemented , "" )
330- }
331- func (srv * SystemServer ) Traceroute (req * gnoi_system_pb.TracerouteRequest , rs gnoi_system_pb.System_TracerouteServer ) error {
332- ctx := rs .Context ()
333- _ , err := authenticate (srv .config , ctx , true )
334- if err != nil {
335- return err
336- }
337- log .V (1 ).Info ("gNOI: Traceroute" )
338- return status .Errorf (codes .Unimplemented , "" )
339- }
340- func (srv * SystemServer ) SetPackage (rs gnoi_system_pb.System_SetPackageServer ) error {
341- ctx := rs .Context ()
342- _ , err := authenticate (srv .config , ctx , true )
343- if err != nil {
344- return err
345- }
346- log .V (1 ).Info ("gNOI: SetPackage" )
347- return status .Errorf (codes .Unimplemented , "" )
348- }
349- func (srv * SystemServer ) SwitchControlProcessor (ctx context.Context , req * gnoi_system_pb.SwitchControlProcessorRequest ) (* gnoi_system_pb.SwitchControlProcessorResponse , error ) {
350- _ , err := authenticate (srv .config , ctx , true )
351- if err != nil {
352- return nil , err
353- }
354- log .V (1 ).Info ("gNOI: SwitchControlProcessor" )
355- return nil , status .Errorf (codes .Unimplemented , "" )
356- }
357- func (srv * SystemServer ) Time (ctx context.Context , req * gnoi_system_pb.TimeRequest ) (* gnoi_system_pb.TimeResponse , error ) {
358- _ , err := authenticate (srv .config , ctx , false )
359- if err != nil {
360- return nil , err
361- }
362- log .V (1 ).Info ("gNOI: Time" )
363- var tm gnoi_system_pb.TimeResponse
364- tm .Time = uint64 (time .Now ().UnixNano ())
365- return & tm , nil
366- }
367-
368192func (srv * Server ) Authenticate (ctx context.Context , req * spb_jwt.AuthenticateRequest ) (* spb_jwt.AuthenticateResponse , error ) {
369193 // Can't enforce normal authentication here.. maybe only enforce client cert auth if enabled?
370194 // ctx,err := authenticate(srv.config, ctx, false)
0 commit comments