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);
53 public void testAccSessionChar() throws InterruptedException {
54 //this.speaker.startSession();
55 assertEquals(1, this.clientListener.getListMsg().size());
56 assertTrue(this.clientListener.getListMsg().get(0) instanceof BGPOpenMessage);
57 final List<BGPParameter> tlvs = Lists.newArrayList();
58 tlvs.add(new MultiprotocolCapability(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast)));
59 this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, tlvs));
60 assertEquals(2, this.clientListener.getListMsg().size());
61 assertTrue(this.clientListener.getListMsg().get(1) instanceof BGPKeepAliveMessage);
62 this.clientListener.sendMessage(new BGPKeepAliveMessage());
63 synchronized (this.speakerListener) {
64 while (!this.speakerListener.up) {
66 this.speakerListener.wait();
67 fail("Exception should have occured.");
68 } catch (final InterruptedException e) {
73 assertTrue(this.speakerListener.up);
74 assertEquals(this.speakerListener.types,
75 Sets.newHashSet(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast)));
76 Thread.sleep(1 * 1000);
77 assertEquals(3, this.clientListener.getListMsg().size());
78 assertTrue(this.clientListener.getListMsg().get(2) instanceof BGPKeepAliveMessage); // test of keepalive timer
79 this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null));
80 assertEquals(4, this.clientListener.getListMsg().size());
81 assertTrue(this.clientListener.getListMsg().get(3) instanceof BGPNotificationMessage);
82 final BGPMessage m = this.clientListener.getListMsg().get(3);
83 assertEquals(BGPError.FSM_ERROR, ((BGPNotificationMessage) m).getError());
88 public void testNotAccChars() throws InterruptedException {
89 //this.speaker.startSession();
90 assertEquals(1, this.clientListener.getListMsg().size());
91 assertTrue(this.clientListener.getListMsg().get(0) instanceof BGPOpenMessage);
92 this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 1, null, null));
93 assertEquals(2, this.clientListener.getListMsg().size());
94 assertTrue(this.clientListener.getListMsg().get(1) instanceof BGPKeepAliveMessage);
95 assertFalse(this.speakerListener.up);
96 Thread.sleep(BGPSessionImpl.HOLD_TIMER_VALUE * 1000);
98 final BGPMessage m = this.clientListener.getListMsg().get(this.clientListener.getListMsg().size() - 1);
99 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
105 public void testNoOpen() throws InterruptedException {
106 //this.speaker.startSession();
107 assertEquals(1, this.clientListener.getListMsg().size());
108 assertTrue(this.clientListener.getListMsg().get(0) instanceof BGPOpenMessage);
109 Thread.sleep(BGPSessionImpl.HOLD_TIMER_VALUE * 1000);
111 final BGPMessage m = this.clientListener.getListMsg().get(this.clientListener.getListMsg().size() - 1);
112 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
117 public void sendNotification() {
118 //this.speaker.startSession();
119 this.clientListener.sendMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null));
120 this.clientListener.sendMessage(new BGPKeepAliveMessage());
121 synchronized (this.speakerListener) {
122 while (!this.speakerListener.up) {
124 this.speakerListener.wait();
125 fail("Exception should have occured.");
126 } catch (final InterruptedException e) {
131 assertTrue(this.speakerListener.up);
132 this.clientListener.sendMessage(new BGPNotificationMessage(BGPError.CEASE));
133 assertFalse(this.speakerListener.up);
137 public void tearDown() {
138 this.speaker.close();