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

View File

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

View File

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

View File

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