Skip to content

Commit 2d2f0b0

Browse files
committed
api: Fix Type Enum in MatchSet
Use _UNSPECIFIED prefix for zero. Signed-off-by: FUJITA Tomonori <[email protected]>
1 parent cc1ac87 commit 2d2f0b0

File tree

6 files changed

+104
-65
lines changed

6 files changed

+104
-65
lines changed

api/gobgp.pb.go

Lines changed: 20 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gobgp/policy.go

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ func prettyString(v interface{}) string {
5656
case *api.MatchSet:
5757
var typ string
5858
switch a.Type {
59-
case api.MatchSet_ALL:
59+
case api.MatchSet_TYPE_ALL:
6060
typ = "all"
61-
case api.MatchSet_ANY:
61+
case api.MatchSet_TYPE_ANY:
6262
typ = "any"
63-
case api.MatchSet_INVERT:
63+
case api.MatchSet_TYPE_INVERT:
6464
typ = "invert"
6565
}
6666
return fmt.Sprintf("%s %s", typ, a.GetName())
@@ -648,7 +648,9 @@ func modCondition(name, op string, args []string) error {
648648
args = args[1:]
649649
switch typ {
650650
case "prefix":
651-
stmt.Conditions.PrefixSet = &api.MatchSet{}
651+
stmt.Conditions.PrefixSet = &api.MatchSet{
652+
Type: api.MatchSet_TYPE_ANY,
653+
}
652654
if len(args) < 1 {
653655
return fmt.Errorf("%s prefix <set-name> [{ any | invert }]", usage)
654656
}
@@ -658,14 +660,16 @@ func modCondition(name, op string, args []string) error {
658660
}
659661
switch strings.ToLower(args[1]) {
660662
case "any":
661-
stmt.Conditions.PrefixSet.Type = api.MatchSet_ANY
663+
stmt.Conditions.PrefixSet.Type = api.MatchSet_TYPE_ANY
662664
case "invert":
663-
stmt.Conditions.PrefixSet.Type = api.MatchSet_INVERT
665+
stmt.Conditions.PrefixSet.Type = api.MatchSet_TYPE_INVERT
664666
default:
665667
return fmt.Errorf("%s prefix <set-name> [{ any | invert }]", usage)
666668
}
667669
case "neighbor":
668-
stmt.Conditions.NeighborSet = &api.MatchSet{}
670+
stmt.Conditions.NeighborSet = &api.MatchSet{
671+
Type: api.MatchSet_TYPE_ANY,
672+
}
669673
if len(args) < 1 {
670674
return fmt.Errorf("%s neighbor <set-name> [{ any | invert }]", usage)
671675
}
@@ -675,14 +679,16 @@ func modCondition(name, op string, args []string) error {
675679
}
676680
switch strings.ToLower(args[1]) {
677681
case "any":
678-
stmt.Conditions.NeighborSet.Type = api.MatchSet_ANY
682+
stmt.Conditions.NeighborSet.Type = api.MatchSet_TYPE_ANY
679683
case "invert":
680-
stmt.Conditions.NeighborSet.Type = api.MatchSet_INVERT
684+
stmt.Conditions.NeighborSet.Type = api.MatchSet_TYPE_INVERT
681685
default:
682686
return fmt.Errorf("%s neighbor <set-name> [{ any | invert }]", usage)
683687
}
684688
case "as-path":
685-
stmt.Conditions.AsPathSet = &api.MatchSet{}
689+
stmt.Conditions.AsPathSet = &api.MatchSet{
690+
Type: api.MatchSet_TYPE_ANY,
691+
}
686692
if len(args) < 1 {
687693
return fmt.Errorf("%s as-path <set-name> [{ any | all | invert }]", usage)
688694
}
@@ -692,16 +698,18 @@ func modCondition(name, op string, args []string) error {
692698
}
693699
switch strings.ToLower(args[1]) {
694700
case "any":
695-
stmt.Conditions.AsPathSet.Type = api.MatchSet_ANY
701+
stmt.Conditions.AsPathSet.Type = api.MatchSet_TYPE_ANY
696702
case "all":
697-
stmt.Conditions.AsPathSet.Type = api.MatchSet_ALL
703+
stmt.Conditions.AsPathSet.Type = api.MatchSet_TYPE_ALL
698704
case "invert":
699-
stmt.Conditions.AsPathSet.Type = api.MatchSet_INVERT
705+
stmt.Conditions.AsPathSet.Type = api.MatchSet_TYPE_INVERT
700706
default:
701707
return fmt.Errorf("%s as-path <set-name> [{ any | all | invert }]", usage)
702708
}
703709
case "community":
704-
stmt.Conditions.CommunitySet = &api.MatchSet{}
710+
stmt.Conditions.CommunitySet = &api.MatchSet{
711+
Type: api.MatchSet_TYPE_ANY,
712+
}
705713
if len(args) < 1 {
706714
return fmt.Errorf("%s community <set-name> [{ any | all | invert }]", usage)
707715
}
@@ -711,16 +719,18 @@ func modCondition(name, op string, args []string) error {
711719
}
712720
switch strings.ToLower(args[1]) {
713721
case "any":
714-
stmt.Conditions.CommunitySet.Type = api.MatchSet_ANY
722+
stmt.Conditions.CommunitySet.Type = api.MatchSet_TYPE_ANY
715723
case "all":
716-
stmt.Conditions.CommunitySet.Type = api.MatchSet_ALL
724+
stmt.Conditions.CommunitySet.Type = api.MatchSet_TYPE_ALL
717725
case "invert":
718-
stmt.Conditions.CommunitySet.Type = api.MatchSet_INVERT
726+
stmt.Conditions.CommunitySet.Type = api.MatchSet_TYPE_INVERT
719727
default:
720728
return fmt.Errorf("%s community <set-name> [{ any | all | invert }]", usage)
721729
}
722730
case "ext-community":
723-
stmt.Conditions.ExtCommunitySet = &api.MatchSet{}
731+
stmt.Conditions.ExtCommunitySet = &api.MatchSet{
732+
Type: api.MatchSet_TYPE_ANY,
733+
}
724734
if len(args) < 1 {
725735
return fmt.Errorf("%s ext-community <set-name> [{ any | all | invert }]", usage)
726736
}
@@ -730,16 +740,18 @@ func modCondition(name, op string, args []string) error {
730740
}
731741
switch strings.ToLower(args[1]) {
732742
case "any":
733-
stmt.Conditions.ExtCommunitySet.Type = api.MatchSet_ANY
743+
stmt.Conditions.ExtCommunitySet.Type = api.MatchSet_TYPE_ANY
734744
case "all":
735-
stmt.Conditions.ExtCommunitySet.Type = api.MatchSet_ALL
745+
stmt.Conditions.ExtCommunitySet.Type = api.MatchSet_TYPE_ALL
736746
case "invert":
737-
stmt.Conditions.ExtCommunitySet.Type = api.MatchSet_INVERT
747+
stmt.Conditions.ExtCommunitySet.Type = api.MatchSet_TYPE_INVERT
738748
default:
739749
return fmt.Errorf("%s ext-community <set-name> [{ any | all | invert }]", usage)
740750
}
741751
case "large-community":
742-
stmt.Conditions.LargeCommunitySet = &api.MatchSet{}
752+
stmt.Conditions.LargeCommunitySet = &api.MatchSet{
753+
Type: api.MatchSet_TYPE_ANY,
754+
}
743755
if len(args) < 1 {
744756
return fmt.Errorf("%s large-community <set-name> [{ any | all | invert }]", usage)
745757
}
@@ -749,11 +761,11 @@ func modCondition(name, op string, args []string) error {
749761
}
750762
switch strings.ToLower(args[1]) {
751763
case "any":
752-
stmt.Conditions.LargeCommunitySet.Type = api.MatchSet_ANY
764+
stmt.Conditions.LargeCommunitySet.Type = api.MatchSet_TYPE_ANY
753765
case "all":
754-
stmt.Conditions.LargeCommunitySet.Type = api.MatchSet_ALL
766+
stmt.Conditions.LargeCommunitySet.Type = api.MatchSet_TYPE_ALL
755767
case "invert":
756-
stmt.Conditions.LargeCommunitySet.Type = api.MatchSet_INVERT
768+
stmt.Conditions.LargeCommunitySet.Type = api.MatchSet_TYPE_INVERT
757769
default:
758770
return fmt.Errorf("%s large-community <set-name> [{ any | all | invert }]", usage)
759771
}

internal/pkg/table/policy.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ const (
9898
MATCH_OPTION_INVERT
9999
)
100100

101+
func (o MatchOption) ToApi() api.MatchSet_Type {
102+
switch o {
103+
case MATCH_OPTION_ANY:
104+
return api.MatchSet_TYPE_ANY
105+
case MATCH_OPTION_ALL:
106+
return api.MatchSet_TYPE_ALL
107+
case MATCH_OPTION_INVERT:
108+
return api.MatchSet_TYPE_INVERT
109+
}
110+
panic(fmt.Sprintf("unknown MatchOption: %d", o))
111+
}
112+
101113
func (o MatchOption) String() string {
102114
switch o {
103115
case MATCH_OPTION_ANY:
@@ -4101,14 +4113,14 @@ func toStatementApi(s *oc.Statement) *api.Statement {
41014113
o, _ := NewMatchOption(s.Conditions.MatchPrefixSet.MatchSetOptions)
41024114
if s.Conditions.MatchPrefixSet.PrefixSet != "" {
41034115
cs.PrefixSet = &api.MatchSet{
4104-
Type: api.MatchSet_Type(o),
4116+
Type: o.ToApi(),
41054117
Name: s.Conditions.MatchPrefixSet.PrefixSet,
41064118
}
41074119
}
41084120
if s.Conditions.MatchNeighborSet.NeighborSet != "" {
41094121
o, _ := NewMatchOption(s.Conditions.MatchNeighborSet.MatchSetOptions)
41104122
cs.NeighborSet = &api.MatchSet{
4111-
Type: api.MatchSet_Type(o),
4123+
Type: o.ToApi(),
41124124
Name: s.Conditions.MatchNeighborSet.NeighborSet,
41134125
}
41144126
}
@@ -4136,26 +4148,30 @@ func toStatementApi(s *oc.Statement) *api.Statement {
41364148
}
41374149
}
41384150
if s.Conditions.BgpConditions.MatchAsPathSet.AsPathSet != "" {
4151+
o, _ := NewMatchOption(s.Conditions.BgpConditions.MatchAsPathSet.MatchSetOptions)
41394152
cs.AsPathSet = &api.MatchSet{
4140-
Type: api.MatchSet_Type(s.Conditions.BgpConditions.MatchAsPathSet.MatchSetOptions.ToInt()),
4153+
Type: o.ToApi(),
41414154
Name: s.Conditions.BgpConditions.MatchAsPathSet.AsPathSet,
41424155
}
41434156
}
41444157
if s.Conditions.BgpConditions.MatchCommunitySet.CommunitySet != "" {
4158+
o, _ := NewMatchOption(s.Conditions.BgpConditions.MatchCommunitySet.MatchSetOptions)
41454159
cs.CommunitySet = &api.MatchSet{
4146-
Type: api.MatchSet_Type(s.Conditions.BgpConditions.MatchCommunitySet.MatchSetOptions.ToInt()),
4160+
Type: o.ToApi(),
41474161
Name: s.Conditions.BgpConditions.MatchCommunitySet.CommunitySet,
41484162
}
41494163
}
41504164
if s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet != "" {
4165+
o, _ := NewMatchOption(s.Conditions.BgpConditions.MatchExtCommunitySet.MatchSetOptions)
41514166
cs.ExtCommunitySet = &api.MatchSet{
4152-
Type: api.MatchSet_Type(s.Conditions.BgpConditions.MatchExtCommunitySet.MatchSetOptions.ToInt()),
4167+
Type: o.ToApi(),
41534168
Name: s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet,
41544169
}
41554170
}
41564171
if s.Conditions.BgpConditions.MatchLargeCommunitySet.LargeCommunitySet != "" {
4172+
o, _ := NewMatchOption(s.Conditions.BgpConditions.MatchLargeCommunitySet.MatchSetOptions)
41574173
cs.LargeCommunitySet = &api.MatchSet{
4158-
Type: api.MatchSet_Type(s.Conditions.BgpConditions.MatchLargeCommunitySet.MatchSetOptions.ToInt()),
4174+
Type: o.ToApi(),
41594175
Name: s.Conditions.BgpConditions.MatchLargeCommunitySet.LargeCommunitySet,
41604176
}
41614177
}

pkg/server/grpc_server.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,11 +1103,11 @@ func matchSetOptionsRestrictedTypeToAPI(t oc.MatchSetOptionsRestrictedType) api.
11031103
t = t.DefaultAsNeeded()
11041104
switch t {
11051105
case oc.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY:
1106-
return api.MatchSet_ANY
1106+
return api.MatchSet_TYPE_ANY
11071107
case oc.MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT:
1108-
return api.MatchSet_INVERT
1108+
return api.MatchSet_TYPE_INVERT
11091109
}
1110-
return api.MatchSet_ANY
1110+
return api.MatchSet_TYPE_ANY
11111111
}
11121112

11131113
func toStatementApi(s *oc.Statement) *api.Statement {
@@ -1137,26 +1137,30 @@ func toStatementApi(s *oc.Statement) *api.Statement {
11371137
}
11381138
}
11391139
if s.Conditions.BgpConditions.MatchAsPathSet.AsPathSet != "" {
1140+
o, _ := table.NewMatchOption(s.Conditions.BgpConditions.MatchAsPathSet.MatchSetOptions)
11401141
cs.AsPathSet = &api.MatchSet{
1141-
Type: api.MatchSet_Type(s.Conditions.BgpConditions.MatchAsPathSet.MatchSetOptions.ToInt()),
1142+
Type: o.ToApi(),
11421143
Name: s.Conditions.BgpConditions.MatchAsPathSet.AsPathSet,
11431144
}
11441145
}
11451146
if s.Conditions.BgpConditions.MatchCommunitySet.CommunitySet != "" {
1147+
o, _ := table.NewMatchOption(s.Conditions.BgpConditions.MatchCommunitySet.MatchSetOptions)
11461148
cs.CommunitySet = &api.MatchSet{
1147-
Type: api.MatchSet_Type(s.Conditions.BgpConditions.MatchCommunitySet.MatchSetOptions.ToInt()),
1149+
Type: o.ToApi(),
11481150
Name: s.Conditions.BgpConditions.MatchCommunitySet.CommunitySet,
11491151
}
11501152
}
11511153
if s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet != "" {
1154+
o, _ := table.NewMatchOption(s.Conditions.BgpConditions.MatchExtCommunitySet.MatchSetOptions)
11521155
cs.ExtCommunitySet = &api.MatchSet{
1153-
Type: api.MatchSet_Type(s.Conditions.BgpConditions.MatchExtCommunitySet.MatchSetOptions.ToInt()),
1156+
Type: o.ToApi(),
11541157
Name: s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet,
11551158
}
11561159
}
11571160
if s.Conditions.BgpConditions.MatchLargeCommunitySet.LargeCommunitySet != "" {
1161+
o, _ := table.NewMatchOption(s.Conditions.BgpConditions.MatchLargeCommunitySet.MatchSetOptions)
11581162
cs.LargeCommunitySet = &api.MatchSet{
1159-
Type: api.MatchSet_Type(s.Conditions.BgpConditions.MatchLargeCommunitySet.MatchSetOptions.ToInt()),
1163+
Type: o.ToApi(),
11601164
Name: s.Conditions.BgpConditions.MatchLargeCommunitySet.LargeCommunitySet,
11611165
}
11621166
}
@@ -1319,27 +1323,27 @@ func toStatementApi(s *oc.Statement) *api.Statement {
13191323
func toConfigMatchSetOption(a api.MatchSet_Type) (oc.MatchSetOptionsType, error) {
13201324
var typ oc.MatchSetOptionsType
13211325
switch a {
1322-
case api.MatchSet_ANY:
1326+
case api.MatchSet_TYPE_ANY:
13231327
typ = oc.MATCH_SET_OPTIONS_TYPE_ANY
1324-
case api.MatchSet_ALL:
1328+
case api.MatchSet_TYPE_ALL:
13251329
typ = oc.MATCH_SET_OPTIONS_TYPE_ALL
1326-
case api.MatchSet_INVERT:
1330+
case api.MatchSet_TYPE_INVERT:
13271331
typ = oc.MATCH_SET_OPTIONS_TYPE_INVERT
13281332
default:
1329-
return typ, fmt.Errorf("invalid match type")
1333+
return typ, status.Errorf(codes.InvalidArgument, "invalid match type %d", a)
13301334
}
13311335
return typ, nil
13321336
}
13331337

13341338
func toConfigMatchSetOptionRestricted(a api.MatchSet_Type) (oc.MatchSetOptionsRestrictedType, error) {
13351339
var typ oc.MatchSetOptionsRestrictedType
13361340
switch a {
1337-
case api.MatchSet_ANY:
1341+
case api.MatchSet_TYPE_ANY:
13381342
typ = oc.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY
1339-
case api.MatchSet_INVERT:
1343+
case api.MatchSet_TYPE_INVERT:
13401344
typ = oc.MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT
13411345
default:
1342-
return typ, fmt.Errorf("invalid match type")
1346+
return typ, status.Errorf(codes.InvalidArgument, "invalid match restricted type %d", a)
13431347
}
13441348
return typ, nil
13451349
}

pkg/server/server_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ func TestListPathEnableFiltered(test *testing.T) {
334334
Conditions: &api.Conditions{
335335
PrefixSet: &api.MatchSet{
336336
Name: "d1",
337+
Type: api.MatchSet_TYPE_ANY,
337338
},
338339
},
339340
Actions: &api.Actions{
@@ -631,6 +632,7 @@ func TestListPathEnableFiltered(test *testing.T) {
631632
Conditions: &api.Conditions{
632633
PrefixSet: &api.MatchSet{
633634
Name: "d2",
635+
Type: api.MatchSet_TYPE_ANY,
634636
},
635637
},
636638
Actions: &api.Actions{
@@ -2463,6 +2465,7 @@ func TestWatchEvent(test *testing.T) {
24632465
Conditions: &api.Conditions{
24642466
PrefixSet: &api.MatchSet{
24652467
Name: "d1",
2468+
Type: api.MatchSet_TYPE_ANY,
24662469
},
24672470
},
24682471
Actions: &api.Actions{

0 commit comments

Comments
 (0)