summaryrefslogtreecommitdiffhomepage
path: root/api
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-12-25 06:39:39 -0800
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-12-25 06:39:39 -0800
commit02f33490e74f7aa9c0680067934da3088cacf754 (patch)
tree05b8722c4d1251b297ff5eec1bdc1b3204268c61 /api
parent4da71624586971c253bfb2625bdd91e121254b22 (diff)
rest: kill RestResponse interface
We can use RestResponse struct for all request types. Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'api')
-rw-r--r--api/rest.go91
1 files changed, 18 insertions, 73 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)