forked from ovn-org/libovsdb
-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
262 lines (220 loc) · 6.25 KB
/
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package libovsdb
import (
"encoding/json"
"errors"
"fmt"
"log"
"net"
"github.com/cenkalti/rpc2"
"github.com/cenkalti/rpc2/jsonrpc"
)
type OvsdbClient struct {
rpcClient *rpc2.Client
Schema map[string]DatabaseSchema
handlers []NotificationHandler
}
func newOvsdbClient(c *rpc2.Client) *OvsdbClient {
ovs := &OvsdbClient{rpcClient: c, Schema: make(map[string]DatabaseSchema)}
connections[c] = ovs
return ovs
}
// Would rather replace this connection map with an OvsdbClient Receiver scoped method
// Unfortunately rpc2 package acts wierd with a receiver scoped method and needs some investigation.
var connections map[*rpc2.Client]*OvsdbClient = make(map[*rpc2.Client]*OvsdbClient)
const DEFAULT_ADDR = "127.0.0.1"
const DEFAULT_PORT = 6640
const DEFAULT_SOCK = "/var/run/openvswitch/db.sock"
func configureConnection(conn net.Conn) (*OvsdbClient, error) {
c := rpc2.NewClientWithCodec(jsonrpc.NewJSONCodec(conn))
c.Handle("echo", echo)
c.Handle("update", update)
go c.Run()
go handleDisconnectNotification(c)
ovs := newOvsdbClient(c)
// Process Async Notifications
dbs, err := ovs.ListDbs()
if err == nil {
for _, db := range dbs {
schema, err := ovs.GetSchema(db)
if err == nil {
ovs.Schema[db] = *schema
} else {
return nil, err
}
}
}
return ovs, nil
}
func ConnectUnix(socketPath string) (*OvsdbClient, error) {
if socketPath == "" {
socketPath = DEFAULT_SOCK
}
conn, err := net.Dial("unix", socketPath)
if err != nil {
return nil, err
}
return configureConnection(conn)
}
func Connect(ipAddr string, port int) (*OvsdbClient, error) {
if ipAddr == "" {
ipAddr = DEFAULT_ADDR
}
if port <= 0 {
port = DEFAULT_PORT
}
target := fmt.Sprintf("%s:%d", ipAddr, port)
conn, err := net.Dial("tcp", target)
if err != nil {
return nil, err
}
return configureConnection(conn)
}
func (ovs *OvsdbClient) Register(handler NotificationHandler) {
ovs.handlers = append(ovs.handlers, handler)
}
type NotificationHandler interface {
// RFC 7047 section 4.1.6 Update Notification
Update(context interface{}, tableUpdates TableUpdates)
// RFC 7047 section 4.1.9 Locked Notification
Locked([]interface{})
// RFC 7047 section 4.1.10 Stolen Notification
Stolen([]interface{})
// RFC 7047 section 4.1.11 Echo Notification
Echo([]interface{})
}
// RFC 7047 : Section 4.1.6 : Echo
func echo(client *rpc2.Client, args []interface{}, reply *[]interface{}) error {
*reply = args
if _, ok := connections[client]; ok {
for _, handler := range connections[client].handlers {
handler.Echo(nil)
}
}
return nil
}
// RFC 7047 : Update Notification Section 4.1.6
// Processing "params": [<json-value>, <table-updates>]
func update(client *rpc2.Client, params []interface{}, reply *interface{}) error {
if len(params) < 2 {
return errors.New("Invalid Update message")
}
// Ignore params[0] as we dont use the <json-value> currently for comparison
raw, ok := params[1].(map[string]interface{})
if !ok {
return errors.New("Invalid Update message")
}
var rowUpdates map[string]map[string]RowUpdate
b, err := json.Marshal(raw)
if err != nil {
return err
}
err = json.Unmarshal(b, &rowUpdates)
if err != nil {
return err
}
// Update the local DB cache with the tableUpdates
tableUpdates := getTableUpdatesFromRawUnmarshal(rowUpdates)
if _, ok := connections[client]; ok {
for _, handler := range connections[client].handlers {
handler.Update(params, tableUpdates)
}
}
return nil
}
// RFC 7047 : get_schema
func (ovs OvsdbClient) GetSchema(dbName string) (*DatabaseSchema, error) {
args := NewGetSchemaArgs(dbName)
var reply DatabaseSchema
err := ovs.rpcClient.Call("get_schema", args, &reply)
if err != nil {
return nil, err
} else {
ovs.Schema[dbName] = reply
}
return &reply, err
}
// RFC 7047 : list_dbs
func (ovs OvsdbClient) ListDbs() ([]string, error) {
var dbs []string
err := ovs.rpcClient.Call("list_dbs", nil, &dbs)
if err != nil {
log.Fatal("ListDbs failure", err)
}
return dbs, err
}
// RFC 7047 : transact
func (ovs OvsdbClient) Transact(database string, operation ...Operation) ([]OperationResult, error) {
var reply []OperationResult
db, ok := ovs.Schema[database]
if !ok {
return nil, errors.New("invalid Database Schema")
}
if ok := db.validateOperations(operation...); !ok {
return nil, errors.New("Validation failed for the operation")
}
args := NewTransactArgs(database, operation...)
err := ovs.rpcClient.Call("transact", args, &reply)
if err != nil {
log.Fatal("transact failure", err)
}
return reply, err
}
// Convenience method to monitor every table/column
func (ovs OvsdbClient) MonitorAll(database string, jsonContext interface{}) (*TableUpdates, error) {
schema, ok := ovs.Schema[database]
if !ok {
return nil, errors.New("invalid Database Schema")
}
requests := make(map[string]MonitorRequest)
for table, tableSchema := range schema.Tables {
var columns []string
for column, _ := range tableSchema.Columns {
columns = append(columns, column)
}
requests[table] = MonitorRequest{
Columns: columns,
Select: MonitorSelect{
Initial: true,
Insert: true,
Delete: true,
Modify: true,
}}
}
return ovs.Monitor(database, jsonContext, requests)
}
// RFC 7047 : monitor
func (ovs OvsdbClient) Monitor(database string, jsonContext interface{}, requests map[string]MonitorRequest) (*TableUpdates, error) {
var reply TableUpdates
args := NewMonitorArgs(database, jsonContext, requests)
// This totally sucks. Refer to golang JSON issue #6213
var response map[string]map[string]RowUpdate
err := ovs.rpcClient.Call("monitor", args, &response)
reply = getTableUpdatesFromRawUnmarshal(response)
if err != nil {
return nil, err
}
return &reply, err
}
func getTableUpdatesFromRawUnmarshal(raw map[string]map[string]RowUpdate) TableUpdates {
var tableUpdates TableUpdates
tableUpdates.Updates = make(map[string]TableUpdate)
for table, update := range raw {
tableUpdate := TableUpdate{update}
tableUpdates.Updates[table] = tableUpdate
}
return tableUpdates
}
func clearConnection(c *rpc2.Client) {
delete(connections, c)
}
func handleDisconnectNotification(c *rpc2.Client) {
disconnected := c.DisconnectNotify()
select {
case <-disconnected:
clearConnection(c)
}
}
func (ovs OvsdbClient) Disconnect() {
ovs.rpcClient.Close()
clearConnection(ovs.rpcClient)
}