From 722975bfafdd723160dced623936c361475c9bff Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Fri, 16 Oct 2015 15:04:32 +0900 Subject: policy: another cleanup Signed-off-by: ISHIDA Wataru --- server/grpc_server.go | 50 +++++------- server/server.go | 215 +++++++++++++------------------------------------- 2 files changed, 74 insertions(+), 191 deletions(-) (limited to 'server') diff --git a/server/grpc_server.go b/server/grpc_server.go index 16b501ec..c3f004df 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -43,18 +43,12 @@ const ( REQ_NEIGHBOR_POLICY REQ_MOD_NEIGHBOR_POLICY REQ_GLOBAL_RIB - REQ_POLICY_PREFIX - REQ_POLICY_PREFIXES REQ_POLICY_PREFIX_ADD REQ_POLICY_PREFIX_DELETE REQ_POLICY_PREFIXES_DELETE - REQ_POLICY_NEIGHBOR - REQ_POLICY_NEIGHBORS REQ_POLICY_NEIGHBOR_ADD REQ_POLICY_NEIGHBOR_DELETE REQ_POLICY_NEIGHBORS_DELETE - REQ_POLICY_ASPATH - REQ_POLICY_ASPATHS REQ_POLICY_ASPATH_ADD REQ_POLICY_ASPATH_DELETE REQ_POLICY_ASPATHS_DELETE @@ -63,13 +57,9 @@ const ( REQ_POLICY_ROUTEPOLICY_ADD REQ_POLICY_ROUTEPOLICY_DELETE REQ_POLICY_ROUTEPOLICIES_DELETE - REQ_POLICY_COMMUNITY - REQ_POLICY_COMMUNITIES REQ_POLICY_COMMUNITY_ADD REQ_POLICY_COMMUNITY_DELETE REQ_POLICY_COMMUNITIES_DELETE - REQ_POLICY_EXTCOMMUNITY - REQ_POLICY_EXTCOMMUNITIES REQ_POLICY_EXTCOMMUNITY_ADD REQ_POLICY_EXTCOMMUNITY_DELETE REQ_POLICY_EXTCOMMUNITIES_DELETE @@ -84,6 +74,7 @@ const ( REQ_VRF_MOD REQ_MOD_PATH REQ_GLOBAL_POLICY + REQ_DEFINED_SET ) const GRPC_PORT = 8080 @@ -406,16 +397,6 @@ func (s *Server) GetPolicyRoutePolicies(arg *api.PolicyArguments, stream api.Gob var rf bgp.RouteFamily var reqType int switch arg.Resource { - case api.Resource_POLICY_PREFIX: - reqType = REQ_POLICY_PREFIXES - case api.Resource_POLICY_NEIGHBOR: - reqType = REQ_POLICY_NEIGHBORS - case api.Resource_POLICY_ASPATH: - reqType = REQ_POLICY_ASPATHS - case api.Resource_POLICY_COMMUNITY: - reqType = REQ_POLICY_COMMUNITIES - case api.Resource_POLICY_EXTCOMMUNITY: - reqType = REQ_POLICY_EXTCOMMUNITIES case api.Resource_POLICY_ROUTEPOLICY: reqType = REQ_POLICY_ROUTEPOLICIES default: @@ -432,16 +413,6 @@ func (s *Server) GetPolicyRoutePolicy(ctx context.Context, arg *api.PolicyArgume var rf bgp.RouteFamily var reqType int switch arg.Resource { - case api.Resource_POLICY_PREFIX: - reqType = REQ_POLICY_PREFIX - case api.Resource_POLICY_NEIGHBOR: - reqType = REQ_POLICY_NEIGHBOR - case api.Resource_POLICY_ASPATH: - reqType = REQ_POLICY_ASPATH - case api.Resource_POLICY_COMMUNITY: - reqType = REQ_POLICY_COMMUNITY - case api.Resource_POLICY_EXTCOMMUNITY: - reqType = REQ_POLICY_EXTCOMMUNITY case api.Resource_POLICY_ROUTEPOLICY: reqType = REQ_POLICY_ROUTEPOLICY default: @@ -529,6 +500,25 @@ func (s *Server) ModVrf(ctx context.Context, arg *api.ModVrfArguments) (*api.Err return none, nil } +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 { + return nil, err + } + return res.Data.(*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)) + }) +} + type GrpcRequest struct { RequestType int Name string diff --git a/server/server.go b/server/server.go index 9f9b70d8..f60d4d6e 100644 --- a/server/server.go +++ b/server/server.go @@ -1580,28 +1580,14 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { def = api.RouteAction_ACCEPT } } - policies := make([]*api.PolicyDefinition, 0, len(names)) - for _, name := range names { - p, ok := server.policy.PolicyMap[name] - if !ok { - log.WithFields(log.Fields{ - "Topic": "Peer", - "Name": name, - }).Error("can't find policy which is applied") - continue - } - policies = append(policies, p.ToApiStruct()) - } - result := &GrpcResponse{ Data: &api.ApplyPolicy{ - Policies: policies, + Policies: names, Default: def, }, } grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) - case REQ_MOD_NEIGHBOR_POLICY: peer, err := server.checkNeighborRequest(grpcReq) if err != nil { @@ -1619,37 +1605,62 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { switch arg.ApplyPolicy.Type { case api.PolicyType_IMPORT: applyPolicy.DefaultImportPolicy = def - applyPolicy.ImportPolicy = table.PoliciesToString(arg.ApplyPolicy.Policies) + applyPolicy.ImportPolicy = arg.ApplyPolicy.Policies case api.PolicyType_EXPORT: applyPolicy.DefaultExportPolicy = def - applyPolicy.ExportPolicy = table.PoliciesToString(arg.ApplyPolicy.Policies) + applyPolicy.ExportPolicy = arg.ApplyPolicy.Policies case api.PolicyType_IN: applyPolicy.DefaultInPolicy = def - applyPolicy.InPolicy = table.PoliciesToString(arg.ApplyPolicy.Policies) + applyPolicy.InPolicy = arg.ApplyPolicy.Policies } case api.Operation_DEL: switch arg.ApplyPolicy.Type { case api.PolicyType_IMPORT: applyPolicy.DefaultImportPolicy = def - applyPolicy.ImportPolicy = []string{} + applyPolicy.ImportPolicy = nil case api.PolicyType_EXPORT: applyPolicy.DefaultExportPolicy = def - applyPolicy.ExportPolicy = []string{} + applyPolicy.ExportPolicy = nil case api.PolicyType_IN: applyPolicy.DefaultInPolicy = def - applyPolicy.InPolicy = []string{} + applyPolicy.InPolicy = nil } } peer.setPolicy(server.policy.PolicyMap) grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) - - case REQ_POLICY_PREFIXES, REQ_POLICY_NEIGHBORS, REQ_POLICY_ASPATHS, - REQ_POLICY_COMMUNITIES, REQ_POLICY_EXTCOMMUNITIES, REQ_POLICY_ROUTEPOLICIES, - REQ_POLICY_PREFIX, REQ_POLICY_NEIGHBOR, REQ_POLICY_ASPATH, - REQ_POLICY_COMMUNITY, REQ_POLICY_EXTCOMMUNITY, REQ_POLICY_ROUTEPOLICY: - server.handleGrpcShowPolicy(grpcReq) + case REQ_DEFINED_SET: + if err := server.handleGrpcGetDefinedSet(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 + arg := grpcReq.Data.(*api.PolicyArguments) + result := &GrpcResponse{} + if len(info) > 0 { + for _, i := range info { + if typ == REQ_POLICY_ROUTEPOLICY && i.Name() != arg.Name { + continue + } + d := i.ToApiStruct() + result = &GrpcResponse{ + Data: d, + } + grpcReq.ResponseCh <- result + if typ == REQ_POLICY_ROUTEPOLICY { + break + } + } + } else { + result.ResponseErr = fmt.Errorf("Route Policy doesn't exist.") + grpcReq.ResponseCh <- result + } + close(grpcReq.ResponseCh) case REQ_MONITOR_GLOBAL_BEST_CHANGED, REQ_MONITOR_NEIGHBOR_PEER_STATE: server.broadcastReqs = append(server.broadcastReqs, grpcReq) case REQ_MRT_GLOBAL_RIB, REQ_MRT_LOCAL_RIB: @@ -1672,144 +1683,26 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { return msgs } -func (server *BgpServer) handleGrpcShowPolicy(grpcReq *GrpcRequest) { - result := &GrpcResponse{} - typ := grpcReq.RequestType - arg := grpcReq.Data.(*api.PolicyArguments) - switch typ { - case REQ_POLICY_PREFIX, REQ_POLICY_PREFIXES: - info := server.policy.DefinedSetMap[table.DEFINED_TYPE_PREFIX] - if len(info) > 0 { - for _, ps := range info { - if typ == REQ_POLICY_PREFIX && ps.Name() != arg.Name { - continue - } - resPrefixSet := ps.(*table.PrefixSet).ToApiStruct() - pd := &api.PolicyDefinition{} - pd.Statements = []*api.Statement{{Conditions: &api.Conditions{PrefixSet: resPrefixSet}}} - result = &GrpcResponse{ - Data: pd, - } - grpcReq.ResponseCh <- result - if typ == REQ_POLICY_PREFIX { - break - } - } - } else { - result.ResponseErr = fmt.Errorf("Policy prefix doesn't exist.") - grpcReq.ResponseCh <- result - } - case REQ_POLICY_NEIGHBOR, REQ_POLICY_NEIGHBORS: - info := server.policy.DefinedSetMap[table.DEFINED_TYPE_NEIGHBOR] - if len(info) > 0 { - for _, ns := range info { - if typ == REQ_POLICY_NEIGHBOR && ns.Name() != arg.Name { - continue - } - resNeighborSet := ns.(*table.NeighborSet).ToApiStruct() - pd := &api.PolicyDefinition{} - pd.Statements = []*api.Statement{{Conditions: &api.Conditions{NeighborSet: resNeighborSet}}} - result = &GrpcResponse{ - Data: pd, - } - grpcReq.ResponseCh <- result - if typ == REQ_POLICY_NEIGHBOR { - break - } - } - } else { - result.ResponseErr = fmt.Errorf("Policy neighbor doesn't exist.") - grpcReq.ResponseCh <- result - } - case REQ_POLICY_ASPATH, REQ_POLICY_ASPATHS: - info := server.policy.DefinedSetMap[table.DEFINED_TYPE_AS_PATH] - if len(info) > 0 { - for _, as := range info { - if typ == REQ_POLICY_ASPATH && as.Name() != arg.Name { - continue - } - resAsPathSet := as.(*table.AsPathSet).ToApiStruct() - pd := &api.PolicyDefinition{} - pd.Statements = []*api.Statement{{Conditions: &api.Conditions{AsPathSet: resAsPathSet}}} - result = &GrpcResponse{ - Data: pd, - } - grpcReq.ResponseCh <- result - if typ == REQ_POLICY_ASPATH { - break - } - } - } else { - result.ResponseErr = fmt.Errorf("Policy aspath doesn't exist.") - grpcReq.ResponseCh <- result - } - case REQ_POLICY_COMMUNITY, REQ_POLICY_COMMUNITIES: - info := server.policy.DefinedSetMap[table.DEFINED_TYPE_COMMUNITY] - if len(info) > 0 { - for _, cs := range info { - if typ == REQ_POLICY_COMMUNITY && cs.Name() != arg.Name { - continue - } - resCommunitySet := cs.(*table.CommunitySet).ToApiStruct() - pd := &api.PolicyDefinition{} - pd.Statements = []*api.Statement{{Conditions: &api.Conditions{CommunitySet: resCommunitySet}}} - result = &GrpcResponse{ - Data: pd, - } - grpcReq.ResponseCh <- result - if typ == REQ_POLICY_COMMUNITY { - break - } - } - } else { - result.ResponseErr = fmt.Errorf("Policy community doesn't exist.") - grpcReq.ResponseCh <- result +func (server *BgpServer) handleGrpcGetDefinedSet(grpcReq *GrpcRequest) error { + arg := grpcReq.Data.(*api.DefinedSet) + typ := table.DefinedType(arg.Type) + name := arg.Name + set, ok := server.policy.DefinedSetMap[typ] + if !ok { + return fmt.Errorf("invalid defined-set type: %d", typ) + } + for _, s := range set { + if name != "" && name != s.Name() { + continue } - case REQ_POLICY_EXTCOMMUNITY, REQ_POLICY_EXTCOMMUNITIES: - info := server.policy.DefinedSetMap[table.DEFINED_TYPE_EXT_COMMUNITY] - if len(info) > 0 { - for _, es := range info { - if typ == REQ_POLICY_EXTCOMMUNITY && es.Name() != arg.Name { - continue - } - resExtcommunitySet := es.(*table.ExtCommunitySet).ToApiStruct() - pd := &api.PolicyDefinition{} - pd.Statements = []*api.Statement{{Conditions: &api.Conditions{ExtCommunitySet: resExtcommunitySet}}} - result = &GrpcResponse{ - Data: pd, - } - grpcReq.ResponseCh <- result - if typ == REQ_POLICY_EXTCOMMUNITY { - break - } - } - } else { - result.ResponseErr = fmt.Errorf("Policy extended community doesn't exist.") - grpcReq.ResponseCh <- result + grpcReq.ResponseCh <- &GrpcResponse{ + Data: s.ToApiStruct(), } - case REQ_POLICY_ROUTEPOLICY, REQ_POLICY_ROUTEPOLICIES: - info := server.policy.PolicyMap - result := &GrpcResponse{} - if len(info) > 0 { - for _, pd := range info { - if typ == REQ_POLICY_ROUTEPOLICY && pd.Name() != arg.Name { - continue - } - resPolicyDefinition := pd.ToApiStruct() - result = &GrpcResponse{ - Data: resPolicyDefinition, - } - grpcReq.ResponseCh <- result - if typ == REQ_POLICY_ROUTEPOLICY { - break - } - } - } else { - result.ResponseErr = fmt.Errorf("Route Policy doesn't exist.") - grpcReq.ResponseCh <- result + if name != "" { + break } } - close(grpcReq.ResponseCh) + return nil } func (server *BgpServer) handleMrt(grpcReq *GrpcRequest) { -- cgit v1.2.3