forked from sonic-net/sonic-gnmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbus_client.go
149 lines (135 loc) · 4.36 KB
/
dbus_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package host_service
import (
"time"
"fmt"
"reflect"
log "github.com/golang/glog"
"github.com/godbus/dbus/v5"
"github.com/sonic-net/sonic-gnmi/common_utils"
)
type Service interface {
ConfigReload(fileName string) error
ConfigSave(fileName string) error
ApplyPatchYang(fileName string) error
ApplyPatchDb(fileName string) error
CreateCheckPoint(cpName string) error
DeleteCheckPoint(cpName string) error
}
type DbusClient struct {
busNamePrefix string
busPathPrefix string
intNamePrefix string
channel chan struct{}
}
func NewDbusClient() (Service, error) {
var client DbusClient
var err error
client.busNamePrefix = "org.SONiC.HostService."
client.busPathPrefix = "/org/SONiC/HostService/"
client.intNamePrefix = "org.SONiC.HostService."
err = nil
return &client, err
}
func DbusApi(busName string, busPath string, intName string, timeout int, args ...interface{}) error {
common_utils.IncCounter(common_utils.DBUS)
conn, err := dbus.SystemBus()
if err != nil {
log.V(2).Infof("Failed to connect to system bus: %v", err)
common_utils.IncCounter(common_utils.DBUS_FAIL)
return err
}
ch := make(chan *dbus.Call, 1)
obj := conn.Object(busName, dbus.ObjectPath(busPath))
obj.Go(intName, 0, ch, args...)
select {
case call := <-ch:
if call.Err != nil {
common_utils.IncCounter(common_utils.DBUS_FAIL)
return call.Err
}
result := call.Body
if len(result) == 0 {
common_utils.IncCounter(common_utils.DBUS_FAIL)
return fmt.Errorf("Dbus result is empty %v", result)
}
if ret, ok := result[0].(int32); ok {
if ret == 0 {
return nil
} else {
if len(result) != 2 {
common_utils.IncCounter(common_utils.DBUS_FAIL)
return fmt.Errorf("Dbus result is invalid %v", result)
}
if msg, check := result[1].(string); check {
common_utils.IncCounter(common_utils.DBUS_FAIL)
return fmt.Errorf(msg)
} else {
common_utils.IncCounter(common_utils.DBUS_FAIL)
return fmt.Errorf("Invalid result message type %v %v", result[1], reflect.TypeOf(result[1]))
}
}
} else {
common_utils.IncCounter(common_utils.DBUS_FAIL)
return fmt.Errorf("Invalid result type %v %v", result[0], reflect.TypeOf(result[0]))
}
case <-time.After(time.Duration(timeout) * time.Second):
log.V(2).Infof("DbusApi: timeout")
common_utils.IncCounter(common_utils.DBUS_FAIL)
return fmt.Errorf("Timeout %v", timeout)
}
return nil
}
func (c *DbusClient) ConfigReload(config string) error {
common_utils.IncCounter(common_utils.DBUS_CONFIG_RELOAD)
modName := "config"
busName := c.busNamePrefix + modName
busPath := c.busPathPrefix + modName
intName := c.intNamePrefix + modName + ".reload"
err := DbusApi(busName, busPath, intName, 10, config)
return err
}
func (c *DbusClient) ConfigSave(fileName string) error {
common_utils.IncCounter(common_utils.DBUS_CONFIG_SAVE)
modName := "config"
busName := c.busNamePrefix + modName
busPath := c.busPathPrefix + modName
intName := c.intNamePrefix + modName + ".save"
err := DbusApi(busName, busPath, intName, 10, fileName)
return err
}
func (c *DbusClient) ApplyPatchYang(patch string) error {
common_utils.IncCounter(common_utils.DBUS_APPLY_PATCH_YANG)
modName := "gcu"
busName := c.busNamePrefix + modName
busPath := c.busPathPrefix + modName
intName := c.intNamePrefix + modName + ".apply_patch_yang"
err := DbusApi(busName, busPath, intName, 60, patch)
return err
}
func (c *DbusClient) ApplyPatchDb(patch string) error {
common_utils.IncCounter(common_utils.DBUS_APPLY_PATCH_DB)
modName := "gcu"
busName := c.busNamePrefix + modName
busPath := c.busPathPrefix + modName
intName := c.intNamePrefix + modName + ".apply_patch_db"
err := DbusApi(busName, busPath, intName, 60, patch)
return err
}
func (c *DbusClient) CreateCheckPoint(fileName string) error {
common_utils.IncCounter(common_utils.DBUS_CREATE_CHECKPOINT)
modName := "gcu"
busName := c.busNamePrefix + modName
busPath := c.busPathPrefix + modName
intName := c.intNamePrefix + modName + ".create_checkpoint"
err := DbusApi(busName, busPath, intName, 10, fileName)
return err
}
func (c *DbusClient) DeleteCheckPoint(fileName string) error {
common_utils.IncCounter(common_utils.DBUS_DELETE_CHECKPOINT)
modName := "gcu"
busName := c.busNamePrefix + modName
busPath := c.busPathPrefix + modName
intName := c.intNamePrefix + modName + ".delete_checkpoint"
err := DbusApi(busName, busPath, intName, 10, fileName)
return err
}