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