Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 16 | package org.onlab.onos.sdnip; |
| 17 | |
| 18 | import static org.easymock.EasyMock.anyObject; |
| 19 | import static org.easymock.EasyMock.createMock; |
| 20 | import static org.easymock.EasyMock.expect; |
| 21 | import static org.easymock.EasyMock.replay; |
| 22 | import static org.easymock.EasyMock.reset; |
| 23 | import static org.easymock.EasyMock.verify; |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertTrue; |
| 26 | |
| 27 | import java.util.HashMap; |
| 28 | import java.util.HashSet; |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | import java.util.concurrent.ConcurrentHashMap; |
| 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.onlab.junit.TestUtils; |
| 36 | import org.onlab.junit.TestUtils.TestUtilsException; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 37 | import org.onlab.onos.core.ApplicationId; |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.ConnectPoint; |
| 39 | import org.onlab.onos.net.DefaultHost; |
| 40 | import org.onlab.onos.net.DeviceId; |
| 41 | import org.onlab.onos.net.Host; |
| 42 | import org.onlab.onos.net.HostId; |
| 43 | import org.onlab.onos.net.HostLocation; |
| 44 | import org.onlab.onos.net.PortNumber; |
| 45 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 46 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 47 | import org.onlab.onos.net.flow.TrafficSelector; |
| 48 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 49 | import org.onlab.onos.net.host.HostEvent; |
| 50 | import org.onlab.onos.net.host.HostService; |
| 51 | import org.onlab.onos.net.host.InterfaceIpAddress; |
| 52 | import org.onlab.onos.net.intent.IntentService; |
| 53 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 54 | import org.onlab.onos.net.provider.ProviderId; |
| 55 | import org.onlab.onos.sdnip.Router.InternalHostListener; |
| 56 | import org.onlab.onos.sdnip.config.BgpPeer; |
| 57 | import org.onlab.onos.sdnip.config.Interface; |
| 58 | import org.onlab.onos.sdnip.config.SdnIpConfigService; |
| 59 | import org.onlab.packet.Ethernet; |
| 60 | import org.onlab.packet.IpAddress; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame^] | 61 | import org.onlab.packet.Ip4Address; |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 62 | import org.onlab.packet.IpPrefix; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame^] | 63 | import org.onlab.packet.Ip4Prefix; |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 64 | import org.onlab.packet.MacAddress; |
| 65 | import org.onlab.packet.VlanId; |
| 66 | |
| 67 | import com.google.common.collect.Sets; |
| 68 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 69 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 70 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
| 71 | |
| 72 | /** |
| 73 | * This class tests adding a route, updating a route, deleting a route, and |
| 74 | * the ARP module answers the MAC address asynchronously. |
| 75 | */ |
| 76 | public class RouterTestWithAsyncArp { |
| 77 | |
| 78 | private SdnIpConfigService sdnIpConfigService; |
| 79 | private InterfaceService interfaceService; |
| 80 | private IntentService intentService; |
| 81 | private HostService hostService; |
| 82 | |
| 83 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 84 | DeviceId.deviceId("of:0000000000000001"), |
| 85 | PortNumber.portNumber(1)); |
| 86 | |
| 87 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 88 | DeviceId.deviceId("of:0000000000000002"), |
| 89 | PortNumber.portNumber(1)); |
| 90 | |
| 91 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 92 | DeviceId.deviceId("of:0000000000000003"), |
| 93 | PortNumber.portNumber(1)); |
| 94 | |
| 95 | private Router router; |
| 96 | private InternalHostListener internalHostListener; |
| 97 | |
| 98 | private static final ApplicationId APPID = new ApplicationId() { |
| 99 | @Override |
| 100 | public short id() { |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String name() { |
| 106 | return "SDNIP"; |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | @Before |
| 111 | public void setUp() throws Exception { |
| 112 | setUpSdnIpConfigService(); |
| 113 | setUpInterfaceService(); |
| 114 | hostService = createMock(HostService.class); |
| 115 | intentService = createMock(IntentService.class); |
| 116 | |
| 117 | router = new Router(APPID, intentService, |
| 118 | hostService, sdnIpConfigService, interfaceService); |
| 119 | internalHostListener = router.new InternalHostListener(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Sets up SdnIpConfigService. |
| 124 | */ |
| 125 | private void setUpSdnIpConfigService() { |
| 126 | |
| 127 | sdnIpConfigService = createMock(SdnIpConfigService.class); |
| 128 | |
| 129 | Map<IpAddress, BgpPeer> peers = new HashMap<>(); |
| 130 | |
| 131 | String peerSw1Eth1 = "192.168.10.1"; |
| 132 | peers.put(IpAddress.valueOf(peerSw1Eth1), |
| 133 | new BgpPeer("00:00:00:00:00:00:00:01", 1, peerSw1Eth1)); |
| 134 | |
| 135 | // Two BGP peers are connected to switch 2 port 1. |
| 136 | String peer1Sw2Eth1 = "192.168.20.1"; |
| 137 | peers.put(IpAddress.valueOf(peer1Sw2Eth1), |
| 138 | new BgpPeer("00:00:00:00:00:00:00:02", 1, peer1Sw2Eth1)); |
| 139 | |
| 140 | String peer2Sw2Eth1 = "192.168.20.2"; |
| 141 | peers.put(IpAddress.valueOf(peer2Sw2Eth1), |
| 142 | new BgpPeer("00:00:00:00:00:00:00:02", 1, peer2Sw2Eth1)); |
| 143 | |
| 144 | expect(sdnIpConfigService.getBgpPeers()).andReturn(peers).anyTimes(); |
| 145 | replay(sdnIpConfigService); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets up InterfaceService. |
| 150 | */ |
| 151 | private void setUpInterfaceService() { |
| 152 | |
| 153 | interfaceService = createMock(InterfaceService.class); |
| 154 | |
| 155 | Set<Interface> interfaces = Sets.newHashSet(); |
| 156 | |
| 157 | Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet(); |
| 158 | interfaceIpAddresses1.add(new InterfaceIpAddress( |
| 159 | IpAddress.valueOf("192.168.10.101"), |
| 160 | IpPrefix.valueOf("192.168.10.0/24"))); |
| 161 | Interface sw1Eth1 = new Interface(SW1_ETH1, |
| 162 | interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01")); |
| 163 | interfaces.add(sw1Eth1); |
| 164 | |
| 165 | Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet(); |
| 166 | interfaceIpAddresses2.add(new InterfaceIpAddress( |
| 167 | IpAddress.valueOf("192.168.20.101"), |
| 168 | IpPrefix.valueOf("192.168.20.0/24"))); |
| 169 | Interface sw2Eth1 = new Interface(SW2_ETH1, |
| 170 | interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02")); |
| 171 | interfaces.add(sw2Eth1); |
| 172 | |
| 173 | Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet(); |
| 174 | interfaceIpAddresses3.add(new InterfaceIpAddress( |
| 175 | IpAddress.valueOf("192.168.30.101"), |
| 176 | IpPrefix.valueOf("192.168.30.0/24"))); |
| 177 | Interface sw3Eth1 = new Interface(SW3_ETH1, |
| 178 | interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03")); |
| 179 | interfaces.add(sw3Eth1); |
| 180 | |
| 181 | expect(interfaceService.getInterface(SW1_ETH1)).andReturn(sw1Eth1).anyTimes(); |
| 182 | expect(interfaceService.getInterface(SW2_ETH1)).andReturn(sw2Eth1).anyTimes(); |
| 183 | expect(interfaceService.getInterface(SW3_ETH1)).andReturn(sw3Eth1).anyTimes(); |
| 184 | expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes(); |
| 185 | replay(interfaceService); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * This method tests adding a route entry. |
| 190 | */ |
| 191 | @Test |
| 192 | public void testProcessRouteAdd() throws TestUtilsException { |
| 193 | |
| 194 | // Construct a route entry |
| 195 | RouteEntry routeEntry = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame^] | 196 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 197 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 198 | |
| 199 | // Construct a route intent |
| 200 | MultiPointToSinglePointIntent intent = staticIntentBuilder(); |
| 201 | |
| 202 | // Set up test expectation |
| 203 | reset(hostService); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 204 | expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn( |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 205 | new HashSet<Host>()).anyTimes(); |
| 206 | hostService.startMonitoringIp(IpAddress.valueOf("192.168.10.1")); |
| 207 | replay(hostService); |
| 208 | |
| 209 | reset(intentService); |
| 210 | intentService.submit(intent); |
| 211 | replay(intentService); |
| 212 | |
| 213 | // Call the processRouteAdd() method in Router class |
| 214 | router.leaderChanged(true); |
| 215 | TestUtils.setField(router, "isActivatedLeader", true); |
| 216 | router.processRouteAdd(routeEntry); |
| 217 | |
| 218 | Host host = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 219 | MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE, |
| 220 | new HostLocation( |
| 221 | SW1_ETH1.deviceId(), |
| 222 | SW1_ETH1.port(), 1), |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 223 | Sets.newHashSet(IpAddress.valueOf("192.168.10.1"))); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 224 | internalHostListener.event( |
| 225 | new HostEvent(HostEvent.Type.HOST_ADDED, host)); |
| 226 | |
| 227 | // Verify |
| 228 | assertEquals(router.getRoutes().size(), 1); |
| 229 | assertTrue(router.getRoutes().contains(routeEntry)); |
| 230 | assertEquals(router.getPushedRouteIntents().size(), 1); |
| 231 | assertEquals(router.getPushedRouteIntents().iterator().next(), |
| 232 | intent); |
| 233 | verify(intentService); |
| 234 | verify(hostService); |
| 235 | |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * This method tests updating a route entry. |
| 240 | * |
| 241 | * @throws TestUtilsException |
| 242 | */ |
| 243 | @Test |
| 244 | public void testRouteUpdate() throws TestUtilsException { |
| 245 | |
| 246 | // Construct the existing route entry |
| 247 | RouteEntry routeEntry = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame^] | 248 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 249 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 250 | |
| 251 | // Construct the existing MultiPointToSinglePointIntent intent |
| 252 | MultiPointToSinglePointIntent intent = staticIntentBuilder(); |
| 253 | |
| 254 | // Set up the bgpRoutes field of Router class with existing route, and |
| 255 | // pushedRouteIntents field with the corresponding existing intent |
| 256 | setBgpRoutesField(routeEntry); |
| 257 | setPushedRouteIntentsField(routeEntry, intent); |
| 258 | |
| 259 | // Start to construct a new route entry and new intent |
| 260 | RouteEntry routeEntryUpdate = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame^] | 261 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 262 | Ip4Address.valueOf("192.168.20.1")); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 263 | |
| 264 | // Construct a new MultiPointToSinglePointIntent intent |
| 265 | TrafficSelector.Builder selectorBuilderNew = |
| 266 | DefaultTrafficSelector.builder(); |
| 267 | selectorBuilderNew.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 268 | routeEntryUpdate.prefix()); |
| 269 | |
| 270 | TrafficTreatment.Builder treatmentBuilderNew = |
| 271 | DefaultTrafficTreatment.builder(); |
| 272 | treatmentBuilderNew.setEthDst(MacAddress.valueOf("00:00:00:00:00:02")); |
| 273 | |
| 274 | Set<ConnectPoint> ingressPointsNew = new HashSet<ConnectPoint>(); |
| 275 | ingressPointsNew.add(SW1_ETH1); |
| 276 | ingressPointsNew.add(SW3_ETH1); |
| 277 | |
| 278 | MultiPointToSinglePointIntent intentNew = |
| 279 | new MultiPointToSinglePointIntent(APPID, |
| 280 | selectorBuilderNew.build(), |
| 281 | treatmentBuilderNew.build(), |
| 282 | ingressPointsNew, SW2_ETH1); |
| 283 | |
| 284 | // Set up test expectation |
| 285 | reset(hostService); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 286 | expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn( |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 287 | new HashSet<Host>()).anyTimes(); |
| 288 | hostService.startMonitoringIp(IpAddress.valueOf("192.168.20.1")); |
| 289 | replay(hostService); |
| 290 | |
| 291 | reset(intentService); |
| 292 | intentService.withdraw(intent); |
| 293 | intentService.submit(intentNew); |
| 294 | replay(intentService); |
| 295 | |
| 296 | // Call the processRouteAdd() method in Router class |
| 297 | router.leaderChanged(true); |
| 298 | TestUtils.setField(router, "isActivatedLeader", true); |
| 299 | router.processRouteAdd(routeEntryUpdate); |
| 300 | |
| 301 | Host host = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 302 | MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE, |
| 303 | new HostLocation( |
| 304 | SW2_ETH1.deviceId(), |
| 305 | SW2_ETH1.port(), 1), |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 306 | Sets.newHashSet(IpAddress.valueOf("192.168.20.1"))); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 307 | internalHostListener.event( |
| 308 | new HostEvent(HostEvent.Type.HOST_ADDED, host)); |
| 309 | |
| 310 | // Verify |
| 311 | assertEquals(router.getRoutes().size(), 1); |
| 312 | assertTrue(router.getRoutes().contains(routeEntryUpdate)); |
| 313 | assertEquals(router.getPushedRouteIntents().size(), 1); |
| 314 | assertEquals(router.getPushedRouteIntents().iterator().next(), |
| 315 | intentNew); |
| 316 | verify(intentService); |
| 317 | verify(hostService); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * This method tests deleting a route entry. |
| 322 | */ |
| 323 | @Test |
| 324 | public void testProcessRouteDelete() throws TestUtilsException { |
| 325 | |
| 326 | // Construct the existing route entry |
| 327 | RouteEntry routeEntry = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame^] | 328 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 329 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 330 | |
| 331 | // Construct the existing MultiPointToSinglePointIntent intent |
| 332 | MultiPointToSinglePointIntent intent = staticIntentBuilder(); |
| 333 | |
| 334 | // Set up the bgpRoutes field of Router class with existing route, and |
| 335 | // pushedRouteIntents field with the corresponding existing intent |
| 336 | setBgpRoutesField(routeEntry); |
| 337 | setPushedRouteIntentsField(routeEntry, intent); |
| 338 | |
| 339 | // Set up expectation |
| 340 | reset(intentService); |
| 341 | intentService.withdraw(intent); |
| 342 | replay(intentService); |
| 343 | |
| 344 | // Call route deleting method in Router class |
| 345 | router.leaderChanged(true); |
| 346 | TestUtils.setField(router, "isActivatedLeader", true); |
| 347 | router.processRouteDelete(routeEntry); |
| 348 | |
| 349 | // Verify |
| 350 | assertEquals(router.getRoutes().size(), 0); |
| 351 | assertEquals(router.getPushedRouteIntents().size(), 0); |
| 352 | verify(intentService); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Constructs a static MultiPointToSinglePointIntent. |
| 357 | */ |
| 358 | private MultiPointToSinglePointIntent staticIntentBuilder() { |
| 359 | |
| 360 | TrafficSelector.Builder selectorBuilder = |
| 361 | DefaultTrafficSelector.builder(); |
| 362 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 363 | IpPrefix.valueOf("1.1.1.0/24")); |
| 364 | |
| 365 | TrafficTreatment.Builder treatmentBuilder = |
| 366 | DefaultTrafficTreatment.builder(); |
| 367 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 368 | |
| 369 | Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>(); |
| 370 | ingressPoints.add(SW2_ETH1); |
| 371 | ingressPoints.add(SW3_ETH1); |
| 372 | |
| 373 | MultiPointToSinglePointIntent intent = |
| 374 | new MultiPointToSinglePointIntent(APPID, |
| 375 | selectorBuilder.build(), treatmentBuilder.build(), |
| 376 | ingressPoints, SW1_ETH1); |
| 377 | |
| 378 | return intent; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Sets bgpRoutesField in Router class. |
| 383 | * |
| 384 | * @throws TestUtilsException |
| 385 | */ |
| 386 | private void setBgpRoutesField(RouteEntry routeEntry) |
| 387 | throws TestUtilsException { |
| 388 | |
| 389 | InvertedRadixTree<RouteEntry> bgpRoutes = |
| 390 | new ConcurrentInvertedRadixTree<>( |
| 391 | new DefaultByteArrayNodeFactory()); |
| 392 | bgpRoutes.put(RouteEntry.createBinaryString(routeEntry.prefix()), |
| 393 | routeEntry); |
| 394 | TestUtils.setField(router, "bgpRoutes", bgpRoutes); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Sets pushedRouteIntentsField in Router class. |
| 399 | * |
| 400 | * @throws TestUtilsException |
| 401 | */ |
| 402 | private void setPushedRouteIntentsField(RouteEntry routeEntry, |
| 403 | MultiPointToSinglePointIntent intent) |
| 404 | throws TestUtilsException { |
| 405 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame^] | 406 | ConcurrentHashMap<Ip4Prefix, MultiPointToSinglePointIntent> |
| 407 | pushedRouteIntents = new ConcurrentHashMap<>(); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 408 | pushedRouteIntents.put(routeEntry.prefix(), intent); |
| 409 | TestUtils.setField(router, "pushedRouteIntents", pushedRouteIntents); |
| 410 | } |
| 411 | } |