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
|
package main
import (
"bytes"
"fmt"
"github.com/BurntSushi/toml"
"github.com/osrg/gobgp/config"
)
func main() {
b := config.Bgp{
Global: config.Global{
Config: config.GlobalConfig{
As: 12332,
RouterId: "10.0.0.1",
},
},
Neighbors: []config.Neighbor{
config.Neighbor{
Config: config.NeighborConfig{
PeerAs: 12333,
AuthPassword: "apple",
NeighborAddress: "192.168.177.33",
},
AfiSafis: []config.AfiSafi{
config.AfiSafi{AfiSafiName: "ipv4-unicast"},
config.AfiSafi{AfiSafiName: "ipv6-unicast"},
},
ApplyPolicy: config.ApplyPolicy{
Config: config.ApplyPolicyConfig{
ImportPolicyList: []string{"pd1"},
DefaultImportPolicy: config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE,
},
},
},
config.Neighbor{
Config: config.NeighborConfig{
PeerAs: 12334,
AuthPassword: "orange",
NeighborAddress: "192.168.177.32",
},
},
config.Neighbor{
Config: config.NeighborConfig{
PeerAs: 12335,
AuthPassword: "grape",
NeighborAddress: "192.168.177.34",
},
},
},
}
var buffer bytes.Buffer
encoder := toml.NewEncoder(&buffer)
err := encoder.Encode(b)
if err != nil {
panic(err)
}
err = encoder.Encode(policy())
if err != nil {
panic(err)
}
fmt.Printf("%v\n", buffer.String())
}
func policy() config.RoutingPolicy {
ps := config.PrefixSet{
PrefixSetName: "ps1",
PrefixList: []config.Prefix{
config.Prefix{
IpPrefix: "10.3.192.0/21",
MasklengthRange: "21..24",
}},
}
ns := config.NeighborSet{
NeighborSetName: "ns1",
NeighborInfoList: []string{"10.0.0.2"},
}
cs := config.CommunitySet{
CommunitySetName: "community1",
CommunityList: []string{"65100:10"},
}
ecs := config.ExtCommunitySet{
ExtCommunitySetName: "ecommunity1",
ExtCommunityList: []string{"RT:65001:200"},
}
as := config.AsPathSet{
AsPathSetName: "aspath1",
AsPathList: []string{"^65100"},
}
bds := config.BgpDefinedSets{
CommunitySets: []config.CommunitySet{cs},
ExtCommunitySets: []config.ExtCommunitySet{ecs},
AsPathSets: []config.AsPathSet{as},
}
ds := config.DefinedSets{
PrefixSets: []config.PrefixSet{ps},
NeighborSets: []config.NeighborSet{ns},
BgpDefinedSets: bds,
}
al := config.AsPathLength{
Operator: "eq",
Value: 2,
}
s := config.Statement{
Name: "statement1",
Conditions: config.Conditions{
MatchPrefixSet: config.MatchPrefixSet{
PrefixSet: "ps1",
MatchSetOptions: config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY,
},
MatchNeighborSet: config.MatchNeighborSet{
NeighborSet: "ns1",
MatchSetOptions: config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY,
},
BgpConditions: config.BgpConditions{
MatchCommunitySet: config.MatchCommunitySet{
CommunitySet: "community1",
MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ANY,
},
MatchExtCommunitySet: config.MatchExtCommunitySet{
ExtCommunitySet: "ecommunity1",
MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ANY,
},
MatchAsPathSet: config.MatchAsPathSet{
AsPathSet: "aspath1",
MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ANY,
},
AsPathLength: al,
},
},
Actions: config.Actions{
RouteDisposition: config.RouteDisposition{
AcceptRoute: false,
RejectRoute: true,
},
BgpActions: config.BgpActions{
SetCommunity: config.SetCommunity{
SetCommunityMethod: config.SetCommunityMethod{
CommunitiesList: []string{"65100:20"},
},
Options: "ADD",
},
SetMed: "-200",
},
},
}
pd := config.PolicyDefinition{
Name: "pd1",
Statements: []config.Statement{s},
}
p := config.RoutingPolicy{
DefinedSets: ds,
PolicyDefinitions: []config.PolicyDefinition{pd},
}
return p
}
|