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.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import java.util.List;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Ignore;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.concepts.BGPAddressFamily;
22 import org.opendaylight.protocol.bgp.concepts.BGPSubsequentAddressFamily;
23 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
24 import org.opendaylight.protocol.bgp.parser.BGPError;
25 import org.opendaylight.protocol.bgp.parser.BGPMessage;
26 import org.opendaylight.protocol.bgp.parser.BGPParameter;
27 import org.opendaylight.protocol.bgp.parser.message.BGPKeepAliveMessage;
28 import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
29 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
30 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
31 import org.opendaylight.protocol.concepts.ASNumber;
33 import com.google.common.collect.Lists;
34 import com.google.common.collect.Sets;
36 public class FSMTest {
38 private SimpleSessionListener clientListener;
40 private final SpeakerSessionListener speakerListener = new SpeakerSessionListener();
42 private SpeakerSessionMock speaker;
46 this.clientListener = new SimpleSessionListener();
47 this.speaker = new SpeakerSessionMock(this.speakerListener, this.clientListener);
48 this.clientListener.addSession(this.speaker);
52 public void testAccSessionChar() throws InterruptedException {
53 this.speaker.startSession();
54 assertEquals(1, this.clientListener.getListMsg().size());
55 assertTrue(this.clientListener.getListMsg().get(0) instanceof BGPOpenMessage);
56 final List<BGPParameter> tlvs = Lists.newArrayList();
57 tlvs.add(new MultiprotocolCapability(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast)));
58 this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, tlvs));
59 assertEquals(2, this.clientListener.getListMsg().size());
60 assertTrue(this.clientListener.getListMsg().get(1) instanceof BGPKeepAliveMessage);
61 this.clientListener.sendMessage(new BGPKeepAliveMessage());
62 synchronized (this.speakerListener) {
63 while (!this.speakerListener.up)
65 this.speakerListener.wait();
66 fail("Exception should have occured.");
67 } catch (final InterruptedException e) {
71 assertTrue(this.speakerListener.up);
72 assertEquals(this.speakerListener.types,
73 Sets.newHashSet(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast)));
74 Thread.sleep(1 * 1000);
75 assertEquals(3, this.clientListener.getListMsg().size());
76 assertTrue(this.clientListener.getListMsg().get(2) instanceof BGPKeepAliveMessage); // test of keepalive timer
77 this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null));
78 assertEquals(4, this.clientListener.getListMsg().size());
79 assertTrue(this.clientListener.getListMsg().get(3) instanceof BGPNotificationMessage);
80 final BGPMessage m = this.clientListener.getListMsg().get(3);
81 assertEquals(BGPError.FSM_ERROR, ((BGPNotificationMessage) m).getError());
85 public void testNotAccChars() throws InterruptedException {
86 this.speaker.startSession();
87 assertEquals(1, this.clientListener.getListMsg().size());
88 assertTrue(this.clientListener.getListMsg().get(0) instanceof BGPOpenMessage);
89 this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 1, null, null));
90 assertEquals(2, this.clientListener.getListMsg().size());
91 assertTrue(this.clientListener.getListMsg().get(1) instanceof BGPKeepAliveMessage);
92 assertFalse(this.speakerListener.up);
93 Thread.sleep(BGPSessionImpl.HOLD_TIMER_VALUE * 1000);
95 final BGPMessage m = this.clientListener.getListMsg().get(this.clientListener.getListMsg().size() - 1);
96 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
102 public void testNoOpen() throws InterruptedException {
103 this.speaker.startSession();
104 assertEquals(1, this.clientListener.getListMsg().size());
105 assertTrue(this.clientListener.getListMsg().get(0) instanceof BGPOpenMessage);
106 Thread.sleep(BGPSessionImpl.HOLD_TIMER_VALUE * 1000);
108 final BGPMessage m = this.clientListener.getListMsg().get(this.clientListener.getListMsg().size() - 1);
109 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
113 public void sendNotification() {
114 this.speaker.startSession();
115 this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null));
116 this.clientListener.sendMessage(new BGPKeepAliveMessage());
117 synchronized (this.speakerListener) {
118 while (!this.speakerListener.up)
120 this.speakerListener.wait();
121 fail("Exception should have occured.");
122 } catch (final InterruptedException e) {
126 assertTrue(this.speakerListener.up);
127 this.clientListener.sendMessage(new BGPNotificationMessage(BGPError.CEASE));
128 assertFalse(this.speakerListener.up);
132 public void complementaryTests() {
133 assertEquals(4096, this.speaker.maximumMessageSize());
137 public void tearDown() {
138 this.speaker.close();