summaryrefslogtreecommitdiffhomepage
path: root/config/default.go
diff options
context:
space:
mode:
Diffstat (limited to 'config/default.go')
-rw-r--r--config/default.go130
1 files changed, 63 insertions, 67 deletions
diff --git a/config/default.go b/config/default.go
index 5e5c8a53..11b62a71 100644
--- a/config/default.go
+++ b/config/default.go
@@ -1,8 +1,10 @@
package config
import (
- "github.com/BurntSushi/toml"
+ "fmt"
"github.com/osrg/gobgp/packet"
+ "github.com/spf13/viper"
+ "net"
"strings"
)
@@ -14,25 +16,12 @@ const (
DEFAULT_MPLS_LABEL_MAX = 1048575
)
-type neighbor struct {
- attributes map[string]bool
-}
-
-func SetDefaultConfigValues(md toml.MetaData, bt *Bgp) error {
- neighbors := []neighbor{}
- global := make(map[string]bool)
-
- for _, key := range md.Keys() {
- if !strings.HasPrefix(key.String(), "Global") {
- continue
- }
- if key.String() != "Global" {
- global[key.String()] = true
- }
+func SetDefaultConfigValues(v *viper.Viper, b *Bgp) error {
+ if v == nil {
+ v = viper.New()
}
-
- if _, ok := global["Global.AfiSafis.AfiSafiList"]; !ok {
- bt.Global.AfiSafis.AfiSafiList = []AfiSafi{
+ if !v.IsSet("global.afisafis.afisafilist") {
+ b.Global.AfiSafis.AfiSafiList = []AfiSafi{
AfiSafi{AfiSafiName: "ipv4-unicast"},
AfiSafi{AfiSafiName: "ipv6-unicast"},
AfiSafi{AfiSafiName: "l3vpn-ipv4-unicast"},
@@ -47,77 +36,84 @@ func SetDefaultConfigValues(md toml.MetaData, bt *Bgp) error {
}
}
- if bt.Global.ListenConfig.Port == 0 {
- bt.Global.ListenConfig.Port = bgp.BGP_PORT
+ if b.Global.ListenConfig.Port == 0 {
+ b.Global.ListenConfig.Port = bgp.BGP_PORT
}
- if _, ok := global["Global.MplsLabelRange.MinLabel"]; !ok {
- bt.Global.MplsLabelRange.MinLabel = DEFAULT_MPLS_LABEL_MIN
+ if !v.IsSet("global.mplslabelrange.minlabel") {
+ b.Global.MplsLabelRange.MinLabel = DEFAULT_MPLS_LABEL_MIN
}
- if _, ok := global["Global.MplsLabelRange.MaxLabel"]; !ok {
- bt.Global.MplsLabelRange.MaxLabel = DEFAULT_MPLS_LABEL_MAX
+ if !v.IsSet("global.mplslabelrange.maxlabel") {
+ b.Global.MplsLabelRange.MaxLabel = DEFAULT_MPLS_LABEL_MAX
}
-
- nidx := 0
- for _, key := range md.Keys() {
- if !strings.HasPrefix(key.String(), "Neighbors.NeighborList") {
- continue
- }
- if key.String() == "Neighbors.NeighborList" {
- neighbors = append(neighbors, neighbor{attributes: make(map[string]bool)})
- nidx++
- } else {
- neighbors[nidx-1].attributes[key.String()] = true
+ var list []interface{}
+ for k, val := range v.GetStringMap("neighbors") {
+ if strings.ToLower(k) == "neighborlist" {
+ var ok bool
+ // yaml is decoded as []interface{}
+ // but toml is decoded as []map[string]interface{}.
+ // currently, viper can't hide this difference.
+ // handle the difference here.
+ list, ok = val.([]interface{})
+ if !ok {
+ l, ok := val.([]map[string]interface{})
+ if !ok {
+ return fmt.Errorf("invalid configuration: neighborlist must be a list")
+ }
+ for _, m := range l {
+ list = append(list, m)
+ }
+ }
}
}
- for i, n := range neighbors {
- neighbor := &bt.Neighbors.NeighborList[i]
- timerConfig := &neighbor.Timers.Config
-
- if _, ok := n.attributes["Neighbors.NeighborList.Timers.Config.ConnectRetry"]; !ok {
- timerConfig.HoldTime = float64(DEFAULT_CONNECT_RETRY)
+ for idx, n := range b.Neighbors.NeighborList {
+ vv := viper.New()
+ if len(list) > idx {
+ vv.Set("neighbor", list[idx])
}
- if _, ok := n.attributes["Neighbors.NeighborList.Timers.Config.HoldTime"]; !ok {
- timerConfig.HoldTime = float64(DEFAULT_HOLDTIME)
+ if !vv.IsSet("neighbor.timers.config.connectretry") {
+ n.Timers.Config.ConnectRetry = float64(DEFAULT_CONNECT_RETRY)
}
- if _, ok := n.attributes["Neighbors.NeighborList.Timers.Config.KeepaliveInterval"]; !ok {
- timerConfig.KeepaliveInterval = timerConfig.HoldTime / 3
+ if !vv.IsSet("neighbor.timers.config.holdtime") {
+ n.Timers.Config.HoldTime = float64(DEFAULT_HOLDTIME)
}
-
- if _, ok := n.attributes["Neighbors.NeighborList.Timers.Config.IdleHoldTimeAfterReset"]; !ok {
- timerConfig.IdleHoldTimeAfterReset = float64(DEFAULT_IDLE_HOLDTIME_AFTER_RESET)
+ if !vv.IsSet("neighbor.timers.config.keepaliveinterval") {
+ n.Timers.Config.KeepaliveInterval = n.Timers.Config.HoldTime / 3
+ }
+ if !vv.IsSet("neighbor.timers.config.idleholdtimeafterreset") {
+ n.Timers.Config.IdleHoldTimeAfterReset = float64(DEFAULT_IDLE_HOLDTIME_AFTER_RESET)
}
- if _, ok := n.attributes["Neighbors.NeighborList.AfiSafis.AfiSafiList"]; !ok {
- if neighbor.Config.NeighborAddress.To4() != nil {
- neighbor.AfiSafis.AfiSafiList = []AfiSafi{
- AfiSafi{AfiSafiName: "ipv4-unicast"}}
- } else {
- neighbor.AfiSafis.AfiSafiList = []AfiSafi{
- AfiSafi{AfiSafiName: "ipv6-unicast"}}
- }
- } else {
- for _, rf := range neighbor.AfiSafis.AfiSafiList {
- _, err := bgp.GetRouteFamily(rf.AfiSafiName)
- if err != nil {
- return err
+ if !vv.IsSet("neighbor.afisafis.afisafilist") {
+ if ip := net.ParseIP(n.Config.NeighborAddress); ip.To4() != nil {
+ n.AfiSafis.AfiSafiList = []AfiSafi{
+ AfiSafi{AfiSafiName: "ipv4-unicast"},
+ }
+ } else if ip.To16() != nil {
+ n.AfiSafis.AfiSafiList = []AfiSafi{
+ AfiSafi{AfiSafiName: "ipv6-unicast"},
}
+ } else {
+ return fmt.Errorf("invalid neighbor address: %s", n.Config.NeighborAddress)
}
}
- if _, ok := n.attributes["Neighbors.NeighborList.Config.PeerType"]; !ok {
- if neighbor.Config.PeerAs != bt.Global.Config.As {
- neighbor.Config.PeerType = PEER_TYPE_EXTERNAL
+ if !vv.IsSet("neighbor.config.peertype") {
+ if n.Config.PeerAs != b.Global.Config.As {
+ n.Config.PeerType = PEER_TYPE_EXTERNAL
} else {
- neighbor.Config.PeerType = PEER_TYPE_INTERNAL
+ n.Config.PeerType = PEER_TYPE_INTERNAL
}
}
+ b.Neighbors.NeighborList[idx] = n
}
- for _, r := range bt.RpkiServers.RpkiServerList {
+
+ for _, r := range b.RpkiServers.RpkiServerList {
if r.Config.Port == 0 {
r.Config.Port = bgp.RPKI_DEFAULT_PORT
}
}
+
return nil
}