github.com/osrg/gobgp@v2.0.0+incompatible/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/internal/pkg/config"
     9  )
    10  
    11  func main() {
    12  	b := config.Bgp{
    13  		Global: config.Global{
    14  			Config: config.GlobalConfig{
    15  				As:       12332,
    16  				RouterId: "10.0.0.1",
    17  			},
    18  		},
    19  		Neighbors: []config.Neighbor{
    20  			config.Neighbor{
    21  				Config: config.NeighborConfig{
    22  					PeerAs:          12333,
    23  					AuthPassword:    "apple",
    24  					NeighborAddress: "192.168.177.33",
    25  				},
    26  				AfiSafis: []config.AfiSafi{
    27  					config.AfiSafi{
    28  						Config: config.AfiSafiConfig{
    29  							AfiSafiName: "ipv4-unicast",
    30  						},
    31  					},
    32  					config.AfiSafi{
    33  						Config: config.AfiSafiConfig{
    34  							AfiSafiName: "ipv6-unicast",
    35  						},
    36  					},
    37  				},
    38  				ApplyPolicy: config.ApplyPolicy{
    39  
    40  					Config: config.ApplyPolicyConfig{
    41  						ImportPolicyList:    []string{"pd1"},
    42  						DefaultImportPolicy: config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE,
    43  					},
    44  				},
    45  			},
    46  
    47  			config.Neighbor{
    48  				Config: config.NeighborConfig{
    49  					PeerAs:          12334,
    50  					AuthPassword:    "orange",
    51  					NeighborAddress: "192.168.177.32",
    52  				},
    53  			},
    54  
    55  			config.Neighbor{
    56  				Config: config.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() config.RoutingPolicy {
    80  
    81  	ps := config.PrefixSet{
    82  		PrefixSetName: "ps1",
    83  		PrefixList: []config.Prefix{
    84  			config.Prefix{
    85  				IpPrefix:        "10.3.192.0/21",
    86  				MasklengthRange: "21..24",
    87  			}},
    88  	}
    89  
    90  	ns := config.NeighborSet{
    91  		NeighborSetName:  "ns1",
    92  		NeighborInfoList: []string{"10.0.0.2"},
    93  	}
    94  
    95  	cs := config.CommunitySet{
    96  		CommunitySetName: "community1",
    97  		CommunityList:    []string{"65100:10"},
    98  	}
    99  
   100  	ecs := config.ExtCommunitySet{
   101  		ExtCommunitySetName: "ecommunity1",
   102  		ExtCommunityList:    []string{"RT:65001:200"},
   103  	}
   104  
   105  	as := config.AsPathSet{
   106  		AsPathSetName: "aspath1",
   107  		AsPathList:    []string{"^65100"},
   108  	}
   109  
   110  	bds := config.BgpDefinedSets{
   111  		CommunitySets:    []config.CommunitySet{cs},
   112  		ExtCommunitySets: []config.ExtCommunitySet{ecs},
   113  		AsPathSets:       []config.AsPathSet{as},
   114  	}
   115  
   116  	ds := config.DefinedSets{
   117  		PrefixSets:     []config.PrefixSet{ps},
   118  		NeighborSets:   []config.NeighborSet{ns},
   119  		BgpDefinedSets: bds,
   120  	}
   121  
   122  	al := config.AsPathLength{
   123  		Operator: "eq",
   124  		Value:    2,
   125  	}
   126  
   127  	s := config.Statement{
   128  		Name: "statement1",
   129  		Conditions: config.Conditions{
   130  
   131  			MatchPrefixSet: config.MatchPrefixSet{
   132  				PrefixSet:       "ps1",
   133  				MatchSetOptions: config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY,
   134  			},
   135  
   136  			MatchNeighborSet: config.MatchNeighborSet{
   137  				NeighborSet:     "ns1",
   138  				MatchSetOptions: config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY,
   139  			},
   140  
   141  			BgpConditions: config.BgpConditions{
   142  				MatchCommunitySet: config.MatchCommunitySet{
   143  					CommunitySet:    "community1",
   144  					MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ANY,
   145  				},
   146  
   147  				MatchExtCommunitySet: config.MatchExtCommunitySet{
   148  					ExtCommunitySet: "ecommunity1",
   149  					MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ANY,
   150  				},
   151  
   152  				MatchAsPathSet: config.MatchAsPathSet{
   153  					AsPathSet:       "aspath1",
   154  					MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ANY,
   155  				},
   156  				AsPathLength: al,
   157  			},
   158  		},
   159  		Actions: config.Actions{
   160  			RouteDisposition: "reject-route",
   161  			BgpActions: config.BgpActions{
   162  				SetCommunity: config.SetCommunity{
   163  					SetCommunityMethod: config.SetCommunityMethod{
   164  						CommunitiesList: []string{"65100:20"},
   165  					},
   166  					Options: "ADD",
   167  				},
   168  				SetMed: "-200",
   169  			},
   170  		},
   171  	}
   172  
   173  	pd := config.PolicyDefinition{
   174  		Name:       "pd1",
   175  		Statements: []config.Statement{s},
   176  	}
   177  
   178  	p := config.RoutingPolicy{
   179  		DefinedSets:       ds,
   180  		PolicyDefinitions: []config.PolicyDefinition{pd},
   181  	}
   182  
   183  	return p
   184  }