From f3ae68b9f28e0ddbe33be3ecf09c2015cfba6078 Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Fri, 29 Jul 2016 08:28:28 +0000 Subject: config: curve out code specific to default global config setting Signed-off-by: ISHIDA Wataru --- config/default.go | 54 ++++++++++++++++++++++++++++++----------------------- docs/sources/lib.md | 18 ++++++++---------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/config/default.go b/config/default.go index 6b47806c..e0564c0e 100644 --- a/config/default.go +++ b/config/default.go @@ -148,32 +148,44 @@ func setDefaultNeighborConfigValuesWithViper(v *viper.Viper, n *Neighbor, asn ui return nil } -func SetDefaultConfigValues(b *BgpConfigSet) error { - return setDefaultConfigValuesWithViper(nil, b) -} +func SetDefaultGlobalConfigValues(g *Global) error { + if len(g.AfiSafis) == 0 { + g.AfiSafis = []AfiSafi{} + for k, _ := range AfiSafiTypeToIntMap { + g.AfiSafis = append(g.AfiSafis, defaultAfiSafi(k, true)) + } + } -func setDefaultConfigValuesWithViper(v *viper.Viper, b *BgpConfigSet) error { - if v == nil { - v = viper.New() + if g.Config.Port == 0 { + g.Config.Port = bgp.BGP_PORT } - if b.Zebra.Config.Url == "" { - b.Zebra.Config.Url = "unix:/var/run/quagga/zserv.api" + if len(g.Config.LocalAddressList) == 0 { + g.Config.LocalAddressList = []string{"0.0.0.0", "::"} } - if len(b.Global.AfiSafis) == 0 { - b.Global.AfiSafis = []AfiSafi{} - for k, _ := range AfiSafiTypeToIntMap { - b.Global.AfiSafis = append(b.Global.AfiSafis, defaultAfiSafi(k, true)) - } + if g.MplsLabelRange.MinLabel == 0 { + g.MplsLabelRange.MinLabel = DEFAULT_MPLS_LABEL_MIN + } + + if g.MplsLabelRange.MaxLabel == 0 { + g.MplsLabelRange.MaxLabel = DEFAULT_MPLS_LABEL_MAX } + return nil + +} + +func SetDefaultConfigValues(b *BgpConfigSet) error { + return setDefaultConfigValuesWithViper(nil, b) +} - if b.Global.Config.Port == 0 { - b.Global.Config.Port = bgp.BGP_PORT +func setDefaultConfigValuesWithViper(v *viper.Viper, b *BgpConfigSet) error { + if v == nil { + v = viper.New() } - if len(b.Global.Config.LocalAddressList) == 0 { - b.Global.Config.LocalAddressList = []string{"0.0.0.0", "::"} + if err := SetDefaultGlobalConfigValues(&b.Global); err != nil { + return err } for idx, server := range b.BmpServers { @@ -183,12 +195,8 @@ func setDefaultConfigValuesWithViper(v *viper.Viper, b *BgpConfigSet) error { b.BmpServers[idx] = server } - if b.Global.MplsLabelRange.MinLabel == 0 { - b.Global.MplsLabelRange.MinLabel = DEFAULT_MPLS_LABEL_MIN - } - - if b.Global.MplsLabelRange.MaxLabel == 0 { - b.Global.MplsLabelRange.MaxLabel = DEFAULT_MPLS_LABEL_MAX + if b.Zebra.Config.Url == "" { + b.Zebra.Config.Url = "unix:/var/run/quagga/zserv.api" } list, err := extractArray(v.Get("neighbors")) diff --git a/docs/sources/lib.md b/docs/sources/lib.md index 7688e71f..ad1e257c 100644 --- a/docs/sources/lib.md +++ b/docs/sources/lib.md @@ -32,21 +32,19 @@ func main() { go g.Serve() // global configuration - b := &config.BgpConfigSet{ - Global: config.Global{ - Config: config.GlobalConfig{ - As: 65000, - RouterId: "10.0.255.254", - Port: -1, // gobgp won't listen on tcp:179 - }, + global := &config.Global{ + Config: config.GlobalConfig{ + As: 65000, + RouterId: "10.0.255.254", + Port: -1, // gobgp won't listen on tcp:179 }, } - if err := config.SetDefaultConfigValues(b); err != nil { + if err := config.SetDefaultGlobalConfigValues(global); err != nil { log.Fatal(err) } - if err := s.Start(&b.Global); err != nil { + if err := s.Start(global); err != nil { log.Fatal(err) } @@ -58,7 +56,7 @@ func main() { }, } - if err := config.SetDefaultNeighborConfigValues(n, b.Global.Config.As); err != nil { + if err := config.SetDefaultNeighborConfigValues(n, global.Config.As); err != nil { log.Fatal(err) } -- cgit v1.2.3