diff options
-rw-r--r-- | api/rest.go | 91 | ||||
-rw-r--r-- | server/peer.go | 2 | ||||
-rw-r--r-- | server/server.go | 12 |
3 files changed, 23 insertions, 82 deletions
diff --git a/api/rest.go b/api/rest.go index 83dba703..79a13597 100644 --- a/api/rest.go +++ b/api/rest.go @@ -17,7 +17,6 @@ package api import ( "encoding/json" - "fmt" log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" "net/http" @@ -61,7 +60,7 @@ var JsonFormat int = JSON_FORMATTED type RestRequest struct { RequestType int RemoteAddr string - ResponseCh chan RestResponse + ResponseCh chan *RestResponse Err error } @@ -69,33 +68,20 @@ func NewRestRequest(reqType int, remoteAddr string) *RestRequest { r := &RestRequest{ RequestType: reqType, RemoteAddr: remoteAddr, - ResponseCh: make(chan RestResponse), + ResponseCh: make(chan *RestResponse), } return r } -type RestResponse interface { - Err() error -} - -type RestResponseDefault struct { +type RestResponse struct { ResponseErr error Data interface{} } -func (r *RestResponseDefault) Err() error { +func (r *RestResponse) Err() error { return r.ResponseErr } -// Response struct for Neighbor -type RestResponseNeighbor struct { - RestResponseDefault - RemoteAddr string - RemoteAs uint32 - NeighborState string - UpdateCount int -} - type RestServer struct { port int bgpServerCh chan *RestRequest @@ -144,55 +130,8 @@ func (rs *RestServer) Serve() { } -// Http request of curl, return the json format infomation of neibor state. -func (rs *RestServer) Neighbor(w http.ResponseWriter, r *http.Request) { - // remotePeerAddr := mux.Vars(r)[PARAM_REMOTE_PEER_ADDR] - - 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_NEIGHBOR, 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.(*RestResponseNeighbor) - - var jns []byte - var err error - switch JsonFormat { - case JSON_FORMATTED: - jns, err = json.MarshalIndent(res, "", " ") // formatted json - case JSON_UN_FORMATTED: - jns, err = json.Marshal(res) // Unformatted json - } - if err != nil { - errStr := fmt.Sprintf("Failed to perth json of neighbor state", remoteAddr) - log.Error(errStr) - http.Error(w, errStr, http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json; charset=utf-8") - w.Write(jns) -} - // TODO: merge the above function -func (rs *RestServer) NeighborLocalRib(w http.ResponseWriter, r *http.Request) { +func (rs *RestServer) neighbor(w http.ResponseWriter, r *http.Request, reqType int) { params := mux.Vars(r) remoteAddr, found := params[PARAM_REMOTE_PEER_ADDR] if !found { @@ -205,37 +144,43 @@ func (rs *RestServer) NeighborLocalRib(w http.ResponseWriter, r *http.Request) { log.Debugf("Look up neighbor with the remote address : %v", remoteAddr) //Send channel of request parameter. - req := NewRestRequest(REQ_LOCAL_RIB, remoteAddr) + req := NewRestRequest(reqType, remoteAddr) rs.bgpServerCh <- req //Wait response - resInf := <-req.ResponseCh - if e := resInf.Err(); e != nil { + res := <-req.ResponseCh + if e := res.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 (rs *RestServer) Neighbor(w http.ResponseWriter, r *http.Request) { + rs.neighbor(w, r, REQ_NEIGHBOR) +} + +func (rs *RestServer) NeighborLocalRib(w http.ResponseWriter, r *http.Request) { + rs.neighbor(w, r, REQ_LOCAL_RIB) +} + 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 { + res := <-req.ResponseCh + if e := res.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) diff --git a/server/peer.go b/server/peer.go index 66514d11..71144067 100644 --- a/server/peer.go +++ b/server/peer.go @@ -105,7 +105,7 @@ func (peer *Peer) path2update(pathList []table.Path) []*bgp.BGPMessage { } func (peer *Peer) handleREST(restReq *api.RestRequest) { - result := &api.RestResponseDefault{} + result := &api.RestResponse{} result.Data = peer.rib.Tables[bgp.RF_IPv4_UC] restReq.ResponseCh <- result close(restReq.ResponseCh) diff --git a/server/server.go b/server/server.go index 6a633d3e..ac1e4f81 100644 --- a/server/server.go +++ b/server/server.go @@ -20,7 +20,6 @@ import ( log "github.com/Sirupsen/logrus" "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/config" - "github.com/osrg/gobgp/packet" "net" "os" "strconv" @@ -153,7 +152,7 @@ func (server *BgpServer) broadcast(msg *message) { func (server *BgpServer) handleRest(restReq *api.RestRequest) { switch restReq.RequestType { case api.REQ_NEIGHBORS: - result := &api.RestResponseDefault{} + result := &api.RestResponse{} peerList := make([]*Peer, 0) for _, peer := range server.peerMap { peerList = append(peerList, peer) @@ -165,13 +164,10 @@ func (server *BgpServer) handleRest(restReq *api.RestRequest) { case api.REQ_NEIGHBOR: // get neighbor state remoteAddr := restReq.RemoteAddr - result := &api.RestResponseNeighbor{} + result := &api.RestResponse{} peer, found := server.peerMap[remoteAddr] if found { - c := peer.peerConfig - result.NeighborState = bgp.FSMState(c.BgpNeighborCommonState.State).String() - result.RemoteAddr = c.NeighborAddress.String() - result.RemoteAs = c.PeerAs + result.Data = peer } else { result.ResponseErr = fmt.Errorf("Neighbor that has %v does not exist.", remoteAddr) } @@ -179,7 +175,7 @@ func (server *BgpServer) handleRest(restReq *api.RestRequest) { close(restReq.ResponseCh) case api.REQ_LOCAL_RIB: remoteAddr := restReq.RemoteAddr - result := &api.RestResponseNeighbor{} + result := &api.RestResponse{} peer, found := server.peerMap[remoteAddr] if found { msg := message{ |