2017-05-16 14:10:19 +02:00
|
|
|
package birdwatcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2017-05-18 18:10:55 +02:00
|
|
|
|
|
|
|
"github.com/ecix/alice-lg/backend/api"
|
2017-05-16 14:10:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Birdwatcher struct {
|
2017-05-18 18:10:55 +02:00
|
|
|
config Config
|
2017-05-19 17:40:22 +02:00
|
|
|
client *Client
|
2017-05-16 14:10:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-18 18:10:55 +02:00
|
|
|
func NewBirdwatcher(config Config) *Birdwatcher {
|
2017-05-19 17:40:22 +02:00
|
|
|
client := NewClient(config.Api)
|
|
|
|
|
2017-05-16 14:10:19 +02:00
|
|
|
birdwatcher := &Birdwatcher{
|
|
|
|
config: config,
|
2017-05-19 17:40:22 +02:00
|
|
|
client: client,
|
2017-05-16 14:10:19 +02:00
|
|
|
}
|
|
|
|
return birdwatcher
|
|
|
|
}
|
|
|
|
|
2017-05-18 18:10:55 +02:00
|
|
|
func (self *Birdwatcher) Status() (api.StatusResponse, error) {
|
2017-05-19 17:40:22 +02:00
|
|
|
bird, err := self.client.GetJson("/status")
|
|
|
|
if err != nil {
|
2017-05-22 11:09:22 +02:00
|
|
|
return api.StatusResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiStatus, err := parseApiStatus(bird, self.config)
|
|
|
|
if err != nil {
|
|
|
|
return api.StatusResponse{}, err
|
2017-05-19 17:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
birdStatus := bird["status"].(map[string]interface{})
|
|
|
|
|
|
|
|
// Get special fields
|
|
|
|
serverTime, _ := parseServerTime(
|
|
|
|
birdStatus["current_server"],
|
|
|
|
SERVER_TIME_SHORT,
|
|
|
|
self.config.Timezone,
|
|
|
|
)
|
|
|
|
|
|
|
|
lastReboot, _ := parseServerTime(
|
|
|
|
birdStatus["last_reboot"],
|
|
|
|
SERVER_TIME_SHORT,
|
|
|
|
self.config.Timezone,
|
|
|
|
)
|
|
|
|
|
|
|
|
lastReconfig, _ := parseServerTime(
|
|
|
|
birdStatus["last_reconfig"],
|
|
|
|
SERVER_TIME_EXT,
|
|
|
|
self.config.Timezone,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Make status response
|
|
|
|
status := api.Status{
|
|
|
|
ServerTime: serverTime,
|
|
|
|
LastReboot: lastReboot,
|
|
|
|
LastReconfig: lastReconfig,
|
|
|
|
Backend: "bird",
|
|
|
|
Version: birdStatus["version"].(string),
|
|
|
|
Message: birdStatus["message"].(string),
|
|
|
|
RouterId: birdStatus["router_id"].(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
response := api.StatusResponse{
|
2017-05-19 17:56:01 +02:00
|
|
|
Api: apiStatus,
|
2017-05-19 17:40:22 +02:00
|
|
|
Status: status,
|
|
|
|
}
|
2017-05-18 18:10:55 +02:00
|
|
|
|
2017-05-19 17:40:22 +02:00
|
|
|
return response, nil
|
2017-05-16 14:10:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-18 18:10:55 +02:00
|
|
|
func (self *Birdwatcher) Neighbours() (api.NeighboursResponse, error) {
|
2017-05-22 11:09:22 +02:00
|
|
|
bird, err := self.client.GetJson("/protocols/bgp")
|
|
|
|
if err != nil {
|
|
|
|
return api.NeighboursResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiStatus, err := parseApiStatus(bird, self.config)
|
|
|
|
if err != nil {
|
|
|
|
return api.NeighboursResponse{}, err
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:47:10 +02:00
|
|
|
neighbours, err := parseNeighbours(bird, self.config)
|
2017-05-22 11:09:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return api.NeighboursResponse{}, err
|
|
|
|
}
|
2017-05-18 18:10:55 +02:00
|
|
|
|
2017-05-22 11:09:22 +02:00
|
|
|
return api.NeighboursResponse{
|
|
|
|
Api: apiStatus,
|
|
|
|
Neighbours: neighbours,
|
|
|
|
}, nil
|
2017-05-16 14:10:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-18 18:10:55 +02:00
|
|
|
func (self *Birdwatcher) Routes(neighbourId string) (api.RoutesResponse, error) {
|
2017-05-16 14:10:19 +02:00
|
|
|
log.Println("Implement me: Routes()")
|
2017-05-18 18:10:55 +02:00
|
|
|
|
|
|
|
return api.RoutesResponse{}, nil
|
2017-05-16 14:10:19 +02:00
|
|
|
}
|