diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-10-18 14:10:46 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-10-20 10:33:10 +0900 |
commit | 7cb50ce1cfdf959a74dad516ef65a6c6c34aaec8 (patch) | |
tree | 188236c04337a475078ec2e57932c3e47c8c6154 /server | |
parent | 29f9682ecbe0e5bf0a44fdcc2e9f8cec3f6ea31c (diff) |
api/cli: add api to retrieve statements and support showing them via cli
$ gobgp policy statement
$ gobgp policy statement st0
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'server')
-rw-r--r-- | server/grpc_server.go | 52 | ||||
-rw-r--r-- | server/server.go | 29 |
2 files changed, 65 insertions, 16 deletions
diff --git a/server/grpc_server.go b/server/grpc_server.go index 1ed56144..6753417b 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -61,6 +61,7 @@ const ( REQ_GLOBAL_POLICY REQ_DEFINED_SET REQ_MOD_DEFINED_SET + REQ_STATEMENT ) const GRPC_PORT = 8080 @@ -419,11 +420,20 @@ func (s *Server) GetVrfs(arg *api.Arguments, stream api.GobgpApi_GetVrfsServer) }) } -func (s *Server) ModVrf(ctx context.Context, arg *api.ModVrfArguments) (*api.Error, error) { - none := &api.Error{} - req := NewGrpcRequest(REQ_VRF_MOD, "", bgp.RouteFamily(0), arg) +func (s *Server) get(typ int, d interface{}) (interface{}, error) { + req := NewGrpcRequest(typ, "", bgp.RouteFamily(0), d) s.bgpServerCh <- req + res := <-req.ResponseCh + if err := res.Err(); err != nil { + return nil, err + } + return res.Data, nil +} +func (s *Server) mod(typ int, d interface{}) (*api.Error, error) { + none := &api.Error{} + req := NewGrpcRequest(typ, "", bgp.RouteFamily(0), d) + s.bgpServerCh <- req res := <-req.ResponseCh if err := res.Err(); err != nil { return none, err @@ -431,34 +441,44 @@ func (s *Server) ModVrf(ctx context.Context, arg *api.ModVrfArguments) (*api.Err return none, nil } +func (s *Server) ModVrf(ctx context.Context, arg *api.ModVrfArguments) (*api.Error, error) { + return s.mod(REQ_VRF_MOD, arg) +} + func (s *Server) GetDefinedSet(ctx context.Context, arg *api.DefinedSet) (*api.DefinedSet, error) { - req := NewGrpcRequest(REQ_DEFINED_SET, "", bgp.RouteFamily(0), arg) - s.bgpServerCh <- req - res := <-req.ResponseCh - if err := res.Err(); err != nil { + d, err := s.get(REQ_DEFINED_SET, arg) + if err != nil { return nil, err } - return res.Data.(*api.DefinedSet), nil + return d.(*api.DefinedSet), nil } func (s *Server) GetDefinedSets(arg *api.DefinedSet, stream api.GobgpApi_GetDefinedSetsServer) error { req := NewGrpcRequest(REQ_DEFINED_SET, "", bgp.RouteFamily(0), arg) s.bgpServerCh <- req - return handleMultipleResponses(req, func(res *GrpcResponse) error { return stream.Send(res.Data.(*api.DefinedSet)) }) } func (s *Server) ModDefinedSet(ctx context.Context, arg *api.ModDefinedSetArguments) (*api.Error, error) { - none := &api.Error{} - req := NewGrpcRequest(REQ_MOD_DEFINED_SET, "", bgp.RouteFamily(0), arg) - s.bgpServerCh <- req - res := <-req.ResponseCh - if err := res.Err(); err != nil { - return none, err + return s.mod(REQ_MOD_DEFINED_SET, arg) +} + +func (s *Server) GetStatement(ctx context.Context, arg *api.Statement) (*api.Statement, error) { + d, err := s.get(REQ_STATEMENT, arg) + if err != nil { + return nil, err } - return none, nil + return d.(*api.Statement), nil +} + +func (s *Server) GetStatements(arg *api.Statement, stream api.GobgpApi_GetStatementsServer) error { + req := NewGrpcRequest(REQ_STATEMENT, "", bgp.RouteFamily(0), arg) + s.bgpServerCh <- req + return handleMultipleResponses(req, func(res *GrpcResponse) error { + return stream.Send(res.Data.(*api.Statement)) + }) } type GrpcRequest struct { diff --git a/server/server.go b/server/server.go index 18c2693e..e25d314f 100644 --- a/server/server.go +++ b/server/server.go @@ -1643,6 +1643,13 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { ResponseErr: err, } close(grpcReq.ResponseCh) + case REQ_STATEMENT: + if err := server.handleGrpcGetStatement(grpcReq); err != nil { + grpcReq.ResponseCh <- &GrpcResponse{ + ResponseErr: err, + } + } + close(grpcReq.ResponseCh) case REQ_POLICY_ROUTEPOLICY, REQ_POLICY_ROUTEPOLICIES: info := server.policy.PolicyMap typ := grpcReq.RequestType @@ -1754,6 +1761,28 @@ func (server *BgpServer) handleGrpcModDefinedSet(grpcReq *GrpcRequest) error { return err } +func (server *BgpServer) handleGrpcGetStatement(grpcReq *GrpcRequest) error { + arg := grpcReq.Data.(*api.Statement) + name := arg.Name + found := false + for _, s := range server.policy.StatementMap { + if name != "" && name != s.Name { + continue + } + grpcReq.ResponseCh <- &GrpcResponse{ + Data: s.ToApiStruct(), + } + found = true + if name != "" { + break + } + } + if !found { + return fmt.Errorf("not found %s", name) + } + return nil +} + func (server *BgpServer) handleMrt(grpcReq *GrpcRequest) { now := uint32(time.Now().Unix()) view := "" |