From 97ad6b32ee61fd439c2edefbc371f748cdf82046 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Sun, 26 Aug 2018 22:04:12 +0900 Subject: api: change Family fixes #1812 Avoid the cast and use strightforward data strcuture; two variables for afi and safi instead of squashing two into one variable. Signed-off-by: FUJITA Tomonori --- internal/pkg/apiutil/attribute.go | 8 +-- internal/pkg/apiutil/attribute_test.go | 102 +++++++++++++++++++++++++------- internal/pkg/apiutil/capability.go | 30 +++++----- internal/pkg/apiutil/capability_test.go | 38 ++++++++---- internal/pkg/apiutil/util.go | 17 +++++- 5 files changed, 143 insertions(+), 52 deletions(-) (limited to 'internal/pkg/apiutil') diff --git a/internal/pkg/apiutil/attribute.go b/internal/pkg/apiutil/attribute.go index e149edbb..d87fff39 100644 --- a/internal/pkg/apiutil/attribute.go +++ b/internal/pkg/apiutil/attribute.go @@ -612,7 +612,7 @@ func NewMpReachNLRIAttributeFromNative(a *bgp.PathAttributeMpReachNLRI) *api.MpR } } return &api.MpReachNLRIAttribute{ - Family: uint32(bgp.AfiSafiToRouteFamily(a.AFI, a.SAFI)), + Family: ToApiFamily(a.AFI, a.SAFI), NextHops: nexthops, Nlris: MarshalNLRIs(a.Value), } @@ -620,7 +620,7 @@ func NewMpReachNLRIAttributeFromNative(a *bgp.PathAttributeMpReachNLRI) *api.MpR func NewMpUnreachNLRIAttributeFromNative(a *bgp.PathAttributeMpUnreachNLRI) *api.MpUnreachNLRIAttribute { return &api.MpUnreachNLRIAttribute{ - Family: uint32(bgp.AfiSafiToRouteFamily(a.AFI, a.SAFI)), + Family: ToApiFamily(a.AFI, a.SAFI), Nlris: MarshalNLRIs(a.Value), } } @@ -1153,7 +1153,7 @@ func unmarshalAttribute(an *any.Any) (bgp.PathAttributeInterface, error) { } return bgp.NewPathAttributeClusterList(a.Ids), nil case *api.MpReachNLRIAttribute: - rf := bgp.RouteFamily(a.Family) + rf := ToRouteFamily(a.Family) nlris, err := UnmarshalNLRIs(rf, a.Nlris) if err != nil { return nil, err @@ -1182,7 +1182,7 @@ func unmarshalAttribute(an *any.Any) (bgp.PathAttributeInterface, error) { attr.LinkLocalNexthop = linkLocalNexthop return attr, nil case *api.MpUnreachNLRIAttribute: - rf := bgp.RouteFamily(a.Family) + rf := ToRouteFamily(a.Family) nlris, err := UnmarshalNLRIs(rf, a.Nlris) if err != nil { return nil, err diff --git a/internal/pkg/apiutil/attribute_test.go b/internal/pkg/apiutil/attribute_test.go index df86427f..60f460cd 100644 --- a/internal/pkg/apiutil/attribute_test.go +++ b/internal/pkg/apiutil/attribute_test.go @@ -214,7 +214,10 @@ func Test_MpReachNLRIAttribute_IPv4_UC(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_IPv4_UC), + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_UNICAST, + }, NextHops: []string{"192.168.1.1"}, Nlris: nlris, } @@ -253,7 +256,10 @@ func Test_MpReachNLRIAttribute_IPv6_UC(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_IPv6_UC), + Family: &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_UNICAST, + }, NextHops: []string{"2001:db8::1", "2001:db8::2"}, Nlris: nlris, } @@ -294,7 +300,10 @@ func Test_MpReachNLRIAttribute_IPv4_MPLS(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_IPv4_MPLS), + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_MPLS_LABEL, + }, NextHops: []string{"192.168.1.1"}, Nlris: nlris, } @@ -335,7 +344,10 @@ func Test_MpReachNLRIAttribute_IPv6_MPLS(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_IPv6_MPLS), + Family: &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_MPLS_LABEL, + }, NextHops: []string{"2001:db8::1"}, Nlris: nlris, } @@ -372,7 +384,10 @@ func Test_MpReachNLRIAttribute_IPv4_ENCAP(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_IPv4_ENCAP), + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_ENCAPSULATION, + }, NextHops: []string{"192.168.1.1"}, Nlris: nlris, } @@ -409,7 +424,10 @@ func Test_MpReachNLRIAttribute_IPv6_ENCAP(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_IPv6_ENCAP), + Family: &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_ENCAPSULATION, + }, NextHops: []string{"2001:db8::1"}, Nlris: nlris, } @@ -453,7 +471,10 @@ func Test_MpReachNLRIAttribute_EVPN_AD_Route(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_EVPN), + Family: &api.Family{ + Afi: api.Family_AFI_L2VPN, + Safi: api.Family_SAFI_EVPN, + }, NextHops: []string{"192.168.1.1"}, Nlris: nlris, } @@ -499,7 +520,10 @@ func Test_MpReachNLRIAttribute_EVPN_MAC_IP_Route(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_EVPN), + Family: &api.Family{ + Afi: api.Family_AFI_L2VPN, + Safi: api.Family_SAFI_EVPN, + }, NextHops: []string{"192.168.1.1"}, Nlris: nlris, } @@ -538,7 +562,10 @@ func Test_MpReachNLRIAttribute_EVPN_MC_Route(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_EVPN), + Family: &api.Family{ + Afi: api.Family_AFI_L2VPN, + Safi: api.Family_SAFI_EVPN, + }, NextHops: []string{"192.168.1.1"}, Nlris: nlris, } @@ -581,7 +608,10 @@ func Test_MpReachNLRIAttribute_EVPN_ES_Route(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_EVPN), + Family: &api.Family{ + Afi: api.Family_AFI_L2VPN, + Safi: api.Family_SAFI_EVPN, + }, NextHops: []string{"192.168.1.1"}, Nlris: nlris, } @@ -627,7 +657,10 @@ func Test_MpReachNLRIAttribute_EVPN_Prefix_Route(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_EVPN), + Family: &api.Family{ + Afi: api.Family_AFI_L2VPN, + Safi: api.Family_SAFI_EVPN, + }, NextHops: []string{"192.168.1.1"}, Nlris: nlris, } @@ -667,7 +700,10 @@ func Test_MpReachNLRIAttribute_IPv4_VPN(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_IPv4_VPN), + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_MPLS_VPN, + }, NextHops: []string{"192.168.1.1"}, Nlris: nlris, } @@ -707,7 +743,10 @@ func Test_MpReachNLRIAttribute_IPv6_VPN(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_IPv6_VPN), + Family: &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_MPLS_VPN, + }, NextHops: []string{"2001:db8::1"}, Nlris: nlris, } @@ -747,7 +786,10 @@ func Test_MpReachNLRIAttribute_RTC_UC(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_RTC_UC), + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_ROUTE_TARGET_CONSTRAINTS, + }, NextHops: []string{"192.168.1.1"}, Nlris: nlris, } @@ -806,7 +848,10 @@ func Test_MpReachNLRIAttribute_FS_IPv4_UC(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_FS_IPv4_UC), + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_FLOW_SPEC_UNICAST, + }, // NextHops: // No nexthop required Nlris: nlris, } @@ -872,7 +917,10 @@ func Test_MpReachNLRIAttribute_FS_IPv4_VPN(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_FS_IPv4_VPN), + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_FLOW_SPEC_VPN, + }, // NextHops: // No nexthop required Nlris: nlris, } @@ -931,7 +979,10 @@ func Test_MpReachNLRIAttribute_FS_IPv6_UC(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_FS_IPv6_UC), + Family: &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_FLOW_SPEC_UNICAST, + }, // NextHops: // No nexthop required Nlris: nlris, } @@ -997,7 +1048,10 @@ func Test_MpReachNLRIAttribute_FS_IPv6_VPN(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_FS_IPv6_VPN), + Family: &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_FLOW_SPEC_VPN, + }, // NextHops: // No nexthop required Nlris: nlris, } @@ -1061,7 +1115,10 @@ func Test_MpReachNLRIAttribute_FS_L2_VPN(t *testing.T) { nlris = append(nlris, a) input := &api.MpReachNLRIAttribute{ - Family: uint32(bgp.RF_FS_L2_VPN), + Family: &api.Family{ + Afi: api.Family_AFI_L2VPN, + Safi: api.Family_SAFI_FLOW_SPEC_VPN, + }, // NextHops: // No nexthop required Nlris: nlris, } @@ -1100,8 +1157,11 @@ func Test_MpUnreachNLRIAttribute_IPv4_UC(t *testing.T) { nlris = append(nlris, a) input := &api.MpUnreachNLRIAttribute{ - Family: uint32(bgp.RF_IPv4_UC), - Nlris: nlris, + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_UNICAST, + }, + Nlris: nlris, } a, err = ptypes.MarshalAny(input) diff --git a/internal/pkg/apiutil/capability.go b/internal/pkg/apiutil/capability.go index 0e3b4c0b..d135713a 100644 --- a/internal/pkg/apiutil/capability.go +++ b/internal/pkg/apiutil/capability.go @@ -26,8 +26,9 @@ import ( ) func NewMultiProtocolCapability(a *bgp.CapMultiProtocol) *api.MultiProtocolCapability { + afi, safi := bgp.RouteFamilyToAfiSafi(a.CapValue) return &api.MultiProtocolCapability{ - Family: api.Family(a.CapValue), + Family: ToApiFamily(afi, safi), } } @@ -43,8 +44,8 @@ func NewExtendedNexthopCapability(a *bgp.CapExtendedNexthop) *api.ExtendedNextho tuples := make([]*api.ExtendedNexthopCapabilityTuple, 0, len(a.Tuples)) for _, t := range a.Tuples { tuples = append(tuples, &api.ExtendedNexthopCapabilityTuple{ - NlriFamily: api.Family(bgp.AfiSafiToRouteFamily(t.NLRIAFI, uint8(t.NLRISAFI))), - NexthopFamily: api.Family(bgp.AfiSafiToRouteFamily(t.NexthopAFI, bgp.SAFI_UNICAST)), + NlriFamily: ToApiFamily(t.NLRIAFI, uint8(t.NLRISAFI)), + NexthopFamily: ToApiFamily(t.NexthopAFI, bgp.SAFI_UNICAST), }) } return &api.ExtendedNexthopCapability{ @@ -56,7 +57,7 @@ func NewGracefulRestartCapability(a *bgp.CapGracefulRestart) *api.GracefulRestar tuples := make([]*api.GracefulRestartCapabilityTuple, 0, len(a.Tuples)) for _, t := range a.Tuples { tuples = append(tuples, &api.GracefulRestartCapabilityTuple{ - Family: api.Family(bgp.AfiSafiToRouteFamily(t.AFI, uint8(t.SAFI))), + Family: ToApiFamily(t.AFI, t.SAFI), Flags: uint32(t.Flags), }) } @@ -76,8 +77,9 @@ func NewFourOctetASNumberCapability(a *bgp.CapFourOctetASNumber) *api.FourOctetA func NewAddPathCapability(a *bgp.CapAddPath) *api.AddPathCapability { tuples := make([]*api.AddPathCapabilityTuple, 0, len(a.Tuples)) for _, t := range a.Tuples { + afi, safi := bgp.RouteFamilyToAfiSafi(t.RouteFamily) tuples = append(tuples, &api.AddPathCapabilityTuple{ - Family: api.Family(t.RouteFamily), + Family: ToApiFamily(afi, safi), Mode: api.AddPathMode(t.Mode), }) } @@ -94,7 +96,7 @@ func NewLongLivedGracefulRestartCapability(a *bgp.CapLongLivedGracefulRestart) * tuples := make([]*api.LongLivedGracefulRestartCapabilityTuple, 0, len(a.Tuples)) for _, t := range a.Tuples { tuples = append(tuples, &api.LongLivedGracefulRestartCapabilityTuple{ - Family: api.Family(bgp.AfiSafiToRouteFamily(t.AFI, uint8(t.SAFI))), + Family: ToApiFamily(t.AFI, uint8(t.SAFI)), Flags: uint32(t.Flags), Time: t.RestartTime, }) @@ -165,7 +167,7 @@ func unmarshalCapability(a *any.Any) (bgp.ParameterCapabilityInterface, error) { } switch a := value.Message.(type) { case *api.MultiProtocolCapability: - return bgp.NewCapMultiProtocol(bgp.RouteFamily(a.Family)), nil + return bgp.NewCapMultiProtocol(ToRouteFamily(a.Family)), nil case *api.RouteRefreshCapability: return bgp.NewCapRouteRefresh(), nil case *api.CarryingLabelInfoCapability: @@ -174,15 +176,15 @@ func unmarshalCapability(a *any.Any) (bgp.ParameterCapabilityInterface, error) { tuples := make([]*bgp.CapExtendedNexthopTuple, 0, len(a.Tuples)) for _, t := range a.Tuples { var nhAfi uint16 - switch t.NexthopFamily { - case api.Family_IPv4: + switch t.NexthopFamily.Afi { + case api.Family_AFI_IP: nhAfi = bgp.AFI_IP - case api.Family_IPv6: + case api.Family_AFI_IP6: nhAfi = bgp.AFI_IP6 default: return nil, fmt.Errorf("invalid address family for nexthop afi in extended nexthop capability: %s", t.NexthopFamily) } - tuples = append(tuples, bgp.NewCapExtendedNexthopTuple(bgp.RouteFamily(t.NlriFamily), nhAfi)) + tuples = append(tuples, bgp.NewCapExtendedNexthopTuple(ToRouteFamily(t.NlriFamily), nhAfi)) } return bgp.NewCapExtendedNexthop(tuples), nil case *api.GracefulRestartCapability: @@ -192,7 +194,7 @@ func unmarshalCapability(a *any.Any) (bgp.ParameterCapabilityInterface, error) { if t.Flags&0x80 > 0 { forward = true } - tuples = append(tuples, bgp.NewCapGracefulRestartTuple(bgp.RouteFamily(t.Family), forward)) + tuples = append(tuples, bgp.NewCapGracefulRestartTuple(ToRouteFamily(t.Family), forward)) } var restarting bool if a.Flags&0x08 > 0 { @@ -208,7 +210,7 @@ func unmarshalCapability(a *any.Any) (bgp.ParameterCapabilityInterface, error) { case *api.AddPathCapability: tuples := make([]*bgp.CapAddPathTuple, 0, len(a.Tuples)) for _, t := range a.Tuples { - tuples = append(tuples, bgp.NewCapAddPathTuple(bgp.RouteFamily(t.Family), bgp.BGPAddPathMode(t.Mode))) + tuples = append(tuples, bgp.NewCapAddPathTuple(ToRouteFamily(t.Family), bgp.BGPAddPathMode(t.Mode))) } return bgp.NewCapAddPath(tuples), nil case *api.EnhancedRouteRefreshCapability: @@ -220,7 +222,7 @@ func unmarshalCapability(a *any.Any) (bgp.ParameterCapabilityInterface, error) { if t.Flags&0x80 > 0 { forward = true } - tuples = append(tuples, bgp.NewCapLongLivedGracefulRestartTuple(bgp.RouteFamily(t.Family), forward, t.Time)) + tuples = append(tuples, bgp.NewCapLongLivedGracefulRestartTuple(ToRouteFamily(t.Family), forward, t.Time)) } return bgp.NewCapLongLivedGracefulRestart(tuples), nil case *api.RouteRefreshCiscoCapability: diff --git a/internal/pkg/apiutil/capability_test.go b/internal/pkg/apiutil/capability_test.go index 0872ed58..9a703a3a 100644 --- a/internal/pkg/apiutil/capability_test.go +++ b/internal/pkg/apiutil/capability_test.go @@ -28,7 +28,10 @@ func Test_MultiProtocolCapability(t *testing.T) { assert := assert.New(t) input := &api.MultiProtocolCapability{ - Family: api.Family_IPv4, + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_UNICAST, + }, } a, err := ptypes.MarshalAny(input) @@ -76,8 +79,14 @@ func Test_ExtendedNexthopCapability(t *testing.T) { input := &api.ExtendedNexthopCapability{ Tuples: []*api.ExtendedNexthopCapabilityTuple{ { - NlriFamily: api.Family_IPv4, - NexthopFamily: api.Family_IPv6, + NlriFamily: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_UNICAST, + }, + NexthopFamily: &api.Family{ + Afi: api.Family_AFI_IP6, + Safi: api.Family_SAFI_UNICAST, + }, }, }, } @@ -104,8 +113,11 @@ func Test_GracefulRestartCapability(t *testing.T) { Time: 90, Tuples: []*api.GracefulRestartCapabilityTuple{ { - Family: api.Family_IPv4, - Flags: 0x80, // forward + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_UNICAST, + }, + Flags: 0x80, // forward }, }, } @@ -152,8 +164,11 @@ func Test_AddPathCapability(t *testing.T) { input := &api.AddPathCapability{ Tuples: []*api.AddPathCapabilityTuple{ { - Family: api.Family_IPv4, - Mode: api.AddPathMode_MODE_BOTH, + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_UNICAST, + }, + Mode: api.AddPathMode_MODE_BOTH, }, }, } @@ -192,9 +207,12 @@ func Test_LongLivedGracefulRestartCapability(t *testing.T) { input := &api.LongLivedGracefulRestartCapability{ Tuples: []*api.LongLivedGracefulRestartCapabilityTuple{ { - Family: api.Family_IPv4, - Flags: 0x80, // forward - Time: 90, + Family: &api.Family{ + Afi: api.Family_AFI_IP, + Safi: api.Family_SAFI_UNICAST, + }, + Flags: 0x80, // forward + Time: 90, }, }, } diff --git a/internal/pkg/apiutil/util.go b/internal/pkg/apiutil/util.go index 45cf1a04..4c18b752 100644 --- a/internal/pkg/apiutil/util.go +++ b/internal/pkg/apiutil/util.go @@ -69,7 +69,7 @@ func NewPath(nlri bgp.AddrPrefixInterface, isWithdraw bool, attrs []bgp.PathAttr AnyPattrs: MarshalPathAttributes(attrs), Age: age.Unix(), IsWithdraw: isWithdraw, - Family: uint32(bgp.AfiSafiToRouteFamily(nlri.AFI(), nlri.SAFI())), + Family: ToApiFamily(nlri.AFI(), nlri.SAFI()), Identifier: nlri.PathIdentifier(), } } @@ -88,9 +88,9 @@ func getNLRI(family bgp.RouteFamily, buf []byte) (bgp.AddrPrefixInterface, error func GetNativeNlri(p *api.Path) (bgp.AddrPrefixInterface, error) { if len(p.Nlri) > 0 { - return getNLRI(bgp.RouteFamily(p.Family), p.Nlri) + return getNLRI(ToRouteFamily(p.Family), p.Nlri) } - return UnmarshalNLRI(bgp.RouteFamily(p.Family), p.AnyNlri) + return UnmarshalNLRI(ToRouteFamily(p.Family), p.AnyNlri) } func GetNativePathAttributes(p *api.Path) ([]bgp.PathAttributeInterface, error) { @@ -112,3 +112,14 @@ func GetNativePathAttributes(p *api.Path) ([]bgp.PathAttributeInterface, error) } return UnmarshalPathAttributes(p.AnyPattrs) } + +func ToRouteFamily(f *api.Family) bgp.RouteFamily { + return bgp.AfiSafiToRouteFamily(uint16(f.Afi), uint8(f.Safi)) +} + +func ToApiFamily(afi uint16, safi uint8) *api.Family { + return &api.Family{ + Afi: api.Family_Afi(afi), + Safi: api.Family_Safi(safi), + } +} -- cgit v1.2.3