diff --git a/feature/aft/aft_summary/otg_tests/route_summary_counters_test/route_summary_counters_test.go b/feature/aft/aft_summary/otg_tests/route_summary_counters_test/route_summary_counters_test.go index ee6193d047e..6d8a28e7880 100644 --- a/feature/aft/aft_summary/otg_tests/route_summary_counters_test/route_summary_counters_test.go +++ b/feature/aft/aft_summary/otg_tests/route_summary_counters_test/route_summary_counters_test.go @@ -339,7 +339,7 @@ func (d *dutData) Configure(t *testing.T, dut *ondatra.DUTDevice) { if !d.ipv4 { aftType = oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST } - bgpOC := cfgplugins.BuildBGPOCConfig(t, dut, d.routerID, aftType, d.neighborConfig) + bgpOC := cfgplugins.BuildBGPOCConfig(t, dut, d.routerID, []oc.E_BgpTypes_AFI_SAFI_TYPE{aftType}, d.neighborConfig) for _, a := range []attrs.Attributes{dutPort1, dutPort2} { ocName := dut.Port(t, a.Name).Name() diff --git a/feature/bgp/bgp_isis_redistribution/otg_tests/bgp_isis_redistribution_test/bgp_isis_redistribution_test.go b/feature/bgp/bgp_isis_redistribution/otg_tests/bgp_isis_redistribution_test/bgp_isis_redistribution_test.go index b732a371bf0..4778afbec5b 100644 --- a/feature/bgp/bgp_isis_redistribution/otg_tests/bgp_isis_redistribution_test/bgp_isis_redistribution_test.go +++ b/feature/bgp/bgp_isis_redistribution/otg_tests/bgp_isis_redistribution_test/bgp_isis_redistribution_test.go @@ -39,8 +39,8 @@ const ( bgpName = "BGP" maskLenExact = "exact" dummyAS = uint32(64655) - dutAS = uint32(65656) - ateAS = uint32(65657) + dutAS = uint32(64656) + ateAS = uint32(64657) v4Route = "203.10.113.0" v4TrafficStart = "203.10.113.1" v4DummyRoute = "192.51.100.0" diff --git a/feature/bgp/multipath/otg_tests/bgp_multipath_ecmp_test/bgp_multipath_ecmp_test.go b/feature/bgp/multipath/otg_tests/bgp_multipath_ecmp_test/bgp_multipath_ecmp_test.go index df68aa2f1e0..9af13da3fdd 100644 --- a/feature/bgp/multipath/otg_tests/bgp_multipath_ecmp_test/bgp_multipath_ecmp_test.go +++ b/feature/bgp/multipath/otg_tests/bgp_multipath_ecmp_test/bgp_multipath_ecmp_test.go @@ -167,8 +167,8 @@ func TestBGPSetup(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - bs := cfgplugins.NewBGPSession(t, cfgplugins.PortCount4) - bs.WithEBGP(t, oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST, true, !tc.enableMultiAS) + bs := cfgplugins.NewBGPSession(t, cfgplugins.PortCount4, nil) + bs.WithEBGP(t, []oc.E_BgpTypes_AFI_SAFI_TYPE{oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST}, []string{"port2", "port3", "port4"}, true, !tc.enableMultiAS) dni := deviations.DefaultNetworkInstance(bs.DUT) bgp := bs.DUTConf.GetOrCreateNetworkInstance(dni).GetOrCreateProtocol(oc.PolicyTypes_INSTALL_PROTOCOL_TYPE_BGP, "BGP").GetOrCreateBgp() gEBGP := bgp.GetOrCreateGlobal().GetOrCreateAfiSafi(oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST).GetOrCreateUseMultiplePaths().GetOrCreateEbgp() diff --git a/feature/bgp/multipath/otg_tests/bgp_multipath_wecmp_lbw_community_test/bgp_multipath_wecmp_test.go b/feature/bgp/multipath/otg_tests/bgp_multipath_wecmp_lbw_community_test/bgp_multipath_wecmp_test.go index 5a8a21640a7..a83268751d1 100644 --- a/feature/bgp/multipath/otg_tests/bgp_multipath_wecmp_lbw_community_test/bgp_multipath_wecmp_test.go +++ b/feature/bgp/multipath/otg_tests/bgp_multipath_wecmp_lbw_community_test/bgp_multipath_wecmp_test.go @@ -141,8 +141,8 @@ func verifyECMPLoadBalance(t *testing.T, ate *ondatra.ATEDevice, pc int, expecte } func TestBGPSetup(t *testing.T) { - bs := cfgplugins.NewBGPSession(t, cfgplugins.PortCount4) - bs.WithEBGP(t, oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST, true, false) + bs := cfgplugins.NewBGPSession(t, cfgplugins.PortCount4, nil) + bs.WithEBGP(t, []oc.E_BgpTypes_AFI_SAFI_TYPE{oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST}, []string{"port3", "port4"}, true, false) dni := deviations.DefaultNetworkInstance(bs.DUT) bgp := bs.DUTConf.GetOrCreateNetworkInstance(dni).GetOrCreateProtocol(oc.PolicyTypes_INSTALL_PROTOCOL_TYPE_BGP, "BGP").GetOrCreateBgp() gEBGP := bgp.GetOrCreateGlobal().GetOrCreateAfiSafi(oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST).GetOrCreateUseMultiplePaths().GetOrCreateEbgp() diff --git a/feature/bgp/policybase/otg_tests/aspath_and_community_test/aspath_and_community_test.go b/feature/bgp/policybase/otg_tests/aspath_and_community_test/aspath_and_community_test.go index e08a901bdaf..4394d2c4248 100644 --- a/feature/bgp/policybase/otg_tests/aspath_and_community_test/aspath_and_community_test.go +++ b/feature/bgp/policybase/otg_tests/aspath_and_community_test/aspath_and_community_test.go @@ -15,7 +15,6 @@ package aspath_and_community_test import ( - "sort" "testing" "time" @@ -139,8 +138,6 @@ func configureOTG(t *testing.T, bs *cfgplugins.BGPSession, prefixesV4 [][]string } devices := bs.ATETop.Devices().Items() - byName := func(i, j int) bool { return devices[i].Name() < devices[j].Name() } - sort.Slice(devices, byName) ipv4 := devices[1].Ethernets().Items()[0].Ipv4Addresses().Items()[0] bgp4Peer := devices[1].Bgp().Ipv4Interfaces().Items()[0].Peers().Items()[0] @@ -243,9 +240,8 @@ func verifyTraffic(t *testing.T, ate *ondatra.ATEDevice, ports int, testResults func TestCommunitySet(t *testing.T) { testResults := [5]bool{true, true, true, false, false} - bs := cfgplugins.NewBGPSession(t, cfgplugins.PortCount2) - bs.WithEBGP(t, oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST, true, true) - bs.WithEBGP(t, oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST, true, true) + bs := cfgplugins.NewBGPSession(t, cfgplugins.PortCount2, nil) + bs.WithEBGP(t, []oc.E_BgpTypes_AFI_SAFI_TYPE{oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST, oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST}, []string{"port2"}, true, true) configureOTG(t, bs, prefixesV4, prefixesV6) bs.PushAndStart(t) diff --git a/feature/bgp/policybase/otg_tests/aspath_test/aspath_test.go b/feature/bgp/policybase/otg_tests/aspath_test/aspath_test.go index b5b05f93d94..31d36dc6c63 100644 --- a/feature/bgp/policybase/otg_tests/aspath_test/aspath_test.go +++ b/feature/bgp/policybase/otg_tests/aspath_test/aspath_test.go @@ -15,7 +15,6 @@ package aspath_test import ( - "sort" "testing" "time" @@ -91,8 +90,6 @@ func configureImportBGPPolicy(t *testing.T, dut *ondatra.DUTDevice, ipv4 string, func configureOTG(t *testing.T, bs *cfgplugins.BGPSession, prefixesV4 [][]string, prefixesV6 [][]string, aspathMembers [][]uint32) { devices := bs.ATETop.Devices().Items() - byName := func(i, j int) bool { return devices[i].Name() < devices[j].Name() } - sort.Slice(devices, byName) ipv4 := devices[1].Ethernets().Items()[0].Ipv4Addresses().Items()[0] bgp4Peer := devices[1].Bgp().Ipv4Interfaces().Items()[0].Peers().Items()[0] @@ -187,9 +184,8 @@ type testCase struct { } func TestCommunitySet(t *testing.T) { - bs := cfgplugins.NewBGPSession(t, cfgplugins.PortCount2) - bs.WithEBGP(t, oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST, true, true) - bs.WithEBGP(t, oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST, true, true) + bs := cfgplugins.NewBGPSession(t, cfgplugins.PortCount2, nil) + bs.WithEBGP(t, []oc.E_BgpTypes_AFI_SAFI_TYPE{oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST, oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST}, []string{"port2"}, true, true) var aspathMembers = [][]uint32{ {100, 200, 300}, diff --git a/feature/bgp/policybase/otg_tests/community_test/community_test.go b/feature/bgp/policybase/otg_tests/community_test/community_test.go index 940e69e8ba6..01a2f5b9c14 100644 --- a/feature/bgp/policybase/otg_tests/community_test/community_test.go +++ b/feature/bgp/policybase/otg_tests/community_test/community_test.go @@ -15,7 +15,6 @@ package community_test import ( - "sort" "testing" "time" @@ -97,8 +96,6 @@ func configureImportBGPPolicy(t *testing.T, dut *ondatra.DUTDevice, ipv4 string, func configureOTG(t *testing.T, bs *cfgplugins.BGPSession, prefixesV4 [][]string, prefixesV6 [][]string, communityMembers [][][]int) { devices := bs.ATETop.Devices().Items() - byName := func(i, j int) bool { return devices[i].Name() < devices[j].Name() } - sort.Slice(devices, byName) ipv4 := devices[1].Ethernets().Items()[0].Ipv4Addresses().Items()[0] bgp4Peer := devices[1].Bgp().Ipv4Interfaces().Items()[0].Peers().Items()[0] @@ -201,9 +198,8 @@ type testCase struct { } func TestCommunitySet(t *testing.T) { - bs := cfgplugins.NewBGPSession(t, cfgplugins.PortCount2) - bs.WithEBGP(t, oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST, true, true) - bs.WithEBGP(t, oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST, true, true) + bs := cfgplugins.NewBGPSession(t, cfgplugins.PortCount2, nil) + bs.WithEBGP(t, []oc.E_BgpTypes_AFI_SAFI_TYPE{oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST, oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST}, []string{"port2"}, true, true) var communityMembers = [][][]int{ { diff --git a/internal/cfgplugins/bgp.go b/internal/cfgplugins/bgp.go index 23815ee23f6..62f00216b0e 100644 --- a/internal/cfgplugins/bgp.go +++ b/internal/cfgplugins/bgp.go @@ -41,15 +41,15 @@ const ( RPLPermitAll = "PERMIT-ALL" // DutAS dut AS - DutAS = uint32(65656) + DutAS = uint32(65501) // AteAS1 for ATE port1 - AteAS1 = uint32(65536) + AteAS1 = uint32(65511) // AteAS2 for ATE port2 - AteAS2 = uint32(65537) + AteAS2 = uint32(65512) // AteAS3 for ATE port3 - AteAS3 = uint32(65538) + AteAS3 = uint32(65513) // AteAS4 for ATE port4 - AteAS4 = uint32(65539) + AteAS4 = uint32(65514) // BGPPeerGroup1 for ATE port1 BGPPeerGroup1 = "BGP-PEER-GROUP1" @@ -149,15 +149,16 @@ type BGPSession struct { DUTConf *oc.Root ATETop gosnappi.Config - DUTPorts []*attrs.Attributes - ATEPorts []*attrs.Attributes - aftType oc.E_BgpTypes_AFI_SAFI_TYPE + DUTPorts []*attrs.Attributes + ATEPorts []*attrs.Attributes + afiTypes []oc.E_BgpTypes_AFI_SAFI_TYPE + networkInstance string } // NewBGPSession creates a new BGPSession using the default global config, and // configures the interfaces on the dut and the ate based in given topology port count. // Only supports 2 and 4 port DUT-ATE topology -func NewBGPSession(t *testing.T, pc PortCount) *BGPSession { +func NewBGPSession(t *testing.T, pc PortCount, ni *string) *BGPSession { conf := &BGPSession{ DUT: ondatra.DUT(t, "dut"), DUTConf: &oc.Root{}, @@ -186,15 +187,25 @@ func NewBGPSession(t *testing.T, pc PortCount) *BGPSession { conf.ATEIntfs[i] = conf.ATEPorts[i].AddToOTG(conf.ATETop, conf.OndatraATEPorts[i], conf.DUTPorts[i]) } } + + if ni == nil { + fptest.ConfigureDefaultNetworkInstance(t, conf.DUT) + conf.networkInstance = deviations.DefaultNetworkInstance(conf.DUT) + } else { + conf.networkInstance = *ni + } + return conf } // WithEBGP adds eBGP specific config -func (bs *BGPSession) WithEBGP(t *testing.T, aftype oc.E_BgpTypes_AFI_SAFI_TYPE, isSamePG, isSameAS bool) *BGPSession { - if aftype != oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST && aftype != oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST { - t.Fatalf("Unsupported AFI type: %v", bs.aftType) +func (bs *BGPSession) WithEBGP(t *testing.T, afiTypes []oc.E_BgpTypes_AFI_SAFI_TYPE, bgpPorts []string, isSamePG, isSameAS bool) *BGPSession { + for _, afiType := range afiTypes { + if afiType != oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST && afiType != oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST { + t.Fatalf("Unsupported AFI type: %v", afiType) + } } - bs.aftType = aftype + bs.afiTypes = afiTypes asNumbers := []uint32{AteAS1, AteAS2, AteAS3, AteAS4} if isSameAS { @@ -205,34 +216,36 @@ func (bs *BGPSession) WithEBGP(t *testing.T, aftype oc.E_BgpTypes_AFI_SAFI_TYPE, byName := func(i, j int) bool { return devices[i].Name() < devices[j].Name() } sort.Slice(devices, byName) for i, otgPort := range bs.ATEPorts { + if !containsValue(bgpPorts, otgPort.Name) { + continue + } bgp := devices[i].Bgp().SetRouterId(otgPort.IPv4) - switch aftype { - case oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST: - ipv4 := devices[i].Ethernets().Items()[0].Ipv4Addresses().Items()[0] - bgp4Peer := bgp.Ipv4Interfaces().Add().SetIpv4Name(ipv4.Name()).Peers().Add().SetName(devices[i].Name() + ".BGP4.peer") - bgp4Peer.SetPeerAddress(ipv4.Gateway()) - bgp4Peer.SetAsNumber(uint32(asNumbers[i])) - bgp4Peer.SetAsType(gosnappi.BgpV4PeerAsType.EBGP) - bgp4Peer.Capability().SetIpv4UnicastAddPath(true).SetIpv6UnicastAddPath(true) - bgp4Peer.LearnedInformationFilter().SetUnicastIpv4Prefix(true).SetUnicastIpv6Prefix(true) - case oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST: - ipv6 := devices[i].Ethernets().Items()[0].Ipv6Addresses().Items()[0] - bgp6Peer := bgp.Ipv6Interfaces().Add().SetIpv6Name(ipv6.Name()).Peers().Add().SetName(devices[i].Name() + ".BGP6.peer") - bgp6Peer.SetPeerAddress(ipv6.Gateway()) - bgp6Peer.SetAsNumber(uint32(asNumbers[i])) - bgp6Peer.SetAsType(gosnappi.BgpV6PeerAsType.EBGP) - bgp6Peer.Capability().SetIpv4UnicastAddPath(true).SetIpv6UnicastAddPath(true).SetExtendedNextHopEncoding(true) - bgp6Peer.LearnedInformationFilter().SetUnicastIpv4Prefix(true).SetUnicastIpv6Prefix(true) + for _, afiType := range afiTypes { + switch afiType { + case oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST: + ipv4 := devices[i].Ethernets().Items()[0].Ipv4Addresses().Items()[0] + bgp4Peer := bgp.Ipv4Interfaces().Add().SetIpv4Name(ipv4.Name()).Peers().Add().SetName(devices[i].Name() + ".BGP4.peer") + bgp4Peer.SetPeerAddress(ipv4.Gateway()) + bgp4Peer.SetAsNumber(uint32(asNumbers[i])) + bgp4Peer.SetAsType(gosnappi.BgpV4PeerAsType.EBGP) + bgp4Peer.Capability().SetIpv4UnicastAddPath(true).SetIpv6UnicastAddPath(true) + bgp4Peer.LearnedInformationFilter().SetUnicastIpv4Prefix(true).SetUnicastIpv6Prefix(true) + case oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST: + ipv6 := devices[i].Ethernets().Items()[0].Ipv6Addresses().Items()[0] + bgp6Peer := bgp.Ipv6Interfaces().Add().SetIpv6Name(ipv6.Name()).Peers().Add().SetName(devices[i].Name() + ".BGP6.peer") + bgp6Peer.SetPeerAddress(ipv6.Gateway()) + bgp6Peer.SetAsNumber(uint32(asNumbers[i])) + bgp6Peer.SetAsType(gosnappi.BgpV6PeerAsType.EBGP) + bgp6Peer.Capability().SetIpv4UnicastAddPath(true).SetIpv6UnicastAddPath(true).SetExtendedNextHopEncoding(true) + bgp6Peer.LearnedInformationFilter().SetUnicastIpv4Prefix(true).SetUnicastIpv6Prefix(true) + } } } - dni := bs.DUTConf.GetOrCreateNetworkInstance(deviations.DefaultNetworkInstance(bs.DUT)) - dni.SetType(oc.NetworkInstanceTypes_NETWORK_INSTANCE_TYPE_DEFAULT_INSTANCE) - - niProtocol := dni.GetOrCreateProtocol(PTBGP, bgpName) - neighborConfig := BuildNeigborConfig(isSamePG, isSameAS, len(bs.DUTPorts)) - niProtocol.Bgp = BuildBGPOCConfig(t, bs.DUT, dutPort1.IPv4, aftype, neighborConfig) + niProtocol := bs.DUTConf.GetOrCreateNetworkInstance(bs.networkInstance).GetOrCreateProtocol(PTBGP, bgpName) + neighborConfig := bs.buildNeigborConfig(isSamePG, isSameAS, bgpPorts) + niProtocol.Bgp = BuildBGPOCConfig(t, bs.DUT, dutPort1.IPv4, afiTypes, neighborConfig) err := bs.configureRoutingPolicy() if err != nil { @@ -273,7 +286,7 @@ func (bs *BGPSession) PushDUT(t testing.TB) error { if deviations.ExplicitInterfaceInDefaultVRF(bs.DUT) { for i := 0; i < len(bs.DUTPorts); i++ { - fptest.AssignToNetworkInstance(t, bs.DUT, bs.OndatraDUTPorts[i].Name(), deviations.DefaultNetworkInstance(bs.DUT), 0) + fptest.AssignToNetworkInstance(t, bs.DUT, bs.OndatraDUTPorts[i].Name(), bs.networkInstance, 0) } } if deviations.ExplicitPortSpeed(bs.DUT) { @@ -291,11 +304,13 @@ func (bs *BGPSession) PushAndStartATE(t testing.TB) { otg.PushConfig(t, bs.ATETop) otg.StartProtocols(t) - switch bs.aftType { - case oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST: - otgutils.WaitForARP(t.(*testing.T), otg, bs.ATETop, "IPv4") - case oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST: - otgutils.WaitForARP(t.(*testing.T), otg, bs.ATETop, "IPv6") + for _, afiType := range bs.afiTypes { + switch afiType { + case oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST: + otgutils.WaitForARP(t.(*testing.T), otg, bs.ATETop, "IPv4") + case oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST: + otgutils.WaitForARP(t.(*testing.T), otg, bs.ATETop, "IPv6") + } } } @@ -334,74 +349,74 @@ func VerifyOTGBGPEstablished(t *testing.T, ate *ondatra.ATEDevice) { // NeighborConfig to hold neighbor specific config type NeighborConfig struct { + Name string IPv4Neighbor string IPv6Neighbor string PeerGroup string AS uint32 } -// BuildNeigborConfig builds neighbor config based on given flags -func BuildNeigborConfig(isSamePG, isSameAS bool, portCount int) []*NeighborConfig { - nc := []*NeighborConfig{ - { - IPv4Neighbor: atePort1.IPv4, - IPv6Neighbor: atePort1.IPv6, - PeerGroup: BGPPeerGroup1, - AS: AteAS1, - }, - { - IPv4Neighbor: atePort2.IPv4, - IPv6Neighbor: atePort2.IPv6, - PeerGroup: BGPPeerGroup2, - AS: AteAS2, - }, - } - - if portCount == int(PortCount4) { - nc = append( - nc, - &NeighborConfig{ - IPv4Neighbor: atePort3.IPv4, - IPv6Neighbor: atePort3.IPv6, - PeerGroup: BGPPeerGroup3, - AS: AteAS3, - }, - &NeighborConfig{ - IPv4Neighbor: atePort4.IPv4, - IPv6Neighbor: atePort4.IPv6, - PeerGroup: BGPPeerGroup4, - AS: AteAS4, - }, - ) +// buildNeigborConfig builds neighbor config based on given flags +func (bs *BGPSession) buildNeigborConfig(isSamePG, isSameAS bool, bgpPorts []string) []*NeighborConfig { + nc1 := &NeighborConfig{ + Name: "port1", + IPv4Neighbor: atePort1.IPv4, + IPv6Neighbor: atePort1.IPv6, + PeerGroup: BGPPeerGroup1, + AS: AteAS1, + } + nc2 := &NeighborConfig{ + Name: "port2", + IPv4Neighbor: atePort2.IPv4, + IPv6Neighbor: atePort2.IPv6, + PeerGroup: BGPPeerGroup2, + AS: AteAS2, + } + nc3 := &NeighborConfig{ + Name: "port3", + IPv4Neighbor: atePort3.IPv4, + IPv6Neighbor: atePort3.IPv6, + PeerGroup: BGPPeerGroup3, + AS: AteAS3, + } + nc4 := &NeighborConfig{ + Name: "port4", + IPv4Neighbor: atePort4.IPv4, + IPv6Neighbor: atePort4.IPv6, + PeerGroup: BGPPeerGroup4, + AS: AteAS4, + } + ncAll := []*NeighborConfig{nc1, nc2, nc3, nc4} + + validNC := []*NeighborConfig{} + for _, nc := range ncAll[:len(bs.DUTPorts)] { + if containsValue(bgpPorts, nc.Name) { + validNC = append(validNC, nc) + } } if isSamePG { - for _, n := range nc { - n.PeerGroup = BGPPeerGroup1 + for _, nc := range validNC { + nc.PeerGroup = BGPPeerGroup1 } } if isSameAS { - for _, n := range nc { - n.AS = AteAS1 + for _, nc := range validNC { + nc.AS = AteAS1 } } - return nc + return validNC } // BuildBGPOCConfig builds the BGP OC config applying global, neighbors and peer-group config -func BuildBGPOCConfig(t *testing.T, dut *ondatra.DUTDevice, routerID string, aftType oc.E_BgpTypes_AFI_SAFI_TYPE, neighborConfig []*NeighborConfig) *oc.NetworkInstance_Protocol_Bgp { - afiSafiGlobal := map[oc.E_BgpTypes_AFI_SAFI_TYPE]*oc.NetworkInstance_Protocol_Bgp_Global_AfiSafi{ - aftType: { - AfiSafiName: aftType, +func BuildBGPOCConfig(t *testing.T, dut *ondatra.DUTDevice, routerID string, afiTypes []oc.E_BgpTypes_AFI_SAFI_TYPE, neighborConfig []*NeighborConfig) *oc.NetworkInstance_Protocol_Bgp { + afiSafiGlobal := map[oc.E_BgpTypes_AFI_SAFI_TYPE]*oc.NetworkInstance_Protocol_Bgp_Global_AfiSafi{} + for _, afiType := range afiTypes { + afiSafiGlobal[afiType] = &oc.NetworkInstance_Protocol_Bgp_Global_AfiSafi{ + AfiSafiName: afiType, Enabled: ygot.Bool(true), - }, - } - afiSafiNeighbor := map[oc.E_BgpTypes_AFI_SAFI_TYPE]*oc.NetworkInstance_Protocol_Bgp_Neighbor_AfiSafi{ - aftType: { - AfiSafiName: aftType, - Enabled: ygot.Bool(true), - }, + } } global := &oc.NetworkInstance_Protocol_Bgp_Global{ @@ -414,23 +429,30 @@ func BuildBGPOCConfig(t *testing.T, dut *ondatra.DUTDevice, routerID string, aft peerGroups := make(map[string]*oc.NetworkInstance_Protocol_Bgp_PeerGroup) var neighbor string for _, nc := range neighborConfig { - switch aftType { - case oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST: - neighbor = nc.IPv4Neighbor - case oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST: - neighbor = nc.IPv6Neighbor - default: - t.Fatalf("Unsupported AFI type: %v", aftType) - } - - neighbors[neighbor] = &oc.NetworkInstance_Protocol_Bgp_Neighbor{ - PeerAs: ygot.Uint32(nc.AS), - PeerGroup: ygot.String(nc.PeerGroup), - NeighborAddress: ygot.String(neighbor), - AfiSafi: afiSafiNeighbor, + for _, afiType := range afiTypes { + switch afiType { + case oc.BgpTypes_AFI_SAFI_TYPE_IPV4_UNICAST: + neighbor = nc.IPv4Neighbor + case oc.BgpTypes_AFI_SAFI_TYPE_IPV6_UNICAST: + neighbor = nc.IPv6Neighbor + default: + t.Fatalf("Unsupported AFI type: %v", afiType) + } + + neighbors[neighbor] = &oc.NetworkInstance_Protocol_Bgp_Neighbor{ + PeerAs: ygot.Uint32(nc.AS), + PeerGroup: ygot.String(nc.PeerGroup), + NeighborAddress: ygot.String(neighbor), + AfiSafi: map[oc.E_BgpTypes_AFI_SAFI_TYPE]*oc.NetworkInstance_Protocol_Bgp_Neighbor_AfiSafi{ + afiType: { + AfiSafiName: afiType, + Enabled: ygot.Bool(true), + }, + }, + } + + peerGroups[nc.PeerGroup] = getPeerGroup(nc.PeerGroup, dut, afiType) } - - peerGroups[nc.PeerGroup] = getPeerGroup(nc.PeerGroup, dut, aftType) } return &oc.NetworkInstance_Protocol_Bgp{ @@ -441,7 +463,7 @@ func BuildBGPOCConfig(t *testing.T, dut *ondatra.DUTDevice, routerID string, aft } // getPeerGroup build peer-config -func getPeerGroup(pgn string, dut *ondatra.DUTDevice, aftype oc.E_BgpTypes_AFI_SAFI_TYPE) *oc.NetworkInstance_Protocol_Bgp_PeerGroup { +func getPeerGroup(pgn string, dut *ondatra.DUTDevice, afiType oc.E_BgpTypes_AFI_SAFI_TYPE) *oc.NetworkInstance_Protocol_Bgp_PeerGroup { bgp := &oc.NetworkInstance_Protocol_Bgp{} pg := bgp.GetOrCreatePeerGroup(pgn) @@ -454,10 +476,19 @@ func getPeerGroup(pgn string, dut *ondatra.DUTDevice, aftype oc.E_BgpTypes_AFI_S } // policy under peer group AFI - afisafi := pg.GetOrCreateAfiSafi(aftype) + afisafi := pg.GetOrCreateAfiSafi(afiType) afisafi.Enabled = ygot.Bool(true) rpl := afisafi.GetOrCreateApplyPolicy() rpl.SetExportPolicy([]string{RPLPermitAll}) rpl.SetImportPolicy([]string{RPLPermitAll}) return pg } + +func containsValue[T comparable](slice []T, value T) bool { + for _, v := range slice { + if v == value { + return true + } + } + return false +}