diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-04-15 05:24:58 +0000 |
---|---|---|
committer | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-04-15 05:24:58 +0000 |
commit | 3f6876df0b16043412972e7fd92377226bc9c89d (patch) | |
tree | d8b1e2a41a0dca1720929807cc8161568e7c3565 /server | |
parent | a1114dc7bb6dcb165c6494799be94b5d10fe5b17 (diff) |
api: add a method to convert internal structs to protobuf structs
- add ToApiStruct() for convertion of internal structs to protobuf structs
to avoid ugly convertion by json.Marshal() && json.Unmarshal()
- move grpc server code under /server instead of /api
- update proto file to include more detailed path information
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'server')
-rw-r--r-- | server/grpc_server.go | 296 | ||||
-rw-r--r-- | server/peer.go | 77 | ||||
-rw-r--r-- | server/peer_test.go | 18 | ||||
-rw-r--r-- | server/server.go | 39 |
4 files changed, 355 insertions, 75 deletions
diff --git a/server/grpc_server.go b/server/grpc_server.go new file mode 100644 index 00000000..76e79943 --- /dev/null +++ b/server/grpc_server.go @@ -0,0 +1,296 @@ +// Copyright (C) 2014,2015 Nippon Telegraph and Telephone Corporation. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +// implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package server + +import ( + "fmt" + log "github.com/Sirupsen/logrus" + "github.com/osrg/gobgp/api" + "github.com/osrg/gobgp/packet" + "golang.org/x/net/context" + "google.golang.org/grpc" + "io" + "net" +) + +const ( + _ = iota + REQ_NEIGHBOR + REQ_NEIGHBORS + REQ_ADJ_RIB_IN + REQ_ADJ_RIB_OUT + REQ_LOCAL_RIB + REQ_NEIGHBOR_SHUTDOWN + REQ_NEIGHBOR_RESET + REQ_NEIGHBOR_SOFT_RESET + REQ_NEIGHBOR_SOFT_RESET_IN + REQ_NEIGHBOR_SOFT_RESET_OUT + REQ_NEIGHBOR_ENABLE + REQ_NEIGHBOR_DISABLE + REQ_GLOBAL_RIB + REQ_GLOBAL_ADD + REQ_GLOBAL_DELETE +) + +const GRPC_PORT = 8080 + +func convertAf2Rf(af *api.AddressFamily) (bgp.RouteFamily, error) { + if af.Equal(api.AF_IPV4_UC) { + return bgp.RF_IPv4_UC, nil + } else if af.Equal(api.AF_IPV6_UC) { + return bgp.RF_IPv6_UC, nil + } else if af.Equal(api.AF_EVPN) { + return bgp.RF_EVPN, nil + } + + return bgp.RouteFamily(0), fmt.Errorf("unsupported address family: %v", af) +} + +type Server struct { + grpcServer *grpc.Server + bgpServerCh chan *GrpcRequest +} + +func (s *Server) Serve() error { + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", GRPC_PORT)) + if err != nil { + return fmt.Errorf("failed to listen: %v", err) + } + s.grpcServer.Serve(lis) + return nil +} + +func (s *Server) GetNeighbor(ctx context.Context, arg *api.Arguments) (*api.Peer, error) { + var rf bgp.RouteFamily + req := NewGrpcRequest(REQ_NEIGHBOR, arg.RouterId, rf, nil) + s.bgpServerCh <- req + + res := <-req.ResponseCh + if err := res.Err(); err != nil { + log.Debug(err.Error()) + return nil, err + } + + return res.Data.(*api.Peer), nil +} + +func (s *Server) GetNeighbors(_ *api.Arguments, stream api.Grpc_GetNeighborsServer) error { + var rf bgp.RouteFamily + req := NewGrpcRequest(REQ_NEIGHBORS, "", rf, nil) + s.bgpServerCh <- req + + for res := range req.ResponseCh { + if err := res.Err(); err != nil { + log.Debug(err.Error()) + return err + } + if err := stream.Send(res.Data.(*api.Peer)); err != nil { + return err + } + } + + return nil +} + +func (s *Server) GetAdjRib(arg *api.Arguments, stream api.Grpc_GetAdjRibServer) error { + var reqType int + switch arg.Resource { + case api.Resource_ADJ_IN: + reqType = REQ_ADJ_RIB_IN + case api.Resource_ADJ_OUT: + reqType = REQ_ADJ_RIB_OUT + default: + return fmt.Errorf("unsupported resource type: %v", arg.Resource) + } + + rf, err := convertAf2Rf(arg.Af) + if err != nil { + return err + } + + req := NewGrpcRequest(reqType, arg.RouterId, rf, nil) + s.bgpServerCh <- req + + for res := range req.ResponseCh { + if err := res.Err(); err != nil { + log.Debug(err.Error()) + return err + } + if err := stream.Send(res.Data.(*api.Path)); err != nil { + return err + } + } + + return nil +} + +func (s *Server) GetRib(arg *api.Arguments, stream api.Grpc_GetRibServer) error { + var reqType int + switch arg.Resource { + case api.Resource_LOCAL: + reqType = REQ_LOCAL_RIB + case api.Resource_GLOBAL: + reqType = REQ_GLOBAL_RIB + default: + return fmt.Errorf("unsupported resource type: %v", arg.Resource) + } + + rf, err := convertAf2Rf(arg.Af) + if err != nil { + return err + } + + req := NewGrpcRequest(reqType, arg.RouterId, rf, nil) + s.bgpServerCh <- req + + for res := range req.ResponseCh { + if err := res.Err(); err != nil { + log.Debug(err.Error()) + return err + } + if err := stream.Send(res.Data.(*api.Destination)); err != nil { + return err + } + } + return nil +} + +func (s *Server) neighbor(reqType int, arg *api.Arguments) (*api.Error, error) { + rf, err := convertAf2Rf(arg.Af) + if err != nil { + return nil, err + } + + none := &api.Error{} + req := NewGrpcRequest(reqType, arg.RouterId, rf, nil) + s.bgpServerCh <- req + + res := <-req.ResponseCh + if err := res.Err(); err != nil { + log.Debug(err.Error()) + return nil, err + } + return none, nil +} + +func (s *Server) Reset(ctx context.Context, arg *api.Arguments) (*api.Error, error) { + return s.neighbor(REQ_NEIGHBOR_RESET, arg) +} + +func (s *Server) SoftReset(ctx context.Context, arg *api.Arguments) (*api.Error, error) { + return s.neighbor(REQ_NEIGHBOR_SOFT_RESET, arg) +} + +func (s *Server) SoftResetIn(ctx context.Context, arg *api.Arguments) (*api.Error, error) { + return s.neighbor(REQ_NEIGHBOR_SOFT_RESET_IN, arg) +} + +func (s *Server) SoftResetOut(ctx context.Context, arg *api.Arguments) (*api.Error, error) { + return s.neighbor(REQ_NEIGHBOR_SOFT_RESET_OUT, arg) +} + +func (s *Server) Shutdown(ctx context.Context, arg *api.Arguments) (*api.Error, error) { + return s.neighbor(REQ_NEIGHBOR_SHUTDOWN, arg) +} + +func (s *Server) Enable(ctx context.Context, arg *api.Arguments) (*api.Error, error) { + return s.neighbor(REQ_NEIGHBOR_ENABLE, arg) +} + +func (s *Server) Disable(ctx context.Context, arg *api.Arguments) (*api.Error, error) { + return s.neighbor(REQ_NEIGHBOR_DISABLE, arg) +} + +func (s *Server) ModPath(stream api.Grpc_ModPathServer) error { + for { + arg, err := stream.Recv() + + if err == io.EOF { + return nil + } else if err != nil { + return err + } + + if arg.Resource != api.Resource_GLOBAL { + return fmt.Errorf("unsupported resource: %s", arg.Resource) + } + + reqType := REQ_GLOBAL_ADD + if arg.Path.IsWithdraw { + reqType = REQ_GLOBAL_DELETE + } + + rf, err := convertAf2Rf(arg.Path.Nlri.Af) + if err != nil { + return nil + } + req := NewGrpcRequest(reqType, "", rf, arg.Path) + s.bgpServerCh <- req + + res := <-req.ResponseCh + if err := res.Err(); err != nil { + log.Debug(err.Error()) + return err + } + + err = stream.Send(&api.Error{ + Code: api.Error_SUCCESS, + }) + + if err != nil { + return err + } + } +} + +type GrpcRequest struct { + RequestType int + RemoteAddr string + RouteFamily bgp.RouteFamily + ResponseCh chan *GrpcResponse + Err error + Data interface{} +} + +func NewGrpcRequest(reqType int, remoteAddr string, rf bgp.RouteFamily, d interface{}) *GrpcRequest { + r := &GrpcRequest{ + RequestType: reqType, + RouteFamily: rf, + RemoteAddr: remoteAddr, + ResponseCh: make(chan *GrpcResponse), + Data: d, + } + return r +} + +type GrpcResponse struct { + ResponseErr error + Data interface{} +} + +func (r *GrpcResponse) Err() error { + return r.ResponseErr +} + +func NewGrpcServer(port int, bgpServerCh chan *GrpcRequest) *Server { + grpcServer := grpc.NewServer() + server := &Server{ + grpcServer: grpcServer, + bgpServerCh: bgpServerCh, + } + api.RegisterGrpcServer(grpcServer, server) + return server +} diff --git a/server/peer.go b/server/peer.go index 194998fb..2570019f 100644 --- a/server/peer.go +++ b/server/peer.go @@ -288,14 +288,20 @@ func (peer *Peer) sendMessages(msgs []*bgp.BGPMessage) { } } -func (peer *Peer) handleGrpc(grpcReq *api.GrpcRequest) { - result := &api.GrpcResponse{} +func (peer *Peer) handleGrpc(grpcReq *GrpcRequest) { + result := &GrpcResponse{} switch grpcReq.RequestType { - case api.REQ_GLOBAL_ADD, api.REQ_GLOBAL_DELETE: + case REQ_GLOBAL_ADD, REQ_GLOBAL_DELETE: rf := grpcReq.RouteFamily - prefix := grpcReq.Data["prefix"].(string) + path, ok := grpcReq.Data.(*api.Path) + if !ok { + result.ResponseErr = fmt.Errorf("type assertion failed") + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) + return + } var isWithdraw bool - if grpcReq.RequestType == api.REQ_GLOBAL_DELETE { + if grpcReq.RequestType == REQ_GLOBAL_DELETE { isWithdraw = true } @@ -306,9 +312,9 @@ func (peer *Peer) handleGrpc(grpcReq *api.GrpcRequest) { pattr = append(pattr, bgp.NewPathAttributeAsPath([]bgp.AsPathParamInterface{asparam})) if rf == bgp.RF_IPv4_UC { - ip, net, _ := net.ParseCIDR(prefix) + ip, net, _ := net.ParseCIDR(path.Nlri.Prefix) if ip.To4() == nil { - result.ResponseErr = fmt.Errorf("Invalid ipv4 prefix: %s", prefix) + result.ResponseErr = fmt.Errorf("Invalid ipv4 prefix: %s", path.Nlri.Prefix) grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) return @@ -321,9 +327,9 @@ func (peer *Peer) handleGrpc(grpcReq *api.GrpcRequest) { pattr = append(pattr, bgp.NewPathAttributeNextHop("0.0.0.0")) } else if rf == bgp.RF_IPv6_UC { - ip, net, _ := net.ParseCIDR(prefix) + ip, net, _ := net.ParseCIDR(path.Nlri.Prefix) if ip.To16() == nil { - result.ResponseErr = fmt.Errorf("Invalid ipv6 prefix: %s", prefix) + result.ResponseErr = fmt.Errorf("Invalid ipv6 prefix: %s", path.Nlri.Prefix) grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) return @@ -348,53 +354,40 @@ func (peer *Peer) handleGrpc(grpcReq *api.GrpcRequest) { } peer.peerMsgCh <- pm - case api.REQ_LOCAL_RIB, api.REQ_GLOBAL_RIB: + case REQ_LOCAL_RIB, REQ_GLOBAL_RIB: if peer.fsm.adminState == ADMIN_STATE_DOWN { close(grpcReq.ResponseCh) return } if t, ok := peer.rib.Tables[grpcReq.RouteFamily]; ok { - type table struct { - Destinations []*api.Destination - } - var tt table - j, _ := json.Marshal(t) - err := json.Unmarshal(j, &tt) - if err != nil { - result := &api.GrpcResponse{} - result.ResponseErr = err - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) - return - } - for _, dst := range tt.Destinations { - result := &api.GrpcResponse{} - result.Data = dst + for _, dst := range t.GetDestinations() { + result := &GrpcResponse{} + result.Data = dst.ToApiStruct() grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) return } - case api.REQ_NEIGHBOR_SHUTDOWN: + case REQ_NEIGHBOR_SHUTDOWN: peer.outgoing <- bgp.NewBGPNotificationMessage(bgp.BGP_ERROR_CEASE, bgp.BGP_ERROR_SUB_ADMINISTRATIVE_SHUTDOWN, nil) - case api.REQ_NEIGHBOR_RESET: + case REQ_NEIGHBOR_RESET: peer.fsm.idleHoldTime = peer.peerConfig.Timers.IdleHoldTimeAfterReset peer.outgoing <- bgp.NewBGPNotificationMessage(bgp.BGP_ERROR_CEASE, bgp.BGP_ERROR_SUB_ADMINISTRATIVE_RESET, nil) - case api.REQ_NEIGHBOR_SOFT_RESET, api.REQ_NEIGHBOR_SOFT_RESET_IN: + case REQ_NEIGHBOR_SOFT_RESET, REQ_NEIGHBOR_SOFT_RESET_IN: // soft-reconfiguration inbound peer.sendPathsToSiblings(peer.adjRib.GetInPathList(grpcReq.RouteFamily)) - if grpcReq.RequestType == api.REQ_NEIGHBOR_SOFT_RESET_IN { + if grpcReq.RequestType == REQ_NEIGHBOR_SOFT_RESET_IN { break } fallthrough - case api.REQ_NEIGHBOR_SOFT_RESET_OUT: + case REQ_NEIGHBOR_SOFT_RESET_OUT: pathList := peer.adjRib.GetOutPathList(grpcReq.RouteFamily) peer.sendMessages(table.CreateUpdateMsgFromPaths(pathList)) - case api.REQ_ADJ_RIB_IN, api.REQ_ADJ_RIB_OUT: + case REQ_ADJ_RIB_IN, REQ_ADJ_RIB_OUT: rf := grpcReq.RouteFamily var paths []table.Path - if grpcReq.RequestType == api.REQ_ADJ_RIB_IN { + if grpcReq.RequestType == REQ_ADJ_RIB_IN { paths = peer.adjRib.GetInPathList(rf) log.Debugf("RouteFamily=%v adj-rib-in found : %d", rf.String(), len(paths)) } else { @@ -403,7 +396,7 @@ func (peer *Peer) handleGrpc(grpcReq *api.GrpcRequest) { } for _, p := range paths { - result := &api.GrpcResponse{} + result := &GrpcResponse{} path := &api.Path{} j, _ := json.Marshal(p) err := json.Unmarshal(j, path) @@ -416,9 +409,9 @@ func (peer *Peer) handleGrpc(grpcReq *api.GrpcRequest) { } close(grpcReq.ResponseCh) return - case api.REQ_NEIGHBOR_ENABLE, api.REQ_NEIGHBOR_DISABLE: + case REQ_NEIGHBOR_ENABLE, REQ_NEIGHBOR_DISABLE: var err api.Error - if grpcReq.RequestType == api.REQ_NEIGHBOR_ENABLE { + if grpcReq.RequestType == REQ_NEIGHBOR_ENABLE { select { case peer.fsm.adminStateCh <- ADMIN_STATE_UP: log.WithFields(log.Fields{ @@ -656,7 +649,7 @@ func (peer *Peer) handleServerMsg(m *serverMsg) { log.Warning("can not find peer: ", d.Address.String()) } case SRV_MSG_API: - peer.handleGrpc(m.msgData.(*api.GrpcRequest)) + peer.handleGrpc(m.msgData.(*GrpcRequest)) case SRV_MSG_POLICY_UPDATED: log.Debug("policy updated") d := m.msgData.(map[string]*policy.Policy) @@ -827,14 +820,10 @@ func (peer *Peer) PassConn(conn *net.TCPConn) { } func (peer *Peer) MarshalJSON() ([]byte, error) { - p, err := peer.ToGrpc() - if err != nil { - return nil, err - } - return json.Marshal(p) + return json.Marshal(peer.ToApiStruct()) } -func (peer *Peer) ToGrpc() (*api.Peer, error) { +func (peer *Peer) ToApiStruct() *api.Peer { f := peer.fsm c := f.peerConfig @@ -904,5 +893,5 @@ func (peer *Peer) ToGrpc() (*api.Peer, error) { return &api.Peer{ Conf: conf, Info: info, - }, nil + } } diff --git a/server/peer_test.go b/server/peer_test.go index cae3b0c7..ab3bfa54 100644 --- a/server/peer_test.go +++ b/server/peer_test.go @@ -121,7 +121,7 @@ func TestPeerAdminShutdownWhileEstablished(t *testing.T) { peer.connCh <- m waitUntil(assert, bgp.BGP_FSM_ESTABLISHED, peer, 1000) - grpcReq := api.NewGrpcRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) + grpcReq := NewGrpcRequest(REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) msg := &serverMsg{ msgType: SRV_MSG_API, msgData: grpcReq, @@ -163,7 +163,7 @@ func TestPeerAdminShutdownWhileIdle(t *testing.T) { waitUntil(assert, bgp.BGP_FSM_IDLE, peer, 1000) - grpcReq := api.NewGrpcRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) + grpcReq := NewGrpcRequest(REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) msg := &serverMsg{ msgType: SRV_MSG_API, msgData: grpcReq, @@ -197,7 +197,7 @@ func TestPeerAdminShutdownWhileActive(t *testing.T) { waitUntil(assert, bgp.BGP_FSM_ACTIVE, peer, 1000) - grpcReq := api.NewGrpcRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) + grpcReq := NewGrpcRequest(REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) msg := &serverMsg{ msgType: SRV_MSG_API, msgData: grpcReq, @@ -233,7 +233,7 @@ func TestPeerAdminShutdownWhileOpensent(t *testing.T) { peer.connCh <- m waitUntil(assert, bgp.BGP_FSM_OPENSENT, peer, 1000) - grpcReq := api.NewGrpcRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) + grpcReq := NewGrpcRequest(REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) msg := &serverMsg{ msgType: SRV_MSG_API, msgData: grpcReq, @@ -277,7 +277,7 @@ func TestPeerAdminShutdownWhileOpenconfirm(t *testing.T) { peer.connCh <- m waitUntil(assert, bgp.BGP_FSM_OPENCONFIRM, peer, 1000) - grpcReq := api.NewGrpcRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) + grpcReq := NewGrpcRequest(REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) msg := &serverMsg{ msgType: SRV_MSG_API, msgData: grpcReq, @@ -327,7 +327,7 @@ func TestPeerAdminEnable(t *testing.T) { waitUntil(assert, bgp.BGP_FSM_ESTABLISHED, peer, 1000) // shutdown peer at first - grpcReq := api.NewGrpcRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) + grpcReq := NewGrpcRequest(REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) msg := &serverMsg{ msgType: SRV_MSG_API, msgData: grpcReq, @@ -342,7 +342,7 @@ func TestPeerAdminEnable(t *testing.T) { assert.Equal(ADMIN_STATE_DOWN, peer.fsm.adminState) // enable peer - grpcReq = api.NewGrpcRequest(api.REQ_NEIGHBOR_ENABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) + grpcReq = NewGrpcRequest(REQ_NEIGHBOR_ENABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) msg = &serverMsg{ msgType: SRV_MSG_API, msgData: grpcReq, @@ -388,7 +388,7 @@ func TestPeerAdminShutdownReject(t *testing.T) { peer.connCh <- m waitUntil(assert, bgp.BGP_FSM_OPENSENT, peer, 1000) - grpcReq := api.NewGrpcRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) + grpcReq := NewGrpcRequest(REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) msg := &serverMsg{ msgType: SRV_MSG_API, msgData: grpcReq, @@ -401,7 +401,7 @@ func TestPeerAdminShutdownReject(t *testing.T) { err := result.Data.(api.Error) assert.Equal(err.Code, api.Error_FAIL) - grpcReq = api.NewGrpcRequest(api.REQ_NEIGHBOR_ENABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) + grpcReq = NewGrpcRequest(REQ_NEIGHBOR_ENABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil) msg = &serverMsg{ msgType: SRV_MSG_API, msgData: grpcReq, diff --git a/server/server.go b/server/server.go index 8212a1a0..c83c48df 100644 --- a/server/server.go +++ b/server/server.go @@ -18,7 +18,6 @@ package server import ( "fmt" log "github.com/Sirupsen/logrus" - "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/policy" "net" @@ -60,7 +59,7 @@ type BgpServer struct { globalTypeCh chan config.Global addedPeerCh chan config.Neighbor deletedPeerCh chan config.Neighbor - GrpcReqCh chan *api.GrpcRequest + GrpcReqCh chan *GrpcRequest listenPort int peerMap map[string]peerMapInfo globalRib *Peer @@ -73,7 +72,7 @@ func NewBgpServer(port int) *BgpServer { b.globalTypeCh = make(chan config.Global) b.addedPeerCh = make(chan config.Neighbor) b.deletedPeerCh = make(chan config.Neighbor) - b.GrpcReqCh = make(chan *api.GrpcRequest, 1) + b.GrpcReqCh = make(chan *GrpcRequest, 1) b.policyUpdateCh = make(chan config.RoutingPolicy) b.listenPort = port return &b @@ -293,53 +292,49 @@ func (p peers) Less(i, j int) bool { return strings.Less(0, 1) } -func (server *BgpServer) handleGrpc(grpcReq *api.GrpcRequest) { +func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { switch grpcReq.RequestType { - case api.REQ_NEIGHBORS: + case REQ_NEIGHBORS: peerList := peers{} for _, info := range server.peerMap { peerList = append(peerList, info.peer) } sort.Sort(peerList) for _, peer := range peerList { - data, err := peer.ToGrpc() - result := &api.GrpcResponse{ - ResponseErr: err, - Data: data, + result := &GrpcResponse{ + Data: peer.ToApiStruct(), } grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) - case api.REQ_NEIGHBOR: + case REQ_NEIGHBOR: remoteAddr := grpcReq.RemoteAddr - var result *api.GrpcResponse + var result *GrpcResponse info, found := server.peerMap[remoteAddr] if found { - data, err := info.peer.ToGrpc() - result = &api.GrpcResponse{ - ResponseErr: err, - Data: data, + result = &GrpcResponse{ + Data: info.peer.ToApiStruct(), } } else { - result = &api.GrpcResponse{ + result = &GrpcResponse{ ResponseErr: fmt.Errorf("Neighbor that has %v does not exist.", remoteAddr), } } grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) - case api.REQ_GLOBAL_RIB, api.REQ_GLOBAL_ADD, api.REQ_GLOBAL_DELETE: + case REQ_GLOBAL_RIB, REQ_GLOBAL_ADD, REQ_GLOBAL_DELETE: msg := &serverMsg{ msgType: SRV_MSG_API, msgData: grpcReq, } server.globalRib.serverMsgCh <- msg - case api.REQ_LOCAL_RIB, api.REQ_NEIGHBOR_SHUTDOWN, api.REQ_NEIGHBOR_RESET, - api.REQ_NEIGHBOR_SOFT_RESET, api.REQ_NEIGHBOR_SOFT_RESET_IN, api.REQ_NEIGHBOR_SOFT_RESET_OUT, - api.REQ_ADJ_RIB_IN, api.REQ_ADJ_RIB_OUT, - api.REQ_NEIGHBOR_ENABLE, api.REQ_NEIGHBOR_DISABLE: + case REQ_LOCAL_RIB, REQ_NEIGHBOR_SHUTDOWN, REQ_NEIGHBOR_RESET, + REQ_NEIGHBOR_SOFT_RESET, REQ_NEIGHBOR_SOFT_RESET_IN, REQ_NEIGHBOR_SOFT_RESET_OUT, + REQ_ADJ_RIB_IN, REQ_ADJ_RIB_OUT, + REQ_NEIGHBOR_ENABLE, REQ_NEIGHBOR_DISABLE: remoteAddr := grpcReq.RemoteAddr - result := &api.GrpcResponse{} + result := &GrpcResponse{} info, found := server.peerMap[remoteAddr] if found { msg := &serverMsg{ |