forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_prefixes.go
122 lines (101 loc) · 2.98 KB
/
host_prefixes.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
package logic
import (
"net"
"github.com/pkg/errors"
)
// HostPrefixes is a combination of Next Hop IP Addresses and
// destination CIDRs that can be reached by those Next Hops.
type hostPrefixes map[string][]string
func makeHostPrefixes() hostPrefixes {
return make(map[string][]string)
}
func getHostPrefixes(hostRoutes []*RouteTableType, subnetCIDR string) (hostPrefixes, error) {
solved, unresolved, err := splitRoutesByReachAbility(hostRoutes, subnetCIDR)
if err != nil {
return nil, err
}
hostPrefs := makeHostPrefixes()
for _, route := range solved {
hostPrefs.addDestinationToIP(route.Destination, route.Nexthop)
reached, notReached, err := extractDestinationsReachableFromCIDR(route.Destination, unresolved)
if err != nil {
return nil, err
}
unresolved = notReached
hostPrefs.addDestinationsToIP(reached, route.Nexthop)
}
return hostPrefs, nil
}
func extractDestinationsReachableFromCIDR(
cidr string, unresolvedRoutes []*RouteTableType,
) ([]string, []*RouteTableType, error) {
reachedDestinations := []string{}
reached, notReached, err := splitRoutesByReachAbility(unresolvedRoutes, cidr)
if err != nil {
return nil, nil, err
}
for _, route := range reached {
reachedDestinations = append(reachedDestinations, route.Destination)
destinations, unresolved, err := extractDestinationsReachableFromCIDR(route.Destination, notReached)
if err != nil {
return nil, nil, err
}
reachedDestinations = append(reachedDestinations, destinations...)
notReached = unresolved
}
return reachedDestinations, notReached, nil
}
func splitRoutesByReachAbility(
routes []*RouteTableType, cidr string,
) ([]*RouteTableType, []*RouteTableType, error) {
_, subnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, nil, err
}
possible := []*RouteTableType{}
notPossible := []*RouteTableType{}
for _, route := range routes {
nextHopIP := net.ParseIP(route.Nexthop)
if nextHopIP == nil {
return nil, nil, errors.Errorf("Following NextHop route cannot be parsed: %v", route.Nexthop)
}
if subnet.Contains(nextHopIP) {
possible = append(possible, route)
} else {
notPossible = append(notPossible, route)
}
}
return possible, notPossible, nil
}
func (hp hostPrefixes) getDestinationsForIP(ip string) []string {
return hp[ip]
}
func (hp hostPrefixes) addDestinationToIP(destination string, ip string) {
hp[ip] = append(hp[ip], destination)
}
func (hp hostPrefixes) addDestinationsToIP(destinations []string, ip string) {
hp[ip] = append(hp[ip], destinations...)
}
func (hp hostPrefixes) removeDestinationsForIP(ip string) {
delete(hp, ip)
}
func (hp hostPrefixes) getIPAddresses() []string {
ips := []string{}
for ip := range hp {
if len(hp[ip]) > 0 {
ips = append(ips, ip)
}
}
return ips
}
func (hp hostPrefixes) hasAnyDestinationsForIP(ip string) bool {
return len(hp[ip]) > 0
}
func (hp hostPrefixes) isEmpty() bool {
for _, addr := range hp.getIPAddresses() {
if hp.hasAnyDestinationsForIP(addr) {
return false
}
}
return true
}