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
|
package config
import (
log "github.com/Sirupsen/logrus"
"github.com/spf13/viper"
"reflect"
)
type BgpConfigSet struct {
Bgp Bgp
Policy RoutingPolicy
}
func ReadConfigfileServe(path, format string, configCh chan BgpConfigSet, reloadCh chan bool) {
cnt := 0
for {
<-reloadCh
b := Bgp{}
v := viper.New()
v.SetConfigFile(path)
v.SetConfigType(format)
err := v.ReadInConfig()
c := struct {
Global Global `mapstructure:"global"`
Neighbors []Neighbor `mapstructure:"neighbors"`
RpkiServers []RpkiServer `mapstructure:"rpki-servers"`
DefinedSets DefinedSets `mapstructure:"defined-sets"`
PolicyDefinitions []PolicyDefinition `mapstructure:"policy-definitions"`
}{}
if err != nil {
goto ERROR
}
err = v.UnmarshalExact(&c)
if err != nil {
goto ERROR
}
b.Global = c.Global
b.Neighbors = c.Neighbors
b.RpkiServers = c.RpkiServers
err = SetDefaultConfigValues(v, &b)
if err != nil {
goto ERROR
}
if cnt == 0 {
log.Info("finished reading the config file")
}
cnt++
configCh <- BgpConfigSet{
Bgp: b,
Policy: RoutingPolicy{
DefinedSets: c.DefinedSets,
PolicyDefinitions: c.PolicyDefinitions,
},
}
continue
ERROR:
if cnt == 0 {
log.Fatal("can't read config file ", path, ", ", err)
} else {
log.Warning("can't read config file ", path, ", ", err)
continue
}
}
}
func inSlice(n Neighbor, b []Neighbor) int {
for i, nb := range b {
if nb.Config.NeighborAddress == n.Config.NeighborAddress {
return i
}
}
return -1
}
func UpdateConfig(curC *Bgp, newC *Bgp) (*Bgp, []Neighbor, []Neighbor, []Neighbor) {
bgpConfig := Bgp{}
if curC == nil {
bgpConfig.Global = newC.Global
curC = &bgpConfig
} else {
// can't update the global config
bgpConfig.Global = curC.Global
}
added := []Neighbor{}
deleted := []Neighbor{}
updated := []Neighbor{}
for _, n := range newC.Neighbors {
if idx := inSlice(n, curC.Neighbors); idx < 0 {
added = append(added, n)
} else {
if !reflect.DeepEqual(n.ApplyPolicy, curC.Neighbors[idx].ApplyPolicy) {
updated = append(updated, n)
}
}
}
for _, n := range curC.Neighbors {
if inSlice(n, newC.Neighbors) < 0 {
deleted = append(deleted, n)
}
}
bgpConfig.Neighbors = newC.Neighbors
return &bgpConfig, added, deleted, updated
}
func CheckPolicyDifference(currentPolicy *RoutingPolicy, newPolicy *RoutingPolicy) bool {
log.Debug("current policy : ", currentPolicy)
log.Debug("newPolicy policy : ", newPolicy)
var result bool = false
if currentPolicy == nil && newPolicy == nil {
result = false
} else {
if currentPolicy != nil && newPolicy != nil {
// TODO: reconsider the way of policy object comparison
result = !reflect.DeepEqual(*currentPolicy, *newPolicy)
} else {
result = true
}
}
return result
}
|