diff options
-rw-r--r-- | api/rest.go | 39 | ||||
-rw-r--r-- | server/peer.go | 11 | ||||
-rw-r--r-- | server/server.go | 18 |
3 files changed, 62 insertions, 6 deletions
diff --git a/api/rest.go b/api/rest.go index aaeeabd0..a110019e 100644 --- a/api/rest.go +++ b/api/rest.go @@ -30,9 +30,9 @@ const ( REQ_NEIGHBORS REQ_ADJ_RIB_IN REQ_ADJ_RIB_OUT - REQ_ADJ_RIB_LOCAL - REQ_ADJ_RIB_LOCAL_BEST + REQ_LOCAL_RIB ) + const ( BASE_VERSION = "/v1" NEIGHBOR = "/bgp/neighbor" @@ -80,6 +80,7 @@ type RestResponse interface { type RestResponseDefault struct { ResponseErr error + Data []byte } func (r *RestResponseDefault) Err() error { @@ -137,12 +138,11 @@ func (rs *RestServer) Serve() { r := mux.NewRouter() // set URLs - r.HandleFunc(neighbor+"/{"+PARAM_REMOTE_PEER_ADDR+"}", rs.Neighbor).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") - // r.HandleFunc(adjRibLocal+"/{"+PARAM_REMOTE_PEER_ADDR+"}", rs.AdjRibLocal).Methods("GET") - // r.HandleFunc(adjRibLocalBest+"/{"+PARAM_REMOTE_PEER_ADDR+"}", rs.AdjRibLocalBest).Methods("GET") + r.HandleFunc(neighbor+"/{"+PARAM_REMOTE_PEER_ADDR+"}/"+"local-rib", rs.NeighborLocalRib).Methods("GET") // Handler when not found url r.NotFoundHandler = http.HandlerFunc(NotFoundHandler) @@ -198,6 +198,35 @@ func (rs *RestServer) Neighbor(w http.ResponseWriter, r *http.Request) { w.Write(jns) } +// TODO: merge the above function +func (rs *RestServer) NeighborLocalRib(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + remoteAddr, found := params[PARAM_REMOTE_PEER_ADDR] + if !found { + errStr := "neighbor address is not specified" + log.Debug(errStr) + http.Error(w, errStr, http.StatusInternalServerError) + return + } + + log.Debugf("Look up neighbor with the remote address : %v", remoteAddr) + + //Send channel of request parameter. + req := NewRestRequest(REQ_LOCAL_RIB, remoteAddr) + 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.Write(res.Data) +} + 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 5c26fc18..a0ec5064 100644 --- a/server/peer.go +++ b/server/peer.go @@ -18,6 +18,7 @@ package server import ( "encoding/json" log "github.com/Sirupsen/logrus" + "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet" "github.com/osrg/gobgp/table" @@ -103,6 +104,14 @@ func (peer *Peer) path2update(pathList []table.Path) []*bgp.BGPMessage { return msgs } +func (peer *Peer) handleREST(restReq *api.RestRequest) { + result := &api.RestResponseDefault{} + j, _ := json.Marshal(peer.rib.Tables[bgp.RF_IPv4_UC]) + result.Data = j + restReq.ResponseCh <- result + close(restReq.ResponseCh) +} + func (peer *Peer) handlePeermessage(m *message) { sendpath := func(pList []table.Path, wList []table.Destination) { @@ -123,6 +132,8 @@ func (peer *Peer) handlePeermessage(m *message) { case PEER_MSG_DOWN: pList, wList, _ := peer.rib.DeletePathsforPeer(m.data.(*table.PeerInfo)) sendpath(pList, wList) + case PEER_MSG_REST: + peer.handleREST(m.data.(*api.RestRequest)) } } diff --git a/server/server.go b/server/server.go index 126bd6a9..2852598b 100644 --- a/server/server.go +++ b/server/server.go @@ -32,6 +32,7 @@ const ( PEER_MSG_NEW PEER_MSG_PATH PEER_MSG_DOWN + PEER_MSG_REST //hacky, fix later ) type message struct { @@ -150,7 +151,6 @@ func (server *BgpServer) broadcast(msg *message) { } func (server *BgpServer) handleRest(restReq *api.RestRequest) { - defer close(restReq.ResponseCh) switch restReq.RequestType { case api.REQ_NEIGHBOR: // get neighbor state @@ -166,5 +166,21 @@ func (server *BgpServer) handleRest(restReq *api.RestRequest) { result.ResponseErr = fmt.Errorf("Neighbor that has %v does not exist.", remoteAddr) } restReq.ResponseCh <- result + close(restReq.ResponseCh) + case api.REQ_LOCAL_RIB: + remoteAddr := restReq.RemoteAddr + result := &api.RestResponseNeighbor{} + peer, found := server.peerMap[remoteAddr] + if found { + msg := message{ + event: PEER_MSG_REST, + data: restReq, + } + peer.SendMessage(&msg) + } else { + result.ResponseErr = fmt.Errorf("Neighbor that has %v does not exist.", remoteAddr) + restReq.ResponseCh <- result + close(restReq.ResponseCh) + } } } |