/
vpp_data_store.go
432 lines (366 loc) · 13.1 KB
/
vpp_data_store.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
429
430
431
432
// Copyright (c) 2018 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.
package datastore
import (
"fmt"
"sort"
"strconv"
"strings"
"sync"
"github.com/pkg/errors"
"github.com/contiv/vpp/plugins/crd/cache/telemetrymodel"
"github.com/contiv/vpp/plugins/ipnet"
"github.com/contiv/vpp/plugins/ipnet/restapi"
"github.com/ligato/cn-infra/health/statuscheck/model/status"
)
//VppDataStore holds various maps which all take different keys but point to the same underlying value.
type VppDataStore struct {
lock *sync.Mutex
NodeMap map[string]*telemetrymodel.Node
LoopIPMap map[string]*telemetrymodel.Node
GigEIPMap map[string]*telemetrymodel.Node
LoopMACMap map[string]*telemetrymodel.Node
HostIPMap map[string]*telemetrymodel.Node
}
// CreateNode will add a node to the node cache with the given parameters,
// making sure there are no duplicates.
func (vds *VppDataStore) CreateNode(ID uint32, nodeName, IPAddr string) error {
vds.lock.Lock()
defer vds.lock.Unlock()
if _, ok := vds.retrieveNode(nodeName); ok {
return fmt.Errorf("node %s already exists", nodeName)
}
n := &telemetrymodel.Node{
NodeInfo: &telemetrymodel.NodeInfo{IPAddr: IPAddr, ID: ID, Name: nodeName},
}
n.PodMap = make(map[string]*telemetrymodel.Pod)
vds.NodeMap[nodeName] = n
if IPAddr != "" {
ipa := strings.Split(IPAddr, "/")
vds.GigEIPMap[ipa[0]] = n
}
return nil
}
// RetrieveNode returns a pointer to a node for the given key.
// Returns an error if that key is not found.
func (vds *VppDataStore) RetrieveNode(nodeName string) (n *telemetrymodel.Node, err error) {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.retrieveNode(nodeName)
if ok {
return node, nil
}
return nil, fmt.Errorf("node %s not found", nodeName)
}
// DeleteNode handles node deletions from the cache. The delete callback
// actually hands off to us the node ID in a string format, so we have
// to first find the node by its ID, not its name. If the nodeName parameter
// is invalid, or it does not identify a node that is present in the cache,
// we return an error.
func (vds *VppDataStore) DeleteNode(nodeName string) error {
vds.lock.Lock()
defer vds.lock.Unlock()
nodeID, err := strconv.Atoi(nodeName)
if err != nil {
return fmt.Errorf("invalid nodeId %s", nodeName)
}
for _, node := range vds.NodeMap {
if node.ID == uint32(nodeID) {
for _, intf := range node.NodeInterfaces {
if intf.Value.Name == ipnet.DefaultVxlanBVIInterfaceName {
delete(vds.LoopMACMap, intf.Value.PhysAddress)
for _, ip := range intf.Value.IpAddresses {
delete(vds.LoopIPMap, ip)
}
}
}
delete(vds.NodeMap, node.Name)
delete(vds.GigEIPMap, node.IPAddr)
return nil
}
}
return fmt.Errorf("node %s does not exist", nodeName)
}
//RetrieveAllNodes returns an ordered slice of all nodes in a database organized by name.
func (vds *VppDataStore) RetrieveAllNodes() []*telemetrymodel.Node {
vds.lock.Lock()
defer vds.lock.Unlock()
var str []string
for k := range vds.NodeMap {
str = append(str, k)
}
var nList []*telemetrymodel.Node
sort.Strings(str)
for _, v := range str {
n, _ := vds.retrieveNode(v)
nList = append(nList, n)
}
return nList
}
// UpdateNode handles updates of node data in the cache. If the node identified
// by 'nodeName' exists, its data is updated and nil error is returned.
// otherwise, an error is returned.
func (vds *VppDataStore) UpdateNode(ID uint32, nodeName, IPAddr string) error {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.NodeMap[nodeName]
if !ok {
return errors.Errorf("Node with name %+vds not found in vpp cache", nodeName)
}
node.IPAddr = IPAddr
node.ID = ID
if IPAddr != "" {
ipa := strings.Split(IPAddr, "/")
vds.GigEIPMap[ipa[0]] = node
}
return nil
}
//ClearCache with clear all vpp cache data except for the base NodeMap that contains
// the discovered nodes..
func (vds *VppDataStore) ClearCache() {
// Clear collected data for each node
for _, node := range vds.NodeMap {
node.NodeInterfaces = nil
node.NodeBridgeDomains = nil
node.NodeL2Fibs = nil
node.NodeLiveness = nil
node.NodeTelemetry = nil
node.NodeIPArp = nil
}
// Clear secondary index maps
vds.LoopMACMap = make(map[string]*telemetrymodel.Node)
vds.LoopIPMap = make(map[string]*telemetrymodel.Node)
vds.HostIPMap = make(map[string]*telemetrymodel.Node)
}
// ReinitializeCache completely re-initializes the cache, clearing all
// data including the discovered nodes.
func (vds *VppDataStore) ReinitializeCache() {
vds.ClearCache()
vds.NodeMap = make(map[string]*telemetrymodel.Node)
}
// DumpCache prints basic cache information to the console. The intended
// use of this function is debugging.
func (vds *VppDataStore) DumpCache() {
fmt.Printf("NodeMap: %+v\n", vds.NodeMap)
fmt.Printf("LoopMACMap: %+v\n", vds.LoopMACMap)
fmt.Printf("GigEIPMap: %+v\n", vds.GigEIPMap)
fmt.Printf("HostIPMap: %+v\n", vds.HostIPMap)
fmt.Printf("LoopIPMap: %+v\n", vds.LoopIPMap)
}
// NewVppDataStore returns a reference to a new Vpp data store
func NewVppDataStore() (n *VppDataStore) {
return &VppDataStore{
lock: &sync.Mutex{},
NodeMap: make(map[string]*telemetrymodel.Node),
LoopIPMap: make(map[string]*telemetrymodel.Node),
GigEIPMap: make(map[string]*telemetrymodel.Node),
LoopMACMap: make(map[string]*telemetrymodel.Node),
HostIPMap: make(map[string]*telemetrymodel.Node),
}
}
// SetNodeLiveness is a simple function to set a nodes liveness given its name.
func (vds *VppDataStore) SetNodeLiveness(nodeName string, nLive *status.AgentStatus) error {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.retrieveNode(nodeName)
if !ok {
return fmt.Errorf("failed to set NodeLiveness for node %s", nodeName)
}
node.NodeLiveness = nLive
return nil
}
// SetNodeInterfaces is a simple function to set a nodes interface given its name.
func (vds *VppDataStore) SetNodeInterfaces(nodeName string, nInt telemetrymodel.NodeInterfaces) error {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.retrieveNode(nodeName)
if !ok {
return fmt.Errorf("failed to set NodeInterfaces for node %s", nodeName)
}
node.NodeInterfaces = make(telemetrymodel.NodeInterfaceMap)
for _, iface := range nInt {
node.NodeInterfaces[iface.Metadata.SwIfIndex] = iface
}
return nil
}
// SetLinuxInterfaces is a simple function to set a nodes interface given its name.
func (vds *VppDataStore) SetLinuxInterfaces(nodeName string, nInt telemetrymodel.LinuxInterfaces) error {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.retrieveNode(nodeName)
if !ok {
return fmt.Errorf("failed to set NodeInterfaces for node %s", nodeName)
}
node.LinuxInterfaces = nInt
return nil
}
//SetNodeStaticRoutes is a simple function to set a nodes static routes given its name.
func (vds *VppDataStore) SetNodeStaticRoutes(nodeName string, nSrs telemetrymodel.NodeStaticRoutes) error {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.retrieveNode(nodeName)
if !ok {
return fmt.Errorf("failed to set NodeStaticRoutes for node %s", nodeName)
}
node.NodeStaticRoutes = nSrs
return nil
}
// SetNodeBridgeDomain is a simple function to set a nodes bridge domain given its name.
func (vds *VppDataStore) SetNodeBridgeDomain(nodeName string, nBridge telemetrymodel.NodeBridgeDomains) error {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.retrieveNode(nodeName)
if !ok {
return fmt.Errorf("failed to set NodeBridgeDomains for node %s", nodeName)
}
node.NodeBridgeDomains = nBridge
return nil
}
// SetNodeL2Fibs is a simple function to set a nodes l2 fibs given its name.
func (vds *VppDataStore) SetNodeL2Fibs(nodeName string, nL2F telemetrymodel.NodeL2FibTable) error {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.retrieveNode(nodeName)
if !ok {
return fmt.Errorf("failed to set NodeL2Fibs for node %s", nodeName)
}
node.NodeL2Fibs = nL2F
return nil
}
// SetNodeTelemetry is a simple function to set a nodes telemetry data given its name.
func (vds *VppDataStore) SetNodeTelemetry(nodeName string, nTele map[string]telemetrymodel.NodeTelemetry) error {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.retrieveNode(nodeName)
if !ok {
return fmt.Errorf("failed to set NodeTelemetry for node %s", nodeName)
}
node.NodeTelemetry = nTele
return nil
}
// SetNodeIPARPs is a simple function to set a nodes ip arp table given its name.
func (vds *VppDataStore) SetNodeIPARPs(nodeName string, nArps telemetrymodel.NodeIPArpTable) error {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.retrieveNode(nodeName)
if !ok {
return fmt.Errorf("failed to set NodeIPArp for node %s", nodeName)
}
node.NodeIPArp = nArps
return nil
}
// SetNodeIPam is a simple function to set the node with the given node name's ipam
func (vds *VppDataStore) SetNodeIPam(nodeName string, nIPam restapi.NodeIPAMInfo) error {
vds.lock.Lock()
defer vds.lock.Unlock()
node, ok := vds.retrieveNode(nodeName)
if !ok {
return fmt.Errorf("failed to set NodeIPam for node %s", nodeName)
}
node.NodeIPam = &nIPam
return nil
}
// SetSecondaryNodeIndices populates many of needed node maps for processing
// once all of the information has been retrieved. It also checks to make
// sure that there are no duplicate addresses within the map.
func (vds *VppDataStore) SetSecondaryNodeIndices(node *telemetrymodel.Node) []string {
vds.lock.Lock()
defer vds.lock.Unlock()
// Clear all the date before creating / recreating the maps
errReport := make([]string, 0)
loopIF, err := GetNodeLoopIFInfo(node)
if err != nil {
errReport = append(errReport, fmt.Sprintf("node %s does not have a loop interface", node.Name))
return errReport
}
if nIP, ok := vds.HostIPMap[node.ManIPAddr]; ok {
errReport = append(errReport,
fmt.Sprintf("duplicate Host IP Address %s, hosts %s, %s", node.ManIPAddr, nIP.Name, node.Name))
} else {
if node.ManIPAddr != "" {
vds.HostIPMap[node.ManIPAddr] = node
}
}
for _, ipAddr := range loopIF.Value.IpAddresses {
if ipAddr == "" {
errReport = append(errReport, fmt.Sprintf("empty IP address for Loop if %s", loopIF.Value.Name))
} else {
if _, ok := vds.LoopIPMap[ipAddr]; ok {
errReport = append(errReport,
fmt.Sprintf("duplicate Loop IP Address %s, interface %s", ipAddr, loopIF.Value.Name))
} else {
vds.LoopIPMap[ipAddr] = node
}
}
}
if loopIF.Value.PhysAddress == "" {
errReport = append(errReport, fmt.Sprintf("empty MAC address for Loop if %s", loopIF.Value.Name))
} else {
if _, ok := vds.LoopMACMap[loopIF.Value.PhysAddress]; ok {
errReport = append(errReport,
fmt.Sprintf("duplicate Loop MAC Address %s, interface %s", loopIF.Value.PhysAddress, loopIF.Value.Name))
} else {
vds.LoopMACMap[loopIF.Value.PhysAddress] = node
}
}
return errReport
}
// RetrieveNodeByHostIPAddr returns a reference to node dat for the specified
// management (host) IP address.
func (vds *VppDataStore) RetrieveNodeByHostIPAddr(ipAddr string) (*telemetrymodel.Node, error) {
if node, ok := vds.HostIPMap[ipAddr]; ok {
return node, nil
}
return nil, fmt.Errorf("node for Host IP address %s not found", ipAddr)
}
// RetrieveNodeByLoopMacAddr returns a reference to node dat for the specified
// loopback Loop0 MAC address.
func (vds *VppDataStore) RetrieveNodeByLoopMacAddr(macAddress string) (*telemetrymodel.Node, error) {
if node, ok := vds.LoopMACMap[macAddress]; ok {
return node, nil
}
return nil, fmt.Errorf("node for Loop MAC address %s not found", macAddress)
}
// RetrieveNodeByLoopIPAddr returns a reference to node dat for the specified
// loopback Loop0 IP address.
func (vds *VppDataStore) RetrieveNodeByLoopIPAddr(ipAddress string) (*telemetrymodel.Node, error) {
if node, ok := vds.LoopIPMap[ipAddress]; ok {
return node, nil
}
return nil, fmt.Errorf("node for Loop IP address %s not found", ipAddress)
}
// RetrieveNodeByGigEIPAddr returns a reference to node dat for the specified
// VPP GigE IP address.
func (vds *VppDataStore) RetrieveNodeByGigEIPAddr(ipAddress string) (*telemetrymodel.Node, error) {
if node, ok := vds.GigEIPMap[ipAddress]; ok {
return node, nil
}
return nil, fmt.Errorf("node for GigE IP address %s not found", ipAddress)
}
// GetNodeLoopIFInfo gets the loop interface for the given node
func GetNodeLoopIFInfo(node *telemetrymodel.Node) (*telemetrymodel.NodeInterface, error) {
for _, ifs := range node.NodeInterfaces {
if ifs.Value.Name == ipnet.DefaultVxlanBVIInterfaceName {
return &ifs, nil
}
}
err := errors.Errorf("loop interface not found %s", node.Name)
return nil, err
}
// retrieveNode returns a pointer to a node for the given key.
// Returns an error if that key is not found.
func (vds *VppDataStore) retrieveNode(key string) (*telemetrymodel.Node, bool) {
node, ok := vds.NodeMap[key]
return node, ok
}