Do not use JdkFutureAdapters in BgpPeerRpc
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / StrictBGPPeerRegistryTest.java
index 98420a5b4b8be3e20d8967ad8f0e81a526a190fb..fb94eb4b649bf370d3d0d23e715c33b37d140866 100644 (file)
@@ -5,13 +5,14 @@
  * 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.rib.impl;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.fail;
+
 import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Futures;
 import java.net.InetSocketAddress;
 import java.util.Collections;
 import java.util.List;
@@ -21,18 +22,20 @@ import org.mockito.Mockito;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
+import org.opendaylight.protocol.bgp.rib.impl.spi.PeerRegistrySessionListener;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.BgpExtendedMessageCapabilityBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Open;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParametersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpId;
+import org.opendaylight.yangtools.concepts.Registration;
 
 public class StrictBGPPeerRegistryTest {
 
@@ -47,36 +50,44 @@ public class StrictBGPPeerRegistryTest {
     private StrictBGPPeerRegistry peerRegistry;
     private BGPSessionPreferences mockPreferences;
 
-    private Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
+    private static Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
         final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder()
             .setOptionalCapabilities(Lists.newArrayList(new OptionalCapabilitiesBuilder()
                 .setCParameters(new CParametersBuilder()
                     .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(as).build())
-                    .setBgpExtendedMessageCapability(new BgpExtendedMessageCapabilityBuilder().build()).build()).build())).build());
+                    .build()).build())).build());
         return new OpenBuilder().setBgpIdentifier(bgpId).setBgpParameters(params).build();
     }
 
     @Before
-    public void setUp() throws Exception {
+    public void setUp() {
         this.peerRegistry = new StrictBGPPeerRegistry();
-        this.mockPreferences =  new BGPSessionPreferences(LOCAL_AS, 1, new Ipv4Address("0.0.0.1"), LOCAL_AS, Collections.<BgpParameters> emptyList());
+        this.mockPreferences = new BGPSessionPreferences(LOCAL_AS, 1, new BgpId("0.0.0.1"), LOCAL_AS,
+                Collections.emptyList());
     }
 
     private static BGPSessionListener getMockSession() {
         final BGPSessionListener mock = Mockito.mock(BGPSessionListener.class);
-        Mockito.doNothing().when(mock).releaseConnection();
+        Mockito.doReturn(Futures.immediateFuture(null)).when(mock).releaseConnection();
+        return mock;
+    }
+
+    private static PeerRegistrySessionListener getMockSessionListener() {
+        final PeerRegistrySessionListener mock = Mockito.mock(PeerRegistrySessionListener.class);
+        Mockito.doNothing().when(mock).onSessionCreated(Mockito.any(IpAddress.class));
+        Mockito.doNothing().when(mock).onSessionRemoved(Mockito.any(IpAddress.class));
         return mock;
     }
 
     @Test
-    public void testIpAddressConstruction() throws Exception {
+    public void testIpAddressConstruction() throws BGPDocumentedException {
         final InetSocketAddress adr = new InetSocketAddress("127.0.0.1", 179);
         final IpAddress ipAdr = StrictBGPPeerRegistry.getIpAddress(adr);
         assertEquals("127.0.0.1", ipAdr.getIpv4Address().getValue());
     }
 
     @Test
-    public void testDuplicatePeerConnection() throws Exception {
+    public void testDuplicatePeerConnection() throws BGPDocumentedException {
         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
         try {
@@ -89,7 +100,7 @@ public class StrictBGPPeerRegistryTest {
     }
 
     @Test
-    public void testPeerNotConfigured() throws Exception {
+    public void testPeerNotConfigured() throws BGPDocumentedException {
         try {
             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
         } catch (final IllegalStateException e) {
@@ -117,7 +128,7 @@ public class StrictBGPPeerRegistryTest {
     }
 
     @Test
-    public void testDropSecondPeer() throws Exception {
+    public void testDropSecondPeer() throws BGPDocumentedException {
         final Ipv4Address higher = new Ipv4Address("192.168.200.200");
         final Ipv4Address lower = new Ipv4Address("10.10.10.10");
         final IpAddress remoteIp = new IpAddress(lower);
@@ -148,7 +159,7 @@ public class StrictBGPPeerRegistryTest {
     }
 
     @Test
-    public void testDuplicatePeersWDifferentIds() throws Exception {
+    public void testDuplicatePeersWDifferentIds() throws BGPDocumentedException {
         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
 
         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
@@ -162,7 +173,7 @@ public class StrictBGPPeerRegistryTest {
     }
 
     @Test
-    public void testDuplicatePeersHigherAs() throws Exception {
+    public void testDuplicatePeersHigherAs() throws BGPDocumentedException {
         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
 
         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
@@ -187,7 +198,7 @@ public class StrictBGPPeerRegistryTest {
     }
 
     @Test
-    public void testAsMismatch() throws Exception {
+    public void testAsMismatch() {
         final AsNumber as2 = new AsNumber(3L);
 
         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
@@ -199,4 +210,44 @@ public class StrictBGPPeerRegistryTest {
         }
         fail("Peer AS number mismatch");
     }
+
+    @Test
+    public void testRegisterPeerSessionListener() throws Exception {
+        final PeerRegistrySessionListener sessionListener1 = getMockSessionListener();
+        this.peerRegistry.registerPeerSessionListener(sessionListener1);
+
+        final PeerRegistrySessionListener sessionListener2 = getMockSessionListener();
+        this.peerRegistry.registerPeerSessionListener(sessionListener2);
+
+        this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
+        this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
+        Mockito.verify(sessionListener1, Mockito.times(1)).onSessionCreated(REMOTE_IP);
+        Mockito.verify(sessionListener2, Mockito.times(1)).onSessionCreated(REMOTE_IP);
+
+        this.peerRegistry.removePeerSession(REMOTE_IP);
+        Mockito.verify(sessionListener1, Mockito.times(1)).onSessionRemoved(REMOTE_IP);
+        Mockito.verify(sessionListener2, Mockito.times(1)).onSessionRemoved(REMOTE_IP);
+    }
+
+    @Test
+    public void testClosePeerSessionOneListener() throws BGPDocumentedException {
+        final PeerRegistrySessionListener sessionListener1 = getMockSessionListener();
+        final Registration registration1 = this.peerRegistry.registerPeerSessionListener(sessionListener1);
+
+        final PeerRegistrySessionListener sessionListener2 = getMockSessionListener();
+        this.peerRegistry.registerPeerSessionListener(sessionListener2);
+
+        this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
+        this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
+        this.peerRegistry.removePeerSession(REMOTE_IP);
+
+        registration1.close();
+        this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
+        this.peerRegistry.removePeerSession(REMOTE_IP);
+
+        Mockito.verify(sessionListener1, Mockito.times(1)).onSessionCreated(REMOTE_IP);
+        Mockito.verify(sessionListener2, Mockito.times(2)).onSessionCreated(REMOTE_IP);
+        Mockito.verify(sessionListener1, Mockito.times(1)).onSessionRemoved(REMOTE_IP);
+        Mockito.verify(sessionListener2, Mockito.times(2)).onSessionRemoved(REMOTE_IP);
+    }
 }