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