forked from eBay/go-ovn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_group.go
187 lines (169 loc) · 4.96 KB
/
port_group.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
/**
* 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"
)
// PortGroup ovnnb item
type PortGroup struct {
UUID string
Name string
Ports []string
ACLs []string
ExternalID map[interface{}]interface{}
}
func (odbi *ovndb) pgAddImp(group string, ports []string, external_ids map[string]string) (*OvnCommand, error) {
namedUUID, err := newRowUUID()
if err != nil {
return nil, err
}
row := make(OVNRow)
row["name"] = group
if uuid := odbi.getRowUUID(TablePortGroup, row); len(uuid) > 0 {
return nil, ErrorExist
}
pgports, err := libovsdb.NewOvsSet(ports)
if err != nil {
return nil, err
}
row["ports"] = pgports
if external_ids != nil {
oMap, err := libovsdb.NewOvsMap(external_ids)
if err != nil {
return nil, err
}
row["external_ids"] = oMap
}
insertOp := libovsdb.Operation{
Op: opInsert,
Table: TablePortGroup,
Row: row,
UUIDName: namedUUID,
}
operations := []libovsdb.Operation{insertOp}
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}
func (odbi *ovndb) pgSetPortsImp(group string, ports []string, external_ids map[string]string) (*OvnCommand, error) {
row := make(OVNRow)
row["name"] = group
if uuid := odbi.getRowUUID(TablePortGroup, row); len(uuid) == 0 {
return nil, ErrorNotFound
}
pgports, err := libovsdb.NewOvsSet(ports)
if err != nil {
return nil, err
}
row["ports"] = pgports
if external_ids != nil {
oMap, err := libovsdb.NewOvsMap(external_ids)
if err != nil {
return nil, err
}
row["external_ids"] = oMap
}
condition := libovsdb.NewCondition("name", "==", group)
updateOp := libovsdb.Operation{
Op: opUpdate,
Table: TablePortGroup,
Row: row,
Where: []interface{}{condition},
}
operations := []libovsdb.Operation{updateOp}
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}
func (odbi *ovndb) pgDelImp(group string) (*OvnCommand, error) {
condition := libovsdb.NewCondition("name", "==", group)
deleteOp := libovsdb.Operation{
Op: opDelete,
Table: TablePortGroup,
Where: []interface{}{condition},
}
operations := []libovsdb.Operation{deleteOp}
return &OvnCommand{operations, odbi, make([][]map[string]interface{}, len(operations))}, nil
}
func (odbi *ovndb) RowToPortGroup(uuid string) *PortGroup {
cachePortGroup, ok := odbi.cache[TablePortGroup][uuid]
if !ok {
return nil
}
pg := &PortGroup{
UUID: uuid,
Name: cachePortGroup.Fields["name"].(string),
ExternalID: cachePortGroup.Fields["external_ids"].(libovsdb.OvsMap).GoMap,
}
ports := cachePortGroup.Fields["ports"]
switch ports.(type) {
case libovsdb.UUID:
pg.Ports = []string{ports.(libovsdb.UUID).GoUUID}
case libovsdb.OvsSet:
pg.Ports = odbi.ConvertGoSetToStringArray(ports.(libovsdb.OvsSet))
}
acls := cachePortGroup.Fields["acls"]
switch acls.(type) {
case libovsdb.UUID:
pg.ACLs = []string{acls.(libovsdb.UUID).GoUUID}
case libovsdb.OvsSet:
pg.ACLs = odbi.ConvertGoSetToStringArray(acls.(libovsdb.OvsSet))
}
return pg
}
func (odbi *ovndb) GetLogicalPortsByPortGroup(group string) ([]*LogicalSwitchPort, error) {
var listLSP []*LogicalSwitchPort
odbi.cachemutex.RLock()
defer odbi.cachemutex.RUnlock()
cachePortGroup, ok := odbi.cache[TablePortGroup]
if !ok {
return nil, ErrorSchema
}
for _, drows := range cachePortGroup {
if pgname, ok := drows.Fields["name"].(string); ok && pgname == group {
ports := drows.Fields["ports"]
if ports != nil {
switch ports.(type) {
case libovsdb.OvsSet:
if ps, ok := ports.(libovsdb.OvsSet); ok {
for _, p := range ps.GoSet {
if vp, ok := p.(libovsdb.UUID); ok {
tp, err := odbi.rowToLogicalPort(vp.GoUUID)
if err != nil {
return nil, fmt.Errorf("Couldn't get logical port: %s", err)
}
listLSP = append(listLSP, tp)
}
}
} else {
return nil, fmt.Errorf("type libovsdb.OvsSet casting failed")
}
case libovsdb.UUID:
if vp, ok := ports.(libovsdb.UUID); ok {
tp, err := odbi.rowToLogicalPort(vp.GoUUID)
if err != nil {
return nil, fmt.Errorf("Couldn't get logical port: %s", err)
}
listLSP = append(listLSP, tp)
} else {
return nil, fmt.Errorf("type libovsdb.UUID casting failed")
}
default:
return nil, fmt.Errorf("Unsupport type found in ovsdb rows")
}
}
break
}
}
return listLSP, nil
}