Switched BGPMessage concept to yangtools.binding.Notification.
[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.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;
24
25 import java.util.List;
26
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.BGPKeepAliveMessage;
39 import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
40 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
41 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateSubsequentAddressFamily;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
48 import org.opendaylight.yangtools.yang.binding.Notification;
49
50 import com.google.common.collect.Lists;
51
52 public class FSMTest {
53
54         private BGPSessionNegotiator clientSession;
55
56         @Mock
57         private Channel speakerListener;
58
59         @Mock
60         private ChannelPipeline pipeline;
61
62         private final BGPTableType ipv4tt = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
63
64         private final BGPTableType linkstatett = new BGPTableType(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
65
66         private final List<Notification> receivedMsgs = Lists.newArrayList();
67
68         private BGPOpenMessage classicOpen;
69
70         @Before
71         public void setUp() {
72                 MockitoAnnotations.initMocks(this);
73                 final List<BGPParameter> tlvs = Lists.newArrayList();
74                 tlvs.add(new MultiprotocolCapability(this.ipv4tt));
75                 tlvs.add(new MultiprotocolCapability(this.linkstatett));
76                 final BGPSessionPreferences prefs = new BGPSessionPreferences(new AsNumber((long) 30), (short) 3, null, tlvs);
77                 this.clientSession = new BGPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, prefs, new SimpleSessionListener());
78                 doAnswer(new Answer<Object>() {
79                         @Override
80                         public Object answer(final InvocationOnMock invocation) {
81                                 final Object[] args = invocation.getArguments();
82                                 FSMTest.this.receivedMsgs.add((Notification) args[0]);
83                                 return null;
84                         }
85                 }).when(this.speakerListener).writeAndFlush(any(Notification.class));
86                 doReturn("TestingChannel").when(this.speakerListener).toString();
87                 doReturn(this.pipeline).when(this.speakerListener).pipeline();
88                 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
89                 doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
90                 this.classicOpen = new BGPOpenMessage(new AsNumber((long) 30), (short) 3, null, tlvs);
91         }
92
93         @Test
94         public void testAccSessionChar() throws InterruptedException {
95                 this.clientSession.channelActive(null);
96                 assertEquals(1, this.receivedMsgs.size());
97                 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
98                 this.clientSession.handleMessage(this.classicOpen);
99                 assertEquals(2, this.receivedMsgs.size());
100                 assertTrue(this.receivedMsgs.get(1) instanceof BGPKeepAliveMessage);
101                 this.clientSession.handleMessage(new BGPKeepAliveMessage());
102                 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
103                 // Thread.sleep(3 * 1000);
104                 // Thread.sleep(100);
105                 // assertEquals(3, this.receivedMsgs.size());
106                 // assertTrue(this.receivedMsgs.get(2) instanceof BGPKeepAliveMessage); // test of keepalive timer
107                 // this.clientSession.handleMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null));
108                 // assertEquals(4, this.receivedMsgs.size());
109                 // assertTrue(this.receivedMsgs.get(3) instanceof BGPNotificationMessage);
110                 // final BGPMessage m = this.clientListener.getListMsg().get(3);
111                 // assertEquals(BGPError.FSM_ERROR, ((BGPNotificationMessage) m).getError());
112         }
113
114         @Test
115         public void testNotAccChars() throws InterruptedException {
116                 this.clientSession.channelActive(null);
117                 assertEquals(1, this.receivedMsgs.size());
118                 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
119                 this.clientSession.handleMessage(new BGPOpenMessage(new AsNumber((long) 30), (short) 1, null, null));
120                 assertEquals(2, this.receivedMsgs.size());
121                 assertTrue(this.receivedMsgs.get(1) instanceof BGPNotificationMessage);
122                 final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
123                 assertEquals(BGPError.UNSPECIFIC_OPEN_ERROR, ((BGPNotificationMessage) m).getError());
124         }
125
126         @Test
127         @Ignore
128         // long duration
129         public void testNoOpen() throws InterruptedException {
130                 this.clientSession.channelActive(null);
131                 assertEquals(1, this.receivedMsgs.size());
132                 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
133                 Thread.sleep(BGPSessionNegotiator.INITIAL_HOLDTIMER * 1000 * 60);
134                 Thread.sleep(100);
135                 final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
136                 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
137         }
138
139         @Test
140         public void sendNotification() {
141                 this.clientSession.channelActive(null);
142                 this.clientSession.handleMessage(this.classicOpen);
143                 this.clientSession.handleMessage(new BGPKeepAliveMessage());
144                 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
145                 try {
146                         this.clientSession.handleMessage(new BGPOpenMessage(new AsNumber((long) 30), (short) 3, null, null));
147                         fail("Exception should be thrown.");
148                 } catch (final IllegalStateException e) {
149                         assertEquals("Unexpected state Finished", e.getMessage());
150                 }
151         }
152
153         @After
154         public void tearDown() {
155
156         }
157 }