X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=bgp%2Frib-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fbgp%2Frib%2Fimpl%2FFSMTest.java;h=bcf1ac5c4133df2348860381d798dc7c292500ae;hb=005610d58a6f663e683179e1547645c3a29921e0;hp=0dec6eaaa929465e56c08880f8b86313a454398c;hpb=a01e8a8629311b3480b2085aca40feea539f4ba0;p=bgpcep.git diff --git a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/FSMTest.java b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/FSMTest.java index 0dec6eaaa9..bcf1ac5c41 100644 --- a/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/FSMTest.java +++ b/bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/FSMTest.java @@ -8,9 +8,19 @@ package org.opendaylight.protocol.bgp.rib.impl; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelPipeline; +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.util.List; @@ -18,123 +28,139 @@ import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import org.opendaylight.protocol.bgp.concepts.BGPAddressFamily; -import org.opendaylight.protocol.bgp.concepts.BGPSubsequentAddressFamily; -import org.opendaylight.protocol.bgp.concepts.BGPTableType; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.opendaylight.protocol.bgp.parser.BGPError; -import org.opendaylight.protocol.bgp.parser.BGPMessage; -import org.opendaylight.protocol.bgp.parser.BGPParameter; -import org.opendaylight.protocol.bgp.parser.message.BGPKeepAliveMessage; -import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage; -import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage; -import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability; -import org.opendaylight.protocol.concepts.ASNumber; +import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl; +import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences; +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; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify; +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.ProtocolVersion; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParametersBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.MultiprotocolCaseBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.multiprotocol._case.MultiprotocolCapabilityBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily; +import org.opendaylight.yangtools.yang.binding.Notification; import com.google.common.collect.Lists; -import com.google.common.collect.Sets; public class FSMTest { - private SimpleSessionListener clientListener; + private BGPSessionNegotiator clientSession; - private final SpeakerSessionListener speakerListener = new SpeakerSessionListener(); + @Mock + private Channel speakerListener; - private SpeakerSessionMock speaker; + @Mock + private ChannelPipeline pipeline; + + private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class); + + private final BgpTableType linkstatett = new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class); + + private final List receivedMsgs = Lists.newArrayList(); + + private Open classicOpen; @Before public void setUp() { - this.clientListener = new SimpleSessionListener(); - this.speaker = new SpeakerSessionMock(this.speakerListener, this.clientListener); - this.clientListener.addSession(this.speaker); + MockitoAnnotations.initMocks(this); + final List tlvs = Lists.newArrayList(); + + tlvs.add(new BgpParametersBuilder().setCParameters( + new MultiprotocolCaseBuilder().setMultiprotocolCapability( + new MultiprotocolCapabilityBuilder().setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build()).build()); + 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(30, (short) 3, null, tlvs); + + final ChannelFuture f = mock(ChannelFuture.class); + doReturn(null).when(f).addListener(any(GenericFutureListener.class)); + this.clientSession = new BGPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise(GlobalEventExecutor.INSTANCE), this.speakerListener, prefs, new SimpleSessionListener()); + doAnswer(new Answer() { + @Override + public Object answer(final InvocationOnMock invocation) { + final Object[] args = invocation.getArguments(); + FSMTest.this.receivedMsgs.add((Notification) args[0]); + return f; + } + }).when(this.speakerListener).writeAndFlush(any(Notification.class)); + doReturn("TestingChannel").when(this.speakerListener).toString(); + 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(); } @Test - @Ignore public void testAccSessionChar() throws InterruptedException { - //this.speaker.startSession(); - assertEquals(1, this.clientListener.getListMsg().size()); - assertTrue(this.clientListener.getListMsg().get(0) instanceof BGPOpenMessage); - final List tlvs = Lists.newArrayList(); - tlvs.add(new MultiprotocolCapability(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast))); - this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, tlvs)); - assertEquals(2, this.clientListener.getListMsg().size()); - assertTrue(this.clientListener.getListMsg().get(1) instanceof BGPKeepAliveMessage); - this.clientListener.sendMessage(new BGPKeepAliveMessage()); - synchronized (this.speakerListener) { - while (!this.speakerListener.up) { - try { - this.speakerListener.wait(); - fail("Exception should have occured."); - } catch (final InterruptedException e) { - e.printStackTrace(); - } - } - } - assertTrue(this.speakerListener.up); - assertEquals(this.speakerListener.types, - Sets.newHashSet(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast))); - Thread.sleep(1 * 1000); - assertEquals(3, this.clientListener.getListMsg().size()); - assertTrue(this.clientListener.getListMsg().get(2) instanceof BGPKeepAliveMessage); // test of keepalive timer - this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null)); - assertEquals(4, this.clientListener.getListMsg().size()); - assertTrue(this.clientListener.getListMsg().get(3) instanceof BGPNotificationMessage); - final BGPMessage m = this.clientListener.getListMsg().get(3); - assertEquals(BGPError.FSM_ERROR, ((BGPNotificationMessage) m).getError()); + this.clientSession.channelActive(null); + assertEquals(1, this.receivedMsgs.size()); + assertTrue(this.receivedMsgs.get(0) instanceof Open); + this.clientSession.handleMessage(this.classicOpen); + 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); + Thread.sleep(1000); + Thread.sleep(100); + assertEquals(3, this.receivedMsgs.size()); + assertTrue(this.receivedMsgs.get(2) instanceof Keepalive); // test of keepalive timer } @Test - @Ignore public void testNotAccChars() throws InterruptedException { - //this.speaker.startSession(); - assertEquals(1, this.clientListener.getListMsg().size()); - assertTrue(this.clientListener.getListMsg().get(0) instanceof BGPOpenMessage); - this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 1, null, null)); - assertEquals(2, this.clientListener.getListMsg().size()); - assertTrue(this.clientListener.getListMsg().get(1) instanceof BGPKeepAliveMessage); - assertFalse(this.speakerListener.up); - Thread.sleep(BGPSessionImpl.HOLD_TIMER_VALUE * 1000); - Thread.sleep(100); - final BGPMessage m = this.clientListener.getListMsg().get(this.clientListener.getListMsg().size() - 1); - assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError()); + this.clientSession.channelActive(null); + assertEquals(1, this.receivedMsgs.size()); + assertTrue(this.receivedMsgs.get(0) instanceof Open); + this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(1).setVersion(new ProtocolVersion((short) 4)).build()); + assertEquals(2, this.receivedMsgs.size()); + assertTrue(this.receivedMsgs.get(1) instanceof Notify); + final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1); + assertEquals(BGPError.UNSPECIFIC_OPEN_ERROR, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode())); } @Test @Ignore // long duration public void testNoOpen() throws InterruptedException { - //this.speaker.startSession(); - assertEquals(1, this.clientListener.getListMsg().size()); - assertTrue(this.clientListener.getListMsg().get(0) instanceof BGPOpenMessage); - Thread.sleep(BGPSessionImpl.HOLD_TIMER_VALUE * 1000); + 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(100); - final BGPMessage m = this.clientListener.getListMsg().get(this.clientListener.getListMsg().size() - 1); - assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError()); + final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1); + assertEquals(BGPError.HOLD_TIMER_EXPIRED, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode())); } @Test - @Ignore public void sendNotification() { - //this.speaker.startSession(); - this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null)); - this.clientListener.sendMessage(new BGPKeepAliveMessage()); - synchronized (this.speakerListener) { - while (!this.speakerListener.up) { - try { - this.speakerListener.wait(); - fail("Exception should have occured."); - } catch (final InterruptedException e) { - e.printStackTrace(); - } - } - } - assertTrue(this.speakerListener.up); - this.clientListener.sendMessage(new BGPNotificationMessage(BGPError.CEASE)); - assertFalse(this.speakerListener.up); + this.clientSession.channelActive(null); + this.clientSession.handleMessage(this.classicOpen); + this.clientSession.handleMessage(new KeepaliveBuilder().build()); + assertEquals(this.clientSession.getState(), BGPSessionNegotiator.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); + final Notification m = this.receivedMsgs.get(2); + assertEquals(BGPError.FSM_ERROR.getCode(), ((Notify) m).getErrorCode().shortValue()); + assertEquals(BGPError.FSM_ERROR.getSubcode(), ((Notify) m).getErrorSubcode().shortValue()); } @After public void tearDown() { - this.speaker.close(); + } }