diff options
-rw-r--r-- | api/gobgp.pb.go | 154 | ||||
-rw-r--r-- | api/gobgp.proto | 50 | ||||
-rw-r--r-- | gobgp/cmd/policy.go | 92 | ||||
-rw-r--r-- | table/policy.go | 98 |
4 files changed, 299 insertions, 95 deletions
diff --git a/api/gobgp.pb.go b/api/gobgp.pb.go index 95ed3410..6f0648e7 100644 --- a/api/gobgp.pb.go +++ b/api/gobgp.pb.go @@ -113,6 +113,84 @@ func (x Operation) String() string { return proto.EnumName(Operation_name, int32(x)) } +type DefinedType int32 + +const ( + DefinedType_PREFIX DefinedType = 0 + DefinedType_NEIGHBOR DefinedType = 1 + DefinedType_TAG DefinedType = 2 + DefinedType_AS_PATH DefinedType = 3 + DefinedType_COMMUNITY DefinedType = 4 + DefinedType_EXT_COMMUNITY DefinedType = 5 +) + +var DefinedType_name = map[int32]string{ + 0: "PREFIX", + 1: "NEIGHBOR", + 2: "TAG", + 3: "AS_PATH", + 4: "COMMUNITY", + 5: "EXT_COMMUNITY", +} +var DefinedType_value = map[string]int32{ + "PREFIX": 0, + "NEIGHBOR": 1, + "TAG": 2, + "AS_PATH": 3, + "COMMUNITY": 4, + "EXT_COMMUNITY": 5, +} + +func (x DefinedType) String() string { + return proto.EnumName(DefinedType_name, int32(x)) +} + +type MatchType int32 + +const ( + MatchType_ANY MatchType = 0 + MatchType_ALL MatchType = 1 + MatchType_INVERT MatchType = 2 +) + +var MatchType_name = map[int32]string{ + 0: "ANY", + 1: "ALL", + 2: "INVERT", +} +var MatchType_value = map[string]int32{ + "ANY": 0, + "ALL": 1, + "INVERT": 2, +} + +func (x MatchType) String() string { + return proto.EnumName(MatchType_name, int32(x)) +} + +type AsPathLengthType int32 + +const ( + AsPathLengthType_EQ AsPathLengthType = 0 + AsPathLengthType_GE AsPathLengthType = 1 + AsPathLengthType_LE AsPathLengthType = 2 +) + +var AsPathLengthType_name = map[int32]string{ + 0: "EQ", + 1: "GE", + 2: "LE", +} +var AsPathLengthType_value = map[string]int32{ + "EQ": 0, + "GE": 1, + "LE": 2, +} + +func (x AsPathLengthType) String() string { + return proto.EnumName(AsPathLengthType_name, int32(x)) +} + type RouteAction int32 const ( @@ -136,6 +214,49 @@ func (x RouteAction) String() string { return proto.EnumName(RouteAction_name, int32(x)) } +type CommunityActionType int32 + +const ( + CommunityActionType_COMMUNITY_ADD CommunityActionType = 0 + CommunityActionType_COMMUNITY_REMOVE CommunityActionType = 1 + CommunityActionType_COMMUNITY_REPLACE CommunityActionType = 2 +) + +var CommunityActionType_name = map[int32]string{ + 0: "COMMUNITY_ADD", + 1: "COMMUNITY_REMOVE", + 2: "COMMUNITY_REPLACE", +} +var CommunityActionType_value = map[string]int32{ + "COMMUNITY_ADD": 0, + "COMMUNITY_REMOVE": 1, + "COMMUNITY_REPLACE": 2, +} + +func (x CommunityActionType) String() string { + return proto.EnumName(CommunityActionType_name, int32(x)) +} + +type MedActionType int32 + +const ( + MedActionType_MED_MOD MedActionType = 0 + MedActionType_MED_REPLACE MedActionType = 1 +) + +var MedActionType_name = map[int32]string{ + 0: "MED_MOD", + 1: "MED_REPLACE", +} +var MedActionType_value = map[string]int32{ + "MED_MOD": 0, + "MED_REPLACE": 1, +} + +func (x MedActionType) String() string { + return proto.EnumName(MedActionType_name, int32(x)) +} + type PolicyType int32 const ( @@ -278,11 +399,11 @@ type ModPolicyArguments struct { Operation Operation `protobuf:"varint,1,opt,name=operation,enum=gobgpapi.Operation" json:"operation,omitempty"` Policy *Policy `protobuf:"bytes,2,opt,name=policy" json:"policy,omitempty"` // if this flag is set, gobgpd won't define new statements - // but refer existing statements using statement's names. + // but refer existing statements using statement's names in this arguments. // this flag only works with Operation_ADD ReferExistingStatements bool `protobuf:"varint,3,opt,name=refer_existing_statements" json:"refer_existing_statements,omitempty"` // if this flag is set, gobgpd won't delete any statements - // even if the policy containing some statements are deleted. + // even if some statements get not used by any policy by this operation. // this flag means nothing if it is used with Operation_ADD PreserveStatements bool `protobuf:"varint,4,opt,name=preserve_statements" json:"preserve_statements,omitempty"` } @@ -430,10 +551,10 @@ func (m *Prefix) String() string { return proto.CompactTextString(m) } func (*Prefix) ProtoMessage() {} type DefinedSet struct { - Type int32 `protobuf:"varint,1,opt,name=type" json:"type,omitempty"` - Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"` - List []string `protobuf:"bytes,3,rep,name=list" json:"list,omitempty"` - Prefixes []*Prefix `protobuf:"bytes,4,rep,name=prefixes" json:"prefixes,omitempty"` + Type DefinedType `protobuf:"varint,1,opt,name=type,enum=gobgpapi.DefinedType" json:"type,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"` + List []string `protobuf:"bytes,3,rep,name=list" json:"list,omitempty"` + Prefixes []*Prefix `protobuf:"bytes,4,rep,name=prefixes" json:"prefixes,omitempty"` } func (m *DefinedSet) Reset() { *m = DefinedSet{} } @@ -448,8 +569,8 @@ func (m *DefinedSet) GetPrefixes() []*Prefix { } type MatchSet struct { - Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` - Option int32 `protobuf:"varint,2,opt,name=option" json:"option,omitempty"` + Type MatchType `protobuf:"varint,1,opt,name=type,enum=gobgpapi.MatchType" json:"type,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"` } func (m *MatchSet) Reset() { *m = MatchSet{} } @@ -457,8 +578,8 @@ func (m *MatchSet) String() string { return proto.CompactTextString(m) } func (*MatchSet) ProtoMessage() {} type AsPathLength struct { - Length uint32 `protobuf:"varint,1,opt,name=length" json:"length,omitempty"` - Type int32 `protobuf:"varint,2,opt,name=type" json:"type,omitempty"` + Type AsPathLengthType `protobuf:"varint,1,opt,name=type,enum=gobgpapi.AsPathLengthType" json:"type,omitempty"` + Length uint32 `protobuf:"varint,2,opt,name=length" json:"length,omitempty"` } func (m *AsPathLength) Reset() { *m = AsPathLength{} } @@ -522,8 +643,8 @@ func (m *Conditions) GetExtCommunitySet() *MatchSet { } type CommunityAction struct { - Communities []string `protobuf:"bytes,1,rep,name=communities" json:"communities,omitempty"` - Option int32 `protobuf:"varint,2,opt,name=option" json:"option,omitempty"` + Type CommunityActionType `protobuf:"varint,1,opt,name=type,enum=gobgpapi.CommunityActionType" json:"type,omitempty"` + Communities []string `protobuf:"bytes,2,rep,name=communities" json:"communities,omitempty"` } func (m *CommunityAction) Reset() { *m = CommunityAction{} } @@ -531,8 +652,8 @@ func (m *CommunityAction) String() string { return proto.CompactTextString(m) } func (*CommunityAction) ProtoMessage() {} type MedAction struct { - Type int32 `protobuf:"varint,1,opt,name=type" json:"type,omitempty"` - Value int64 `protobuf:"varint,2,opt,name=value" json:"value,omitempty"` + Type MedActionType `protobuf:"varint,1,opt,name=type,enum=gobgpapi.MedActionType" json:"type,omitempty"` + Value int64 `protobuf:"varint,2,opt,name=value" json:"value,omitempty"` } func (m *MedAction) Reset() { *m = MedAction{} } @@ -723,7 +844,12 @@ func (*Vrf) ProtoMessage() {} func init() { proto.RegisterEnum("gobgpapi.Resource", Resource_name, Resource_value) proto.RegisterEnum("gobgpapi.Operation", Operation_name, Operation_value) + proto.RegisterEnum("gobgpapi.DefinedType", DefinedType_name, DefinedType_value) + proto.RegisterEnum("gobgpapi.MatchType", MatchType_name, MatchType_value) + proto.RegisterEnum("gobgpapi.AsPathLengthType", AsPathLengthType_name, AsPathLengthType_value) proto.RegisterEnum("gobgpapi.RouteAction", RouteAction_name, RouteAction_value) + proto.RegisterEnum("gobgpapi.CommunityActionType", CommunityActionType_name, CommunityActionType_value) + proto.RegisterEnum("gobgpapi.MedActionType", MedActionType_name, MedActionType_value) proto.RegisterEnum("gobgpapi.PolicyType", PolicyType_name, PolicyType_value) proto.RegisterEnum("gobgpapi.Error_ErrorCode", Error_ErrorCode_name, Error_ErrorCode_value) } diff --git a/api/gobgp.proto b/api/gobgp.proto index 055ab318..1a17a2cd 100644 --- a/api/gobgp.proto +++ b/api/gobgp.proto @@ -197,22 +197,42 @@ message Prefix { uint32 mask_length_max = 3; } +enum DefinedType { + PREFIX = 0; + NEIGHBOR = 1; + TAG = 2; + AS_PATH = 3; + COMMUNITY = 4; + EXT_COMMUNITY = 5; +} + message DefinedSet { - int32 type = 1; + DefinedType type = 1; string name = 2; repeated string list = 3; repeated Prefix prefixes = 4; } +enum MatchType { + ANY = 0; + ALL = 1; + INVERT = 2; +} + message MatchSet { - string name = 1; - // see table/policy.go MatchOption for the usage - int32 option = 2; + MatchType type = 1; + string name = 2; +} + +enum AsPathLengthType { + EQ = 0; + GE = 1; + LE = 2; } message AsPathLength { - uint32 length = 1; - int32 type = 2; + AsPathLengthType type = 1; + uint32 length = 2; } message Conditions { @@ -231,14 +251,24 @@ enum RouteAction { REJECT = 2; } +enum CommunityActionType { + COMMUNITY_ADD = 0; + COMMUNITY_REMOVE = 1; + COMMUNITY_REPLACE = 2; +} + message CommunityAction { - repeated string communities = 1; - // see config/bgp_configs.go BgpSetCommunityOptionType for the usage - int32 option = 2; + CommunityActionType type = 1; + repeated string communities = 2; +} + +enum MedActionType { + MED_MOD = 0; + MED_REPLACE = 1; } message MedAction { - int32 type = 1; + MedActionType type = 1; int64 value = 2; } diff --git a/gobgp/cmd/policy.go b/gobgp/cmd/policy.go index fd429c2a..7e9a1464 100644 --- a/gobgp/cmd/policy.go +++ b/gobgp/cmd/policy.go @@ -132,25 +132,25 @@ func formatDefinedSet(head bool, typ string, indent int, list []*api.DefinedSet) } func showDefinedSet(v string, args []string) error { - var typ table.DefinedType + var typ api.DefinedType switch v { case CMD_PREFIX: - typ = table.DEFINED_TYPE_PREFIX + typ = api.DefinedType_PREFIX case CMD_NEIGHBOR: - typ = table.DEFINED_TYPE_NEIGHBOR + typ = api.DefinedType_NEIGHBOR case CMD_ASPATH: - typ = table.DEFINED_TYPE_AS_PATH + typ = api.DefinedType_AS_PATH case CMD_COMMUNITY: - typ = table.DEFINED_TYPE_COMMUNITY + typ = api.DefinedType_COMMUNITY case CMD_EXTCOMMUNITY: - typ = table.DEFINED_TYPE_EXT_COMMUNITY + typ = api.DefinedType_EXT_COMMUNITY default: return fmt.Errorf("unknown defined type: %s", v) } m := sets{} if len(args) > 0 { arg := &api.DefinedSet{ - Type: int32(typ), + Type: typ, Name: args[0], } p, e := client.GetDefinedSet(context.Background(), arg) @@ -160,7 +160,7 @@ func showDefinedSet(v string, args []string) error { m = append(m, p) } else { arg := &api.DefinedSet{ - Type: int32(typ), + Type: typ, } stream, e := client.GetDefinedSets(context.Background(), arg) if e != nil { @@ -266,7 +266,7 @@ func parsePrefixSet(args []string) (*api.DefinedSet, error) { list = []*api.Prefix{prefix} } return &api.DefinedSet{ - Type: int32(table.DEFINED_TYPE_PREFIX), + Type: api.DefinedType_PREFIX, Name: name, Prefixes: list, }, nil @@ -285,7 +285,7 @@ func parseNeighborSet(args []string) (*api.DefinedSet, error) { } } return &api.DefinedSet{ - Type: int32(table.DEFINED_TYPE_NEIGHBOR), + Type: api.DefinedType_NEIGHBOR, Name: name, List: args, }, nil @@ -304,7 +304,7 @@ func parseAsPathSet(args []string) (*api.DefinedSet, error) { } } return &api.DefinedSet{ - Type: int32(table.DEFINED_TYPE_AS_PATH), + Type: api.DefinedType_AS_PATH, Name: name, List: args, }, nil @@ -322,7 +322,7 @@ func parseCommunitySet(args []string) (*api.DefinedSet, error) { } } return &api.DefinedSet{ - Type: int32(table.DEFINED_TYPE_COMMUNITY), + Type: api.DefinedType_COMMUNITY, Name: name, List: args, }, nil @@ -340,7 +340,7 @@ func parseExtCommunitySet(args []string) (*api.DefinedSet, error) { } } return &api.DefinedSet{ - Type: int32(table.DEFINED_TYPE_EXT_COMMUNITY), + Type: api.DefinedType_EXT_COMMUNITY, Name: name, List: args, }, nil @@ -409,40 +409,40 @@ func printStatement(indent int, s *api.Statement) { ps := s.Conditions.PrefixSet if ps != nil { - fmt.Printf("%sPrefixSet: %s %s\n", sIndent(indent+4), table.MatchOption(ps.Option), ps.Name) + fmt.Printf("%sPrefixSet: %s %s\n", sIndent(indent+4), ps.Type, ps.Name) } ns := s.Conditions.NeighborSet if ns != nil { - fmt.Printf("%sNeighborSet: %s %s\n", sIndent(indent+4), table.MatchOption(ns.Option), ns.Name) + fmt.Printf("%sNeighborSet: %s %s\n", sIndent(indent+4), ns.Type, ns.Name) } aps := s.Conditions.AsPathSet if aps != nil { - fmt.Printf("%sAsPathSet: %s %s\n", sIndent(indent+4), table.MatchOption(aps.Option), aps.Name) + fmt.Printf("%sAsPathSet: %s %s\n", sIndent(indent+4), aps.Type, aps.Name) } cs := s.Conditions.CommunitySet if cs != nil { - fmt.Printf("%sCommunitySet: %s %s\n", sIndent(indent+4), table.MatchOption(cs.Option), cs.Name) + fmt.Printf("%sCommunitySet: %s %s\n", sIndent(indent+4), cs.Type, cs.Name) } ecs := s.Conditions.ExtCommunitySet if ecs != nil { - fmt.Printf("%sExtCommunitySet: %s %s\n", sIndent(indent+4), table.MatchOption(ecs.Option), ecs.Name) + fmt.Printf("%sExtCommunitySet: %s %s\n", sIndent(indent+4), ecs.Type, ecs.Name) } asPathLentgh := s.Conditions.AsPathLength if asPathLentgh != nil { - fmt.Printf("%sAsPathLength: %s %s\n", sIndent(indent+4), asPathLentgh.Type, asPathLentgh.Length) + fmt.Printf("%sAsPathLength: %s %d\n", sIndent(indent+4), asPathLentgh.Type, asPathLentgh.Length) } fmt.Printf("%sActions:\n", sIndent(indent+2)) formatComAction := func(c *api.CommunityAction) string { - option := table.CommunityOptionNameMap[config.BgpSetCommunityOptionType(c.Option)] + option := c.Type.String() if len(c.Communities) != 0 { communities := strings.Join(c.Communities, ",") - option = fmt.Sprintf("%s[%s]", option, communities) + option = fmt.Sprintf("%s[%s]", c.Type, communities) } return option } @@ -617,9 +617,9 @@ func modCondition(name, op string, args []string) error { } switch strings.ToLower(args[1]) { case "any": - stmt.Conditions.PrefixSet.Option = int32(config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY) + stmt.Conditions.PrefixSet.Type = api.MatchType_ANY case "invert": - stmt.Conditions.PrefixSet.Option = int32(config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT) + stmt.Conditions.PrefixSet.Type = api.MatchType_INVERT default: return fmt.Errorf("%s prefix <set-name> [{ any | invert }]", usage) } @@ -635,9 +635,9 @@ func modCondition(name, op string, args []string) error { } switch strings.ToLower(args[1]) { case "any": - stmt.Conditions.NeighborSet.Option = int32(config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY) + stmt.Conditions.NeighborSet.Type = api.MatchType_ANY case "invert": - stmt.Conditions.NeighborSet.Option = int32(config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT) + stmt.Conditions.NeighborSet.Type = api.MatchType_ANY default: return fmt.Errorf("%s neighbor <set-name> [{ any | invert }]", usage) } @@ -653,11 +653,11 @@ func modCondition(name, op string, args []string) error { } switch strings.ToLower(args[1]) { case "any": - stmt.Conditions.AsPathSet.Option = int32(config.MATCH_SET_OPTIONS_TYPE_ANY) + stmt.Conditions.AsPathSet.Type = api.MatchType_ANY case "all": - stmt.Conditions.AsPathSet.Option = int32(config.MATCH_SET_OPTIONS_TYPE_ALL) + stmt.Conditions.AsPathSet.Type = api.MatchType_ALL case "invert": - stmt.Conditions.AsPathSet.Option = int32(config.MATCH_SET_OPTIONS_TYPE_INVERT) + stmt.Conditions.AsPathSet.Type = api.MatchType_INVERT default: return fmt.Errorf("%s as-path <set-name> [{ any | all | invert }]", usage) } @@ -673,11 +673,11 @@ func modCondition(name, op string, args []string) error { } switch strings.ToLower(args[1]) { case "any": - stmt.Conditions.CommunitySet.Option = int32(config.MATCH_SET_OPTIONS_TYPE_ANY) + stmt.Conditions.CommunitySet.Type = api.MatchType_ANY case "all": - stmt.Conditions.CommunitySet.Option = int32(config.MATCH_SET_OPTIONS_TYPE_ALL) + stmt.Conditions.CommunitySet.Type = api.MatchType_ALL case "invert": - stmt.Conditions.CommunitySet.Option = int32(config.MATCH_SET_OPTIONS_TYPE_INVERT) + stmt.Conditions.CommunitySet.Type = api.MatchType_INVERT default: return fmt.Errorf("%s community <set-name> [{ any | all | invert }]", usage) } @@ -693,11 +693,11 @@ func modCondition(name, op string, args []string) error { } switch strings.ToLower(args[1]) { case "any": - stmt.Conditions.ExtCommunitySet.Option = int32(config.MATCH_SET_OPTIONS_TYPE_ANY) + stmt.Conditions.ExtCommunitySet.Type = api.MatchType_ANY case "all": - stmt.Conditions.ExtCommunitySet.Option = int32(config.MATCH_SET_OPTIONS_TYPE_ALL) + stmt.Conditions.ExtCommunitySet.Type = api.MatchType_ALL case "invert": - stmt.Conditions.ExtCommunitySet.Option = int32(config.MATCH_SET_OPTIONS_TYPE_INVERT) + stmt.Conditions.ExtCommunitySet.Type = api.MatchType_INVERT default: return fmt.Errorf("%s ext-community <set-name> [{ any | all | invert }]", usage) } @@ -714,11 +714,11 @@ func modCondition(name, op string, args []string) error { } switch strings.ToLower(args[1]) { case "eq": - stmt.Conditions.AsPathLength.Type = int32(table.ATTRIBUTE_EQ) + stmt.Conditions.AsPathLength.Type = api.AsPathLengthType_EQ case "ge": - stmt.Conditions.AsPathLength.Type = int32(table.ATTRIBUTE_GE) + stmt.Conditions.AsPathLength.Type = api.AsPathLengthType_GE case "le": - stmt.Conditions.AsPathLength.Type = int32(table.ATTRIBUTE_LE) + stmt.Conditions.AsPathLength.Type = api.AsPathLengthType_LE default: return fmt.Errorf("%s as-path-length <length> { eq | ge | le }", usage) } @@ -781,11 +781,11 @@ func modAction(name, op string, args []string) error { } switch strings.ToLower(args[0]) { case "add": - stmt.Actions.Community.Option = int32(config.BGP_SET_COMMUNITY_OPTION_TYPE_ADD) + stmt.Actions.Community.Type = api.CommunityActionType_COMMUNITY_ADD case "remove": - stmt.Actions.Community.Option = int32(config.BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE) + stmt.Actions.Community.Type = api.CommunityActionType_COMMUNITY_REMOVE case "replace": - stmt.Actions.Community.Option = int32(config.BGP_SET_COMMUNITY_OPTION_TYPE_REPLACE) + stmt.Actions.Community.Type = api.CommunityActionType_COMMUNITY_REPLACE default: return fmt.Errorf("%s community { add | remove | replace } <value>...", usage) } @@ -798,11 +798,11 @@ func modAction(name, op string, args []string) error { } switch strings.ToLower(args[0]) { case "add": - stmt.Actions.ExtCommunity.Option = int32(config.BGP_SET_COMMUNITY_OPTION_TYPE_ADD) + stmt.Actions.ExtCommunity.Type = api.CommunityActionType_COMMUNITY_ADD case "remove": - stmt.Actions.ExtCommunity.Option = int32(config.BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE) + stmt.Actions.ExtCommunity.Type = api.CommunityActionType_COMMUNITY_REMOVE case "replace": - stmt.Actions.ExtCommunity.Option = int32(config.BGP_SET_COMMUNITY_OPTION_TYPE_REPLACE) + stmt.Actions.ExtCommunity.Type = api.CommunityActionType_COMMUNITY_REPLACE default: return fmt.Errorf("%s ext-community { add | remove | replace } <value>...", usage) } @@ -819,12 +819,12 @@ func modAction(name, op string, args []string) error { } switch strings.ToLower(args[0]) { case "add": - stmt.Actions.Med.Type = int32(table.MED_ACTION_MOD) + stmt.Actions.Med.Type = api.MedActionType_MED_MOD case "sub": - stmt.Actions.Med.Type = int32(table.MED_ACTION_MOD) + stmt.Actions.Med.Type = api.MedActionType_MED_MOD stmt.Actions.Med.Value *= -1 case "set": - stmt.Actions.Med.Type = int32(table.MED_ACTION_REPLACE) + stmt.Actions.Med.Type = api.MedActionType_MED_REPLACE default: return fmt.Errorf("%s med { add | sub | set } <value>") } diff --git a/table/policy.go b/table/policy.go index 7698bad6..2a97e42b 100644 --- a/table/policy.go +++ b/table/policy.go @@ -88,6 +88,34 @@ func (o MatchOption) String() string { } } +func toConfigMatchSetOption(a api.MatchType) (config.MatchSetOptionsType, error) { + var typ config.MatchSetOptionsType + switch a { + case api.MatchType_ANY: + typ = config.MATCH_SET_OPTIONS_TYPE_ANY + case api.MatchType_ALL: + typ = config.MATCH_SET_OPTIONS_TYPE_ALL + case api.MatchType_INVERT: + typ = config.MATCH_SET_OPTIONS_TYPE_INVERT + default: + return typ, fmt.Errorf("invalid match type") + } + return typ, nil +} + +func toConfigMatchSetOptionRestricted(a api.MatchType) (config.MatchSetOptionsRestrictedType, error) { + var typ config.MatchSetOptionsRestrictedType + switch a { + case api.MatchType_ANY: + typ = config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY + case api.MatchType_INVERT: + typ = config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT + default: + return typ, fmt.Errorf("invalid match type") + } + return typ, nil +} + type MedActionType int const ( @@ -340,7 +368,7 @@ func (s *PrefixSet) ToApiStruct() *api.DefinedSet { list = append(list, p.ToApiStruct()) } return &api.DefinedSet{ - Type: int32(s.Type()), + Type: api.DefinedType(s.Type()), Name: s.name, Prefixes: list, } @@ -445,7 +473,7 @@ func (s *NeighborSet) ToApiStruct() *api.DefinedSet { list = append(list, n.String()) } return &api.DefinedSet{ - Type: int32(s.Type()), + Type: api.DefinedType(s.Type()), Name: s.name, List: list, } @@ -567,7 +595,7 @@ func (s *regExpSet) ToApiStruct() *api.DefinedSet { list = append(list, exp.String()) } return &api.DefinedSet{ - Type: int32(s.typ), + Type: api.DefinedType(s.typ), Name: s.name, List: list, } @@ -749,7 +777,7 @@ func (s *ExtCommunitySet) ToApiStruct() *api.DefinedSet { list = append(list, f(idx, exp.String())) } return &api.DefinedSet{ - Type: int32(s.typ), + Type: api.DefinedType(s.typ), Name: s.name, List: list, } @@ -867,8 +895,8 @@ func (c *PrefixCondition) Evaluate(path *Path) bool { func (c *PrefixCondition) ToApiStruct() *api.MatchSet { return &api.MatchSet{ - Name: c.set.Name(), - Option: int32(c.option), + Type: api.MatchType(c.option), + Name: c.set.Name(), } } @@ -876,9 +904,13 @@ func NewPrefixConditionFromApiStruct(a *api.MatchSet, m map[string]DefinedSet) ( if a == nil { return nil, nil } + typ, err := toConfigMatchSetOptionRestricted(a.Type) + if err != nil { + return nil, err + } c := config.MatchPrefixSet{ PrefixSet: a.Name, - MatchSetOptions: config.MatchSetOptionsRestrictedType(a.Option), + MatchSetOptions: typ, } return NewPrefixCondition(c, m) } @@ -959,8 +991,8 @@ func (c *NeighborCondition) Evaluate(path *Path) bool { func (c *NeighborCondition) ToApiStruct() *api.MatchSet { return &api.MatchSet{ - Name: c.set.Name(), - Option: int32(c.option), + Type: api.MatchType(c.option), + Name: c.set.Name(), } } @@ -968,9 +1000,13 @@ func NewNeighborConditionFromApiStruct(a *api.MatchSet, m map[string]DefinedSet) if a == nil { return nil, nil } + typ, err := toConfigMatchSetOptionRestricted(a.Type) + if err != nil { + return nil, err + } c := config.MatchNeighborSet{ NeighborSet: a.Name, - MatchSetOptions: config.MatchSetOptionsRestrictedType(a.Option), + MatchSetOptions: typ, } return NewNeighborCondition(c, m) } @@ -1016,8 +1052,8 @@ func (c *AsPathCondition) Option() MatchOption { func (c *AsPathCondition) ToApiStruct() *api.MatchSet { return &api.MatchSet{ - Name: c.set.Name(), - Option: int32(c.option), + Type: api.MatchType(c.option), + Name: c.set.Name(), } } @@ -1052,9 +1088,13 @@ func NewAsPathConditionFromApiStruct(a *api.MatchSet, m map[string]DefinedSet) ( if a == nil { return nil, nil } + typ, err := toConfigMatchSetOption(a.Type) + if err != nil { + return nil, err + } c := config.MatchAsPathSet{ AsPathSet: a.Name, - MatchSetOptions: config.MatchSetOptionsType(a.Option), + MatchSetOptions: typ, } return NewAsPathCondition(c, m) } @@ -1100,8 +1140,8 @@ func (c *CommunityCondition) Option() MatchOption { func (c *CommunityCondition) ToApiStruct() *api.MatchSet { return &api.MatchSet{ - Name: c.set.Name(), - Option: int32(c.option), + Type: api.MatchType(c.option), + Name: c.set.Name(), } } @@ -1139,9 +1179,13 @@ func NewCommunityConditionFromApiStruct(a *api.MatchSet, m map[string]DefinedSet if a == nil { return nil, nil } + typ, err := toConfigMatchSetOption(a.Type) + if err != nil { + return nil, err + } c := config.MatchCommunitySet{ CommunitySet: a.Name, - MatchSetOptions: config.MatchSetOptionsType(a.Option), + MatchSetOptions: typ, } return NewCommunityCondition(c, m) } @@ -1187,8 +1231,8 @@ func (c *ExtCommunityCondition) Option() MatchOption { func (c *ExtCommunityCondition) ToApiStruct() *api.MatchSet { return &api.MatchSet{ - Name: c.set.Name(), - Option: int32(c.option), + Type: api.MatchType(c.option), + Name: c.set.Name(), } } @@ -1232,9 +1276,13 @@ func NewExtCommunityConditionFromApiStruct(a *api.MatchSet, m map[string]Defined if a == nil { return nil, nil } + typ, err := toConfigMatchSetOption(a.Type) + if err != nil { + return nil, err + } c := config.MatchExtCommunitySet{ ExtCommunitySet: a.Name, - MatchSetOptions: config.MatchSetOptionsType(a.Option), + MatchSetOptions: typ, } return NewExtCommunityCondition(c, m) } @@ -1302,7 +1350,7 @@ func (c *AsPathLengthCondition) Set() DefinedSet { func (c *AsPathLengthCondition) ToApiStruct() *api.AsPathLength { return &api.AsPathLength{ Length: c.length, - Type: int32(c.operator), + Type: api.AsPathLengthType(c.operator), } } @@ -1504,8 +1552,8 @@ func (a *CommunityAction) ToApiStruct() *api.CommunityAction { cs = append(cs, exp.String()) } return &api.CommunityAction{ + Type: api.CommunityActionType(a.action), Communities: cs, - Option: int32(a.action), } } @@ -1515,7 +1563,7 @@ func NewCommunityActionFromApiStruct(a *api.CommunityAction) (*CommunityAction, } var list []uint32 var removeList []*regexp.Regexp - op := config.BgpSetCommunityOptionType(a.Option) + op := config.BgpSetCommunityOptionType(a.Type) if op == config.BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE { removeList = make([]*regexp.Regexp, 0, len(a.Communities)) } else { @@ -1622,8 +1670,8 @@ func (a *ExtCommunityAction) ToApiStruct() *api.CommunityAction { cs = append(cs, f(idx, exp.String())) } return &api.CommunityAction{ + Type: api.CommunityActionType(a.action), Communities: cs, - Option: int32(a.action), } } @@ -1634,7 +1682,7 @@ func NewExtCommunityActionFromApiStruct(a *api.CommunityAction) (*ExtCommunityAc var list []bgp.ExtendedCommunityInterface var removeList []*regexp.Regexp subtypeList := make([]bgp.ExtendedCommunityAttrSubType, 0, len(a.Communities)) - op := config.BgpSetCommunityOptionType(a.Option) + op := config.BgpSetCommunityOptionType(a.Type) if op == config.BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE { removeList = make([]*regexp.Regexp, 0, len(a.Communities)) } else { @@ -1745,7 +1793,7 @@ func (a *MedAction) Apply(path *Path) *Path { func (a *MedAction) ToApiStruct() *api.MedAction { return &api.MedAction{ - Type: int32(a.action), + Type: api.MedActionType(a.action), Value: int64(a.value), } } |