1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

138 lines
2.9 KiB
Go
Raw Normal View History

2017-05-16 14:10:19 +02:00
package birdwatcher
import (
2017-05-23 13:58:58 +02:00
"fmt"
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-22 12:47:55 +02:00
// Get bird BGP protocols
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-22 12:47:55 +02:00
// Get filtered and exported routes
2017-05-18 18:10:55 +02:00
func (self *Birdwatcher) Routes(neighbourId string) (api.RoutesResponse, error) {
2017-05-22 12:47:55 +02:00
// Exported
bird, err := self.client.GetJson("/routes/protocol/" + neighbourId)
if err != nil {
return api.RoutesResponse{}, err
}
// Use api status from first request
apiStatus, err := parseApiStatus(bird, self.config)
if err != nil {
return api.RoutesResponse{}, err
}
exported, err := parseRoutes(bird, self.config)
if err != nil {
return api.RoutesResponse{}, err
}
// Filtered
2017-05-22 13:26:37 +02:00
bird, err = self.client.GetJson("/routes/filtered/" + neighbourId)
if err != nil {
return api.RoutesResponse{}, err
}
filtered, err := parseRoutes(bird, self.config)
if err != nil {
return api.RoutesResponse{}, err
}
2017-05-18 18:10:55 +02:00
2017-05-22 12:47:55 +02:00
return api.RoutesResponse{
Api: apiStatus,
Exported: exported,
2017-05-22 13:26:37 +02:00
Filtered: filtered,
2017-05-22 12:47:55 +02:00
}, nil
2017-05-16 14:10:19 +02:00
}
2017-05-23 13:58:58 +02:00
// Make routes lookup
func (self *Birdwatcher) LookupPrefix(prefix string) (api.LookupResponse, error) {
return api.LookupResponse{}, fmt.Errorf("not implemented")
}