summaryrefslogtreecommitdiffhomepage
path: root/config/default.go
blob: 8b7f1fb7eb1153f3ce3498efa583ee9ab3deca2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package config

import (
	"github.com/BurntSushi/toml"
	"github.com/osrg/gobgp/packet"
	"strings"
)

const (
	DEFAULT_HOLDTIME                  = 90
	DEFAULT_IDLE_HOLDTIME_AFTER_RESET = 30
	DEFAULT_CONNECT_RETRY             = 120
)

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
		}
	}

	if _, ok := global["Global.AfiSafiList"]; !ok {
		bt.Global.AfiSafiList = []AfiSafi{
			AfiSafi{AfiSafiName: "ipv4-unicast"},
			AfiSafi{AfiSafiName: "ipv6-unicast"},
			AfiSafi{AfiSafiName: "l2vpn-evpn"},
		}
	}

	nidx := 0
	for _, key := range md.Keys() {
		if !strings.HasPrefix(key.String(), "NeighborList") {
			continue
		}
		if key.String() == "NeighborList" {
			neighbors = append(neighbors, neighbor{attributes: make(map[string]bool)})
			nidx++
		} else {
			neighbors[nidx-1].attributes[key.String()] = true
		}
	}
	for i, n := range neighbors {
		if _, ok := n.attributes["NeighborList.Timers.ConnectRetry"]; !ok {
			bt.NeighborList[i].Timers.HoldTime = float64(DEFAULT_CONNECT_RETRY)
		}
		if _, ok := n.attributes["NeighborList.Timers.HoldTime"]; !ok {
			bt.NeighborList[i].Timers.HoldTime = float64(DEFAULT_HOLDTIME)
		}
		if _, ok := n.attributes["NeighborList.Timers.KeepaliveInterval"]; !ok {
			bt.NeighborList[i].Timers.KeepaliveInterval = bt.NeighborList[i].Timers.HoldTime / 3
		}

		if _, ok := n.attributes["NeighborList.Timers.IdleHoldTimeAfterReset"]; !ok {
			bt.NeighborList[i].Timers.IdleHoldTimeAfterReset = float64(DEFAULT_IDLE_HOLDTIME_AFTER_RESET)
		}

		if _, ok := n.attributes["NeighborList.AfiSafiList"]; !ok {
			if bt.NeighborList[i].NeighborAddress.To4() != nil {
				bt.NeighborList[i].AfiSafiList = []AfiSafi{
					AfiSafi{AfiSafiName: "ipv4-unicast"}}
			} else {
				bt.NeighborList[i].AfiSafiList = []AfiSafi{
					AfiSafi{AfiSafiName: "ipv6-unicast"}}
			}
		} else {
			for _, rf := range bt.NeighborList[i].AfiSafiList {
				_, err := bgp.GetRouteFamily(rf.AfiSafiName)
				if err != nil {
					return err
				}
			}
		}

		if _, ok := n.attributes["NeighborList.PeerType"]; !ok {
			if bt.NeighborList[i].PeerAs != bt.Global.As {
				bt.NeighborList[i].PeerType = PEER_TYPE_EXTERNAL
			} else {
				bt.NeighborList[i].PeerType = PEER_TYPE_INTERNAL
			}
		}
	}
	return nil
}