forked from eBay/go-ovn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logical_switch.go
428 lines (397 loc) · 12.3 KB
/
logical_switch.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
* Copyright (c) 2017 eBay Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package goovn
import (
"fmt"
"github.com/ebay/libovsdb"
)
// LogicalSwitch ovnnb item
type LogicalSwitch struct {
UUID string
Name string
Ports []string
LoadBalancer []string
ACLs []string
QoSRules []string
DNSRecords []string
OtherConfig map[interface{}]interface{}
ExternalID map[interface{}]interface{}
}
func (odbi *ovndb) lsAddImp(lsw string) (*OvnCommand, error) {
namedUUID, err := newRowUUID()
if err != nil {
return nil, err
}
//row to insert
lswitch := make(OVNRow)
lswitch["name"] = lsw
if uuid := odbi.getRowUUID(TableLogicalSwitch, lswitch); len(uuid) > 0 {
return nil, ErrorExist
}
insertOp := libovsdb.Operation{
Op: opInsert,
Table: TableLogicalSwitch,
Row: lswitch,
UUIDName: namedUUID,
}
operations := []libovsdb.Operation{insertOp}
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}
func (odbi *ovndb) lsDelImp(lsw string) (*OvnCommand, error) {
condition := libovsdb.NewCondition("name", "==", lsw)
deleteOp := libovsdb.Operation{
Op: opDelete,
Table: TableLogicalSwitch,
Where: []interface{}{condition},
}
operations := []libovsdb.Operation{deleteOp}
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}
func (odbi *ovndb) rowToLogicalSwitch(uuid string) *LogicalSwitch {
cacheLogicalSwitch, ok := odbi.cache[TableLogicalSwitch][uuid]
if !ok {
return nil
}
ls := &LogicalSwitch{
UUID: uuid,
Name: cacheLogicalSwitch.Fields["name"].(string),
OtherConfig: cacheLogicalSwitch.Fields["other_config"].(libovsdb.OvsMap).GoMap,
ExternalID: cacheLogicalSwitch.Fields["external_ids"].(libovsdb.OvsMap).GoMap,
}
if ports, ok := cacheLogicalSwitch.Fields["ports"]; ok {
switch ports.(type) {
case libovsdb.UUID:
ls.Ports = []string{ports.(libovsdb.UUID).GoUUID}
case libovsdb.OvsSet:
ls.Ports = odbi.ConvertGoSetToStringArray(ports.(libovsdb.OvsSet))
}
}
if lbs, ok := cacheLogicalSwitch.Fields["load_balancer"]; ok {
switch lbs.(type) {
case libovsdb.UUID:
ls.LoadBalancer = []string{lbs.(libovsdb.UUID).GoUUID}
case libovsdb.OvsSet:
ls.LoadBalancer = odbi.ConvertGoSetToStringArray(lbs.(libovsdb.OvsSet))
}
}
if acls, ok := cacheLogicalSwitch.Fields["acls"]; ok {
switch acls.(type) {
case libovsdb.UUID:
ls.ACLs = []string{acls.(libovsdb.UUID).GoUUID}
case libovsdb.OvsSet:
ls.ACLs = odbi.ConvertGoSetToStringArray(acls.(libovsdb.OvsSet))
}
}
if qosrules, ok := cacheLogicalSwitch.Fields["qos_rules"]; ok {
switch qosrules.(type) {
case libovsdb.UUID:
ls.QoSRules = []string{qosrules.(libovsdb.UUID).GoUUID}
case libovsdb.OvsSet:
ls.QoSRules = odbi.ConvertGoSetToStringArray(qosrules.(libovsdb.OvsSet))
}
}
if dnsrecords, ok := cacheLogicalSwitch.Fields["dns_records"]; ok {
switch dnsrecords.(type) {
case libovsdb.UUID:
ls.DNSRecords = []string{dnsrecords.(libovsdb.UUID).GoUUID}
case libovsdb.OvsSet:
ls.DNSRecords = odbi.ConvertGoSetToStringArray(dnsrecords.(libovsdb.OvsSet))
}
}
return ls
}
func (odbi *ovndb) lsGetImp(ls string) ([]*LogicalSwitch, error) {
var lsList []*LogicalSwitch
odbi.cachemutex.RLock()
defer odbi.cachemutex.RUnlock()
cacheLogicalSwitch, ok := odbi.cache[TableLogicalSwitch]
if !ok {
return nil, ErrorNotFound
}
for uuid, drows := range cacheLogicalSwitch {
if rlsw, ok := drows.Fields["name"].(string); ok && rlsw == ls {
lsList = append(lsList, odbi.rowToLogicalSwitch(uuid))
}
}
if len(lsList) == 0 {
return nil, ErrorNotFound
}
return lsList, nil
}
func (odbi *ovndb) lsListImp() ([]*LogicalSwitch, error) {
odbi.cachemutex.RLock()
defer odbi.cachemutex.RUnlock()
cacheLogicalSwitch, ok := odbi.cache[TableLogicalSwitch]
if !ok {
return nil, ErrorSchema
}
listLS := make([]*LogicalSwitch, 0, len(cacheLogicalSwitch))
for uuid := range cacheLogicalSwitch {
listLS = append(listLS, odbi.rowToLogicalSwitch(uuid))
}
return listLS, nil
}
func (odbi *ovndb) lslbAddImp(lswitch string, lb string) (*OvnCommand, error) {
var operations []libovsdb.Operation
row := make(OVNRow)
row["name"] = lb
lbuuid := odbi.getRowUUID(TableLoadBalancer, row)
if len(lbuuid) == 0 {
return nil, ErrorNotFound
}
mutateUUID := []libovsdb.UUID{stringToGoUUID(lbuuid)}
mutateSet, err := libovsdb.NewOvsSet(mutateUUID)
mutation := libovsdb.NewMutation("load_balancer", opInsert, mutateSet)
if err != nil {
return nil, err
}
row = make(OVNRow)
row["name"] = lswitch
lsuuid := odbi.getRowUUID(TableLogicalSwitch, row)
if len(lsuuid) == 0 {
return nil, ErrorNotFound
}
condition := libovsdb.NewCondition("name", "==", lswitch)
mutateOp := libovsdb.Operation{
Op: opMutate,
Table: TableLogicalSwitch,
Mutations: []interface{}{mutation},
Where: []interface{}{condition},
}
operations = append(operations, mutateOp)
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}
func (odbi *ovndb) lslbDelImp(lswitch string, lb string) (*OvnCommand, error) {
var operations []libovsdb.Operation
row := make(OVNRow)
row["name"] = lb
lbuuid := odbi.getRowUUID(TableLoadBalancer, row)
if len(lbuuid) == 0 {
return nil, ErrorNotFound
}
row = make(OVNRow)
row["name"] = lswitch
lsuuid := odbi.getRowUUID(TableLogicalSwitch, row)
if len(lsuuid) == 0 {
return nil, ErrorNotFound
}
mutateUUID := []libovsdb.UUID{stringToGoUUID(lbuuid)}
mutateSet, err := libovsdb.NewOvsSet(mutateUUID)
if err != nil {
return nil, err
}
mutation := libovsdb.NewMutation("load_balancer", opDelete, mutateSet)
// mutate lswitch for the corresponding load_balancer
mucondition := libovsdb.NewCondition("name", "==", lswitch)
mutateOp := libovsdb.Operation{
Op: opMutate,
Table: TableLogicalSwitch,
Mutations: []interface{}{mutation},
Where: []interface{}{mucondition},
}
operations = append(operations, mutateOp)
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}
func (odbi *ovndb) lslbListImp(lswitch string) ([]*LoadBalancer, error) {
odbi.cachemutex.RLock()
defer odbi.cachemutex.RUnlock()
cacheLogicalSwitch, ok := odbi.cache[TableLogicalSwitch]
if !ok {
return nil, ErrorSchema
}
for _, drows := range cacheLogicalSwitch {
if rlsw, ok := drows.Fields["name"].(string); ok && rlsw == lswitch {
lbs := drows.Fields["load_balancer"]
if lbs != nil {
switch lbs.(type) {
case libovsdb.OvsSet:
if lb, ok := lbs.(libovsdb.OvsSet); ok {
listLB := make([]*LoadBalancer, 0, len(lb.GoSet))
for _, l := range lb.GoSet {
if lb, ok := l.(libovsdb.UUID); ok {
lb, err := odbi.rowToLB(lb.GoUUID)
if err != nil {
return nil, err
}
listLB = append(listLB, lb)
}
}
return listLB, nil
} else {
return nil, fmt.Errorf("type libovsdb.OvsSet casting failed")
}
case libovsdb.UUID:
if lb, ok := lbs.(libovsdb.UUID); ok {
lb, err := odbi.rowToLB(lb.GoUUID)
if err != nil {
return nil, err
}
return []*LoadBalancer{lb}, nil
} else {
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
}
default:
return nil, fmt.Errorf("Unsupport type found in ovsdb rows")
}
}
return []*LoadBalancer{}, nil
}
}
return nil, ErrorNotFound
}
func (odbi *ovndb) lsExtIdsAddImp(ls string, external_ids map[string]string) (*OvnCommand, error) {
var operations []libovsdb.Operation
row := make(OVNRow)
row["name"] = ls
lsuuid := odbi.getRowUUID(TableLogicalSwitch, row)
if len(lsuuid) == 0 {
return nil, ErrorNotFound
}
if len(external_ids) == 0 {
return nil, fmt.Errorf("external_ids is nil or empty")
}
mutateSet, err := libovsdb.NewOvsMap(external_ids)
if err != nil {
return nil, err
}
mutation := libovsdb.NewMutation("external_ids", opInsert, mutateSet)
condition := libovsdb.NewCondition("name", "==", ls)
mutateOp := libovsdb.Operation{
Op: opMutate,
Table: TableLogicalSwitch,
Mutations: []interface{}{mutation},
Where: []interface{}{condition},
}
operations = append(operations, mutateOp)
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}
func (odbi *ovndb) lsExtIdsDelImp(ls string, external_ids map[string]string) (*OvnCommand, error) {
var operations []libovsdb.Operation
row := make(OVNRow)
row["name"] = ls
lsuuid := odbi.getRowUUID(TableLogicalSwitch, row)
if len(lsuuid) == 0 {
return nil, ErrorNotFound
}
if len(external_ids) == 0 {
return nil, fmt.Errorf("external_ids is nil or empty")
}
mutateSet, err := libovsdb.NewOvsMap(external_ids)
if err != nil {
return nil, err
}
mutation := libovsdb.NewMutation("external_ids", opDelete, mutateSet)
condition := libovsdb.NewCondition("name", "==", ls)
mutateOp := libovsdb.Operation{
Op: opMutate,
Table: TableLogicalSwitch,
Mutations: []interface{}{mutation},
Where: []interface{}{condition},
}
operations = append(operations, mutateOp)
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}
func (odbi *ovndb) linkSwitchToRouterImp(lsw, lsp, lr, lrp, lrpMac string, networks []string, externalIds map[string]string) (*OvnCommand, error) {
// validate logical switch
row := make(OVNRow)
row["name"] = lsw
lswUUID := odbi.getRowUUID(TableLogicalSwitch, row)
if len(lswUUID) == 0 {
return nil, fmt.Errorf("logical switch %s not found", lsw)
}
// add logical router port
strLrpUUID, err := newRowUUID()
if err != nil {
return nil, err
}
row = make(OVNRow)
row["name"] = lrp
row["mac"] = lrpMac
// validate
if uuid := odbi.getRowUUID(TableLogicalRouterPort, row); len(uuid) > 0 {
return nil, fmt.Errorf("logical router port %s already existed", lrp)
}
networkSet, err := libovsdb.NewOvsSet(networks)
if err != nil {
return nil, err
}
row["networks"] = networkSet
if externalIds != nil {
oMap, err := libovsdb.NewOvsMap(externalIds)
if err != nil {
return nil, err
}
row["external_ids"] = oMap
}
addLrpOp := libovsdb.Operation{
Op: opInsert,
Table: TableLogicalRouterPort,
Row: row,
UUIDName: strLrpUUID,
}
// add lrp to lr
objLrpUUID := []libovsdb.UUID{stringToGoUUID(strLrpUUID)}
lrpSet, err := libovsdb.NewOvsSet(objLrpUUID)
if err != nil {
return nil, err
}
lrPortChange := libovsdb.NewMutation("ports", opInsert, lrpSet)
lrCondition := libovsdb.NewCondition("name", "==", lr)
addLrpToLrOp := libovsdb.Operation{
Op: opMutate,
Table: TableLogicalRouter,
Mutations: []interface{}{lrPortChange},
Where: []interface{}{lrCondition},
}
// add logical switch port
strLspUUID, err := newRowUUID()
if err != nil {
return nil, err
}
port := make(OVNRow)
port["name"] = lsp
port["type"] = "router"
port["addresses"] = "router"
options := make(map[string]string)
options["router-port"] = lrp
optMap, _ := libovsdb.NewOvsMap(options)
port["options"] = optMap
if uuid := odbi.getRowUUID(TableLogicalSwitchPort, port); len(uuid) > 0 {
return nil, ErrorExist
}
addLspOp := libovsdb.Operation{
Op: opInsert,
Table: TableLogicalSwitchPort,
Row: port,
UUIDName: strLspUUID,
}
// add logical switch port to switch
objLspUUID := []libovsdb.UUID{stringToGoUUID(strLspUUID)}
lspSet, err := libovsdb.NewOvsSet(objLspUUID)
if err != nil {
return nil, err
}
lsPortChange := libovsdb.NewMutation("ports", opInsert, lspSet)
lsCondition := libovsdb.NewCondition("name", "==", lsw)
addLspToLsOp := libovsdb.Operation{
Op: opMutate,
Table: TableLogicalSwitch,
Mutations: []interface{}{lsPortChange},
Where: []interface{}{lsCondition},
}
operations := []libovsdb.Operation{addLrpOp, addLrpToLrOp, addLspOp, addLspToLsOp}
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}