mirror of
https://github.com/alice-lg/alice-lg.git
synced 2024-05-11 05:55:03 +00:00
parser reject reasons as bgp communities
This commit is contained in:
@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/alice-lg/alice-lg/backend/sources"
|
||||
@ -25,18 +24,11 @@ type ServerConfig struct {
|
||||
}
|
||||
|
||||
type RejectionsConfig struct {
|
||||
Asn int `ini:"asn"`
|
||||
RejectId int `ini:"reject_id"`
|
||||
|
||||
Reasons map[int]string
|
||||
Reasons BgpCommunities
|
||||
}
|
||||
|
||||
type NoexportsConfig struct {
|
||||
Asn int `ini:"asn"`
|
||||
NoexportId int `ini:"noexport_id"`
|
||||
|
||||
Reasons map[int]string
|
||||
|
||||
Reasons BgpCommunities
|
||||
LoadOnDemand bool `ini:"load_on_demand"`
|
||||
}
|
||||
|
||||
@ -268,34 +260,60 @@ func getLookupColumns(config *ini.File) (
|
||||
return columns, order, nil
|
||||
}
|
||||
|
||||
// Get UI config: Get rejections
|
||||
func getRoutesRejections(config *ini.File) (RejectionsConfig, error) {
|
||||
reasons := make(map[int]string)
|
||||
baseConfig := config.Section("rejection")
|
||||
reasonsConfig := config.Section("rejection_reasons")
|
||||
// Helper parse communities from a section body
|
||||
func parseAndMergeCommunities(
|
||||
communities BgpCommunities, body string,
|
||||
) BgpCommunities {
|
||||
|
||||
// Map base configuration
|
||||
rejectionsConfig := RejectionsConfig{}
|
||||
baseConfig.MapTo(&rejectionsConfig)
|
||||
|
||||
// Map reasons
|
||||
keys := reasonsConfig.Keys()
|
||||
for _, key := range keys {
|
||||
id, err := strconv.Atoi(key.Name())
|
||||
if err != nil {
|
||||
return rejectionsConfig, err
|
||||
// Parse and merge communities
|
||||
lines := strings.Split(body, "\n")
|
||||
for _, line := range lines {
|
||||
kv := strings.SplitN(line, "=", 2)
|
||||
if len(kv) != 2 {
|
||||
log.Println("Skipping malformed BGP community:", line)
|
||||
continue
|
||||
}
|
||||
reasons[id] = reasonsConfig.Key(key.Name()).MustString("")
|
||||
|
||||
community := strings.TrimSpace(kv[0])
|
||||
label := strings.TrimSpace(kv[1])
|
||||
communities.Set(community, label)
|
||||
}
|
||||
|
||||
rejectionsConfig.Reasons = reasons
|
||||
return communities
|
||||
}
|
||||
|
||||
// Get UI config: Bgp Communities
|
||||
func getBgpCommunities(config *ini.File) BgpCommunities {
|
||||
// Load defaults
|
||||
communities := MakeWellKnownBgpCommunities()
|
||||
communitiesConfig := config.Section("bgp_communities")
|
||||
if communitiesConfig == nil {
|
||||
return communities // nothing else to do here, go with the default
|
||||
}
|
||||
|
||||
return parseAndMergeCommunities(communities, communitiesConfig.Body())
|
||||
}
|
||||
|
||||
// Get UI config: Get rejections
|
||||
func getRoutesRejections(config *ini.File) (RejectionsConfig, error) {
|
||||
reasonsConfig := config.Section("rejection_reasons")
|
||||
if reasonsConfig == nil {
|
||||
return RejectionsConfig{}, nil
|
||||
}
|
||||
|
||||
reasons := parseAndMergeCommunities(
|
||||
make(BgpCommunities),
|
||||
reasonsConfig.Body())
|
||||
|
||||
rejectionsConfig := RejectionsConfig{
|
||||
Reasons: reasons,
|
||||
}
|
||||
|
||||
return rejectionsConfig, nil
|
||||
}
|
||||
|
||||
// Get UI config: Get no export config
|
||||
func getRoutesNoexports(config *ini.File) (NoexportsConfig, error) {
|
||||
reasons := make(map[int]string)
|
||||
baseConfig := config.Section("noexport")
|
||||
reasonsConfig := config.Section("noexport_reasons")
|
||||
|
||||
@ -303,15 +321,9 @@ func getRoutesNoexports(config *ini.File) (NoexportsConfig, error) {
|
||||
noexportsConfig := NoexportsConfig{}
|
||||
baseConfig.MapTo(&noexportsConfig)
|
||||
|
||||
// Map reasons for missing export
|
||||
keys := reasonsConfig.Keys()
|
||||
for _, key := range keys {
|
||||
id, err := strconv.Atoi(key.Name())
|
||||
if err != nil {
|
||||
return noexportsConfig, err
|
||||
}
|
||||
reasons[id] = reasonsConfig.Key(key.Name()).MustString("")
|
||||
}
|
||||
reasons := parseAndMergeCommunities(
|
||||
make(BgpCommunities),
|
||||
reasonsConfig.Body())
|
||||
|
||||
noexportsConfig.Reasons = reasons
|
||||
|
||||
@ -409,32 +421,6 @@ func getOwnASN(config *ini.File) (int, error) {
|
||||
return asn, nil
|
||||
}
|
||||
|
||||
// Get UI config: Bgp Communities
|
||||
func getBgpCommunities(config *ini.File) BgpCommunities {
|
||||
// Load defaults
|
||||
communities := MakeWellKnownBgpCommunities()
|
||||
communitiesConfig := config.Section("bgp_communities")
|
||||
if communitiesConfig == nil {
|
||||
return communities // nothing else to do here, go with the default
|
||||
}
|
||||
|
||||
// Parse and merge communities
|
||||
lines := strings.Split(communitiesConfig.Body(), "\n")
|
||||
for _, line := range lines {
|
||||
kv := strings.SplitN(line, "=", 2)
|
||||
if len(kv) != 2 {
|
||||
log.Println("Skipping malformed BGP community:", line)
|
||||
continue
|
||||
}
|
||||
|
||||
community := strings.TrimSpace(kv[0])
|
||||
label := strings.TrimSpace(kv[1])
|
||||
communities.Set(community, label)
|
||||
}
|
||||
|
||||
return communities
|
||||
}
|
||||
|
||||
// Get UI config: Theme settings
|
||||
func getThemeConfig(config *ini.File) ThemeConfig {
|
||||
baseConfig := config.Section("theme")
|
||||
@ -622,7 +608,11 @@ func loadConfig(file string) (*Config, error) {
|
||||
// Load configuration, but handle bgp communities section
|
||||
// with our own parser
|
||||
parsedConfig, err := ini.LoadSources(ini.LoadOptions{
|
||||
UnparseableSections: []string{"bgp_communities"},
|
||||
UnparseableSections: []string{
|
||||
"bgp_communities",
|
||||
"rejection_reasons",
|
||||
"noexport_reasons",
|
||||
},
|
||||
}, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -69,6 +69,33 @@ func TestSourceConfigDefaultsOverride(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRejectAndNoexportReasons(t *testing.T) {
|
||||
config, err := loadConfig("../etc/alicelg/alice.example.conf")
|
||||
if err != nil {
|
||||
t.Error("Could not load test config:", err)
|
||||
}
|
||||
|
||||
// Rejection reasons
|
||||
description, err := config.Ui.RoutesRejections.Reasons.Lookup("23:42:1")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if description != "Some made up reason" {
|
||||
t.Error("Unexpected reason for 23:42:1 -", description)
|
||||
}
|
||||
|
||||
// Noexport reasons
|
||||
description, err = config.Ui.RoutesNoexports.Reasons.Lookup("23:46:1")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if description != "Some other made up reason" {
|
||||
t.Error("Unexpected reason for 23:46:1 -", description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlackholeParsing(t *testing.T) {
|
||||
config, err := loadConfig("../etc/alicelg/alice.example.conf")
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user