BUG-338 Allow incomming BGP connections.
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / FSMTest.java
index 676a3a023f46664c11c36ce7b8a110d1b8e3980d..3ea8c4b4b2d830cc441792af519a52c27a68da9b 100644 (file)
@@ -15,7 +15,6 @@ import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
 import com.google.common.collect.Lists;
-
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelHandler;
@@ -24,9 +23,10 @@ import io.netty.util.HashedWheelTimer;
 import io.netty.util.concurrent.DefaultPromise;
 import io.netty.util.concurrent.GenericFutureListener;
 import io.netty.util.concurrent.GlobalEventExecutor;
-
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.UnknownHostException;
 import java.util.List;
-
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
@@ -37,8 +37,13 @@ import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
+import org.opendaylight.protocol.bgp.rib.impl.client.BGPClientSessionNegotiator;
+import org.opendaylight.protocol.bgp.rib.impl.client.BGPClientSessionValidator;
+import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
 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.linkstate.rev131125.LinkstateAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateSubsequentAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
@@ -58,7 +63,7 @@ import org.opendaylight.yangtools.yang.binding.Notification;
 
 public class FSMTest {
 
-    private BGPSessionNegotiator clientSession;
+    private BGPClientSessionNegotiator clientSession;
 
     @Mock
     private Channel speakerListener;
@@ -75,7 +80,7 @@ public class FSMTest {
     private Open classicOpen;
 
     @Before
-    public void setUp() {
+    public void setUp() throws UnknownHostException {
         MockitoAnnotations.initMocks(this);
         final List<BgpParameters> tlvs = Lists.newArrayList();
 
@@ -85,11 +90,16 @@ public class FSMTest {
         tlvs.add(new BgpParametersBuilder().setCParameters(
                 new MultiprotocolCaseBuilder().setMultiprotocolCapability(
                         new MultiprotocolCapabilityBuilder().setAfi(this.linkstatett.getAfi()).setSafi(this.linkstatett.getSafi()).build()).build()).build());
-        final BGPSessionPreferences prefs = new BGPSessionPreferences(new AsNumber(30L), (short) 3, null, tlvs);
+        final BGPSessionPreferences prefs = new BGPSessionPreferences(new AsNumber(30L), (short) 3, new Ipv4Address("1.1.1.1"), tlvs);
 
         final ChannelFuture f = mock(ChannelFuture.class);
         doReturn(null).when(f).addListener(any(GenericFutureListener.class));
-        this.clientSession = new BGPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, prefs, new AsNumber(30L), new SimpleSessionListener());
+
+        final InetAddress peerAddress = InetAddress.getByName("1.1.1.2");
+        final BGPPeerRegistry peerRegistry = new StrictBGPPeerRegistry();
+        peerRegistry.addPeer(new IpAddress(new Ipv4Address(peerAddress.getHostAddress())), new SimpleSessionListener(), prefs);
+
+        this.clientSession = new BGPClientSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, peerRegistry, new BGPClientSessionValidator(new AsNumber(30L), peerRegistry));
         doAnswer(new Answer<Object>() {
             @Override
             public Object answer(final InvocationOnMock invocation) {
@@ -98,12 +108,14 @@ public class FSMTest {
                 return f;
             }
         }).when(this.speakerListener).writeAndFlush(any(Notification.class));
+
         doReturn("TestingChannel").when(this.speakerListener).toString();
+        doReturn(new InetSocketAddress(peerAddress, 179)).when(this.speakerListener).remoteAddress();
         doReturn(this.pipeline).when(this.speakerListener).pipeline();
         doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
         doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
         this.classicOpen = new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).setBgpParameters(
-                tlvs).build();
+                tlvs).setBgpIdentifier(new Ipv4Address("1.1.1.2")).build();
     }
 
     @Test
@@ -115,7 +127,7 @@ public class FSMTest {
         assertEquals(2, this.receivedMsgs.size());
         assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
         this.clientSession.handleMessage(new KeepaliveBuilder().build());
-        assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
+        assertEquals(this.clientSession.getState(), BGPClientSessionNegotiator.State.Finished);
         Thread.sleep(1000);
         Thread.sleep(100);
         assertEquals(3, this.receivedMsgs.size());
@@ -141,7 +153,7 @@ public class FSMTest {
         this.clientSession.channelActive(null);
         assertEquals(1, this.receivedMsgs.size());
         assertTrue(this.receivedMsgs.get(0) instanceof Open);
-        Thread.sleep(BGPSessionNegotiator.INITIAL_HOLDTIMER * 1000 * 60);
+        Thread.sleep(BGPClientSessionNegotiator.INITIAL_HOLDTIMER * 1000 * 60);
         Thread.sleep(100);
         final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
         assertEquals(BGPError.HOLD_TIMER_EXPIRED, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
@@ -152,7 +164,7 @@ public class FSMTest {
         this.clientSession.channelActive(null);
         this.clientSession.handleMessage(this.classicOpen);
         this.clientSession.handleMessage(new KeepaliveBuilder().build());
-        assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
+        assertEquals(this.clientSession.getState(), BGPClientSessionNegotiator.State.Finished);
         this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).build());
         assertEquals(3, this.receivedMsgs.size());
         assertTrue(this.receivedMsgs.get(2) instanceof Notify);