| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package l2topology |
| 4 | |
| 5 | import ( |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | type segmentProjectionBuilder struct { |
| 10 | attachments []Attachment |
| 11 | adjacencies []Adjacency |
| 12 | layer string |
| 13 | source string |
| 14 | collectedAt time.Time |
| 15 | deviceByID map[string]Device |
| 16 | ifaceByDeviceIndex map[string]Interface |
| 17 | ifIndexByDeviceName map[string]int |
| 18 | bridgeLinks []bridgeBridgeLinkRecord |
| 19 | reporterAliases map[string][]string |
| 20 | endpointMatchByID map[string]Match |
| 21 | endpointLabelsByID map[string]map[string]string |
| 22 | actorIndex map[string]struct{} |
| 23 | probabilisticConnectivity bool |
| 24 | strategyConfig topologyInferenceStrategyConfig |
| 25 | out projectedSegments |
| 26 | segmentIDs []string |
| 27 | segmentMatchByID map[string]Match |
| 28 | segmentByID map[string]*bridgeDomainSegment |
| 29 | deviceSegmentEdgeSeen map[string]struct{} |
| 30 | endpointSegmentEdgeSeen map[string]struct{} |
| 31 | endpointSegmentCandidates map[string][]string |
| 32 | segmentPortKeys map[string]map[string]struct{} |
| 33 | segmentIfIndexes map[string]map[string]struct{} |
| 34 | segmentIfNames map[string]map[string]struct{} |
| 35 | rawFDBObservations fdbReporterObservation |
| 36 | rawFDBReporterHints map[string]map[string][]bridgePortRef |
| 37 | fdbObservations fdbReporterObservation |
| 38 | fdbOwners map[string]fdbEndpointOwner |
| 39 | deviceIdentityByID map[string]topologyIdentityKeySet |
| 40 | reporterSegmentIndex segmentReporterIndex |
| 41 | aliasOwnerIDs map[string]map[string]struct{} |
| 42 | managedDeviceIDs map[string]struct{} |
| 43 | managedDeviceIDList []string |
| 44 | allowedEndpointBySegment map[string]map[string]struct{} |
| 45 | strictEndpointBySegment map[string]map[string]struct{} |
| 46 | probableEndpointBySegment map[string]map[string]struct{} |
| 47 | probableAttachmentModes map[string]map[string]string |
| 48 | assignedEndpoints map[string]struct{} |
| 49 | baseCandidatesByEndpoint map[string][]string |
| 50 | probableCandidatesByEP map[string][]string |
| 51 | strictLinkedEndpoints map[string]struct{} |
| 52 | } |
| 53 | |
| 54 | func newSegmentProjectionBuilder( |
| 55 | attachments []Attachment, |
| 56 | adjacencies []Adjacency, |
| 57 | layer string, |
| 58 | source string, |
| 59 | collectedAt time.Time, |
| 60 | deviceByID map[string]Device, |
| 61 | ifaceByDeviceIndex map[string]Interface, |
| 62 | ifIndexByDeviceName map[string]int, |
| 63 | bridgeLinks []bridgeBridgeLinkRecord, |
| 64 | reporterAliases map[string][]string, |
| 65 | endpointMatchByID map[string]Match, |
| 66 | endpointLabelsByID map[string]map[string]string, |
| 67 | actorIndex map[string]struct{}, |
| 68 | probabilisticConnectivity bool, |
| 69 | strategyConfig topologyInferenceStrategyConfig, |
| 70 | ) *segmentProjectionBuilder { |
| 71 | return &segmentProjectionBuilder{ |
| 72 | attachments: attachments, |
| 73 | adjacencies: adjacencies, |
| 74 | layer: layer, |
| 75 | source: source, |
| 76 | collectedAt: collectedAt, |
| 77 | deviceByID: deviceByID, |
| 78 | ifaceByDeviceIndex: ifaceByDeviceIndex, |
| 79 | ifIndexByDeviceName: ifIndexByDeviceName, |
| 80 | bridgeLinks: bridgeLinks, |
| 81 | reporterAliases: reporterAliases, |
| 82 | endpointMatchByID: endpointMatchByID, |
| 83 | endpointLabelsByID: endpointLabelsByID, |
| 84 | actorIndex: actorIndex, |
| 85 | probabilisticConnectivity: probabilisticConnectivity, |
| 86 | strategyConfig: strategyConfig, |
| 87 | out: projectedSegments{ |
| 88 | actors: make([]Actor, 0), |
| 89 | links: make([]Link, 0), |
| 90 | }, |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func (b *segmentProjectionBuilder) build() projectedSegments { |
| 95 | if len(b.attachments) == 0 && len(b.adjacencies) == 0 { |
| 96 | return b.out |
| 97 | } |
| 98 | if !b.initializeSegments() { |
| 99 | return b.out |
| 100 | } |
| 101 | endpointIDs := b.initializeEndpointCandidates() |
| 102 | b.assignProbableEndpoints(endpointIDs) |
| 103 | b.assignRemainingProbableEndpoints(endpointIDs) |
| 104 | b.emitLinks() |
| 105 | sortTopologyLinks(b.out.links) |
| 106 | return b.out |
| 107 | } |