github.com/osrg/gobgp/v3@v3.30.0/tools/config/example_toml.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "github.com/BurntSushi/toml" 8 "github.com/osrg/gobgp/v3/pkg/config/oc" 9 ) 10 11 func main() { 12 b := oc.Bgp{ 13 Global: oc.Global{ 14 Config: oc.GlobalConfig{ 15 As: 12332, 16 RouterId: "10.0.0.1", 17 }, 18 }, 19 Neighbors: []oc.Neighbor{ 20 { 21 Config: oc.NeighborConfig{ 22 PeerAs: 12333, 23 AuthPassword: "apple", 24 NeighborAddress: "192.168.177.33", 25 }, 26 AfiSafis: []oc.AfiSafi{ 27 { 28 Config: oc.AfiSafiConfig{ 29 AfiSafiName: "ipv4-unicast", 30 }, 31 }, 32 { 33 Config: oc.AfiSafiConfig{ 34 AfiSafiName: "ipv6-unicast", 35 }, 36 }, 37 }, 38 ApplyPolicy: oc.ApplyPolicy{ 39 40 Config: oc.ApplyPolicyConfig{ 41 ImportPolicyList: []string{"pd1"}, 42 DefaultImportPolicy: oc.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE, 43 }, 44 }, 45 }, 46 47 { 48 Config: oc.NeighborConfig{ 49 PeerAs: 12334, 50 AuthPassword: "orange", 51 NeighborAddress: "192.168.177.32", 52 }, 53 }, 54 55 { 56 Config: oc.NeighborConfig{ 57 PeerAs: 12335, 58 AuthPassword: "grape", 59 NeighborAddress: "192.168.177.34", 60 }, 61 }, 62 }, 63 } 64 65 var buffer bytes.Buffer 66 encoder := toml.NewEncoder(&buffer) 67 err := encoder.Encode(b) 68 if err != nil { 69 panic(err) 70 } 71 72 err = encoder.Encode(policy()) 73 if err != nil { 74 panic(err) 75 } 76 fmt.Printf("%v\n", buffer.String()) 77 } 78 79 func policy() oc.RoutingPolicy { 80 81 ps := oc.PrefixSet{ 82 PrefixSetName: "ps1", 83 PrefixList: []oc.Prefix{ 84 { 85 IpPrefix: "10.3.192.0/21", 86 MasklengthRange: "21..24", 87 }}, 88 } 89 90 ns := oc.NeighborSet{ 91 NeighborSetName: "ns1", 92 NeighborInfoList: []string{"10.0.0.2"}, 93 } 94 95 cs := oc.CommunitySet{ 96 CommunitySetName: "community1", 97 CommunityList: []string{"65100:10"}, 98 } 99 100 ecs := oc.ExtCommunitySet{ 101 ExtCommunitySetName: "ecommunity1", 102 ExtCommunityList: []string{"RT:65001:200"}, 103 } 104 105 as := oc.AsPathSet{ 106 AsPathSetName: "aspath1", 107 AsPathList: []string{"^65100"}, 108 } 109 110 bds := oc.BgpDefinedSets{ 111 CommunitySets: []oc.CommunitySet{cs}, 112 ExtCommunitySets: []oc.ExtCommunitySet{ecs}, 113 AsPathSets: []oc.AsPathSet{as}, 114 } 115 116 ds := oc.DefinedSets{ 117 PrefixSets: []oc.PrefixSet{ps}, 118 NeighborSets: []oc.NeighborSet{ns}, 119 BgpDefinedSets: bds, 120 } 121 122 al := oc.AsPathLength{ 123 Operator: "eq", 124 Value: 2, 125 } 126 127 s := oc.Statement{ 128 Name: "statement1", 129 Conditions: oc.Conditions{ 130 131 MatchPrefixSet: oc.MatchPrefixSet{ 132 PrefixSet: "ps1", 133 MatchSetOptions: oc.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY, 134 }, 135 136 MatchNeighborSet: oc.MatchNeighborSet{ 137 NeighborSet: "ns1", 138 MatchSetOptions: oc.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY, 139 }, 140 141 BgpConditions: oc.BgpConditions{ 142 MatchCommunitySet: oc.MatchCommunitySet{ 143 CommunitySet: "community1", 144 MatchSetOptions: oc.MATCH_SET_OPTIONS_TYPE_ANY, 145 }, 146 147 MatchExtCommunitySet: oc.MatchExtCommunitySet{ 148 ExtCommunitySet: "ecommunity1", 149 MatchSetOptions: oc.MATCH_SET_OPTIONS_TYPE_ANY, 150 }, 151 152 MatchAsPathSet: oc.MatchAsPathSet{ 153 AsPathSet: "aspath1", 154 MatchSetOptions: oc.MATCH_SET_OPTIONS_TYPE_ANY, 155 }, 156 AsPathLength: al, 157 }, 158 }, 159 Actions: oc.Actions{ 160 RouteDisposition: "reject-route", 161 BgpActions: oc.BgpActions{ 162 SetCommunity: oc.SetCommunity{ 163 SetCommunityMethod: oc.SetCommunityMethod{ 164 CommunitiesList: []string{"65100:20"}, 165 }, 166 Options: "ADD", 167 }, 168 SetMed: "-200", 169 }, 170 }, 171 } 172 173 pd := oc.PolicyDefinition{ 174 Name: "pd1", 175 Statements: []oc.Statement{s}, 176 } 177 178 p := oc.RoutingPolicy{ 179 DefinedSets: ds, 180 PolicyDefinitions: []oc.PolicyDefinition{pd}, 181 } 182 183 return p 184 }