summaryrefslogtreecommitdiffhomepage
path: root/api/rest.go
diff options
context:
space:
mode:
authorISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-04-04 14:02:46 +0000
committerISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-04-04 14:02:46 +0000
commit01a0fc94cc86a536544ab641bbaf9f54aad1593c (patch)
tree141716b2b98da6bad1a59a2500c2601ddf82c11f /api/rest.go
parent6d2fec2f4f86edc29a058b4971553fe48b1a7c7c (diff)
api/server: add apis to add/delete routes to/from global rib
add: curl -X POST -d prefix=10.0.0.0/24 http://localhost:8080/v1/bgp/global/ipv4 delete: curl -X DELETE -d prefix=10.0.0.0/24 http://localhost:8080/v1/bgp/global/ipv4 Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'api/rest.go')
-rw-r--r--api/rest.go59
1 files changed, 57 insertions, 2 deletions
diff --git a/api/rest.go b/api/rest.go
index 1201abea..fd57253d 100644
--- a/api/rest.go
+++ b/api/rest.go
@@ -16,11 +16,13 @@
package api
import (
+ "bytes"
log "github.com/Sirupsen/logrus"
"github.com/fukata/golang-stats-api-handler"
"github.com/gorilla/mux"
"github.com/osrg/gobgp/packet"
"net/http"
+ "net/url"
"strconv"
)
@@ -39,6 +41,8 @@ const (
REQ_NEIGHBOR_ENABLE
REQ_NEIGHBOR_DISABLE
REQ_GLOBAL_RIB
+ REQ_GLOBAL_ADD
+ REQ_GLOBAL_DELETE
)
const (
@@ -66,14 +70,16 @@ type RestRequest struct {
RouteFamily bgp.RouteFamily
ResponseCh chan *RestResponse
Err error
+ Data map[string]interface{}
}
-func NewRestRequest(reqType int, remoteAddr string, rf bgp.RouteFamily) *RestRequest {
+func NewRestRequest(reqType int, remoteAddr string, rf bgp.RouteFamily, d map[string]interface{}) *RestRequest {
r := &RestRequest{
RequestType: reqType,
RouteFamily: rf,
RemoteAddr: remoteAddr,
ResponseCh: make(chan *RestResponse),
+ Data: d,
}
return r
}
@@ -122,6 +128,8 @@ func (rs *RestServer) Serve() {
operationURL := "/{" + PARAM_OPERATION + "}"
routeFamilyURL := "/{" + PARAM_ROUTE_FAMILY + "}"
r.HandleFunc(global+showObjectURL+routeFamilyURL, rs.GlobalGET).Methods("GET")
+ r.HandleFunc(global+routeFamilyURL, rs.GlobalPOST).Methods("POST")
+ r.HandleFunc(global+routeFamilyURL, rs.GlobalDELETE).Methods("DELETE")
r.HandleFunc(neighbors, rs.NeighborGET).Methods("GET")
r.HandleFunc(neighbor+perPeerURL, rs.NeighborGET).Methods("GET")
r.HandleFunc(neighbor+perPeerURL+showObjectURL+routeFamilyURL, rs.NeighborGET).Methods("GET")
@@ -160,7 +168,7 @@ func (rs *RestServer) neighbor(w http.ResponseWriter, r *http.Request, reqType i
}
//Send channel of request parameter.
- req := NewRestRequest(reqType, remoteAddr, rf)
+ req := NewRestRequest(reqType, remoteAddr, rf, nil)
rs.bgpServerCh <- req
//Wait response
@@ -233,6 +241,53 @@ func (rs *RestServer) GlobalGET(w http.ResponseWriter, r *http.Request) {
}
+func (rs *RestServer) global(w http.ResponseWriter, r *http.Request, reqType int) {
+ params := mux.Vars(r)
+ var rf bgp.RouteFamily
+ routeFamily, ok := params[PARAM_ROUTE_FAMILY]
+ if ok {
+ switch routeFamily {
+ case "ipv4":
+ rf = bgp.RF_IPv4_UC
+ case "ipv6":
+ rf = bgp.RF_IPv6_UC
+ case "evpn":
+ rf = bgp.RF_EVPN
+ default:
+ NotFoundHandler(w, r)
+ return
+ }
+ }
+ var buf bytes.Buffer
+ buf.ReadFrom(r.Body)
+ query, _ := url.ParseQuery(buf.String())
+ d := make(map[string]interface{})
+ for k, v := range query {
+ d[k] = v
+ }
+ req := NewRestRequest(reqType, "", rf, d)
+ rs.bgpServerCh <- req
+
+ //Wait response
+ res := <-req.ResponseCh
+ if e := res.Err(); e != nil {
+ log.Debug(e.Error())
+ http.Error(w, e.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json; charset=utf-8")
+ w.Write(res.Data)
+}
+
+func (rs *RestServer) GlobalPOST(w http.ResponseWriter, r *http.Request) {
+ rs.global(w, r, REQ_GLOBAL_ADD)
+}
+
+func (rs *RestServer) GlobalDELETE(w http.ResponseWriter, r *http.Request) {
+ rs.global(w, r, REQ_GLOBAL_DELETE)
+}
+
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}