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; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 38 | import org.onlab.packet.Ethernet; |
| 39 | import org.onlab.packet.Ip4Address; |
| 40 | import org.onlab.packet.Ip4Prefix; |
| 41 | import org.onlab.packet.IpAddress; |
| 42 | import org.onlab.packet.IpPrefix; |
| 43 | import org.onlab.packet.MacAddress; |
| 44 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.core.ApplicationId; |
| 46 | import org.onosproject.net.ConnectPoint; |
| 47 | import org.onosproject.net.DefaultHost; |
| 48 | import org.onosproject.net.DeviceId; |
| 49 | import org.onosproject.net.Host; |
| 50 | import org.onosproject.net.HostId; |
| 51 | import org.onosproject.net.HostLocation; |
| 52 | import org.onosproject.net.PortNumber; |
| 53 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 54 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 55 | import org.onosproject.net.flow.TrafficSelector; |
| 56 | import org.onosproject.net.flow.TrafficTreatment; |
| 57 | import org.onosproject.net.host.HostEvent; |
| 58 | import org.onosproject.net.host.HostService; |
| 59 | import org.onosproject.net.host.InterfaceIpAddress; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 60 | import org.onosproject.net.intent.AbstractIntentTest; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 61 | import org.onosproject.net.intent.Intent; |
| 62 | import org.onosproject.net.intent.IntentOperations; |
| 63 | import org.onosproject.net.intent.IntentService; |
| 64 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 65 | import org.onosproject.net.provider.ProviderId; |
| 66 | import org.onosproject.sdnip.IntentSynchronizer.IntentKey; |
| 67 | import org.onosproject.sdnip.Router.InternalHostListener; |
| 68 | import org.onosproject.sdnip.config.BgpPeer; |
| 69 | import org.onosproject.sdnip.config.Interface; |
| 70 | import org.onosproject.sdnip.config.SdnIpConfigurationService; |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 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 | */ |
Jonathan Hart | b834920 | 2014-12-10 11:23:59 -0800 | [diff] [blame] | 81 | public class RouterAsyncArpTest 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, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 171 | interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"), |
| 172 | VlanId.NONE); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 173 | interfaces.add(sw1Eth1); |
| 174 | |
| 175 | Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet(); |
| 176 | interfaceIpAddresses2.add(new InterfaceIpAddress( |
| 177 | IpAddress.valueOf("192.168.20.101"), |
| 178 | IpPrefix.valueOf("192.168.20.0/24"))); |
| 179 | Interface sw2Eth1 = new Interface(SW2_ETH1, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 180 | interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"), |
| 181 | VlanId.NONE); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 182 | interfaces.add(sw2Eth1); |
| 183 | |
| 184 | Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet(); |
| 185 | interfaceIpAddresses3.add(new InterfaceIpAddress( |
| 186 | IpAddress.valueOf("192.168.30.101"), |
| 187 | IpPrefix.valueOf("192.168.30.0/24"))); |
| 188 | Interface sw3Eth1 = new Interface(SW3_ETH1, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 189 | interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"), |
| 190 | VlanId.NONE); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 191 | interfaces.add(sw3Eth1); |
| 192 | |
| 193 | expect(interfaceService.getInterface(SW1_ETH1)).andReturn(sw1Eth1).anyTimes(); |
| 194 | expect(interfaceService.getInterface(SW2_ETH1)).andReturn(sw2Eth1).anyTimes(); |
| 195 | expect(interfaceService.getInterface(SW3_ETH1)).andReturn(sw3Eth1).anyTimes(); |
| 196 | expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes(); |
| 197 | replay(interfaceService); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * This method tests adding a route entry. |
| 202 | */ |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 203 | @Test |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 204 | public void testRouteAdd() throws TestUtilsException { |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 205 | |
| 206 | // Construct a route entry |
| 207 | RouteEntry routeEntry = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 208 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 209 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 210 | |
| 211 | // Construct a route intent |
| 212 | MultiPointToSinglePointIntent intent = staticIntentBuilder(); |
| 213 | |
| 214 | // Set up test expectation |
| 215 | reset(hostService); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 216 | expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn( |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 217 | new HashSet<Host>()).anyTimes(); |
| 218 | hostService.startMonitoringIp(IpAddress.valueOf("192.168.10.1")); |
| 219 | replay(hostService); |
| 220 | |
| 221 | reset(intentService); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 222 | IntentOperations.Builder builder = IntentOperations.builder(APPID); |
| 223 | builder.addSubmitOperation(intent); |
| 224 | intentService.execute(TestIntentServiceHelper.eqExceptId( |
| 225 | builder.build())); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 226 | replay(intentService); |
| 227 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 228 | // Call the processRouteUpdates() method in Router class |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 229 | intentSynchronizer.leaderChanged(true); |
| 230 | TestUtils.setField(intentSynchronizer, "isActivatedLeader", true); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 231 | RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE, |
| 232 | routeEntry); |
| 233 | router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate)); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 234 | |
| 235 | Host host = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 236 | MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE, |
| 237 | new HostLocation( |
| 238 | SW1_ETH1.deviceId(), |
| 239 | SW1_ETH1.port(), 1), |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 240 | Sets.newHashSet(IpAddress.valueOf("192.168.10.1"))); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 241 | internalHostListener.event( |
| 242 | new HostEvent(HostEvent.Type.HOST_ADDED, host)); |
| 243 | |
| 244 | // Verify |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 245 | assertEquals(router.getRoutes4().size(), 1); |
| 246 | assertTrue(router.getRoutes4().contains(routeEntry)); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 247 | assertEquals(intentSynchronizer.getRouteIntents().size(), 1); |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 248 | Intent firstIntent = |
| 249 | intentSynchronizer.getRouteIntents().iterator().next(); |
| 250 | IntentKey firstIntentKey = new IntentKey(firstIntent); |
| 251 | IntentKey intentKey = new IntentKey(intent); |
| 252 | assertTrue(firstIntentKey.equals(intentKey)); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 253 | verify(intentService); |
| 254 | verify(hostService); |
| 255 | |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * This method tests updating a route entry. |
| 260 | * |
| 261 | * @throws TestUtilsException |
| 262 | */ |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 263 | @Test |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 264 | public void testRouteUpdate() throws TestUtilsException { |
| 265 | |
| 266 | // Construct the existing route entry |
| 267 | RouteEntry routeEntry = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 268 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 269 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 270 | |
| 271 | // Construct the existing MultiPointToSinglePointIntent intent |
| 272 | MultiPointToSinglePointIntent intent = staticIntentBuilder(); |
| 273 | |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 274 | // Set up the ribTable field of Router class with existing route, and |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 275 | // routeIntents field with the corresponding existing intent |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 276 | setRibTableField(routeEntry); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 277 | setRouteIntentsField(routeEntry, intent); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 278 | |
| 279 | // Start to construct a new route entry and new intent |
| 280 | RouteEntry routeEntryUpdate = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 281 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 282 | Ip4Address.valueOf("192.168.20.1")); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 283 | |
| 284 | // Construct a new MultiPointToSinglePointIntent intent |
| 285 | TrafficSelector.Builder selectorBuilderNew = |
| 286 | DefaultTrafficSelector.builder(); |
| 287 | selectorBuilderNew.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 288 | routeEntryUpdate.prefix()); |
| 289 | |
| 290 | TrafficTreatment.Builder treatmentBuilderNew = |
| 291 | DefaultTrafficTreatment.builder(); |
| 292 | treatmentBuilderNew.setEthDst(MacAddress.valueOf("00:00:00:00:00:02")); |
| 293 | |
| 294 | Set<ConnectPoint> ingressPointsNew = new HashSet<ConnectPoint>(); |
| 295 | ingressPointsNew.add(SW1_ETH1); |
| 296 | ingressPointsNew.add(SW3_ETH1); |
| 297 | |
| 298 | MultiPointToSinglePointIntent intentNew = |
| 299 | new MultiPointToSinglePointIntent(APPID, |
| 300 | selectorBuilderNew.build(), |
| 301 | treatmentBuilderNew.build(), |
| 302 | ingressPointsNew, SW2_ETH1); |
| 303 | |
| 304 | // Set up test expectation |
| 305 | reset(hostService); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 306 | expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn( |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 307 | new HashSet<Host>()).anyTimes(); |
| 308 | hostService.startMonitoringIp(IpAddress.valueOf("192.168.20.1")); |
| 309 | replay(hostService); |
| 310 | |
| 311 | reset(intentService); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 312 | IntentOperations.Builder builder = IntentOperations.builder(APPID); |
| 313 | builder.addWithdrawOperation(intent.id()); |
| 314 | intentService.execute(TestIntentServiceHelper.eqExceptId( |
| 315 | builder.build())); |
| 316 | builder = IntentOperations.builder(APPID); |
| 317 | builder.addSubmitOperation(intentNew); |
| 318 | intentService.execute(TestIntentServiceHelper.eqExceptId( |
| 319 | builder.build())); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 320 | replay(intentService); |
| 321 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 322 | // Call the processRouteUpdates() method in Router class |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 323 | intentSynchronizer.leaderChanged(true); |
| 324 | TestUtils.setField(intentSynchronizer, "isActivatedLeader", true); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 325 | RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE, |
| 326 | routeEntryUpdate); |
| 327 | router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate)); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 328 | |
| 329 | Host host = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 330 | MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE, |
| 331 | new HostLocation( |
| 332 | SW2_ETH1.deviceId(), |
| 333 | SW2_ETH1.port(), 1), |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 334 | Sets.newHashSet(IpAddress.valueOf("192.168.20.1"))); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 335 | internalHostListener.event( |
| 336 | new HostEvent(HostEvent.Type.HOST_ADDED, host)); |
| 337 | |
| 338 | // Verify |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 339 | assertEquals(router.getRoutes4().size(), 1); |
| 340 | assertTrue(router.getRoutes4().contains(routeEntryUpdate)); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 341 | assertEquals(intentSynchronizer.getRouteIntents().size(), 1); |
Pavlin Radoslavov | 97e8a8b | 2014-11-24 17:51:28 -0800 | [diff] [blame] | 342 | Intent firstIntent = |
| 343 | intentSynchronizer.getRouteIntents().iterator().next(); |
| 344 | IntentKey firstIntentKey = new IntentKey(firstIntent); |
| 345 | IntentKey intentNewKey = new IntentKey(intentNew); |
| 346 | assertTrue(firstIntentKey.equals(intentNewKey)); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 347 | verify(intentService); |
| 348 | verify(hostService); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * This method tests deleting a route entry. |
| 353 | */ |
| 354 | @Test |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 355 | public void testRouteDelete() throws TestUtilsException { |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 356 | |
| 357 | // Construct the existing route entry |
| 358 | RouteEntry routeEntry = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 359 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 360 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 361 | |
| 362 | // Construct the existing MultiPointToSinglePointIntent intent |
| 363 | MultiPointToSinglePointIntent intent = staticIntentBuilder(); |
| 364 | |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 365 | // Set up the ribTable field of Router class with existing route, and |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 366 | // routeIntents field with the corresponding existing intent |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 367 | setRibTableField(routeEntry); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 368 | setRouteIntentsField(routeEntry, intent); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 369 | |
| 370 | // Set up expectation |
| 371 | reset(intentService); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 372 | IntentOperations.Builder builder = IntentOperations.builder(APPID); |
| 373 | builder.addWithdrawOperation(intent.id()); |
| 374 | intentService.execute(TestIntentServiceHelper.eqExceptId( |
| 375 | builder.build())); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 376 | replay(intentService); |
| 377 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 378 | // Call the processRouteUpdates() method in Router class |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 379 | intentSynchronizer.leaderChanged(true); |
| 380 | TestUtils.setField(intentSynchronizer, "isActivatedLeader", true); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 381 | RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.DELETE, |
| 382 | routeEntry); |
| 383 | router.processRouteUpdates(Collections.<RouteUpdate>singletonList(routeUpdate)); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 384 | |
| 385 | // Verify |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 386 | assertEquals(router.getRoutes4().size(), 0); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 387 | assertEquals(intentSynchronizer.getRouteIntents().size(), 0); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 388 | verify(intentService); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Constructs a static MultiPointToSinglePointIntent. |
| 393 | */ |
| 394 | private MultiPointToSinglePointIntent staticIntentBuilder() { |
| 395 | |
| 396 | TrafficSelector.Builder selectorBuilder = |
| 397 | DefaultTrafficSelector.builder(); |
| 398 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 399 | IpPrefix.valueOf("1.1.1.0/24")); |
| 400 | |
| 401 | TrafficTreatment.Builder treatmentBuilder = |
| 402 | DefaultTrafficTreatment.builder(); |
| 403 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 404 | |
| 405 | Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>(); |
| 406 | ingressPoints.add(SW2_ETH1); |
| 407 | ingressPoints.add(SW3_ETH1); |
| 408 | |
| 409 | MultiPointToSinglePointIntent intent = |
| 410 | new MultiPointToSinglePointIntent(APPID, |
| 411 | selectorBuilder.build(), treatmentBuilder.build(), |
| 412 | ingressPoints, SW1_ETH1); |
| 413 | |
| 414 | return intent; |
| 415 | } |
| 416 | |
| 417 | /** |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 418 | * Sets ribTable Field in Router class. |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 419 | * |
| 420 | * @throws TestUtilsException |
| 421 | */ |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 422 | private void setRibTableField(RouteEntry routeEntry) |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 423 | throws TestUtilsException { |
| 424 | |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 425 | InvertedRadixTree<RouteEntry> ribTable = |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 426 | new ConcurrentInvertedRadixTree<>( |
| 427 | new DefaultByteArrayNodeFactory()); |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 428 | ribTable.put(RouteEntry.createBinaryString(routeEntry.prefix()), |
| 429 | routeEntry); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 430 | TestUtils.setField(router, "ribTable4", ribTable); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 434 | * Sets routeIntentsField in IntentSynchronizer class. |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 435 | * |
| 436 | * @throws TestUtilsException |
| 437 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 438 | private void setRouteIntentsField(RouteEntry routeEntry, |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 439 | MultiPointToSinglePointIntent intent) |
| 440 | throws TestUtilsException { |
| 441 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 442 | ConcurrentHashMap<IpPrefix, MultiPointToSinglePointIntent> |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 443 | routeIntents = new ConcurrentHashMap<>(); |
| 444 | routeIntents.put(routeEntry.prefix(), intent); |
| 445 | TestUtils.setField(intentSynchronizer, "routeIntents", routeIntents); |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 446 | } |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 447 | } |