From eb67afbdad499466e9407b4b30e9854664dd0486 Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Sun, 4 Oct 2015 15:18:12 +0900 Subject: api: make enum RouteAction to avoid using raw string Signed-off-by: ISHIDA Wataru --- api/gobgp.pb.go | 32 ++++++++++++++++++++++++++++---- api/gobgp.proto | 14 ++++++++++---- gobgp/cmd/neighbor.go | 2 +- gobgp/cmd/policy.go | 12 ++++++------ policy/policy.go | 8 ++++---- server/server.go | 20 ++++++++++---------- 6 files changed, 59 insertions(+), 29 deletions(-) diff --git a/api/gobgp.pb.go b/api/gobgp.pb.go index 6d60411c..07e42a19 100644 --- a/api/gobgp.pb.go +++ b/api/gobgp.pb.go @@ -128,6 +128,29 @@ func (x Operation) String() string { return proto.EnumName(Operation_name, int32(x)) } +type RouteAction int32 + +const ( + RouteAction_NONE RouteAction = 0 + RouteAction_ACCEPT RouteAction = 1 + RouteAction_REJECT RouteAction = 2 +) + +var RouteAction_name = map[int32]string{ + 0: "NONE", + 1: "ACCEPT", + 2: "REJECT", +} +var RouteAction_value = map[string]int32{ + "NONE": 0, + "ACCEPT": 1, + "REJECT": 2, +} + +func (x RouteAction) String() string { + return proto.EnumName(RouteAction_name, int32(x)) +} + type Error_ErrorCode int32 const ( @@ -507,7 +530,7 @@ func (m *AsPrependAction) String() string { return proto.CompactTextString(m) } func (*AsPrependAction) ProtoMessage() {} type Actions struct { - RouteAction string `protobuf:"bytes,1,opt,name=route_action" json:"route_action,omitempty"` + RouteAction RouteAction `protobuf:"varint,1,opt,name=route_action,enum=gobgpapi.RouteAction" json:"route_action,omitempty"` Community *CommunityAction `protobuf:"bytes,2,opt,name=community" json:"community,omitempty"` Med string `protobuf:"bytes,3,opt,name=med" json:"med,omitempty"` AsPrepend *AsPrependAction `protobuf:"bytes,4,opt,name=as_prepend" json:"as_prepend,omitempty"` @@ -581,11 +604,11 @@ func (m *PolicyDefinition) GetStatementList() []*Statement { type ApplyPolicy struct { ImportPolicies []*PolicyDefinition `protobuf:"bytes,1,rep,name=import_policies" json:"import_policies,omitempty"` - DefaultImportPolicy string `protobuf:"bytes,2,opt,name=default_import_policy" json:"default_import_policy,omitempty"` + DefaultImportPolicy RouteAction `protobuf:"varint,2,opt,name=default_import_policy,enum=gobgpapi.RouteAction" json:"default_import_policy,omitempty"` ExportPolicies []*PolicyDefinition `protobuf:"bytes,3,rep,name=export_policies" json:"export_policies,omitempty"` - DefaultExportPolicy string `protobuf:"bytes,4,opt,name=default_export_policy" json:"default_export_policy,omitempty"` + DefaultExportPolicy RouteAction `protobuf:"varint,4,opt,name=default_export_policy,enum=gobgpapi.RouteAction" json:"default_export_policy,omitempty"` InPolicies []*PolicyDefinition `protobuf:"bytes,5,rep,name=in_policies" json:"in_policies,omitempty"` - DefaultInPolicy string `protobuf:"bytes,6,opt,name=default_in_policy" json:"default_in_policy,omitempty"` + DefaultInPolicy RouteAction `protobuf:"varint,6,opt,name=default_in_policy,enum=gobgpapi.RouteAction" json:"default_in_policy,omitempty"` } func (m *ApplyPolicy) Reset() { *m = ApplyPolicy{} } @@ -688,6 +711,7 @@ func (*Vrf) ProtoMessage() {} func init() { proto.RegisterEnum("gobgpapi.Resource", Resource_name, Resource_value) proto.RegisterEnum("gobgpapi.Operation", Operation_name, Operation_value) + proto.RegisterEnum("gobgpapi.RouteAction", RouteAction_name, RouteAction_value) proto.RegisterEnum("gobgpapi.Error_ErrorCode", Error_ErrorCode_name, Error_ErrorCode_value) } diff --git a/api/gobgp.proto b/api/gobgp.proto index ec480470..cc58a3d9 100644 --- a/api/gobgp.proto +++ b/api/gobgp.proto @@ -234,8 +234,14 @@ message AsPrependAction { uint32 repeatn = 2; } +enum RouteAction { + NONE = 0; + ACCEPT = 1; + REJECT = 2; +} + message Actions { - string route_action = 1; + RouteAction route_action = 1; CommunityAction community = 2; string med = 3; AsPrependAction as_prepend = 4; @@ -255,11 +261,11 @@ message PolicyDefinition { message ApplyPolicy { repeated PolicyDefinition import_policies = 1; - string default_import_policy = 2; + RouteAction default_import_policy = 2; repeated PolicyDefinition export_policies = 3; - string default_export_policy = 4; + RouteAction default_export_policy = 4; repeated PolicyDefinition in_policies = 5; - string default_in_policy = 6; + RouteAction default_in_policy = 6; } message MrtMessage { diff --git a/gobgp/cmd/neighbor.go b/gobgp/cmd/neighbor.go index 79717cbf..ba3ca03b 100644 --- a/gobgp/cmd/neighbor.go +++ b/gobgp/cmd/neighbor.go @@ -668,7 +668,7 @@ func modNeighborPolicy(remoteIP net.IP, cmdType string, eArg []string) error { switch cmdType { case CMD_ADD: if len(eArg) < 4 { - return fmt.Errorf("Usage: gobgp neighbor policy %s {%s|%s|%s} {%s|%s}", cmdType, CMD_IMPORT, CMD_EXPORT, CMD_IN, policy.ROUTE_ACCEPT, policy.ROUTE_REJECT) + return fmt.Errorf("Usage: gobgp neighbor policy %s {%s|%s|%s} {%s|%s}", cmdType, CMD_IMPORT, CMD_EXPORT, CMD_IN, policy.ROUTE_TYPE_ACCEPT, policy.ROUTE_TYPE_REJECT) } policies := parsePolicy(eArg[1]) defaultPolicy, err := parseRouteAction(eArg[2]) diff --git a/gobgp/cmd/policy.go b/gobgp/cmd/policy.go index fd0eba31..6bb710dc 100644 --- a/gobgp/cmd/policy.go +++ b/gobgp/cmd/policy.go @@ -1232,16 +1232,16 @@ func parseConditions() (*api.Conditions, error) { return conditions, nil } -func parseRouteAction(rType string) (string, error) { +func parseRouteAction(rType string) (api.RouteAction, error) { routeActionUpper := strings.ToUpper(rType) - var routeAction string switch routeActionUpper { - case policy.ROUTE_ACCEPT, policy.ROUTE_REJECT: - routeAction = routeActionUpper + case "ACCEPT": + return api.RouteAction_ACCEPT, nil + case "REJECT": + return api.RouteAction_REJECT, nil default: - return "", fmt.Errorf("invalid route action: %s\nPlease enter the accept or reject", rType) + return api.RouteAction_NONE, fmt.Errorf("invalid route action: %s\nPlease enter the accept or reject", rType) } - return routeAction, nil } func parseCommunityAction(communityStr string) (*api.CommunityAction, error) { diff --git a/policy/policy.go b/policy/policy.go index 839cb926..e04d56a3 100644 --- a/policy/policy.go +++ b/policy/policy.go @@ -1856,9 +1856,9 @@ func ConditionsToConfigStruct(reqConditions *gobgpapi.Conditions) config.Conditi } func ActionsToApiStruct(conActions config.Actions) *gobgpapi.Actions { - action := ROUTE_REJECT + action := gobgpapi.RouteAction_REJECT if conActions.RouteDisposition.AcceptRoute { - action = ROUTE_ACCEPT + action = gobgpapi.RouteAction_ACCEPT } //TODO: support CommunitySetRef @@ -1904,9 +1904,9 @@ func ActionsToConfigStruct(reqActions *gobgpapi.Actions) config.Actions { } switch reqActions.RouteAction { - case ROUTE_ACCEPT: + case gobgpapi.RouteAction_ACCEPT: actions.RouteDisposition.AcceptRoute = true - case ROUTE_REJECT: + case gobgpapi.RouteAction_REJECT: actions.RouteDisposition.RejectRoute = true } return actions diff --git a/server/server.go b/server/server.go index eda7a3d2..d65fbace 100644 --- a/server/server.go +++ b/server/server.go @@ -1604,17 +1604,17 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { inPolicyNames := peer.conf.ApplyPolicy.ApplyPolicyConfig.InPolicy resInPolicies := extract(inPolicyNames) - defaultImportPolicy := policy.ROUTE_REJECT - defaultExportPolicy := policy.ROUTE_REJECT - defaultInPolicy := policy.ROUTE_REJECT + defaultImportPolicy := api.RouteAction_REJECT + defaultExportPolicy := api.RouteAction_REJECT + defaultInPolicy := api.RouteAction_REJECT if peer.defaultImportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { - defaultImportPolicy = policy.ROUTE_ACCEPT + defaultImportPolicy = api.RouteAction_ACCEPT } if peer.defaultExportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { - defaultExportPolicy = policy.ROUTE_ACCEPT + defaultExportPolicy = api.RouteAction_ACCEPT } if peer.defaultInPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { - defaultInPolicy = policy.ROUTE_ACCEPT + defaultInPolicy = api.RouteAction_ACCEPT } result := &GrpcResponse{ Data: &api.ApplyPolicy{ @@ -1641,19 +1641,19 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { applyPolicy := &peer.conf.ApplyPolicy.ApplyPolicyConfig var defInPolicy, defOutPolicy, defDistPolicy config.DefaultPolicyType if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_IMPORT { - if reqApplyPolicy.DefaultImportPolicy != policy.ROUTE_ACCEPT { + if reqApplyPolicy.DefaultImportPolicy != api.RouteAction_ACCEPT { defInPolicy = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE } applyPolicy.DefaultImportPolicy = defInPolicy applyPolicy.ImportPolicy = policy.PoliciesToString(reqApplyPolicy.ImportPolicies) } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_EXPORT { - if reqApplyPolicy.DefaultExportPolicy != policy.ROUTE_ACCEPT { + if reqApplyPolicy.DefaultExportPolicy != api.RouteAction_ACCEPT { defOutPolicy = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE } applyPolicy.DefaultExportPolicy = defOutPolicy applyPolicy.ExportPolicy = policy.PoliciesToString(reqApplyPolicy.ExportPolicies) } else if grpcReq.RequestType == REQ_NEIGHBOR_POLICY_ADD_IN { - if reqApplyPolicy.DefaultInPolicy != policy.ROUTE_ACCEPT { + if reqApplyPolicy.DefaultInPolicy != api.RouteAction_ACCEPT { defDistPolicy = config.DEFAULT_POLICY_TYPE_REJECT_ROUTE } applyPolicy.DefaultInPolicy = defDistPolicy @@ -2072,7 +2072,7 @@ func (server *BgpServer) handleGrpcAddPolicy(grpcReq *GrpcRequest) { if reqConditions.MatchAsPathLength != nil { conConditions.BgpConditions.AsPathLength = statement.Conditions.BgpConditions.AsPathLength } - if reqActions.RouteAction != "" { + if reqActions.RouteAction != api.RouteAction_NONE { conActions.RouteDisposition.AcceptRoute = statement.Actions.RouteDisposition.AcceptRoute conActions.RouteDisposition.RejectRoute = statement.Actions.RouteDisposition.RejectRoute } -- cgit v1.2.3