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