2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelFuture;
19 import io.netty.channel.ChannelHandler;
20 import io.netty.channel.ChannelPipeline;
21 import io.netty.util.HashedWheelTimer;
22 import io.netty.util.concurrent.DefaultPromise;
23 import io.netty.util.concurrent.GlobalEventExecutor;
25 import java.util.List;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.mockito.invocation.InvocationOnMock;
34 import org.mockito.stubbing.Answer;
35 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
36 import org.opendaylight.protocol.bgp.parser.BGPError;
37 import org.opendaylight.protocol.bgp.parser.BGPMessage;
38 import org.opendaylight.protocol.bgp.parser.BGPParameter;
39 import org.opendaylight.protocol.bgp.parser.message.BGPKeepAliveMessage;
40 import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
41 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
42 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
43 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateSubsequentAddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
50 import com.google.common.collect.Lists;
52 public class FSMTest {
54 private BGPSessionNegotiator clientSession;
57 private Channel speakerListener;
60 private ChannelPipeline pipeline;
62 private final BGPTableType ipv4tt = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
64 private final BGPTableType linkstatett = new BGPTableType(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
66 private final List<BGPMessage> receivedMsgs = Lists.newArrayList();
68 private BGPOpenMessage classicOpen;
72 MockitoAnnotations.initMocks(this);
73 final List<BGPParameter> tlvs = Lists.newArrayList();
74 tlvs.add(new MultiprotocolCapability(this.ipv4tt));
75 tlvs.add(new MultiprotocolCapability(this.linkstatett));
76 final BGPSessionPreferences prefs = new BGPSessionPreferences(new AsNumber((long) 30), (short) 3, null, tlvs);
77 this.clientSession = new BGPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, prefs, new SimpleSessionListener());
78 doAnswer(new Answer<Object>() {
80 public Object answer(final InvocationOnMock invocation) {
81 final Object[] args = invocation.getArguments();
82 FSMTest.this.receivedMsgs.add((BGPMessage) args[0]);
85 }).when(this.speakerListener).writeAndFlush(any(BGPMessage.class));
86 doReturn("TestingChannel").when(this.speakerListener).toString();
87 doReturn(this.pipeline).when(this.speakerListener).pipeline();
88 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
89 doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
90 this.classicOpen = new BGPOpenMessage(new AsNumber((long) 30), (short) 3, null, tlvs);
94 public void testAccSessionChar() throws InterruptedException {
95 this.clientSession.channelActive(null);
96 assertEquals(1, this.receivedMsgs.size());
97 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
98 this.clientSession.handleMessage(this.classicOpen);
99 assertEquals(2, this.receivedMsgs.size());
100 assertTrue(this.receivedMsgs.get(1) instanceof BGPKeepAliveMessage);
101 this.clientSession.handleMessage(new BGPKeepAliveMessage());
102 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
103 // Thread.sleep(3 * 1000);
104 // Thread.sleep(100);
105 // assertEquals(3, this.receivedMsgs.size());
106 // assertTrue(this.receivedMsgs.get(2) instanceof BGPKeepAliveMessage); // test of keepalive timer
107 // this.clientSession.handleMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null));
108 // assertEquals(4, this.receivedMsgs.size());
109 // assertTrue(this.receivedMsgs.get(3) instanceof BGPNotificationMessage);
110 // final BGPMessage m = this.clientListener.getListMsg().get(3);
111 // assertEquals(BGPError.FSM_ERROR, ((BGPNotificationMessage) m).getError());
115 public void testNotAccChars() throws InterruptedException {
116 this.clientSession.channelActive(null);
117 assertEquals(1, this.receivedMsgs.size());
118 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
119 this.clientSession.handleMessage(new BGPOpenMessage(new AsNumber((long) 30), (short) 1, null, null));
120 assertEquals(2, this.receivedMsgs.size());
121 assertTrue(this.receivedMsgs.get(1) instanceof BGPNotificationMessage);
122 final BGPMessage m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
123 assertEquals(BGPError.UNSPECIFIC_OPEN_ERROR, ((BGPNotificationMessage) m).getError());
129 public void testNoOpen() throws InterruptedException {
130 this.clientSession.channelActive(null);
131 assertEquals(1, this.receivedMsgs.size());
132 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
133 Thread.sleep(BGPSessionNegotiator.INITIAL_HOLDTIMER * 1000 * 60);
135 final BGPMessage m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
136 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
140 public void sendNotification() {
141 this.clientSession.channelActive(null);
142 this.clientSession.handleMessage(this.classicOpen);
143 this.clientSession.handleMessage(new BGPKeepAliveMessage());
144 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
146 this.clientSession.handleMessage(new BGPOpenMessage(new AsNumber((long) 30), (short) 3, null, null));
147 fail("Exception should be thrown.");
148 } catch (final IllegalStateException e) {
149 assertEquals("Unexpected state Finished", e.getMessage());
154 public void tearDown() {