BUG-7976: Race between peer removal and routes update
[bgpcep.git] / bgp / path-selection-mode / src / test / java / org / opendaylight / protocol / bgp / mode / impl / AbstractRouteEntryTest.java
index ae78af67554472c573ebc22b0f27a4b231a81a63..a6f5f91155cd4009376fd496930eb1899be206f0 100644 (file)
@@ -8,23 +8,28 @@
 
 package org.opendaylight.protocol.bgp.mode.impl;
 
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 import static org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectorTest.ATTRS_EXTENSION_Q;
 import static org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectorTest.SEGMENTS_NID;
 
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.primitives.UnsignedInteger;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.function.BiConsumer;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup;
+import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup.PeerExporTuple;
 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.Ipv4Routes;
@@ -54,7 +59,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 
-public class AbstractRouteEntryTest {
+public abstract class AbstractRouteEntryTest {
     protected static final long REMOTE_PATH_ID = 1;
     protected static final PeerId PEER_ID = new PeerId("bgp://42.42.42.42");
     protected static final YangInstanceIdentifier PEER_YII2 = YangInstanceIdentifier.of(QName.create("urn:opendaylight:params:xml:ns:yang:bgp-inet:test", "2015-03-05", "peer2"));
@@ -65,7 +70,6 @@ public class AbstractRouteEntryTest {
         .node(LocRib.QNAME).node(Tables.QNAME).node(RibSupportUtils.toYangTablesKey(TABLES_KEY)).getPathArguments());
     private static final long PATH_ID = 1;
     private static final PeerId PEER_ID2 = new PeerId("bgp://43.43.43.43");
-    private static final PeerId PEER_DISCONNECTED = new PeerId("bgp://44.44.44.44");
     private static final String PREFIX = "1.2.3.4/32";
     private static final String PREFIX2 = "2.2.2.2/32";
     private static final YangInstanceIdentifier PEER_YII = YangInstanceIdentifier.of(QName.create("urn:opendaylight:params:xml:ns:yang:bgp-inet:test", "2015-03-05", "peer1"));
@@ -108,7 +112,7 @@ public class AbstractRouteEntryTest {
     protected void setUp() {
         MockitoAnnotations.initMocks(this);
         this.yIIChanges = new ArrayList<>();
-        this.peerPT = Mockito.mock(ExportPolicyPeerTracker.class);
+        this.peerPT = mock(ExportPolicyPeerTracker.class);
         this.attributes = createAttr();
         this.locRibTargetYii = LOC_RIB_TARGET.node(ROUTES_IDENTIFIER);
         this.locRibOutTargetYii = PEER_YII.node(AdjRibOut.QNAME).node(Tables.QNAME).node(RibSupportUtils.toYangTablesKey(TABLES_KEY)).node(ROUTES_IDENTIFIER);
@@ -129,89 +133,100 @@ public class AbstractRouteEntryTest {
     }
 
     private void mockTransactionChain() {
-        Mockito.doAnswer(invocation -> {
+        doAnswer(invocation -> {
             final Object[] args = invocation.getArguments();
-            yIIChanges.add((YangInstanceIdentifier) args[1]);
+            this.yIIChanges.add((YangInstanceIdentifier) args[1]);
             return args[1];
-        }).when(this.tx).put(Mockito.any(LogicalDatastoreType.class), Mockito.any(YangInstanceIdentifier.class), Mockito.any(NormalizedNode.class));
+        }).when(this.tx).put(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class));
 
-        Mockito.doAnswer(invocation -> {
+        doAnswer(invocation -> {
             final Object[] args = invocation.getArguments();
-            if (routePaYii.equals(args[1])) {
-                yIIChanges.remove(routePaYii);
-            } else if (routePaAddPathYii.equals(args[1])) {
-                yIIChanges.remove(routePaAddPathYii);
-            } else if (routeRiboutYii.equals(args[1])) {
-                yIIChanges.remove(routeRiboutYii);
-                yIIChanges.remove(routeAddRiboutAttYii);
-            } else if (routeAddRiboutYii.equals(args[1])) {
-                yIIChanges.remove(routeAddRiboutYii);
-                yIIChanges.remove(routeAddRiboutAttYii);
+            if (this.routePaYii.equals(args[1])) {
+                this.yIIChanges.remove(this.routePaYii);
+            } else if (this.routePaAddPathYii.equals(args[1])) {
+                this.yIIChanges.remove(this.routePaAddPathYii);
+            } else if (this.routeRiboutYii.equals(args[1])) {
+                this.yIIChanges.remove(this.routeRiboutYii);
+                this.yIIChanges.remove(this.routeAddRiboutAttYii);
+            } else if (this.routeAddRiboutYii.equals(args[1])) {
+                this.yIIChanges.remove(this.routeAddRiboutYii);
+                this.yIIChanges.remove(this.routeAddRiboutAttYii);
             }
             return args[1];
-        }).when(this.tx).delete(Mockito.any(LogicalDatastoreType.class), Mockito.any(YangInstanceIdentifier.class));
+        }).when(this.tx).delete(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class));
     }
 
     private void mockExportGroup() {
-        Mockito.doReturn(this.attributes).when(this.peg).effectiveAttributes(Mockito.any(PeerRole.class), Mockito.any(ContainerNode.class));
-        Mockito.doReturn(null).when(this.pegNot).effectiveAttributes(Mockito.any(PeerRole.class), Mockito.any(ContainerNode.class));
-
-        Map<PeerId, PeerExportGroup.PeerExporTuple> peers = new HashMap<>();
-        Mockito.doReturn(ImmutableList.copyOf(peers.entrySet())).when(this.pegNot).getPeers();
-        Mockito.doReturn(true).when(this.pegNot).containsPeer(Mockito.any(PeerId.class));
+        doReturn(this.attributes).when(this.peg).effectiveAttributes(any(PeerRole.class), any(ContainerNode.class));
+        doReturn(null).when(this.pegNot).effectiveAttributes(any(PeerRole.class), any(ContainerNode.class));
+
+        final Map<PeerId, PeerExportGroup.PeerExporTuple> peers = new HashMap<>();
+        doAnswer(invocation -> {
+            final BiConsumer action = (BiConsumer) invocation.getArguments()[0];
+            for (final Map.Entry<PeerId, PeerExporTuple> pid : peers.entrySet()) {
+                action.accept(pid.getKey(), pid.getValue().getYii());
+            }
+            return null;
+        }).when(this.pegNot).forEach(any());
+        doReturn(true).when(this.pegNot).containsPeer(any(PeerId.class));
 
         peers.put(PEER_ID, new PeerExportGroup.PeerExporTuple(PEER_YII, PeerRole.Ibgp));
         peers.put(PEER_ID2, new PeerExportGroup.PeerExporTuple(PEER_YII2, PeerRole.Ibgp));
-
-        Mockito.doReturn(ImmutableList.copyOf(peers.entrySet())).when(this.peg).getPeers();
+        doAnswer(invocation -> {
+            final BiConsumer action = (BiConsumer) invocation.getArguments()[0];
+            for (final Map.Entry<PeerId, PeerExporTuple> pid : peers.entrySet()) {
+                action.accept(pid.getKey(), pid.getValue().getYii());
+            }
+            return null;
+        }).when(this.peg).forEach(any());
     }
 
     private void mockExportPolicies() {
-        Mockito.doReturn(true).when(this.peerPT).isTableSupported(PEER_ID);
-        Mockito.doReturn(false).when(this.peerPT).isTableSupported(PEER_ID2);
-        Mockito.doAnswer(invocation -> {
+        doReturn(true).when(this.peerPT).isTableSupported(PEER_ID);
+        doReturn(false).when(this.peerPT).isTableSupported(PEER_ID2);
+        doAnswer(invocation -> {
             final Object[] args = invocation.getArguments();
             if (PeerRole.Ibgp.equals(args[0])) {
-                return peg;
+                return this.peg;
             } else if (PeerRole.Ebgp.equals(args[0])) {
-                return pegNot;
+                return this.pegNot;
             } else {
                 return null;
             }
-        }).when(this.peerPT).getPeerGroup(Mockito.any(PeerRole.class));
+        }).when(this.peerPT).getPeerGroup(any(PeerRole.class));
 
-        Mockito.doReturn(true).when(this.peerPT).isAddPathSupportedByPeer(PEER_ID);
-        Mockito.doReturn(false).when(this.peerPT).isAddPathSupportedByPeer(PEER_ID2);
+        doReturn(true).when(this.peerPT).isAddPathSupportedByPeer(PEER_ID);
+        doReturn(false).when(this.peerPT).isAddPathSupportedByPeer(PEER_ID2);
     }
 
     private void mockRibSupport() {
-        Mockito.doReturn(ROUTE_ATTRIBUTES_IDENTIFIER).when(this.ribSupport).routeAttributesIdentifier();
-        Mockito.doReturn(ROUTE_ID_PA_ADD_PATH).when(this.ribSupport).getRouteIdAddPath(Mockito.any(Long.class), Mockito.eq(ROUTE_ID_PA_ADD_PATH));
-        Mockito.doReturn(null).when(this.ribSupport).getRouteIdAddPath(Mockito.any(Long.class), Mockito.eq(ROUTE_ID_PA));
-        Mockito.doAnswer(invocation -> {
+        doReturn(ROUTE_ATTRIBUTES_IDENTIFIER).when(this.ribSupport).routeAttributesIdentifier();
+        doReturn(ROUTE_ID_PA_ADD_PATH).when(this.ribSupport).getRouteIdAddPath(any(Long.class), eq(ROUTE_ID_PA_ADD_PATH));
+        doReturn(null).when(this.ribSupport).getRouteIdAddPath(any(Long.class), eq(ROUTE_ID_PA));
+        doAnswer(invocation -> {
             final Object[] args = invocation.getArguments();
             final YangInstanceIdentifier yii = (YangInstanceIdentifier) args[0];
             final PathArgument paa = (PathArgument) args[1];
 
             if (ROUTE_ID_PA.equals(paa)) {
-                if (yii.equals(locRibTargetYii)) {
-                    return routePaYii;
-                } else if (yii.equals(locRibOutTargetYii)) {
-                    return routeRiboutYii;
-                } else if (yii.equals(locRibOutTargetYiiPeer2)) {
-                    return routeRiboutYiiPeer2;
+                if (yii.equals(this.locRibTargetYii)) {
+                    return this.routePaYii;
+                } else if (yii.equals(this.locRibOutTargetYii)) {
+                    return this.routeRiboutYii;
+                } else if (yii.equals(this.locRibOutTargetYiiPeer2)) {
+                    return this.routeRiboutYiiPeer2;
                 }
             } else if (ROUTE_ID_PA_ADD_PATH.equals(paa)) {
-                if (yii.equals(locRibTargetYii)) {
-                    return routePaAddPathYii;
-                } else if (yii.equals(locRibOutTargetYii)) {
-                    return routeAddRiboutYii;
-                } else if (yii.equals(locRibOutTargetYiiPeer2)) {
-                    return routeAddRiboutYiiPeer2;
+                if (yii.equals(this.locRibTargetYii)) {
+                    return this.routePaAddPathYii;
+                } else if (yii.equals(this.locRibOutTargetYii)) {
+                    return this.routeAddRiboutYii;
+                } else if (yii.equals(this.locRibOutTargetYiiPeer2)) {
+                    return this.routeAddRiboutYiiPeer2;
                 }
             }
             return null;
-        }).when(this.ribSupport).routePath(Mockito.any(YangInstanceIdentifier.class), Mockito.any(PathArgument.class));
+        }).when(this.ribSupport).routePath(any(YangInstanceIdentifier.class), any(PathArgument.class));
     }
 
     private NormalizedNode<?, ?> createAttr() {