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