From 721337d0a68584fa71d32a1f2f69a2e1e80c49fc Mon Sep 17 00:00:00 2001 From: "Claudio D. Gasparini" Date: Fri, 9 Mar 2018 14:02:16 +0100 Subject: [PATCH] BGPCEP-754: Fix NPE and rework - Fix npe - Simplify PeerTracker - Fix test name Change-Id: I239d634a98799c73220e55cbb9b295b84576f7ec Signed-off-by: Claudio D. Gasparini --- .../spi/registry/StatementRegistry.java | 5 +- ...sPathLength.java => AsPathLengthTest.java} | 2 +- .../statement/ExportDefaultStatementTest.java | 37 +----- .../statement/ImportDefaultStatementTest.java | 7 -- .../policy/statement/PrefixMatchTest.java | 108 ------------------ .../BGPRouteEntryExportParametersImpl.java | 43 +++++++ .../impl/add/AddPathAbstractRouteEntry.java | 7 +- .../impl/base/BaseAbstractRouteEntry.java | 8 +- .../bgp/mode/impl/BGPPeerTrackerMock.java | 4 - .../bgp/rib/impl/BGPPeerTrackerImpl.java | 62 ++-------- .../protocol/bgp/rib/spi/BGPPeerTracker.java | 40 +------ .../policy/BGPRouteEntryExportParameters.java | 2 +- 12 files changed, 72 insertions(+), 253 deletions(-) rename bgp/openconfig-rp-spi/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/{AsPathLength.java => AsPathLengthTest.java} (99%) delete mode 100644 bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/PrefixMatchTest.java create mode 100644 bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/BGPRouteEntryExportParametersImpl.java diff --git a/bgp/openconfig-rp-spi/src/main/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/registry/StatementRegistry.java b/bgp/openconfig-rp-spi/src/main/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/registry/StatementRegistry.java index 34eb75b23c..fc08ddefa6 100644 --- a/bgp/openconfig-rp-spi/src/main/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/registry/StatementRegistry.java +++ b/bgp/openconfig-rp-spi/src/main/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/registry/StatementRegistry.java @@ -46,10 +46,11 @@ public final class StatementRegistry implements StatementRegistryConsumer, State final BGPRouteEntryExportParameters routeEntryExportParameters, final RouteAttributeContainer attributes, final Statement statement) { - if (!this.conditionsRegistry.matchExportConditions( + final Attributes att = attributes.getAttributes(); + if (att == null || !this.conditionsRegistry.matchExportConditions( routeEntryInfo, routeEntryExportParameters, - attributes.getAttributes(), + att, statement.getConditions())) { return attributes; } diff --git a/bgp/openconfig-rp-spi/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/AsPathLength.java b/bgp/openconfig-rp-spi/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/AsPathLengthTest.java similarity index 99% rename from bgp/openconfig-rp-spi/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/AsPathLength.java rename to bgp/openconfig-rp-spi/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/AsPathLengthTest.java index b3bebb5152..d970c6c41f 100644 --- a/bgp/openconfig-rp-spi/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/AsPathLength.java +++ b/bgp/openconfig-rp-spi/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/spi/AsPathLengthTest.java @@ -26,7 +26,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mess import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.AsPathBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.SegmentsBuilder; -public class AsPathLength extends AbstractStatementRegistryTest { +public class AsPathLengthTest extends AbstractStatementRegistryTest { @Mock private BGPRouteEntryExportParameters exportParameters; private List basicStatements; diff --git a/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/ExportDefaultStatementTest.java b/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/ExportDefaultStatementTest.java index 9761cad4fd..97286e0279 100644 --- a/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/ExportDefaultStatementTest.java +++ b/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/ExportDefaultStatementTest.java @@ -15,7 +15,6 @@ import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement. import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.createPathInput; import static org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.ExportAttributeTestUtil.createPathInputWithAs; -import com.google.common.collect.ImmutableMap; import java.util.List; import org.junit.Before; import org.junit.Test; @@ -24,16 +23,10 @@ import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBas import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer; import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters; import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; public class ExportDefaultStatementTest extends AbstractStatementRegistryConsumerTest { - private static final YangInstanceIdentifier.NodeIdentifierWithPredicates ROUTE_ID_PA - = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Ipv4Route.QNAME, - ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "1.2.3.4/32")); @Mock private BGPRouteEntryExportParameters exportParameters; private List defaultExportStatements; @@ -47,9 +40,6 @@ public class ExportDefaultStatementTest extends AbstractStatementRegistryConsume this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER); } - /** - * To eBGP. - */ @Test public void testToEbgp() { final Statement statement = getStatementAndSetToRole("to-external", PeerRole.Ebgp); @@ -63,10 +53,6 @@ public class ExportDefaultStatementTest extends AbstractStatementRegistryConsume assertApplyExportStatement(statement, PeerRole.RrClient, attributeContainer, expectedOutput); } - - /** - * From iBGP To iBGP. - */ @Test public void testFromInternalToInternal() { final Statement statement = getStatementAndSetToRole("from-internal-to-internal", PeerRole.Ibgp); @@ -74,9 +60,6 @@ public class ExportDefaultStatementTest extends AbstractStatementRegistryConsume assertApplyExportStatement(statement, PeerRole.Ibgp, attributeContainer, null); } - /** - * From iBGP To iBGP. - */ @Test public void testFromExternalToInternal() { final Statement statement = getStatementAndSetToRole("from-external-to-internal", PeerRole.Ibgp); @@ -84,9 +67,6 @@ public class ExportDefaultStatementTest extends AbstractStatementRegistryConsume assertApplyExportStatement(statement, PeerRole.Ebgp, attributeContainer, attributeContainer.getAttributes()); } - /** - * From Internal To iBGP. - */ @Test public void testFromOdlInternalToInternal() { final Statement statement = getStatementAndSetToRole("from-odl-internal-to-internal-or-rr-client", @@ -95,9 +75,6 @@ public class ExportDefaultStatementTest extends AbstractStatementRegistryConsume assertApplyExportStatement(statement, PeerRole.Internal, attributeContainer, createClusterInput()); } - /** - * From RR-Client To iBGP. - */ @Test public void testFromRRclientToInternal() { final Statement statement = getStatementAndSetToRole("from-rr-client-to-internal", PeerRole.Ibgp); @@ -105,9 +82,6 @@ public class ExportDefaultStatementTest extends AbstractStatementRegistryConsume assertApplyExportStatement(statement, PeerRole.RrClient, attributeContainer, createInputWithOriginator()); } - /** - * Any role To Internal. - */ @Test public void testOdlInternal() { final Statement statement = getStatementAndSetToRole("to-odl-internal", PeerRole.Internal); @@ -119,9 +93,6 @@ public class ExportDefaultStatementTest extends AbstractStatementRegistryConsume assertApplyExportStatement(statement, PeerRole.RrClient, attributeContainer, null); } - /** - * To RrClient. - */ @Test public void testFromExternalToRRClient() { final Statement statement = getStatementAndSetToRole("from-external-to-route-reflector", PeerRole.RrClient); @@ -129,9 +100,6 @@ public class ExportDefaultStatementTest extends AbstractStatementRegistryConsume assertApplyExportStatement(statement, PeerRole.Ebgp, attributeContainer, attributeContainer.getAttributes()); } - /** - * To RrClient. - */ @Test public void testFromInternalOrRRClientToRRClient() { final Statement statement = getStatementAndSetToRole("from-internal-or-rr-client-to-route-reflector", @@ -143,11 +111,8 @@ public class ExportDefaultStatementTest extends AbstractStatementRegistryConsume assertApplyExportStatement(statement, PeerRole.RrClient, attributeContainer, expectedOutput); } - /** - * To RrClient. - */ @Test - public void testFromOdlInternalRRClient() { + public void testFromOdlInternalToRRClient() { final Statement statement = getStatementAndSetToRole("from-odl-internal-to-internal-or-rr-client", PeerRole.RrClient); final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput()); diff --git a/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/ImportDefaultStatementTest.java b/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/ImportDefaultStatementTest.java index d7836c26c3..71326b8cbd 100644 --- a/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/ImportDefaultStatementTest.java +++ b/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/ImportDefaultStatementTest.java @@ -28,7 +28,6 @@ public class ImportDefaultStatementTest extends AbstractStatementRegistryConsume private List defaultImportStatements; private PolicyRIBBaseParametersImpl baseAttributes; - @Before @Override public void setUp() throws Exception { @@ -37,9 +36,6 @@ public class ImportDefaultStatementTest extends AbstractStatementRegistryConsume this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER); } - /** - * From eBGP. - */ @Test public void testFromEbgp() { final Statement statement = getStatement("from-external"); @@ -51,9 +47,6 @@ public class ImportDefaultStatementTest extends AbstractStatementRegistryConsume ImportAttributeTestUtil.createOutput()); } - /** - * From NonExternal. - */ @Test public void testFromNonExternal() { final Statement statement = getStatement("from-non-external"); diff --git a/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/PrefixMatchTest.java b/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/PrefixMatchTest.java deleted file mode 100644 index 41e271a37c..0000000000 --- a/bgp/openconfig-rp-statement/src/test/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/PrefixMatchTest.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement; - -import org.junit.Ignore; - -@Ignore -public class PrefixMatchTest extends AbstractStatementRegistryConsumerTest {/* - @Mock - private BGPRouteEntryExportParameters exportParameters; - private List basicStatements; - private PolicyRIBBaseParametersImpl baseAttributes; - - @Before - @Override - public void setUp() throws Exception { - super.setUp(); - this.basicStatements = loadStatement("basic-statements-test"); - this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER); - } - - @Test - public void testPrefixRange() { - //RANGE - doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME, - ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.191.0/22"))) - .when(this.exportParameters).getRouteId(); - Statement statement = this.basicStatements.stream() - .filter(st -> st.getName().equals("reject-prefix-test")).findFirst().get(); - final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput()); - RouteAttributeContainer result = this.statementRegistry.applyExportStatement( - this.baseAttributes, - this.exportParameters, - attributeContainer, - statement); - assertNull(result.getAttributes()); - - doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME, - ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "14.3.191.0/22"))) - .when(this.exportParameters).getRouteId(); - result = this.statementRegistry.applyExportStatement( - this.baseAttributes, - this.exportParameters, - attributeContainer, - statement); - assertNotNull(result.getAttributes()); - } - - @Test - public void testPrefixExact() { - /* final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput()); - Statement statement = this.basicStatements.stream() - .filter(st -> st.getName().equals("reject-prefix-test")).findFirst().get(); - - doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME, - ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.192.0/21"))) - .when(this.exportParameters).getRouteId(); - RouteAttributeContainer result = this.statementRegistry.applyExportStatement( - this.baseAttributes, - this.exportParameters, - attributeContainer, - statement); - assertNull(result.getAttributes()); - - doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME, - ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "11.3.192.0/21"))) - .when(this.exportParameters).getRouteId(); - result = this.statementRegistry.applyExportStatement( - this.baseAttributes, - this.exportParameters, - attributeContainer, - statement); - assertNotNull(result.getAttributes()); - } - - @Test - public void testPrefixInverse() { - /* final RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(createClusterInput()); - - doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME, - ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "10.3.192.0/21"))) - .when(this.exportParameters).getRouteId(); - final Statement statement = this.basicStatements.stream() - .filter(st -> st.getName().equals("reject-prefix-inverse-test")).findFirst().get(); - RouteAttributeContainer result = this.statementRegistry.applyExportStatement( - this.baseAttributes, - this.exportParameters, - attributeContainer, - statement); - assertNotNull(result.getAttributes()); - - doReturn(new NodeIdentifierWithPredicates(Ipv4Route.QNAME, - ImmutableMap.of(QName.create(Ipv4Route.QNAME, "prefix").intern(), "11.3.192.0/21"))) - .when(this.exportParameters).getRouteId(); - result = this.statementRegistry.applyExportStatement( - this.baseAttributes, - this.exportParameters, - attributeContainer, - statement); - assertNull(result.getAttributes()); -}*/ -} diff --git a/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/BGPRouteEntryExportParametersImpl.java b/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/BGPRouteEntryExportParametersImpl.java new file mode 100644 index 0000000000..84e3602807 --- /dev/null +++ b/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/BGPRouteEntryExportParametersImpl.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.protocol.bgp.mode.impl; + +import org.opendaylight.protocol.bgp.rib.spi.Peer; +import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole; + +public final class BGPRouteEntryExportParametersImpl implements BGPRouteEntryExportParameters { + private final Peer fromPeer; + private final Peer toPeer; + + public BGPRouteEntryExportParametersImpl(final Peer fromPeer, final Peer toPeer) { + this.fromPeer = fromPeer; + this.toPeer = toPeer; + } + + @Override + public PeerRole getFromPeerRole() { + return this.fromPeer.getRole(); + } + + @Override + public PeerId getFromPeerId() { + return this.fromPeer.getPeerId(); + } + + @Override + public PeerRole getToPeerRole() { + return this.toPeer.getRole(); + } + + @Override + public PeerId getToPeerId() { + return this.toPeer.getPeerId(); + } +} diff --git a/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/add/AddPathAbstractRouteEntry.java b/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/add/AddPathAbstractRouteEntry.java index 57b7226f59..c6f7830d51 100644 --- a/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/add/AddPathAbstractRouteEntry.java +++ b/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/add/AddPathAbstractRouteEntry.java @@ -19,6 +19,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction; import org.opendaylight.protocol.bgp.mode.spi.AbstractRouteEntry; import org.opendaylight.protocol.bgp.rib.spi.BGPPeerTracker; import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker; +import org.opendaylight.protocol.bgp.rib.spi.Peer; import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup; import org.opendaylight.protocol.bgp.rib.spi.RIBSupport; import org.opendaylight.protocol.bgp.rib.spi.RouterIds; @@ -180,8 +181,9 @@ public abstract class AddPathAbstractRouteEntry extends AbstractRouteEntry { final boolean destPeerSupAddPath = peerPT.isAddPathSupportedByPeer(destPeer); if (filterRoutes(routePeerId, destPeer, localTK) diff --git a/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/base/BaseAbstractRouteEntry.java b/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/base/BaseAbstractRouteEntry.java index b240b3bf73..9cc49dc593 100644 --- a/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/base/BaseAbstractRouteEntry.java +++ b/bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/base/BaseAbstractRouteEntry.java @@ -14,6 +14,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction; import org.opendaylight.protocol.bgp.mode.spi.AbstractRouteEntry; import org.opendaylight.protocol.bgp.rib.spi.BGPPeerTracker; import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker; +import org.opendaylight.protocol.bgp.rib.spi.Peer; import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup; import org.opendaylight.protocol.bgp.rib.spi.RIBSupport; import org.opendaylight.protocol.bgp.rib.spi.entry.RouteEntryDependenciesContainer; @@ -133,8 +134,9 @@ abstract class BaseAbstractRouteEntry extends AbstractRouteEntry { if (routeIdDest == null) { routeIdDest = routeId; } + final Peer fromPeer = this.peerTracker.getPeer(path.getPeerId()); final ContainerNode effAttrib = peerGroup.effectiveAttributes( - this.peerTracker.getRole(path.getPeerId()), path.getAttributes()); + fromPeer.getRole(), path.getAttributes()); final YangInstanceIdentifier rootPath = entryInfo.getRootPath(); writeRoute(toPeerId, getAdjRibOutYII(ribSupport, rootPath, routeIdDest, localTK), effAttrib, createValue(routeIdDest, path), ribSupport, tx); @@ -198,8 +200,8 @@ abstract class BaseAbstractRouteEntry extends AbstractRouteEntry { for (final PeerRole role : PeerRole.values()) { final PeerExportGroup peerGroup = peerPT.getPeerGroup(role); if (peerGroup != null) { - final ContainerNode effAttrib = peerGroup - .effectiveAttributes(this.peerTracker.getRole(fromPeerId), attributes); + final Peer fromPeer = this.peerTracker.getPeer(fromPeerId); + final ContainerNode effAttrib = peerGroup.effectiveAttributes(fromPeer.getRole(), attributes); peerGroup.forEach((destPeer, rootPath) -> { if (!filterRoutes(fromPeerId, destPeer, localTK)) { return; diff --git a/bgp/path-selection-mode/src/test/java/org/opendaylight/protocol/bgp/mode/impl/BGPPeerTrackerMock.java b/bgp/path-selection-mode/src/test/java/org/opendaylight/protocol/bgp/mode/impl/BGPPeerTrackerMock.java index d28649a32d..a5690412d3 100644 --- a/bgp/path-selection-mode/src/test/java/org/opendaylight/protocol/bgp/mode/impl/BGPPeerTrackerMock.java +++ b/bgp/path-selection-mode/src/test/java/org/opendaylight/protocol/bgp/mode/impl/BGPPeerTrackerMock.java @@ -44,12 +44,8 @@ public class BGPPeerTrackerMock { final PeerId pId = new PeerId("bgp://0.0.0.1"); doReturn(this.peerMock).when(this.peerTracker).getPeer(eq(pId)); doReturn(this.peerMock).when(this.peerTracker).getPeer(eq(PEER_ID)); - doReturn(PeerRole.Ibgp).when(this.peerTracker).getRole(eq(pId)); - doReturn(PeerRole.Ibgp).when(this.peerTracker).getRole(eq(PEER_ID)); doReturn(true).when(this.peerMock).supportsTable(Mockito.eq(TABLES_KEY)); doReturn(PeerRole.Ibgp).when(this.peerMock).getRole(); - - doReturn(this.peerMock2).when(this.peerTracker).getPeer(eq(PEER_ID2)); doReturn(false).when(this.peerMock2).supportsTable(Mockito.eq(TABLES_KEY)); } diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeerTrackerImpl.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeerTrackerImpl.java index 642d27fbde..aea5facf73 100644 --- a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeerTrackerImpl.java +++ b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeerTrackerImpl.java @@ -7,82 +7,42 @@ */ package org.opendaylight.protocol.bgp.rib.impl; -import java.util.ArrayList; +import com.google.common.collect.ImmutableList; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; import javax.annotation.concurrent.GuardedBy; import org.opendaylight.protocol.bgp.rib.spi.BGPPeerTracker; import org.opendaylight.protocol.bgp.rib.spi.Peer; import org.opendaylight.protocol.concepts.AbstractRegistration; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerId; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.TablesKey; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; public final class BGPPeerTrackerImpl implements BGPPeerTracker { @GuardedBy("this") - private final List peers = new ArrayList<>(); - private Map> rolePerPeerId; + private Map peers = new HashMap<>(); + private ImmutableList peersList; @Override public synchronized AbstractRegistration registerPeer(final Peer peer) { - this.peers.add(peer); - this.rolePerPeerId = this.peers.stream().collect(Collectors.groupingBy(Peer::getRole, - Collectors.mapping(Peer::getPeerId, Collectors.toList()))); + this.peers.put(peer.getPeerId(), peer); + this.peersList = ImmutableList.copyOf(this.peers.values()); return new AbstractRegistration() { @Override protected void removeRegistration() { synchronized (BGPPeerTrackerImpl.this) { - BGPPeerTrackerImpl.this.peers.remove(peer); - BGPPeerTrackerImpl.this.rolePerPeerId = BGPPeerTrackerImpl.this.peers - .stream().collect(Collectors.groupingBy(Peer::getRole, - Collectors.mapping(Peer::getPeerId, Collectors.toList()))); + BGPPeerTrackerImpl.this.peers.remove(peer.getPeerId()); } } }; } @Override - public Peer getPeer(final PeerId peerId) { - synchronized (this.peers) { - return this.peers.stream().filter(peer -> peer.getPeerId().equals(peerId)).findFirst().orElse(null); - } + public synchronized Peer getPeer(final PeerId peerId) { + return this.peers.get(peerId); } @Override - public boolean supportsTable(final PeerId peerIdOfNewPeer, final TablesKey tableKey) { - final Peer peer = getPeer(peerIdOfNewPeer); - return peer != null && peer.supportsTable(tableKey); - } - - @Override - public synchronized Map> getRoles() { - return this.rolePerPeerId; - } - - @Override - public boolean supportsAddPathSupported(final PeerId toPeer, final TablesKey localTK) { - final Peer peer = getPeer(toPeer); - return peer != null && peer.supportsAddPathSupported(localTK); - } - - @Override - public PeerRole getRole(final PeerId peerId) { - synchronized (this.peers) { - final Optional peerOptional = this.peers.stream() - .filter(peer -> peer.getPeerId().equals(peerId)).findFirst(); - if (peerOptional.isPresent()) { - return peerOptional.get().getRole(); - } - } - return null; - } - - @Override - public YangInstanceIdentifier getPeerRibInstanceIdentifier(final PeerId peerId) { - final Peer peer = getPeer(peerId); - return peer == null ? null : peer.getPeerRibInstanceIdentifier(); + public synchronized List getPeers() { + return this.peersList; } } \ No newline at end of file diff --git a/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/BGPPeerTracker.java b/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/BGPPeerTracker.java index cb3ba6ec73..06391b3614 100644 --- a/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/BGPPeerTracker.java +++ b/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/BGPPeerTracker.java @@ -16,7 +16,6 @@ import org.opendaylight.protocol.concepts.AbstractRegistration; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.TablesKey; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; /** * Tracks Peers under RIB. @@ -39,47 +38,12 @@ public interface BGPPeerTracker { @Nullable Peer getPeer(@Nonnull PeerId peerId); - /** - * Returns if peer supports table. - * - * @param peerIdOfNewPeer PeerId - * @param tableKey table - * @return true if Additional Path is supported for defined table - */ - boolean supportsTable(@Nonnull PeerId peerIdOfNewPeer, @Nonnull TablesKey tableKey); - /** * Returns map of PeerId per PeerRole. * Role with none peerId will be filtered. * - * @return Returns map of PeerId group by PeerRole + * @return Returns map of Peer group by PeerRole */ @Nonnull - Map> getRoles(); - - /** - * Returns if peer supports Additional Path for specific table. - * - * @param toPeer peer ID - * @param localTK table - * @return true if Additional Path is supported for defined table - */ - boolean supportsAddPathSupported(@Nonnull PeerId toPeer, @Nonnull TablesKey localTK); - - /** - * Returns role of specific peer if present. - * - * @param peerId peer identifier - * @return role - */ - @Nullable - PeerRole getRole(@Nonnull PeerId peerId); - - /** - * Returns YangInstanceIdentifier pointing peer under specific rib. - * - * @return Peer YangInstanceIdentifier - */ - @Nullable - YangInstanceIdentifier getPeerRibInstanceIdentifier(@Nonnull PeerId peerId); + List getPeers(); } diff --git a/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/policy/BGPRouteEntryExportParameters.java b/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/policy/BGPRouteEntryExportParameters.java index 9ac74a9850..dc61c9b616 100644 --- a/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/policy/BGPRouteEntryExportParameters.java +++ b/bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/policy/BGPRouteEntryExportParameters.java @@ -21,7 +21,7 @@ public interface BGPRouteEntryExportParameters extends BGPRouteEntryImportParame * @return peer Id of announced Peer */ @Nonnull - PeerId getToPeer(); + PeerId getToPeerId(); /** * Peer role of Peer destiny for route entry. -- 2.36.6