diff options
-rw-r--r-- | api/rest.go | 23 | ||||
-rw-r--r-- | server/peer.go | 130 | ||||
-rw-r--r-- | server/server.go | 10 |
3 files changed, 84 insertions, 79 deletions
diff --git a/api/rest.go b/api/rest.go index fc421805..83dba703 100644 --- a/api/rest.go +++ b/api/rest.go @@ -122,7 +122,7 @@ func NewRestServer(port int, bgpServerCh chan *RestRequest) *RestServer { // -- curl -i -X GET http://<ownIP>:8080/v1/bgp/neighbor/<remote address of target neighbor>/local-rib func (rs *RestServer) Serve() { neighbor := BASE_VERSION + NEIGHBOR - // neighbors := BASE_VERSION + NEIGHBORS + neighbors := BASE_VERSION + NEIGHBORS // adjRibIn := BASE_VERSION + ADJ_RIB_IN // adjRibOut := BASE_VERSION + ADJ_RIB_OUT // adjRibLocal := BASE_VERSION + ADJ_RIB_LOCAL @@ -130,7 +130,7 @@ func (rs *RestServer) Serve() { r := mux.NewRouter() // set URLs - // r.HandleFunc(neighbors, rs.Neighbors).Methods("GET") + r.HandleFunc(neighbors, rs.Neighbors).Methods("GET") r.HandleFunc(neighbor+"/{"+PARAM_REMOTE_PEER_ADDR+"}", rs.Neighbor).Methods("GET") // r.HandleFunc(adjRibIn+"/{"+PARAM_REMOTE_PEER_ADDR+"}", rs.AdjRibIn).Methods("GET") // r.HandleFunc(adjRibOut+"/{"+PARAM_REMOTE_PEER_ADDR+"}", rs.AdjRibOut).Methods("GET") @@ -222,6 +222,25 @@ func (rs *RestServer) NeighborLocalRib(w http.ResponseWriter, r *http.Request) { w.Write(j) } +func (rs *RestServer) Neighbors(w http.ResponseWriter, r *http.Request) { + //Send channel of request parameter. + req := NewRestRequest(REQ_NEIGHBORS, "") + rs.bgpServerCh <- req + + //Wait response + resInf := <-req.ResponseCh + if e := resInf.Err(); e != nil { + log.Debug(e.Error()) + http.Error(w, e.Error(), http.StatusInternalServerError) + return + } + + res := resInf.(*RestResponseDefault) + w.Header().Set("Content-Type", "application/json; charset=utf-8") + j, _ := json.Marshal(res.Data) + w.Write(j) +} + func NotFoundHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) } diff --git a/server/peer.go b/server/peer.go index f3a23d5b..66514d11 100644 --- a/server/peer.go +++ b/server/peer.go @@ -205,88 +205,64 @@ func (peer *Peer) MarshalJSON() ([]byte, error) { c := peer.peerConfig f := peer.fsm - conf, confErr := json.Marshal( - struct { - RemoteIP string `json:"remote_ip"` - Id string `json:"id"` - Description string `json:"description"` - RemoteAS uint32 `json:"remote_as"` - LocalAddress string `json:"local_address"` - LocalPort int `json:"local_port"` - CapRefresh bool `json:"cap_refresh"` - CapEnhancedRefresh bool `json:"cap_enhanced_refresh"` - CapMbgpVpnv4 bool `json:"cap_mbgp_vpnv4"` - CapMbgpVpnv6 bool `json:"cap_mbgp_vpnv6"` - }{ - RemoteIP: c.NeighborAddress.String(), - Id: f.routerId.String(), - Description: "", - RemoteAS: c.PeerAs, - LocalAddress: f.passiveConn.LocalAddr().String(), - LocalPort: f.passiveConn.LocalAddr().(*net.TCPAddr).Port, - CapRefresh: false, - CapEnhancedRefresh: false, - CapMbgpVpnv4: false, - CapMbgpVpnv6: false, - }) + p := make(map[string]interface{}) - if confErr != nil { - return nil, confErr + p["conf"] = struct { + RemoteIP string `json:"remote_ip"` + Id string `json:"id"` + Description string `json:"description"` + RemoteAS uint32 `json:"remote_as"` + LocalAddress string `json:"local_address"` + LocalPort int `json:"local_port"` + CapRefresh bool `json:"cap_refresh"` + CapEnhancedRefresh bool `json:"cap_enhanced_refresh"` + }{ + RemoteIP: c.NeighborAddress.String(), + Id: f.routerId.To4().String(), + Description: "", + RemoteAS: c.PeerAs, + //LocalAddress: f.passiveConn.LocalAddr().String(), + //LocalPort: f.passiveConn.LocalAddr().(*net.TCPAddr).Port, + CapRefresh: false, + CapEnhancedRefresh: false, } s := peer.peerConfig.BgpNeighborCommonState + p["info"] = struct { + BgpState string `json:"bgp_state"` + FsmEstablishedTransitions uint32 `json:"fsm_established_transitions"` + TotalMessageOut uint32 `json:"total_message_out"` + TotalMessageIn uint32 `json:"total_message_in"` + UpdateMessageOut uint32 `json:"update_message_out"` + UpdateMessageIn uint32 `json:"update_message_in"` + KeepAliveMessageOut uint32 `json:"keepalive_message_out"` + KeepAliveMessageIn uint32 `json:"keepalive_message_in"` + OpenMessageOut uint32 `json:"open_message_out"` + OpenMessageIn uint32 `json:"open_message_in"` + NotificationOut uint32 `json:"notification_out"` + NotificationIn uint32 `json:"notification_in"` + RefreshMessageOut uint32 `json:"refresh_message_out"` + RefreshMessageIn uint32 `json:"refresh_message_in"` + Uptime float64 `json:"uptime"` + LastError string `json:"last_error"` + }{ - info, infoErr := json.Marshal( - struct { - BgpState string `json:"bgp_state"` - FsmEstablishedTransitions uint32 `json:"fsm_established_transitions"` - TotalMessageOut uint32 `json:"total_message_out"` - TotalMessageIn uint32 `json:"total_message_in"` - UpdateMessageOut uint32 `json:"update_message_out"` - UpdateMessageIn uint32 `json:"update_message_in"` - KeepAliveMessageOut uint32 `json:"keepalive_message_out"` - KeepAliveMessageIn uint32 `json:"keepalive_message_in"` - OpenMessageOut uint32 `json:"open_message_out"` - OpenMessageIn uint32 `json:"open_message_in"` - NotificationOut uint32 `json:"notification_out"` - NotificationIn uint32 `json:"notification_in"` - RefreshMessageOut uint32 `json:"refresh_message_out"` - RefreshMessageIn uint32 `json:"refresh_message_in"` - Uptime float64 `json:"uptime"` - LastError string `json:"last_error"` - }{ - - BgpState: f.state.String(), - FsmEstablishedTransitions: s.EstablishedCount, - TotalMessageOut: 0, - TotalMessageIn: 0, - UpdateMessageOut: s.UpdateOut, - UpdateMessageIn: s.UpdateIn, - KeepAliveMessageOut: s.KeepaliveOut, - KeepAliveMessageIn: s.KeepaliveIn, - OpenMessageOut: s.OpenOut, - OpenMessageIn: s.OpenIn, - NotificationOut: s.NotifyOut, - NotificationIn: s.NotifyIn, - RefreshMessageOut: s.RefreshOut, - RefreshMessageIn: s.RefreshIn, - Uptime: time.Now().Sub(s.Uptime).Seconds(), - }) - - if infoErr != nil { - return nil, infoErr + BgpState: f.state.String(), + FsmEstablishedTransitions: s.EstablishedCount, + TotalMessageOut: 0, + TotalMessageIn: 0, + UpdateMessageOut: s.UpdateOut, + UpdateMessageIn: s.UpdateIn, + KeepAliveMessageOut: s.KeepaliveOut, + KeepAliveMessageIn: s.KeepaliveIn, + OpenMessageOut: s.OpenOut, + OpenMessageIn: s.OpenIn, + NotificationOut: s.NotifyOut, + NotificationIn: s.NotifyIn, + RefreshMessageOut: s.RefreshOut, + RefreshMessageIn: s.RefreshIn, + Uptime: time.Now().Sub(s.Uptime).Seconds(), } - confTag := []byte("{'conf':") - delimiter := []byte(",") - infoTag := []byte("'info':") - endDoc := []byte("}") - - jsonval := append(confTag, conf...) - jsonval = append(jsonval, delimiter...) - jsonval = append(jsonval, infoTag...) - jsonval = append(jsonval, info...) - jsonval = append(jsonval, endDoc...) - - return jsonval, nil + return json.Marshal(p) } diff --git a/server/server.go b/server/server.go index 2852598b..6a633d3e 100644 --- a/server/server.go +++ b/server/server.go @@ -152,6 +152,16 @@ func (server *BgpServer) broadcast(msg *message) { func (server *BgpServer) handleRest(restReq *api.RestRequest) { switch restReq.RequestType { + case api.REQ_NEIGHBORS: + result := &api.RestResponseDefault{} + peerList := make([]*Peer, 0) + for _, peer := range server.peerMap { + peerList = append(peerList, peer) + } + result.Data = peerList + restReq.ResponseCh <- result + close(restReq.ResponseCh) + case api.REQ_NEIGHBOR: // get neighbor state remoteAddr := restReq.RemoteAddr |