diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/grpc_server.go | 21 | ||||
-rw-r--r-- | server/server.go | 52 |
2 files changed, 73 insertions, 0 deletions
diff --git a/server/grpc_server.go b/server/grpc_server.go index cc0a0e16..2965d647 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -48,6 +48,8 @@ const ( REQ_POLICY_PREFIX_ADD REQ_POLICY_PREFIX_DELETE REQ_POLICY_PREFIXES_DELETE + REQ_POLICY_NEIGHBOR + REQ_POLICY_NEIGHBORS ) const GRPC_PORT = 8080 @@ -277,6 +279,8 @@ func (s *Server) getPolicies(reqType int, arg *api.PolicyArguments, stream inter switch arg.Resource { case api.Resource_POLICY_PREFIX: err = stream.(api.Grpc_GetPolicyPrefixesServer).Send(res.Data.(*api.PrefixSet)) + case api.Resource_POLICY_NEIGHBOR: + err = stream.(api.Grpc_GetPolicyNeighborsServer).Send(res.Data.(*api.NeighborSet)) default: return fmt.Errorf("unsupported resource type: %v", arg.Resource) } @@ -293,6 +297,8 @@ func (s *Server) getPolicy(arg *api.PolicyArguments) (interface{}, error) { switch arg.Resource { case api.Resource_POLICY_PREFIX: reqType = REQ_POLICY_PREFIX + case api.Resource_POLICY_NEIGHBOR: + reqType = REQ_POLICY_NEIGHBOR default: return nil, fmt.Errorf("unsupported resource type: %v", arg.Resource) } @@ -375,6 +381,21 @@ func (s *Server) ModPolicyPrefix(stream api.Grpc_ModPolicyPrefixServer) error { } } +func (s *Server) GetPolicyNeighbors(arg *api.PolicyArguments, stream api.Grpc_GetPolicyNeighborsServer) error { + if err := s.getPolicies(REQ_POLICY_NEIGHBORS, arg, stream); err != nil { + return err + } + return nil +} + +func (s *Server) GetPolicyNeighbor(ctx context.Context, arg *api.PolicyArguments) (*api.NeighborSet, error) { + data, err := s.getPolicy(arg) + if err != nil { + return nil, err + } + return data.(*api.NeighborSet), nil +} + type GrpcRequest struct { RequestType int RemoteAddr string diff --git a/server/server.go b/server/server.go index 443a4ba2..5a847045 100644 --- a/server/server.go +++ b/server/server.go @@ -429,6 +429,43 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { server.handlePolicy(pl) grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) + case REQ_POLICY_NEIGHBORS: + info := server.routingPolicy.DefinedSets.NeighborSetList + result := &GrpcResponse{} + if len(info) > 0 { + for _, ns := range info { + resNeighborSet := neighborToApiStruct(ns) + result = &GrpcResponse{ + Data: resNeighborSet, + } + grpcReq.ResponseCh <- result + } + } else { + result.ResponseErr = fmt.Errorf("Policy Neighbor is not exist.") + grpcReq.ResponseCh <- result + } + close(grpcReq.ResponseCh) + case REQ_POLICY_NEIGHBOR: + name := grpcReq.Data.(string) + info := server.routingPolicy.DefinedSets.NeighborSetList + result := &GrpcResponse{} + resNeighborSet := &api.NeighborSet{} + for _, ns := range info { + if ns.NeighborSetName == name { + resNeighborSet = neighborToApiStruct(ns) + break + } + } + if len(resNeighborSet.NeighborList) > 0 { + result = &GrpcResponse{ + Data: resNeighborSet, + } + grpcReq.ResponseCh <- result + } else { + result.ResponseErr = fmt.Errorf("Policy Neighbor that has %v does not exist.", name) + grpcReq.ResponseCh <- result + } + close(grpcReq.ResponseCh) } } @@ -492,3 +529,18 @@ func prefixToConfigStruct(reqPrefixSet *api.PrefixSet) (bool, config.PrefixSet) } return isReqPrefixSet, prefixSet } + +func neighborToApiStruct(ns config.NeighborSet) *api.NeighborSet { + resNeighborList := make([]*api.Neighbor, 0) + for _, n := range ns.NeighborInfoList { + resNeighbor := &api.Neighbor{ + Address: n.Address.String(), + } + resNeighborList = append(resNeighborList, resNeighbor) + } + resNeighborSet := &api.NeighborSet{ + NeighborSetName: ns.NeighborSetName, + NeighborList: resNeighborList, + } + return resNeighborSet +} |