BUG-58: refactor to take advantage of 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         @Ignore
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) {
65                                 try {
66                                         this.speakerListener.wait();
67                                         fail("Exception should have occured.");
68                                 } catch (final InterruptedException e) {
69                                         e.printStackTrace();
70                                 }
71                         }
72                 }
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());
84         }
85
86         @Test
87         @Ignore
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);
97                 Thread.sleep(100);
98                 final BGPMessage m = this.clientListener.getListMsg().get(this.clientListener.getListMsg().size() - 1);
99                 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
100         }
101
102         @Test
103         @Ignore
104         // long duration
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);
110                 Thread.sleep(100);
111                 final BGPMessage m = this.clientListener.getListMsg().get(this.clientListener.getListMsg().size() - 1);
112                 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
113         }
114
115         @Test
116         @Ignore
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) {
123                                 try {
124                                         this.speakerListener.wait();
125                                         fail("Exception should have occured.");
126                                 } catch (final InterruptedException e) {
127                                         e.printStackTrace();
128                                 }
129                         }
130                 }
131                 assertTrue(this.speakerListener.up);
132                 this.clientListener.sendMessage(new BGPNotificationMessage(BGPError.CEASE));
133                 assertFalse(this.speakerListener.up);
134         }
135
136         @After
137         public void tearDown() {
138                 this.speaker.close();
139         }
140 }