forked from ligato/sfc-controller
/
database.go
326 lines (256 loc) · 10.4 KB
/
database.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
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// 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.
// The database for the SFC Controller is stroed in ETCD. The controller uses
// the ligate/cn-infra which connects to ETCD. The config for the controller
// is derived from the config yaml file which is passed vi command line args or
// it can be configured via HTTP REST calls.
// This module stores configuration for hosts, external routers, and the SFC's.
// The SFC's are chains or sets of containers that should be wired together.
package core
// routines to "write" the sfc controller data to the datastore. The datastore is makes use of the ETCD infra
// to CRUD sntities of type host, exteranl, and sfc The ETCD keys are defined in keys_controller.go
import (
"github.com/ligato/sfc-controller/controller/model/controller"
)
// flush the ram cache to the sfc cache to the tree in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) WriteRamCacheToEtcd() error {
for _, ee := range sfcCtrlPlugin.ramConfigCache.EEs {
if err := sfcCtrlPlugin.DatastoreExternalEntityCreate(&ee); err != nil {
return err
}
}
for _, he := range sfcCtrlPlugin.ramConfigCache.HEs {
if err := sfcCtrlPlugin.DatastoreHostEntityCreate(&he); err != nil {
return err
}
}
for _, sfc := range sfcCtrlPlugin.ramConfigCache.SFCs {
if err := sfcCtrlPlugin.DatastoreSfcEntityCreate(&sfc); err != nil {
return err
}
}
return nil
}
// pull the sfc db from the etcd tree into the ram cache
func (sfcCtrlPlugin *SfcControllerPluginHandler) ReadEtcdDatastoreIntoRamCache() error {
log.Infof("ReadEtcdDatastoreIntoRamCache: start ...")
if err := sfcCtrlPlugin.DatastoreExternalEntityRetrieveAllIntoRamCache(); err != nil {
return err
}
if err := sfcCtrlPlugin.DatastoreHostEntityRetrieveAllIntoRamCache(); err != nil {
return err
}
if err := sfcCtrlPlugin.DatastoreSfcEntityRetrieveAllIntoRamCache(); err != nil {
return err
}
log.Infof("ReadEtcdDatastoreIntoRamCache: end ...")
return nil
}
// clear the sfc tree in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreReInitialize() error {
log.Infof("DatastoreReInitialize: clearing etc tree")
if err := sfcCtrlPlugin.DatastoreExternalEntityDeleteAll(); err != nil {
log.Error("DatastoreReInitialize: DatastoreExternalEntityDeleteAll: ", err)
}
if err := sfcCtrlPlugin.DatastoreHostEntityDeleteAll(); err != nil {
log.Error("DatastoreReInitialize: DatastoreHostEntityDeleteAll: ", err)
}
if err := sfcCtrlPlugin.DatastoreSfcEntityDeleteAll(); err != nil {
log.Error("DatastoreReInitialize: DatastoreSfcEntityDeleteAll: ", err)
}
return nil
}
// create the specified entity in the sfc db in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreExternalEntityCreate(ee *controller.ExternalEntity) error {
name := controller.ExternalEntityNameKey(ee.Name)
log.Infof("DatastoreExternalEntityCreate: setting key: '%s'", name)
err := sfcCtrlPlugin.db.Put(name, ee)
if err != nil {
log.Error("DatastoreExternalEntityCreate: error storing key: '%s'", name)
log.Error("DatastoreExternalEntityCreate: databroker put: ", err)
return err
}
return nil
}
// pull the specified entities from the sfc db in etcd into the sfc ram cache
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreExternalEntityRetrieveAllIntoRamCache() error {
return sfcCtrlPlugin.DatastoreExternalEntityIterate(func(key string, ee *controller.ExternalEntity) {
sfcCtrlPlugin.ramConfigCache.EEs[key] = *ee
log.Infof("DatastoreExternalEntityRetrieveAllIntoRamCache: adding ee: '%s': ", key, *ee)
})
}
// remove the specified entities from the sfc db in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreExternalEntityDeleteAll() error {
log.Info("DatastoreExternalEntityDeleteAll: begin ...")
defer log.Info("DatastoreExternalEntityDeleteAll: exit ...")
return sfcCtrlPlugin.DatastoreExternalEntityIterate(func(name string, ee *controller.ExternalEntity) {
key := controller.ExternalEntityNameKey(name)
log.Infof("DatastoreExternalEntityDeleteAll: deleting ee: '%s': ", key, *ee)
sfcCtrlPlugin.db.Delete(key)
})
}
// iterate over the set of specified entities in the sfc tree in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreExternalEntityIterate(actionFunc func(key string,
val *controller.ExternalEntity)) error {
kvi, err := sfcCtrlPlugin.db.ListValues(controller.ExternalEntityKeyPrefix())
if err != nil {
log.Fatal(err)
return nil
}
for {
kv, allReceived := kvi.GetNext()
if allReceived {
return nil
}
ee := &controller.ExternalEntity{}
err := kv.GetValue(ee)
if err != nil {
log.Fatal(err)
return nil
}
log.Infof("DatastoreExternalEntityIterate: iterating ee: '%s': ", ee.Name, ee)
actionFunc(ee.Name, ee)
}
return nil
}
// Update the specified entity in the sfc db in the etcd tree
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreExternalEntityUpdate(ee *controller.ExternalEntity) error {
return nil
}
// Delete the specified entity from the sfc db in the etcd tree
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreExternalEntityDelete(ee *controller.ExternalEntity) error {
return nil
}
// create the specified entity in the sfc db in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreHostEntityCreate(he *controller.HostEntity) error {
name := controller.HostEntityNameKey(he.Name)
log.Infof("DatastoreHostEntityCreate: setting key: '%s'", name)
err := sfcCtrlPlugin.db.Put(name, he)
if err != nil {
log.Error("DatastoreHostEntityCreate: error storing key: '%s'", name)
log.Error("DatastoreHostEntityCreate: databroker put: ", err)
return err
}
return nil
}
// pull the specified entities from the sfc db in etcd into the sfc ram cache
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreHostEntityRetrieveAllIntoRamCache() error {
return sfcCtrlPlugin.DatastoreHostEntityIterate(func(key string, he *controller.HostEntity) {
sfcCtrlPlugin.ramConfigCache.HEs[key] = *he
log.Infof("DatastoreHostEntityRetrieveAllIntoRamCache: adding he: '%s': ", key, *he)
})
}
// remove the specified entities from the sfc db in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreHostEntityDeleteAll() error {
log.Info("DatastoreHostEntityDeleteAll: begin ...")
defer log.Info("DatastoreHostEntityDeleteAll: exit ...")
return sfcCtrlPlugin.DatastoreHostEntityIterate(func(name string, he *controller.HostEntity) {
key := controller.HostEntityNameKey(name)
log.Infof("DatastoreHostsEntityDeleteAll: deleting he: '%s': ", key, *he)
sfcCtrlPlugin.db.Delete(key)
})
}
// iterate over the set of specified entities in the sfc tree in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreHostEntityIterate(actionFunc func(key string,
he *controller.HostEntity)) error {
kvi, err := sfcCtrlPlugin.db.ListValues(controller.HostEntityKeyPrefix())
if err != nil {
log.Fatal(err)
return nil
}
for {
kv, allReceived := kvi.GetNext()
if allReceived {
return nil
}
he := &controller.HostEntity{}
err := kv.GetValue(he)
if err != nil {
log.Fatal(err)
return nil
}
log.Infof("DatastoreHostEntityIterate: getting he: '%s': ", he.Name, he)
actionFunc(he.Name, he)
}
return nil
}
// Update the specified entity in the sfc db in the etcd tree
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreHostEntityUpdate(ee *controller.HostEntity) error {
return nil
}
// Delete the specified entity from the sfc db in the etcd tree
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreHostEntityDelete(ee *controller.HostEntity) error {
return nil
}
// create the specified entity in the sfc db in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreSfcEntityCreate(sfc *controller.SfcEntity) error {
name := controller.SfcEntityNameKey(sfc.Name)
log.Infof("DatastoreSfcEntityCreate: setting key: '%s'", name)
err := sfcCtrlPlugin.db.Put(name, sfc)
if err != nil {
log.Error("DatastoreSfcEntityCreate: error storing key: '%s'", name)
log.Error("DatastoreSfcEntityCreate: databroker put: ", err)
return err
}
return nil
}
// pull the specified entities from the sfc db in etcd into the sfc ram cache
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreSfcEntityRetrieveAllIntoRamCache() error {
return sfcCtrlPlugin.DatastoreSfcEntityIterate(func(key string, sfc *controller.SfcEntity) {
sfcCtrlPlugin.ramConfigCache.SFCs[key] = *sfc
log.Infof("DatastoreSfcEntityRetrieveAllIntoRamCache: adding sfc: '%s': ", key, *sfc)
})
}
// remove the specified entities from the sfc db in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreSfcEntityDeleteAll() error {
log.Info("DatastoreSfcEntityDeleteAll: begin ...")
defer log.Info("DatastoreSfcEntityDeleteAll: exit ...")
return sfcCtrlPlugin.DatastoreSfcEntityIterate(func(name string, sfc *controller.SfcEntity) {
key := controller.SfcEntityNameKey(name)
log.Infof("DatastoreSfcEntityDeleteAll: deleting sfc: '%s': ", key, *sfc)
sfcCtrlPlugin.db.Delete(key)
})
}
// iterate over the set of specified entities in the sfc tree in etcd
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreSfcEntityIterate(actionFunc func(key string,
sfc *controller.SfcEntity)) error {
kvi, err := sfcCtrlPlugin.db.ListValues(controller.SfcEntityKeyPrefix())
if err != nil {
log.Fatal(err)
return nil
}
for {
kv, allReceived := kvi.GetNext()
if allReceived {
return nil
}
sfc := &controller.SfcEntity{}
err := kv.GetValue(sfc)
if err != nil {
log.Fatal(err)
return nil
}
log.Infof("DatastoreSfcEntityIterate: getting sfc: '%s': ", sfc.Name, sfc)
actionFunc(sfc.Name, sfc)
}
return nil
}
// Update the specified entity in the sfc db in the etcd tree
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreSfcEntityUpdate(ee *controller.HostEntity) error {
return nil
}
// Delete the specified entity from the sfc db in the etcd tree
func (sfcCtrlPlugin *SfcControllerPluginHandler) DatastoreSfcEntityDelete(ee *controller.HostEntity) error {
return nil
}