blob: 36b1d6fcac00abebb5753adb7ad9bbdd246df0f4 [file] [log] [blame]
Vidyashree Rama7bd3d782015-11-23 08:46:33 +05301/*
2 * Copyright 2014-2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.bgp;
17
18import com.google.common.net.InetAddresses;
19import org.jboss.netty.bootstrap.ClientBootstrap;
20import org.jboss.netty.channel.Channel;
21import org.jboss.netty.channel.ChannelFactory;
22import org.jboss.netty.channel.ChannelPipeline;
23import org.jboss.netty.channel.ChannelPipelineFactory;
24import org.jboss.netty.channel.Channels;
25import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
26import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
29
30import static org.hamcrest.core.Is.is;
31import static org.junit.Assert.assertThat;
32
33import org.onlab.junit.TestUtils;
34
35import java.net.InetAddress;
36import java.net.InetSocketAddress;
37import java.net.SocketAddress;
38import java.util.LinkedList;
39import java.util.concurrent.Executors;
40import java.util.concurrent.TimeUnit;
41
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053045import org.onosproject.bgp.controller.BgpCfg;
46import org.onosproject.bgp.controller.impl.BgpControllerImpl;
47import org.onosproject.bgpio.types.BgpValueType;
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053048import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv;
49import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv;
50
51/**
52 * Test case for BGPControllerImpl.
53 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053054public class BgpControllerImplTest {
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053055
56 protected static final Logger log = LoggerFactory
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053057 .getLogger(BgpControllerImplTest.class);
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053058
59 private static final String IP_LOOPBACK_ID1 = "127.0.0.1";
60
61 private static final int MESSAGE_TIMEOUT_MS = 3000;
62 public byte version;
63 public short asNumber;
64 public short holdTime;
65 public int bgpId = InetAddresses.coerceToInteger(InetAddresses.forString(IP_LOOPBACK_ID1));
66 public boolean isLargeAsCapabilitySet = false;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053067 public LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053068
69 @Before
70 public void setUp() throws Exception {
71 peer1 = new BgpPeerTest(version, asNumber,
72 holdTime, bgpId, isLargeAsCapabilitySet,
73 capabilityTlv);
74
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 bgpControllerImpl = new BgpControllerImpl();
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053076
77 // NOTE: We use port 0 to bind on any available port
78 bgpControllerImpl.controller().setBgpPortNum();
79 bgpControllerImpl.activate();
80
81 Channel serverChannel = TestUtils.getField(bgpControllerImpl.controller(),
82 "serverChannel");
83 SocketAddress socketAddress = serverChannel.getLocalAddress();
84 InetSocketAddress inetSocketAddress =
85 (InetSocketAddress) socketAddress;
86 InetAddress connectToAddress = InetAddresses.forString("127.0.0.1");
87 connectToSocket = new InetSocketAddress(connectToAddress,
88 inetSocketAddress.getPort());
89
90 bgpControllerImpl.getConfig().setRouterId("1.1.1.1");
91 bgpControllerImpl.getConfig().setAsNumber(200);
92 bgpControllerImpl.getConfig().setHoldTime((short) 120);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053093 bgpControllerImpl.getConfig().setState(BgpCfg.State.IP_AS_CONFIGURED);
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053094
95 bgpControllerImpl.getConfig().addPeer("127.0.0.1", 200);
96 }
97
98 @After
99 public void tearDown() throws Exception {
100 bgpControllerImpl.deactivate();
101 bgpControllerImpl = null;
102 }
103
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530104 private BgpControllerImpl bgpControllerImpl;
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530105
106 BgpPeerTest peer1;
107
108 // The socket that the remote peers should connect to
109 private InetSocketAddress connectToSocket;
110
111 @Test
112 public void bgpOpenMessageTest1() throws InterruptedException {
113 peer1.peerChannelHandler.asNumber = 200;
114 peer1.peerChannelHandler.version = 4;
115 peer1.peerChannelHandler.holdTime = 120;
116 peer1.connect(connectToSocket);
117 boolean result;
118 result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await(
119 MESSAGE_TIMEOUT_MS,
120 TimeUnit.MILLISECONDS);
121 assertThat(result, is(true));
122 result = peer1.peerFrameDecoder.receivedKeepaliveMessageLatch.await(
123 MESSAGE_TIMEOUT_MS,
124 TimeUnit.MILLISECONDS);
125 assertThat(result, is(true));
126 }
127
128 @Test
129 public void bgpOpenMessageTest2() throws InterruptedException {
130 // Open message with as number which is not configured at peer
131 peer1.peerChannelHandler.asNumber = 500;
132 peer1.peerChannelHandler.version = 4;
133 peer1.peerChannelHandler.holdTime = 120;
134 peer1.connect(connectToSocket);
135
136 boolean result;
137 result = peer1.peerFrameDecoder.receivedNotificationMessageLatch.await(
138 MESSAGE_TIMEOUT_MS,
139 TimeUnit.MILLISECONDS);
140 assertThat(result, is(true));
141 }
142
143 @Test
144 public void bgpOpenMessageTest3() throws InterruptedException {
145 // Open message with invalid hold time value
146 peer1.peerChannelHandler.asNumber = 200;
147 peer1.peerChannelHandler.version = 4;
148 peer1.peerChannelHandler.holdTime = 1;
149 peer1.connect(connectToSocket);
150
151 boolean result;
152 result = peer1.peerFrameDecoder.receivedNotificationMessageLatch.await(
153 MESSAGE_TIMEOUT_MS,
154 TimeUnit.MILLISECONDS);
155 assertThat(result, is(true));
156 }
157
158 @Test
159 public void bgpOpenMessageTest4() throws InterruptedException {
160 // Open message with invalid as number
161 peer1.peerChannelHandler.asNumber = 200;
162 peer1.peerChannelHandler.version = 4;
163 peer1.peerChannelHandler.holdTime = 120;
164 peer1.peerChannelHandler.isLargeAsCapabilitySet = true;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530165 BgpValueType tempTlv = new FourOctetAsNumCapabilityTlv(766545);
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530166 peer1.peerChannelHandler.capabilityTlv.add(tempTlv);
167 peer1.connect(connectToSocket);
168
169 boolean result;
170 result = peer1.peerFrameDecoder.receivedNotificationMessageLatch.await(
171 MESSAGE_TIMEOUT_MS,
172 TimeUnit.MILLISECONDS);
173 assertThat(result, is(true));
174 }
175
176 @Test
177 public void bgpOpenMessageTest5() throws InterruptedException {
178 // Open message with LS capability
179 short afi = 16388;
180 byte res = 0;
181 byte safi = 71;
182 peer1.peerChannelHandler.asNumber = 200;
183 peer1.peerChannelHandler.version = 4;
184 peer1.peerChannelHandler.holdTime = 120;
185 bgpControllerImpl.getConfig().setLsCapability(true);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530186 BgpValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530187 peer1.peerChannelHandler.capabilityTlv.add(tempTlv1);
188 peer1.connect(connectToSocket);
189
190 boolean result;
191 result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await(
192 MESSAGE_TIMEOUT_MS,
193 TimeUnit.MILLISECONDS);
194 assertThat(result, is(true));
195 result = peer1.peerFrameDecoder.receivedKeepaliveMessageLatch.await(
196 MESSAGE_TIMEOUT_MS,
197 TimeUnit.MILLISECONDS);
198 assertThat(result, is(true));
199 }
200
201 @Test
202 public void bgpOpenMessageTest6() throws InterruptedException {
203 // Open message with as4 capability
204 peer1.peerChannelHandler.asNumber = 200;
205 peer1.peerChannelHandler.version = 4;
206 peer1.peerChannelHandler.holdTime = 120;
207 peer1.peerChannelHandler.isLargeAsCapabilitySet = true;
208 bgpControllerImpl.getConfig().setLargeASCapability(true);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530209 BgpValueType tempTlv = new FourOctetAsNumCapabilityTlv(200);
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530210 peer1.peerChannelHandler.capabilityTlv.add(tempTlv);
211 peer1.connect(connectToSocket);
212
213 boolean result;
214 result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await(
215 MESSAGE_TIMEOUT_MS,
216 TimeUnit.MILLISECONDS);
217 assertThat(result, is(true));
218 result = peer1.peerFrameDecoder.receivedKeepaliveMessageLatch.await(
219 MESSAGE_TIMEOUT_MS,
220 TimeUnit.MILLISECONDS);
221 assertThat(result, is(true));
222
223 result = peer1.peerFrameDecoder.receivedKeepaliveMessageLatch.await(
224 MESSAGE_TIMEOUT_MS,
225 TimeUnit.MILLISECONDS);
226 assertThat(result, is(true));
227 }
228
229 @Test
230 public void bgpOpenMessageTest7() throws InterruptedException {
231 // Open message with both LS capability and as4 capability
232 short afi = 16388;
233 byte res = 0;
234 byte safi = 71;
235 peer1.peerChannelHandler.asNumber = 200;
236 peer1.peerChannelHandler.version = 4;
237 peer1.peerChannelHandler.holdTime = 120;
238
239 peer1.peerChannelHandler.isLargeAsCapabilitySet = true;
240 bgpControllerImpl.getConfig().setLargeASCapability(true);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530241 BgpValueType tempTlv = new FourOctetAsNumCapabilityTlv(200);
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530242 peer1.peerChannelHandler.capabilityTlv.add(tempTlv);
243
244 bgpControllerImpl.getConfig().setLsCapability(true);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530245 BgpValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530246 peer1.peerChannelHandler.capabilityTlv.add(tempTlv1);
247 peer1.connect(connectToSocket);
248
249 boolean result;
250 result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await(
251 MESSAGE_TIMEOUT_MS,
252 TimeUnit.MILLISECONDS);
253 assertThat(result, is(true));
254 }
255
256 /**
257 * A class to capture the state for a BGP peer.
258 */
259 private final class BgpPeerTest {
260 private ClientBootstrap peerBootstrap;
261 private BgpPeerFrameDecoderTest peerFrameDecoder =
262 new BgpPeerFrameDecoderTest();
263 private BgpPeerChannelHandlerTest peerChannelHandler;
264
265 private BgpPeerTest(byte version, short asNumber,
266 short holdTime, int bgpId, boolean isLargeAsCapabilitySet,
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530267 LinkedList<BgpValueType> capabilityTlv) {
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530268 peerChannelHandler = new BgpPeerChannelHandlerTest(version,
269 asNumber, holdTime, bgpId, isLargeAsCapabilitySet, capabilityTlv);
270 }
271
272 /**
273 * Starts the BGP peer.
274 *
275 * @param connectToSocket the socket to connect to
276 */
277 private void connect(InetSocketAddress connectToSocket)
278 throws InterruptedException {
279
280 ChannelFactory channelFactory =
281 new NioClientSocketChannelFactory(
282 Executors.newCachedThreadPool(),
283 Executors.newCachedThreadPool());
284 ChannelPipelineFactory pipelineFactory = () -> {
285 ChannelPipeline pipeline = Channels.pipeline();
286 pipeline.addLast("BgpPeerFrameDecoderTest",
287 peerFrameDecoder);
288 pipeline.addLast("BgpPeerChannelHandlerTest",
289 peerChannelHandler);
290 return pipeline;
291 };
292
293 peerBootstrap = new ClientBootstrap(channelFactory);
294 peerBootstrap.setOption("child.keepAlive", true);
295 peerBootstrap.setOption("child.tcpNoDelay", true);
296 peerBootstrap.setPipelineFactory(pipelineFactory);
297 peerBootstrap.connect(connectToSocket);
298 }
299 }
300}