2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
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;
24 import java.util.List;
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.BGPTableType;
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.rev130918.LinkstateAddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateSubsequentAddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.KeepaliveBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Notify;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Open;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.OpenBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.ProtocolVersion;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParametersBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.CMultiprotocolBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.c.multiprotocol.MultiprotocolCapabilityBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
51 import org.opendaylight.yangtools.yang.binding.Notification;
53 import com.google.common.collect.Lists;
55 public class FSMTest {
57 private BGPSessionNegotiator clientSession;
60 private Channel speakerListener;
63 private ChannelPipeline pipeline;
65 private final BGPTableType ipv4tt = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
67 private final BGPTableType linkstatett = new BGPTableType(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
69 private final List<Notification> receivedMsgs = Lists.newArrayList();
71 private Open classicOpen;
75 MockitoAnnotations.initMocks(this);
76 final List<BgpParameters> tlvs = Lists.newArrayList();
78 tlvs.add(new BgpParametersBuilder().setCParameters(
79 new CMultiprotocolBuilder().setMultiprotocolCapability(
80 new MultiprotocolCapabilityBuilder().setAfi(this.ipv4tt.getAddressFamily()).setSafi(
81 this.ipv4tt.getSubsequentAddressFamily()).build()).build()).build());
82 tlvs.add(new BgpParametersBuilder().setCParameters(
83 new CMultiprotocolBuilder().setMultiprotocolCapability(
84 new MultiprotocolCapabilityBuilder().setAfi(this.linkstatett.getAddressFamily()).setSafi(
85 this.linkstatett.getSubsequentAddressFamily()).build()).build()).build());
86 final BGPSessionPreferences prefs = new BGPSessionPreferences(30, (short) 3, null, tlvs);
87 this.clientSession = new BGPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, prefs, new SimpleSessionListener());
88 doAnswer(new Answer<Object>() {
90 public Object answer(final InvocationOnMock invocation) {
91 final Object[] args = invocation.getArguments();
92 FSMTest.this.receivedMsgs.add((Notification) args[0]);
95 }).when(this.speakerListener).writeAndFlush(any(Notification.class));
96 doReturn("TestingChannel").when(this.speakerListener).toString();
97 doReturn(this.pipeline).when(this.speakerListener).pipeline();
98 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
99 doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
100 this.classicOpen = new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).setBgpParameters(
105 public void testAccSessionChar() throws InterruptedException {
106 this.clientSession.channelActive(null);
107 assertEquals(1, this.receivedMsgs.size());
108 assertTrue(this.receivedMsgs.get(0) instanceof Open);
109 this.clientSession.handleMessage(this.classicOpen);
110 assertEquals(2, this.receivedMsgs.size());
111 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
112 this.clientSession.handleMessage(new KeepaliveBuilder().build());
113 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
116 assertEquals(3, this.receivedMsgs.size());
117 assertTrue(this.receivedMsgs.get(2) instanceof Keepalive); // test of keepalive timer
121 public void testNotAccChars() throws InterruptedException {
122 this.clientSession.channelActive(null);
123 assertEquals(1, this.receivedMsgs.size());
124 assertTrue(this.receivedMsgs.get(0) instanceof Open);
125 this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(1).setVersion(new ProtocolVersion((short) 4)).build());
126 assertEquals(2, this.receivedMsgs.size());
127 assertTrue(this.receivedMsgs.get(1) instanceof Notify);
128 final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
129 assertEquals(BGPError.UNSPECIFIC_OPEN_ERROR, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
135 public void testNoOpen() throws InterruptedException {
136 this.clientSession.channelActive(null);
137 assertEquals(1, this.receivedMsgs.size());
138 assertTrue(this.receivedMsgs.get(0) instanceof Open);
139 Thread.sleep(BGPSessionNegotiator.INITIAL_HOLDTIMER * 1000 * 60);
141 final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
142 assertEquals(BGPError.HOLD_TIMER_EXPIRED, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
146 public void sendNotification() {
147 this.clientSession.channelActive(null);
148 this.clientSession.handleMessage(this.classicOpen);
149 this.clientSession.handleMessage(new KeepaliveBuilder().build());
150 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
151 this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).build());
152 assertEquals(3, this.receivedMsgs.size());
153 assertTrue(this.receivedMsgs.get(2) instanceof Notify);
154 final Notification m = this.receivedMsgs.get(2);
155 assertEquals(BGPError.FSM_ERROR.getCode(), ((Notify) m).getErrorCode().shortValue());
156 assertEquals(BGPError.FSM_ERROR.getSubcode(), ((Notify) m).getErrorSubcode().shortValue());
160 public void tearDown() {