diff options
author | Naoto Hanaue <hanaue.naoto@po.ntts.co.jp> | 2015-05-15 15:11:11 +0900 |
---|---|---|
committer | Naoto Hanaue <hanaue.naoto@po.ntts.co.jp> | 2015-05-26 17:28:40 +0900 |
commit | fa5d2fe25ee3670a5ba2e66431dfd617f48737af (patch) | |
tree | 69241afd5e5c39fb3e9896cab546cec836e35cec /server | |
parent | dffb57e43dcdbd419b49fcab089ad4c68342c76a (diff) |
cli: add the add/del commands to neighbor condition
% gobgp -u 10.0.255.1 policy neighbor
Name Address
ns0 10.0.0.2
10.0.0.3
ns1 2001::192:168:0:2
% gobgp -u 10.0.255.1 policy neighbor add ns2 10.0.0.4
% gobgp -u 10.0.255.1 policy neighbor del ns1
% gobgp -u 10.0.255.1 policy neighbor
Name Address
ns0 10.0.0.2
10.0.0.3
ns2 10.0.0.4
Diffstat (limited to 'server')
-rw-r--r-- | server/grpc_server.go | 46 | ||||
-rw-r--r-- | server/server.go | 97 |
2 files changed, 127 insertions, 16 deletions
diff --git a/server/grpc_server.go b/server/grpc_server.go index d8188c56..dbd1be1e 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -51,6 +51,9 @@ const ( REQ_POLICY_PREFIXES_DELETE REQ_POLICY_NEIGHBOR REQ_POLICY_NEIGHBORS + REQ_POLICY_NEIGHBOR_ADD + REQ_POLICY_NEIGHBOR_DELETE + REQ_POLICY_NEIGHBORS_DELETE REQ_POLICY_ROUTEPOLICIES REQ_POLICY_ROUTEPOLICY ) @@ -341,6 +344,7 @@ func (s *Server) getPolicy(arg *api.PolicyArguments) (interface{}, error) { func (s *Server) modPolicy(arg *api.PolicyArguments, stream interface{}) error { var rf bgp.RouteFamily var reqType int + var err error switch arg.Resource { case api.Resource_POLICY_PREFIX: switch arg.Operation { @@ -361,17 +365,38 @@ func (s *Server) modPolicy(arg *api.PolicyArguments, stream interface{}) error { log.Debug(err.Error()) return err } - - err := stream.(api.Grpc_ModPolicyPrefixServer).Send(&api.Error{ + err = stream.(api.Grpc_ModPolicyPrefixServer).Send(&api.Error{ Code: api.Error_SUCCESS, }) + case api.Resource_POLICY_NEIGHBOR: + switch arg.Operation { + case api.Operation_ADD: + reqType = REQ_POLICY_NEIGHBOR_ADD + case api.Operation_DEL: + reqType = REQ_POLICY_NEIGHBOR_DELETE + case api.Operation_DEL_ALL: + reqType = REQ_POLICY_NEIGHBORS_DELETE + default: + return fmt.Errorf("unsupported operation: %s", arg.Operation) + } + req := NewGrpcRequest(reqType, "", rf, arg.NeighborSet) + s.bgpServerCh <- req - if err != nil { + res := <-req.ResponseCh + if err := res.Err(); err != nil { + log.Debug(err.Error()) return err } + err = stream.(api.Grpc_ModPolicyNeighborServer).Send(&api.Error{ + Code: api.Error_SUCCESS, + }) default: return fmt.Errorf("unsupported resource type: %v", arg.Resource) } + + if err != nil { + return err + } return nil } @@ -421,6 +446,21 @@ func (s *Server) GetPolicyNeighbor(ctx context.Context, arg *api.PolicyArguments return data.(*api.NeighborSet), nil } +func (s *Server) ModPolicyNeighbor(stream api.Grpc_ModPolicyNeighborServer) error { + for { + arg, err := stream.Recv() + if err == io.EOF { + return nil + } else if err != nil { + return err + } + if err := s.modPolicy(arg, stream); err != nil { + return err + } + return nil + } +} + func (s *Server) GetPolicyRoutePolicies(arg *api.PolicyArguments, stream api.Grpc_GetPolicyRoutePoliciesServer) error { if err := s.getPolicies(REQ_POLICY_ROUTEPOLICIES, arg, stream); err != nil { return err diff --git a/server/server.go b/server/server.go index cff4dbb2..adc2bc57 100644 --- a/server/server.go +++ b/server/server.go @@ -337,7 +337,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result } } else { - result.ResponseErr = fmt.Errorf("Prefix doesn't exist.") + result.ResponseErr = fmt.Errorf("Policy prefix doesn't exist.") grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -358,7 +358,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } grpcReq.ResponseCh <- result } else { - result.ResponseErr = fmt.Errorf("Prefix that has %v doesn't exist.", name) + result.ResponseErr = fmt.Errorf("Policy prefix that has %v doesn't exist.", name) grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -368,11 +368,10 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { result := &GrpcResponse{} isReqPrefixSet, prefixSet := policy.PrefixSetToConfigStruct(reqPrefixSet) if !isReqPrefixSet { - result.ResponseErr = fmt.Errorf("dose not reqest of policy prefix.") + result.ResponseErr = fmt.Errorf("doesn't reqest of policy prefix.") grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) } - // If the same PrefixSet is not set, add PrefixSet of request to the end. // If only name of the PrefixSet is same, overwrite with PrefixSet of request idxPrefixSet, idxPrefix := policy.IndexOfPrefixSet(conPrefixSetList, prefixSet) @@ -393,16 +392,15 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { result := &GrpcResponse{} isReqPrefixSet, prefixSet := policy.PrefixSetToConfigStruct(reqPrefixSet) if isReqPrefixSet { - // If only name of the PrefixSet is same, delete all of the elements of the PrefixSet. // If the same element PrefixSet, delete the it's element from PrefixSet. idxPrefixSet, idxPrefix := policy.IndexOfPrefixSet(conPrefixSetList, prefixSet) if idxPrefixSet == -1 { - result.ResponseErr = fmt.Errorf("Prefix %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, + result.ResponseErr = fmt.Errorf("Policy prefix that has %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, prefixSet.PrefixList[0].Address, prefixSet.PrefixList[0].Masklength, prefixSet.PrefixList[0].MasklengthRange) } else { if idxPrefix == -1 { - result.ResponseErr = fmt.Errorf("Prefix %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, + result.ResponseErr = fmt.Errorf("Policy prefix that has %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, prefixSet.PrefixList[0].Address, prefixSet.PrefixList[0].Masklength, prefixSet.PrefixList[0].MasklengthRange) } else { copy(conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix:], conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix+1:]) @@ -418,7 +416,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } } if idxPrefixSet == -1 { - result.ResponseErr = fmt.Errorf("Prefix %v doesn't exist.", prefixSet.PrefixSetName) + result.ResponseErr = fmt.Errorf("Policy prefix that has %v doesn't exist.", prefixSet.PrefixSetName) } else { copy(conPrefixSetList[idxPrefixSet:], conPrefixSetList[idxPrefixSet+1:]) conPrefixSetList = conPrefixSetList[:len(conPrefixSetList)-1] @@ -430,8 +428,8 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { close(grpcReq.ResponseCh) case REQ_POLICY_PREFIXES_DELETE: result := &GrpcResponse{} - pl := config.RoutingPolicy{} - server.handlePolicy(pl) + server.routingPolicy.DefinedSets.PrefixSetList = make([]config.PrefixSet, 0) + server.handlePolicy(server.routingPolicy) grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) case REQ_POLICY_NEIGHBORS: @@ -446,7 +444,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result } } else { - result.ResponseErr = fmt.Errorf("Neighbor doesn't exist.") + result.ResponseErr = fmt.Errorf("Policy neighbor doesn't exist.") grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -467,9 +465,82 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } grpcReq.ResponseCh <- result } else { - result.ResponseErr = fmt.Errorf("Neighbor that has %v doesn't exist.", name) + result.ResponseErr = fmt.Errorf("Policy neighbor that has %v doesn't exist.", name) + grpcReq.ResponseCh <- result + } + close(grpcReq.ResponseCh) + case REQ_POLICY_NEIGHBOR_ADD: + reqNeighborSet := grpcReq.Data.(*api.NeighborSet) + conNeighborSetList := server.routingPolicy.DefinedSets.NeighborSetList + result := &GrpcResponse{} + isReqNeighborSet, neighborSet := policy.NeighborSetToConfigStruct(reqNeighborSet) + if !isReqNeighborSet { + result.ResponseErr = fmt.Errorf("doesn't reqest of policy neighbor.") grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) + } + // If the same NeighborSet is not set, add NeighborSet of request to the end. + // If only name of the NeighborSet is same, overwrite with NeighborSet of request + idxNeighborSet, idxNeighbor := policy.IndexOfNeighborSet(conNeighborSetList, neighborSet) + if idxNeighborSet == -1 { + conNeighborSetList = append(conNeighborSetList, neighborSet) + } else { + if idxNeighbor == -1 { + conNeighborSetList[idxNeighborSet].NeighborInfoList = + append(conNeighborSetList[idxNeighborSet].NeighborInfoList, neighborSet.NeighborInfoList[0]) + } + } + server.routingPolicy.DefinedSets.NeighborSetList = conNeighborSetList + server.handlePolicy(server.routingPolicy) + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) + case REQ_POLICY_NEIGHBOR_DELETE: + reqNeighborSet := grpcReq.Data.(*api.NeighborSet) + conNeighborSetList := server.routingPolicy.DefinedSets.NeighborSetList + result := &GrpcResponse{} + isReqNeighborSet, neighborSet := policy.NeighborSetToConfigStruct(reqNeighborSet) + if isReqNeighborSet { + // If only name of the NeighborSet is same, delete all of the elements of the NeighborSet. + // If the same element NeighborSet, delete the it's element from NeighborSet. + idxNeighborSet, idxNeighbor := policy.IndexOfNeighborSet(conNeighborSetList, neighborSet) + if idxNeighborSet == -1 { + result.ResponseErr = fmt.Errorf("Policy neighbor that has %v %v doesn't exist.", neighborSet.NeighborSetName, + neighborSet.NeighborInfoList[0].Address) + } else { + if idxNeighbor == -1 { + result.ResponseErr = fmt.Errorf("Policy neighbor that has %v %v doesn't exist.", neighborSet.NeighborSetName, + neighborSet.NeighborInfoList[0].Address) + } else { + copy(conNeighborSetList[idxNeighborSet].NeighborInfoList[idxNeighbor:], + conNeighborSetList[idxNeighborSet].NeighborInfoList[idxNeighbor+1:]) + conNeighborSetList[idxNeighborSet].NeighborInfoList = + conNeighborSetList[idxNeighborSet].NeighborInfoList[:len(conNeighborSetList[idxNeighborSet].NeighborInfoList)-1] + } + } + } else { + idxNeighborSet := -1 + for i, conNeighborSet := range conNeighborSetList { + if conNeighborSet.NeighborSetName == reqNeighborSet.NeighborSetName { + idxNeighborSet = i + break + } + } + if idxNeighborSet == -1 { + result.ResponseErr = fmt.Errorf("Policy neighbor %v doesn't exist.", neighborSet.NeighborSetName) + } else { + copy(conNeighborSetList[idxNeighborSet:], conNeighborSetList[idxNeighborSet+1:]) + conNeighborSetList = conNeighborSetList[:len(conNeighborSetList)-1] + } } + server.routingPolicy.DefinedSets.NeighborSetList = conNeighborSetList + server.handlePolicy(server.routingPolicy) + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) + case REQ_POLICY_NEIGHBORS_DELETE: + result := &GrpcResponse{} + server.routingPolicy.DefinedSets.NeighborSetList = make([]config.NeighborSet, 0) + server.handlePolicy(server.routingPolicy) + grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) case REQ_POLICY_ROUTEPOLICIES: info := server.routingPolicy.PolicyDefinitionList @@ -484,7 +555,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result } } else { - result.ResponseErr = fmt.Errorf("Route Policy doesn't exist.") + result.ResponseErr = fmt.Errorf("Route Policy doesn't exist.") grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) |