Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.bgp; |
| 17 | |
| 18 | import com.google.common.net.InetAddresses; |
| 19 | import org.jboss.netty.bootstrap.ClientBootstrap; |
| 20 | import org.jboss.netty.channel.Channel; |
| 21 | import org.jboss.netty.channel.ChannelFactory; |
| 22 | import org.jboss.netty.channel.ChannelPipeline; |
| 23 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 24 | import org.jboss.netty.channel.Channels; |
| 25 | import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; |
| 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | |
| 30 | import static org.hamcrest.core.Is.is; |
| 31 | import static org.junit.Assert.assertThat; |
| 32 | |
| 33 | import org.onlab.junit.TestUtils; |
| 34 | |
| 35 | import java.net.InetAddress; |
| 36 | import java.net.InetSocketAddress; |
| 37 | import java.net.SocketAddress; |
| 38 | import java.util.LinkedList; |
| 39 | import java.util.concurrent.Executors; |
| 40 | import java.util.concurrent.TimeUnit; |
| 41 | |
| 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | import org.onosproject.bgp.controller.BGPCfg; |
| 46 | import org.onosproject.bgp.controller.impl.BGPControllerImpl; |
| 47 | import org.onosproject.bgpio.types.BGPValueType; |
| 48 | import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv; |
| 49 | import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; |
| 50 | |
| 51 | /** |
| 52 | * Test case for BGPControllerImpl. |
| 53 | */ |
| 54 | public class BGPControllerImplTest { |
| 55 | |
| 56 | protected static final Logger log = LoggerFactory |
| 57 | .getLogger(BGPControllerImplTest.class); |
| 58 | |
| 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; |
| 67 | public LinkedList<BGPValueType> capabilityTlv = new LinkedList<>(); |
| 68 | |
| 69 | @Before |
| 70 | public void setUp() throws Exception { |
| 71 | peer1 = new BgpPeerTest(version, asNumber, |
| 72 | holdTime, bgpId, isLargeAsCapabilitySet, |
| 73 | capabilityTlv); |
| 74 | |
| 75 | bgpControllerImpl = new BGPControllerImpl(); |
| 76 | |
| 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); |
| 93 | bgpControllerImpl.getConfig().setState(BGPCfg.State.IP_AS_CONFIGURED); |
| 94 | |
| 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 | |
| 104 | private BGPControllerImpl bgpControllerImpl; |
| 105 | |
| 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; |
| 165 | BGPValueType tempTlv = new FourOctetAsNumCapabilityTlv(766545); |
| 166 | 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); |
| 186 | BGPValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi); |
| 187 | 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); |
| 209 | BGPValueType tempTlv = new FourOctetAsNumCapabilityTlv(200); |
| 210 | 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); |
| 241 | BGPValueType tempTlv = new FourOctetAsNumCapabilityTlv(200); |
| 242 | peer1.peerChannelHandler.capabilityTlv.add(tempTlv); |
| 243 | |
| 244 | bgpControllerImpl.getConfig().setLsCapability(true); |
| 245 | BGPValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi); |
| 246 | 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, |
| 267 | LinkedList<BGPValueType> capabilityTlv) { |
| 268 | 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 | } |