alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [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 | f5d9093 | 2014-10-27 10:50:04 -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.expectLastCall; |
| 22 | import static org.easymock.EasyMock.replay; |
| 23 | import static org.easymock.EasyMock.reset; |
| 24 | import static org.easymock.EasyMock.verify; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 25 | import static org.hamcrest.Matchers.is; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.assertEquals; |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.assertFalse; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 28 | import static org.junit.Assert.assertThat; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 29 | import static org.junit.Assert.assertTrue; |
| 30 | |
| 31 | import java.util.HashSet; |
| 32 | import java.util.Set; |
| 33 | import java.util.concurrent.ConcurrentHashMap; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.onlab.junit.TestUtils; |
| 38 | import org.onlab.junit.TestUtils.TestUtilsException; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame^] | 39 | import org.onlab.packet.Ethernet; |
| 40 | import org.onlab.packet.Ip4Address; |
| 41 | import org.onlab.packet.Ip4Prefix; |
| 42 | import org.onlab.packet.IpAddress; |
| 43 | import org.onlab.packet.IpPrefix; |
| 44 | import org.onlab.packet.MacAddress; |
| 45 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.core.ApplicationId; |
| 47 | import org.onosproject.net.ConnectPoint; |
| 48 | import org.onosproject.net.DefaultHost; |
| 49 | import org.onosproject.net.DeviceId; |
| 50 | import org.onosproject.net.Host; |
| 51 | import org.onosproject.net.HostId; |
| 52 | import org.onosproject.net.HostLocation; |
| 53 | import org.onosproject.net.PortNumber; |
| 54 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 55 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 56 | import org.onosproject.net.flow.TrafficSelector; |
| 57 | import org.onosproject.net.flow.TrafficTreatment; |
| 58 | import org.onosproject.net.host.HostListener; |
| 59 | import org.onosproject.net.host.HostService; |
| 60 | import org.onosproject.net.host.InterfaceIpAddress; |
| 61 | import org.onosproject.net.intent.AbstractIntentTest; |
| 62 | import org.onosproject.net.intent.Intent; |
| 63 | import org.onosproject.net.intent.IntentOperations; |
| 64 | import org.onosproject.net.intent.IntentService; |
| 65 | import org.onosproject.net.intent.IntentState; |
| 66 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 67 | import org.onosproject.net.provider.ProviderId; |
| 68 | import org.onosproject.sdnip.config.Interface; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 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 | /** |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 76 | * This class tests the intent synchronization function in the |
| 77 | * IntentSynchronizer class. |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 78 | */ |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 79 | public class IntentSyncTest extends AbstractIntentTest { |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 80 | |
| 81 | private InterfaceService interfaceService; |
| 82 | private IntentService intentService; |
| 83 | private HostService hostService; |
| 84 | |
| 85 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 86 | DeviceId.deviceId("of:0000000000000001"), |
| 87 | PortNumber.portNumber(1)); |
| 88 | |
| 89 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 90 | DeviceId.deviceId("of:0000000000000002"), |
| 91 | PortNumber.portNumber(1)); |
| 92 | |
| 93 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 94 | DeviceId.deviceId("of:0000000000000003"), |
| 95 | PortNumber.portNumber(1)); |
| 96 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 97 | private IntentSynchronizer intentSynchronizer; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 98 | private Router router; |
| 99 | |
| 100 | private static final ApplicationId APPID = new ApplicationId() { |
| 101 | @Override |
| 102 | public short id() { |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public String name() { |
| 108 | return "SDNIP"; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | @Before |
| 113 | public void setUp() throws Exception { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 114 | super.setUp(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 115 | setUpInterfaceService(); |
| 116 | setUpHostService(); |
| 117 | intentService = createMock(IntentService.class); |
| 118 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 119 | intentSynchronizer = new IntentSynchronizer(APPID, intentService); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 120 | router = new Router(APPID, intentSynchronizer, null, interfaceService, |
| 121 | hostService); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Sets up InterfaceService. |
| 126 | */ |
| 127 | private void setUpInterfaceService() { |
| 128 | |
| 129 | interfaceService = createMock(InterfaceService.class); |
| 130 | |
| 131 | Set<Interface> interfaces = Sets.newHashSet(); |
| 132 | |
| 133 | Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet(); |
| 134 | interfaceIpAddresses1.add(new InterfaceIpAddress( |
| 135 | IpAddress.valueOf("192.168.10.101"), |
| 136 | IpPrefix.valueOf("192.168.10.0/24"))); |
| 137 | Interface sw1Eth1 = new Interface(SW1_ETH1, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame^] | 138 | interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"), |
| 139 | VlanId.NONE); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 140 | interfaces.add(sw1Eth1); |
| 141 | |
| 142 | Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet(); |
| 143 | interfaceIpAddresses2.add(new InterfaceIpAddress( |
| 144 | IpAddress.valueOf("192.168.20.101"), |
| 145 | IpPrefix.valueOf("192.168.20.0/24"))); |
| 146 | Interface sw2Eth1 = new Interface(SW2_ETH1, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame^] | 147 | interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"), |
| 148 | VlanId.NONE); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 149 | interfaces.add(sw2Eth1); |
| 150 | |
| 151 | Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet(); |
| 152 | interfaceIpAddresses3.add(new InterfaceIpAddress( |
| 153 | IpAddress.valueOf("192.168.30.101"), |
| 154 | IpPrefix.valueOf("192.168.30.0/24"))); |
| 155 | Interface sw3Eth1 = new Interface(SW3_ETH1, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame^] | 156 | interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"), |
| 157 | VlanId.NONE); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 158 | interfaces.add(sw3Eth1); |
| 159 | |
| 160 | expect(interfaceService.getInterface(SW1_ETH1)).andReturn( |
| 161 | sw1Eth1).anyTimes(); |
| 162 | expect(interfaceService.getInterface(SW2_ETH1)).andReturn( |
| 163 | sw2Eth1).anyTimes(); |
| 164 | expect(interfaceService.getInterface(SW3_ETH1)).andReturn( |
| 165 | sw3Eth1).anyTimes(); |
| 166 | expect(interfaceService.getInterfaces()).andReturn( |
| 167 | interfaces).anyTimes(); |
| 168 | replay(interfaceService); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Sets up the host service with details of hosts. |
| 173 | */ |
| 174 | private void setUpHostService() { |
| 175 | hostService = createMock(HostService.class); |
| 176 | |
| 177 | hostService.addListener(anyObject(HostListener.class)); |
| 178 | expectLastCall().anyTimes(); |
| 179 | |
| 180 | IpAddress host1Address = IpAddress.valueOf("192.168.10.1"); |
| 181 | Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 182 | MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE, |
| 183 | new HostLocation(SW1_ETH1, 1), |
| 184 | Sets.newHashSet(host1Address)); |
| 185 | |
| 186 | expect(hostService.getHostsByIp(host1Address)) |
| 187 | .andReturn(Sets.newHashSet(host1)).anyTimes(); |
| 188 | hostService.startMonitoringIp(host1Address); |
| 189 | expectLastCall().anyTimes(); |
| 190 | |
| 191 | |
| 192 | IpAddress host2Address = IpAddress.valueOf("192.168.20.1"); |
| 193 | Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 194 | MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE, |
| 195 | new HostLocation(SW2_ETH1, 1), |
| 196 | Sets.newHashSet(host2Address)); |
| 197 | |
| 198 | expect(hostService.getHostsByIp(host2Address)) |
| 199 | .andReturn(Sets.newHashSet(host2)).anyTimes(); |
| 200 | hostService.startMonitoringIp(host2Address); |
| 201 | expectLastCall().anyTimes(); |
| 202 | |
| 203 | |
| 204 | IpAddress host3Address = IpAddress.valueOf("192.168.30.1"); |
| 205 | Host host3 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 206 | MacAddress.valueOf("00:00:00:00:00:03"), VlanId.NONE, |
| 207 | new HostLocation(SW3_ETH1, 1), |
| 208 | Sets.newHashSet(host3Address)); |
| 209 | |
| 210 | expect(hostService.getHostsByIp(host3Address)) |
| 211 | .andReturn(Sets.newHashSet(host3)).anyTimes(); |
| 212 | hostService.startMonitoringIp(host3Address); |
| 213 | expectLastCall().anyTimes(); |
| 214 | |
| 215 | |
| 216 | replay(hostService); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * This method tests the behavior of intent Synchronizer. |
| 221 | * |
| 222 | * @throws TestUtilsException |
| 223 | */ |
| 224 | @Test |
| 225 | public void testIntentSync() throws TestUtilsException { |
| 226 | |
| 227 | // |
| 228 | // Construct routes and intents. |
| 229 | // This test simulates the following cases during the master change |
| 230 | // time interval: |
| 231 | // 1. RouteEntry1 did not change and the intent also did not change. |
| 232 | // 2. RouteEntry2 was deleted, but the intent was not deleted. |
| 233 | // 3. RouteEntry3 was newly added, and the intent was also submitted. |
| 234 | // 4. RouteEntry4 was updated to RouteEntry4Update, and the intent was |
| 235 | // also updated to a new one. |
| 236 | // 5. RouteEntry5 did not change, but its intent id changed. |
| 237 | // 6. RouteEntry6 was newly added, but the intent was not submitted. |
| 238 | // |
| 239 | RouteEntry routeEntry1 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 240 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 241 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 242 | |
| 243 | RouteEntry routeEntry2 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 244 | Ip4Prefix.valueOf("2.2.2.0/24"), |
| 245 | Ip4Address.valueOf("192.168.20.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 246 | |
| 247 | RouteEntry routeEntry3 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 248 | Ip4Prefix.valueOf("3.3.3.0/24"), |
| 249 | Ip4Address.valueOf("192.168.30.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 250 | |
| 251 | RouteEntry routeEntry4 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 252 | Ip4Prefix.valueOf("4.4.4.0/24"), |
| 253 | Ip4Address.valueOf("192.168.30.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 254 | |
| 255 | RouteEntry routeEntry4Update = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 256 | Ip4Prefix.valueOf("4.4.4.0/24"), |
| 257 | Ip4Address.valueOf("192.168.20.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 258 | |
| 259 | RouteEntry routeEntry5 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 260 | Ip4Prefix.valueOf("5.5.5.0/24"), |
| 261 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 262 | |
| 263 | RouteEntry routeEntry6 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 264 | Ip4Prefix.valueOf("6.6.6.0/24"), |
| 265 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 266 | |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 267 | RouteEntry routeEntry7 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 268 | Ip4Prefix.valueOf("7.7.7.0/24"), |
| 269 | Ip4Address.valueOf("192.168.10.1")); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 270 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 271 | MultiPointToSinglePointIntent intent1 = intentBuilder( |
| 272 | routeEntry1.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
| 273 | MultiPointToSinglePointIntent intent2 = intentBuilder( |
| 274 | routeEntry2.prefix(), "00:00:00:00:00:02", SW2_ETH1); |
| 275 | MultiPointToSinglePointIntent intent3 = intentBuilder( |
| 276 | routeEntry3.prefix(), "00:00:00:00:00:03", SW3_ETH1); |
| 277 | MultiPointToSinglePointIntent intent4 = intentBuilder( |
| 278 | routeEntry4.prefix(), "00:00:00:00:00:03", SW3_ETH1); |
| 279 | MultiPointToSinglePointIntent intent4Update = intentBuilder( |
| 280 | routeEntry4Update.prefix(), "00:00:00:00:00:02", SW2_ETH1); |
| 281 | MultiPointToSinglePointIntent intent5 = intentBuilder( |
| 282 | routeEntry5.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 283 | MultiPointToSinglePointIntent intent7 = intentBuilder( |
| 284 | routeEntry7.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 285 | |
| 286 | // Compose a intent, which is equal to intent5 but the id is different. |
| 287 | MultiPointToSinglePointIntent intent5New = |
| 288 | staticIntentBuilder(intent5, routeEntry5, "00:00:00:00:00:01"); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 289 | assertThat(IntentSynchronizer.IntentKey.equalIntents( |
| 290 | intent5, intent5New), |
| 291 | is(true)); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 292 | assertFalse(intent5.equals(intent5New)); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 293 | |
| 294 | MultiPointToSinglePointIntent intent6 = intentBuilder( |
| 295 | routeEntry6.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
| 296 | |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 297 | // Set up the ribTable field in Router class and routeIntents fields |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 298 | // in IntentSynchronizer class |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 299 | InvertedRadixTree<RouteEntry> ribTable = |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 300 | new ConcurrentInvertedRadixTree<>( |
| 301 | new DefaultByteArrayNodeFactory()); |
Pavlin Radoslavov | 64c1ed1 | 2014-12-10 18:10:55 -0800 | [diff] [blame] | 302 | ribTable.put(RouteEntry.createBinaryString(routeEntry1.prefix()), |
| 303 | routeEntry1); |
| 304 | ribTable.put(RouteEntry.createBinaryString(routeEntry3.prefix()), |
| 305 | routeEntry3); |
| 306 | ribTable.put(RouteEntry.createBinaryString(routeEntry4Update.prefix()), |
| 307 | routeEntry4Update); |
| 308 | ribTable.put(RouteEntry.createBinaryString(routeEntry5.prefix()), |
| 309 | routeEntry5); |
| 310 | ribTable.put(RouteEntry.createBinaryString(routeEntry6.prefix()), |
| 311 | routeEntry6); |
| 312 | ribTable.put(RouteEntry.createBinaryString(routeEntry7.prefix()), |
| 313 | routeEntry7); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 314 | TestUtils.setField(router, "ribTable4", ribTable); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 315 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 316 | ConcurrentHashMap<IpPrefix, MultiPointToSinglePointIntent> |
| 317 | routeIntents = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 318 | routeIntents.put(routeEntry1.prefix(), intent1); |
| 319 | routeIntents.put(routeEntry3.prefix(), intent3); |
| 320 | routeIntents.put(routeEntry4Update.prefix(), intent4Update); |
| 321 | routeIntents.put(routeEntry5.prefix(), intent5New); |
| 322 | routeIntents.put(routeEntry6.prefix(), intent6); |
| 323 | routeIntents.put(routeEntry7.prefix(), intent7); |
| 324 | TestUtils.setField(intentSynchronizer, "routeIntents", routeIntents); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 325 | |
| 326 | // Set up expectation |
| 327 | reset(intentService); |
| 328 | Set<Intent> intents = new HashSet<Intent>(); |
| 329 | intents.add(intent1); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 330 | expect(intentService.getIntentState(intent1.id())) |
| 331 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 332 | intents.add(intent2); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 333 | expect(intentService.getIntentState(intent2.id())) |
| 334 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 335 | intents.add(intent4); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 336 | expect(intentService.getIntentState(intent4.id())) |
| 337 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 338 | intents.add(intent5); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 339 | expect(intentService.getIntentState(intent5.id())) |
| 340 | .andReturn(IntentState.INSTALLED).anyTimes(); |
| 341 | intents.add(intent7); |
| 342 | expect(intentService.getIntentState(intent7.id())) |
| 343 | .andReturn(IntentState.WITHDRAWING).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 344 | expect(intentService.getIntents()).andReturn(intents).anyTimes(); |
| 345 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 346 | IntentOperations.Builder builder = IntentOperations.builder(APPID); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 347 | builder.addWithdrawOperation(intent2.id()); |
| 348 | builder.addWithdrawOperation(intent4.id()); |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 349 | intentService.execute(TestIntentServiceHelper.eqExceptId( |
| 350 | builder.build())); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 351 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 352 | builder = IntentOperations.builder(APPID); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 353 | builder.addSubmitOperation(intent3); |
| 354 | builder.addSubmitOperation(intent4Update); |
| 355 | builder.addSubmitOperation(intent6); |
| 356 | builder.addSubmitOperation(intent7); |
Pavlin Radoslavov | dde22ae | 2014-11-24 11:47:17 -0800 | [diff] [blame] | 357 | intentService.execute(TestIntentServiceHelper.eqExceptId( |
| 358 | builder.build())); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 359 | replay(intentService); |
| 360 | |
| 361 | // Start the test |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 362 | intentSynchronizer.leaderChanged(true); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 363 | /* |
| 364 | TestUtils.callMethod(intentSynchronizer, "synchronizeIntents", |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 365 | new Class<?>[] {}); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 366 | */ |
| 367 | intentSynchronizer.synchronizeIntents(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 368 | |
| 369 | // Verify |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 370 | assertEquals(router.getRoutes4().size(), 6); |
| 371 | assertTrue(router.getRoutes4().contains(routeEntry1)); |
| 372 | assertTrue(router.getRoutes4().contains(routeEntry3)); |
| 373 | assertTrue(router.getRoutes4().contains(routeEntry4Update)); |
| 374 | assertTrue(router.getRoutes4().contains(routeEntry5)); |
| 375 | assertTrue(router.getRoutes4().contains(routeEntry6)); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 376 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 377 | assertEquals(intentSynchronizer.getRouteIntents().size(), 6); |
| 378 | assertTrue(intentSynchronizer.getRouteIntents().contains(intent1)); |
| 379 | assertTrue(intentSynchronizer.getRouteIntents().contains(intent3)); |
| 380 | assertTrue(intentSynchronizer.getRouteIntents().contains(intent4Update)); |
| 381 | assertTrue(intentSynchronizer.getRouteIntents().contains(intent5)); |
| 382 | assertTrue(intentSynchronizer.getRouteIntents().contains(intent6)); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 383 | |
| 384 | verify(intentService); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * MultiPointToSinglePointIntent builder. |
| 389 | * |
| 390 | * @param ipPrefix the ipPrefix to match |
| 391 | * @param nextHopMacAddress to which the destination MAC address in packet |
| 392 | * should be rewritten |
| 393 | * @param egressPoint to which packets should be sent |
| 394 | * @return the constructed MultiPointToSinglePointIntent |
| 395 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 396 | private MultiPointToSinglePointIntent intentBuilder(IpPrefix ipPrefix, |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 397 | String nextHopMacAddress, ConnectPoint egressPoint) { |
| 398 | |
| 399 | TrafficSelector.Builder selectorBuilder = |
| 400 | DefaultTrafficSelector.builder(); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 401 | if (ipPrefix.version() == Ip4Address.VERSION) { |
| 402 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); // IPv4 |
| 403 | } else { |
| 404 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); // IPv6 |
| 405 | } |
| 406 | selectorBuilder.matchIPDst(ipPrefix); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 407 | |
| 408 | TrafficTreatment.Builder treatmentBuilder = |
| 409 | DefaultTrafficTreatment.builder(); |
| 410 | treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress)); |
| 411 | |
| 412 | Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>(); |
| 413 | for (Interface intf : interfaceService.getInterfaces()) { |
| 414 | if (!intf.equals(interfaceService.getInterface(egressPoint))) { |
| 415 | ConnectPoint srcPort = intf.connectPoint(); |
| 416 | ingressPoints.add(srcPort); |
| 417 | } |
| 418 | } |
| 419 | MultiPointToSinglePointIntent intent = |
| 420 | new MultiPointToSinglePointIntent(APPID, |
| 421 | selectorBuilder.build(), treatmentBuilder.build(), |
| 422 | ingressPoints, egressPoint); |
| 423 | return intent; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * A static MultiPointToSinglePointIntent builder, the returned intent is |
| 428 | * equal to the input intent except that the id is different. |
| 429 | * |
| 430 | * |
| 431 | * @param intent the intent to be used for building a new intent |
| 432 | * @param routeEntry the relative routeEntry of the intent |
| 433 | * @return the newly constructed MultiPointToSinglePointIntent |
| 434 | * @throws TestUtilsException |
| 435 | */ |
| 436 | private MultiPointToSinglePointIntent staticIntentBuilder( |
| 437 | MultiPointToSinglePointIntent intent, RouteEntry routeEntry, |
| 438 | String nextHopMacAddress) throws TestUtilsException { |
| 439 | |
| 440 | // Use a different egress ConnectPoint with that in intent |
| 441 | // to generate a different id |
| 442 | MultiPointToSinglePointIntent intentNew = intentBuilder( |
| 443 | routeEntry.prefix(), nextHopMacAddress, SW2_ETH1); |
| 444 | TestUtils.setField(intentNew, "egressPoint", intent.egressPoint()); |
| 445 | TestUtils.setField(intentNew, |
| 446 | "ingressPoints", intent.ingressPoints()); |
| 447 | return intentNew; |
| 448 | } |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 449 | } |