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