summaryrefslogtreecommitdiffhomepage
path: root/config/serve.go
blob: 02eb2e6ecbfc0d55bb6f30826f9dcba0a08e33b0 (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
package config

import (
	"github.com/BurntSushi/toml"
	log "github.com/Sirupsen/logrus"
)

func ReadConfigfileServe(path string, configCh chan BgpType, reloadCh chan bool) {
	for {
		<-reloadCh

		b := BgpType{}
		_, err := toml.DecodeFile(path, &b)
		if err != nil {
			log.Fatal("can't read config file ", path)
		} else {
			// TODO: validate configuration
			for i, _ := range b.NeighborList {
				SetNeighborTypeDefault(&b.NeighborList[i])
			}
		}

		configCh <- b
	}
}

func inSlice(n NeighborType, b []NeighborType) bool {
	for _, nb := range b {
		if nb.NeighborAddress.String() == n.NeighborAddress.String() {
			return true
		}
	}
	return false
}

func UpdateConfig(curC *BgpType, newC *BgpType) (*BgpType, []NeighborType, []NeighborType) {
	bgpConfig := BgpType{}
	if curC == nil {
		bgpConfig.Global = newC.Global
		curC = &bgpConfig
	} else {
		// can't update the global config
		bgpConfig.Global = curC.Global
	}
	added := []NeighborType{}
	deleted := []NeighborType{}

	for _, n := range newC.NeighborList {
		if inSlice(n, curC.NeighborList) == false {
			added = append(added, n)
		}
	}

	for _, n := range curC.NeighborList {
		if inSlice(n, newC.NeighborList) == false {
			deleted = append(deleted, n)
		}
	}

	bgpConfig.NeighborList = newC.NeighborList
	return &bgpConfig, added, deleted
}