updated parsing

This commit is contained in:
Annika Hannig 2022-11-24 17:59:12 +01:00
parent d335a22666
commit 9c8093e630
4 changed files with 30 additions and 24 deletions

View File

@ -181,12 +181,12 @@ const (
BGPCommunityTypeLarge
)
// RangedBGPCommunity is a list of BGPCommunity ranges,
// with 3 ranges in large communities.
type RangedBGPCommunity []interface{}
// BGPCommunityRange is a list of tuples with the start and end
// of the range defining a community.
type BGPCommunityRange []interface{}
// Type classifies the BGP Ranged BGP Community into: std, large, ext
func (c RangedBGPCommunity) Type() int {
func (c BGPCommunityRange) Type() int {
if len(c) == 2 {
return BGPCommunityTypeStd
}
@ -199,7 +199,7 @@ func (c RangedBGPCommunity) Type() int {
// A BGPCommunitiesSet is a set of communities, large and extended.
// The communities are described as ranges.
type BGPCommunitiesSet struct {
Communities []RangedBGPCommunity `json:"standard"`
ExtCommunities []RangedBGPCommunity `json:"extended"`
LargeCommunities []RangedBGPCommunity `json:"large"`
Standard []BGPCommunityRange `json:"standard"`
Extended []BGPCommunityRange `json:"extended"`
Large []BGPCommunityRange `json:"large"`
}

View File

@ -39,9 +39,9 @@ func parseAndMergeCommunities(
// Parse a communities set with ranged communities
func parseRangeCommunitiesSet(body string) (*api.BGPCommunitiesSet, error) {
comms := []api.RangedBGPCommunity{}
large := []api.RangedBGPCommunity{}
ext := []api.RangedBGPCommunity{}
comms := []api.BGPCommunityRange{}
large := []api.BGPCommunityRange{}
ext := []api.BGPCommunityRange{}
lines := strings.Split(body, "\n")
for _, line := range lines {
@ -67,14 +67,14 @@ func parseRangeCommunitiesSet(body string) (*api.BGPCommunitiesSet, error) {
}
set := &api.BGPCommunitiesSet{
Communities: comms,
LargeCommunities: large,
ExtCommunities: ext,
Standard: comms,
Large: large,
Extended: ext,
}
return set, nil
}
func parseRangeCommunity(s string) (api.RangedBGPCommunity, error) {
func parseRangeCommunity(s string) (api.BGPCommunityRange, error) {
tokens := strings.Split(s, ":")
if len(tokens) < 2 {
return nil, ErrInvalidCommunity(s)
@ -107,13 +107,13 @@ func parseRangeCommunity(s string) (api.RangedBGPCommunity, error) {
return nil, ErrInvalidCommunity(s)
}
if isExt {
return api.RangedBGPCommunity{
return api.BGPCommunityRange{
[]string{parts[0][0], parts[0][0]},
decoders.IntListFromStrings(parts[1]),
decoders.IntListFromStrings(parts[2]),
}, nil
}
comm := api.RangedBGPCommunity{}
comm := api.BGPCommunityRange{}
for _, p := range parts {
comm = append(comm, decoders.IntListFromStrings(p))
}

View File

@ -441,13 +441,19 @@ func getRoutesNoexports(config *ini.File) (NoexportsConfig, error) {
// Get UI config: blackhole communities
func getBlackholeCommunities(config *ini.File) (api.BGPCommunitiesSet, error) {
section := config.Section("blackhole_communities")
defaultBlackholes := api.BGPCommunitiesSet{
Standard: []api.BGPCommunityRange{
{[]interface{}{65535, 65535}, []interface{}{666, 666}},
},
}
if section == nil {
return api.BGPCommunitiesSet{}, nil
return defaultBlackholes, nil
}
set, err := parseRangeCommunitiesSet(section.Body())
if err != nil {
return api.BGPCommunitiesSet{}, err
return defaultBlackholes, err
}
set.Standard = append(set.Standard, defaultBlackholes.Standard...)
return *set, nil
}

View File

@ -254,14 +254,14 @@ func TestGetBlackholeCommunities(t *testing.T) {
config, _ := LoadConfig("testdata/alice.conf")
comms := config.UI.BGPBlackholeCommunities
if comms.Communities[0][0].([]int)[0] != 1337 {
t.Error("unexpected community:", comms.Communities[0])
if comms.Standard[0][0].([]int)[0] != 1337 {
t.Error("unexpected community:", comms.Standard[0])
}
if len(comms.ExtCommunities) != 1 {
t.Error("unexpected communities:", comms.ExtCommunities)
if len(comms.Extended) != 1 {
t.Error("unexpected communities:", comms.Extended)
}
if len(comms.LargeCommunities) != 1 {
t.Error("unexpected communities:", comms.LargeCommunities)
if len(comms.Large) != 1 {
t.Error("unexpected communities:", comms.Large)
}
t.Log(comms)
}