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