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.BGPParameter;
38 import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
39 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
40 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateSubsequentAddressFamily;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.KeepaliveBuilder;
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;
49 import org.opendaylight.yangtools.yang.binding.Notification;
51 import com.google.common.collect.Lists;
53 public class FSMTest {
55 private BGPSessionNegotiator clientSession;
58 private Channel speakerListener;
61 private ChannelPipeline pipeline;
63 private final BGPTableType ipv4tt = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
65 private final BGPTableType linkstatett = new BGPTableType(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
67 private final List<Notification> receivedMsgs = Lists.newArrayList();
69 private BGPOpenMessage classicOpen;
73 MockitoAnnotations.initMocks(this);
74 final List<BGPParameter> tlvs = Lists.newArrayList();
75 tlvs.add(new MultiprotocolCapability(this.ipv4tt));
76 tlvs.add(new MultiprotocolCapability(this.linkstatett));
77 final BGPSessionPreferences prefs = new BGPSessionPreferences(new AsNumber((long) 30), (short) 3, null, tlvs);
78 this.clientSession = new BGPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, prefs, new SimpleSessionListener());
79 doAnswer(new Answer<Object>() {
81 public Object answer(final InvocationOnMock invocation) {
82 final Object[] args = invocation.getArguments();
83 FSMTest.this.receivedMsgs.add((Notification) args[0]);
86 }).when(this.speakerListener).writeAndFlush(any(Notification.class));
87 doReturn("TestingChannel").when(this.speakerListener).toString();
88 doReturn(this.pipeline).when(this.speakerListener).pipeline();
89 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
90 doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
91 this.classicOpen = new BGPOpenMessage(new AsNumber((long) 30), (short) 3, null, tlvs);
95 public void testAccSessionChar() throws InterruptedException {
96 this.clientSession.channelActive(null);
97 assertEquals(1, this.receivedMsgs.size());
98 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
99 this.clientSession.handleMessage(this.classicOpen);
100 assertEquals(2, this.receivedMsgs.size());
101 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
102 this.clientSession.handleMessage(new KeepaliveBuilder().build());
103 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
104 // Thread.sleep(3 * 1000);
105 // Thread.sleep(100);
106 // assertEquals(3, this.receivedMsgs.size());
107 // assertTrue(this.receivedMsgs.get(2) instanceof BGPKeepAliveMessage); // test of keepalive timer
108 // this.clientSession.handleMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null));
109 // assertEquals(4, this.receivedMsgs.size());
110 // assertTrue(this.receivedMsgs.get(3) instanceof BGPNotificationMessage);
111 // final BGPMessage m = this.clientListener.getListMsg().get(3);
112 // assertEquals(BGPError.FSM_ERROR, ((BGPNotificationMessage) m).getError());
116 public void testNotAccChars() throws InterruptedException {
117 this.clientSession.channelActive(null);
118 assertEquals(1, this.receivedMsgs.size());
119 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
120 this.clientSession.handleMessage(new BGPOpenMessage(new AsNumber((long) 30), (short) 1, null, null));
121 assertEquals(2, this.receivedMsgs.size());
122 assertTrue(this.receivedMsgs.get(1) instanceof BGPNotificationMessage);
123 final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
124 assertEquals(BGPError.UNSPECIFIC_OPEN_ERROR, ((BGPNotificationMessage) m).getError());
130 public void testNoOpen() throws InterruptedException {
131 this.clientSession.channelActive(null);
132 assertEquals(1, this.receivedMsgs.size());
133 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
134 Thread.sleep(BGPSessionNegotiator.INITIAL_HOLDTIMER * 1000 * 60);
136 final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
137 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
141 public void sendNotification() {
142 this.clientSession.channelActive(null);
143 this.clientSession.handleMessage(this.classicOpen);
144 this.clientSession.handleMessage(new KeepaliveBuilder().build());
145 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
147 this.clientSession.handleMessage(new BGPOpenMessage(new AsNumber((long) 30), (short) 3, null, null));
148 fail("Exception should be thrown.");
149 } catch (final IllegalStateException e) {
150 assertEquals("Unexpected state Finished", e.getMessage());
155 public void tearDown() {