-
Notifications
You must be signed in to change notification settings - Fork 36
/
fabricdiscovery.go
73 lines (63 loc) · 2.47 KB
/
fabricdiscovery.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
package hostagent
import "fmt"
const (
FabricDiscoveryMethodLLDPNMState = iota
FabricDiscoveryMethodLLDPRawSocket
FabricDiscoveryMethodStatic
)
type FabricAttachmentData struct {
StaticPath string `json:"staticPath"`
SystemName string `json:"systemName"`
}
type FabricDiscoveryAgent interface {
Init(agent *HostAgent) error
CollectDiscoveryData(stopCh <-chan struct{})
TriggerCollectionDiscoveryData()
GetNeighborData(iface string) ([]*FabricAttachmentData, error)
PopulateAdjacencies(adjs map[string][]FabricAttachmentData)
}
func (ha *HostAgent) getFabricDiscoveryAgent(method int) FabricDiscoveryAgent {
switch method {
case FabricDiscoveryMethodLLDPNMState:
return NewFabricDiscoveryAgentLLDPNMState()
case FabricDiscoveryMethodLLDPRawSocket:
return NewFabricDiscoveryAgentLLDPRawSocket()
}
return nil
}
// TBD: Consider passing a profile to make sure only the required methods are run
func (ha *HostAgent) FabricDiscoveryRegistryInit() (err error) {
ha.fabricDiscoveryRegistry = make(map[int]FabricDiscoveryAgent)
for method := FabricDiscoveryMethodLLDPNMState; method < FabricDiscoveryMethodStatic; method++ {
if fabricDiscoveryAgent := ha.getFabricDiscoveryAgent(method); fabricDiscoveryAgent != nil {
ha.fabricDiscoveryRegistry[method] = ha.getFabricDiscoveryAgent(method)
err = ha.fabricDiscoveryRegistry[method].Init(ha)
if err != nil {
return err
}
}
}
return nil
}
func (ha *HostAgent) FabricDiscoveryCollectDiscoveryData(stopCh <-chan struct{}) {
for method := FabricDiscoveryMethodLLDPNMState; method < FabricDiscoveryMethodStatic; method++ {
ha.fabricDiscoveryRegistry[method].CollectDiscoveryData(stopCh)
}
}
func (ha *HostAgent) FabricDiscoveryTriggerCollectionDiscoveryData() {
for method := FabricDiscoveryMethodLLDPNMState; method < FabricDiscoveryMethodStatic; method++ {
ha.fabricDiscoveryRegistry[method].TriggerCollectionDiscoveryData()
}
}
func (ha *HostAgent) FabricDiscoveryPopulateAdjacencies(method int, adjs map[string][]FabricAttachmentData) {
ha.fabricDiscoveryRegistry[method].PopulateAdjacencies(adjs)
}
func (ha *HostAgent) GetFabricDiscoveryNeighborDataLocked(iface string) ([]*FabricAttachmentData, error) {
err := fmt.Errorf("Interface %s not discovered or does not have a neighbor", iface)
for method := FabricDiscoveryMethodLLDPNMState; method < FabricDiscoveryMethodStatic; method++ {
if adjs, err := ha.fabricDiscoveryRegistry[method].GetNeighborData(iface); err == nil {
return adjs, err
}
}
return nil, err
}