summaryrefslogtreecommitdiffhomepage
path: root/config/serve.go
blob: 390229c1561561806ba06cae815081e633796b58 (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"
	log "github.com/Sirupsen/logrus"
	"reflect"
)

type BgpConfigSet struct {
	Bgp    Bgp
	Policy RoutingPolicy
}

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

		b := Bgp{}
		md, err := toml.DecodeFile(path, &b)
		if err == nil {
			err = SetDefaultConfigValues(md, &b)
		}
		if err != nil {
			log.Fatal("can't read config file ", path, ", ", err)
		}

		p := RoutingPolicy{}
		md, err = toml.DecodeFile(path, &p)
		if err != nil {
			log.Fatal("can't read config file ", path, ", ", err)
		}

		bgpConfig := BgpConfigSet{Bgp: b, Policy: p}
		configCh <- bgpConfig
	}
}

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

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

	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
}

func CheckPolicyDifference(currentPolicy *RoutingPolicy, newPolicy *RoutingPolicy) bool {

	log.Debug("current policy : ", currentPolicy)
	log.Debug("newPolicy policy : ", newPolicy)

	var result bool = false
	if currentPolicy == nil && newPolicy == nil {

		result = false
	} else {
		if currentPolicy != nil && newPolicy != nil {
			// TODO: reconsider the way of policy object comparison
			result = !reflect.DeepEqual(*currentPolicy, *newPolicy)
		} else {
			result = true
		}
	}
	return result
}