Initial framework migration to netty.
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / FSMTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
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;
14
15 import java.util.List;
16
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;
32
33 import com.google.common.collect.Lists;
34 import com.google.common.collect.Sets;
35
36 public class FSMTest {
37
38         private SimpleSessionListener clientListener;
39
40         private final SpeakerSessionListener speakerListener = new SpeakerSessionListener();
41
42         private SpeakerSessionMock speaker;
43
44         @Before
45         public void setUp() {
46                 this.clientListener = new SimpleSessionListener();
47                 this.speaker = new SpeakerSessionMock(this.speakerListener, this.clientListener);
48                 this.clientListener.addSession(this.speaker);
49         }
50
51         @Test
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)
64                                 try {
65                                         this.speakerListener.wait();
66                                         fail("Exception should have occured.");
67                                 } catch (final InterruptedException e) {
68                                         e.printStackTrace();
69                                 }
70                 }
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());
82         }
83
84         @Test
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);
94                 Thread.sleep(100);
95                 final BGPMessage m = this.clientListener.getListMsg().get(this.clientListener.getListMsg().size() - 1);
96                 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
97         }
98
99         @Test
100         @Ignore
101         // long duration
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);
107                 Thread.sleep(100);
108                 final BGPMessage m = this.clientListener.getListMsg().get(this.clientListener.getListMsg().size() - 1);
109                 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
110         }
111
112         @Test
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)
119                                 try {
120                                         this.speakerListener.wait();
121                                         fail("Exception should have occured.");
122                                 } catch (final InterruptedException e) {
123                                         e.printStackTrace();
124                                 }
125                 }
126                 assertTrue(this.speakerListener.up);
127                 this.clientListener.sendMessage(new BGPNotificationMessage(BGPError.CEASE));
128                 assertFalse(this.speakerListener.up);
129         }
130
131         @Test
132         public void complementaryTests() {
133                 assertEquals(4096, this.speaker.maximumMessageSize());
134         }
135
136         @After
137         public void tearDown() {
138                 this.speaker.close();
139         }
140 }