Add debugging for tracking messages leaving the channel
[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.mockito.Matchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import io.netty.channel.Channel;
17 import io.netty.channel.ChannelFuture;
18 import io.netty.channel.ChannelHandler;
19 import io.netty.channel.ChannelPipeline;
20 import io.netty.util.HashedWheelTimer;
21 import io.netty.util.concurrent.DefaultPromise;
22 import io.netty.util.concurrent.GenericFutureListener;
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.parser.BGPError;
36 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
37 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateSubsequentAddressFamily;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.ProtocolVersion;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParametersBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.MultiprotocolCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.multiprotocol._case.MultiprotocolCapabilityBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
53 import org.opendaylight.yangtools.yang.binding.Notification;
54
55 import com.google.common.collect.Lists;
56
57 public class FSMTest {
58
59         private BGPSessionNegotiator clientSession;
60
61         @Mock
62         private Channel speakerListener;
63
64         @Mock
65         private ChannelPipeline pipeline;
66
67         private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
68
69         private final BgpTableType linkstatett = new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
70
71         private final List<Notification> receivedMsgs = Lists.newArrayList();
72
73         private Open classicOpen;
74
75         @Before
76         public void setUp() {
77                 MockitoAnnotations.initMocks(this);
78                 final List<BgpParameters> tlvs = Lists.newArrayList();
79
80                 tlvs.add(new BgpParametersBuilder().setCParameters(
81                                 new MultiprotocolCaseBuilder().setMultiprotocolCapability(
82                                                 new MultiprotocolCapabilityBuilder().setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build()).build());
83                 tlvs.add(new BgpParametersBuilder().setCParameters(
84                                 new MultiprotocolCaseBuilder().setMultiprotocolCapability(
85                                                 new MultiprotocolCapabilityBuilder().setAfi(this.linkstatett.getAfi()).setSafi(this.linkstatett.getSafi()).build()).build()).build());
86                 final BGPSessionPreferences prefs = new BGPSessionPreferences(30, (short) 3, null, tlvs);
87
88                 final ChannelFuture f = mock(ChannelFuture.class);
89                 doReturn(null).when(f).addListener(any(GenericFutureListener.class));
90                 this.clientSession = new BGPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, prefs, new SimpleSessionListener());
91                 doAnswer(new Answer<Object>() {
92                         @Override
93                         public Object answer(final InvocationOnMock invocation) {
94                                 final Object[] args = invocation.getArguments();
95                                 FSMTest.this.receivedMsgs.add((Notification) args[0]);
96                                 return f;
97                         }
98                 }).when(this.speakerListener).writeAndFlush(any(Notification.class));
99                 doReturn("TestingChannel").when(this.speakerListener).toString();
100                 doReturn(this.pipeline).when(this.speakerListener).pipeline();
101                 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
102                 doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
103                 this.classicOpen = new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).setBgpParameters(
104                                 tlvs).build();
105         }
106
107         @Test
108         public void testAccSessionChar() throws InterruptedException {
109                 this.clientSession.channelActive(null);
110                 assertEquals(1, this.receivedMsgs.size());
111                 assertTrue(this.receivedMsgs.get(0) instanceof Open);
112                 this.clientSession.handleMessage(this.classicOpen);
113                 assertEquals(2, this.receivedMsgs.size());
114                 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
115                 this.clientSession.handleMessage(new KeepaliveBuilder().build());
116                 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
117                 Thread.sleep(1000);
118                 Thread.sleep(100);
119                 assertEquals(3, this.receivedMsgs.size());
120                 assertTrue(this.receivedMsgs.get(2) instanceof Keepalive); // test of keepalive timer
121         }
122
123         @Test
124         public void testNotAccChars() throws InterruptedException {
125                 this.clientSession.channelActive(null);
126                 assertEquals(1, this.receivedMsgs.size());
127                 assertTrue(this.receivedMsgs.get(0) instanceof Open);
128                 this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(1).setVersion(new ProtocolVersion((short) 4)).build());
129                 assertEquals(2, this.receivedMsgs.size());
130                 assertTrue(this.receivedMsgs.get(1) instanceof Notify);
131                 final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
132                 assertEquals(BGPError.UNSPECIFIC_OPEN_ERROR, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
133         }
134
135         @Test
136         @Ignore
137         // long duration
138         public void testNoOpen() throws InterruptedException {
139                 this.clientSession.channelActive(null);
140                 assertEquals(1, this.receivedMsgs.size());
141                 assertTrue(this.receivedMsgs.get(0) instanceof Open);
142                 Thread.sleep(BGPSessionNegotiator.INITIAL_HOLDTIMER * 1000 * 60);
143                 Thread.sleep(100);
144                 final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
145                 assertEquals(BGPError.HOLD_TIMER_EXPIRED, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
146         }
147
148         @Test
149         public void sendNotification() {
150                 this.clientSession.channelActive(null);
151                 this.clientSession.handleMessage(this.classicOpen);
152                 this.clientSession.handleMessage(new KeepaliveBuilder().build());
153                 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
154                 this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).build());
155                 assertEquals(3, this.receivedMsgs.size());
156                 assertTrue(this.receivedMsgs.get(2) instanceof Notify);
157                 final Notification m = this.receivedMsgs.get(2);
158                 assertEquals(BGPError.FSM_ERROR.getCode(), ((Notify) m).getErrorCode().shortValue());
159                 assertEquals(BGPError.FSM_ERROR.getSubcode(), ((Notify) m).getErrorSubcode().shortValue());
160         }
161
162         @After
163         public void tearDown() {
164
165         }
166 }