diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-11-23 23:33:00 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-11-25 10:56:32 +0900 |
commit | 048cc6968d6af5da330bf45f5891ef81d4f6b33d (patch) | |
tree | 9e80e800b6ceb36585ae699911c8d9443f794440 | |
parent | f08b355930c6d18ac184b82c44858d0c10bded92 (diff) |
config: add helper functions to handle config file
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | config/serve.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/config/serve.go b/config/serve.go new file mode 100644 index 00000000..23e4c1de --- /dev/null +++ b/config/serve.go @@ -0,0 +1,59 @@ +package config + +import ( + "github.com/BurntSushi/toml" +) + +func ReadConfigfileServe(path string, configCh chan BgpType, reloadCh chan bool) { + for { + <-reloadCh + + b := BgpType{} + _, err := toml.DecodeFile(path, &b) + if err == nil { + // 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 +} |