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; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 8 | import static org.easymock.EasyMock.reportMatcher; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 9 | import static org.easymock.EasyMock.reset; |
| 10 | import static org.easymock.EasyMock.verify; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 11 | import static org.hamcrest.Matchers.is; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 12 | import static org.junit.Assert.assertEquals; |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 13 | import static org.junit.Assert.assertFalse; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 14 | import static org.junit.Assert.assertThat; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 15 | import static org.junit.Assert.assertTrue; |
| 16 | |
| 17 | import java.util.HashSet; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 18 | import java.util.LinkedList; |
| 19 | import java.util.List; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 20 | import java.util.Set; |
| 21 | import java.util.concurrent.ConcurrentHashMap; |
| 22 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 23 | import org.apache.commons.collections4.CollectionUtils; |
| 24 | import org.easymock.IArgumentMatcher; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.junit.TestUtils; |
| 28 | import org.onlab.junit.TestUtils.TestUtilsException; |
| 29 | import org.onlab.onos.core.ApplicationId; |
| 30 | import org.onlab.onos.net.ConnectPoint; |
| 31 | import org.onlab.onos.net.DefaultHost; |
| 32 | import org.onlab.onos.net.DeviceId; |
| 33 | import org.onlab.onos.net.Host; |
| 34 | import org.onlab.onos.net.HostId; |
| 35 | import org.onlab.onos.net.HostLocation; |
| 36 | import org.onlab.onos.net.PortNumber; |
| 37 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 38 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 39 | import org.onlab.onos.net.flow.TrafficSelector; |
| 40 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 41 | import org.onlab.onos.net.host.HostListener; |
| 42 | import org.onlab.onos.net.host.HostService; |
| 43 | import org.onlab.onos.net.host.InterfaceIpAddress; |
| 44 | import org.onlab.onos.net.intent.Intent; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 45 | import org.onlab.onos.net.intent.IntentId; |
| 46 | import org.onlab.onos.net.intent.IntentOperation; |
| 47 | import org.onlab.onos.net.intent.IntentOperations; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 48 | import org.onlab.onos.net.intent.IntentService; |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 49 | import org.onlab.onos.net.intent.IntentState; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 50 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 51 | import org.onlab.onos.net.provider.ProviderId; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 52 | import org.onlab.onos.sdnip.IntentSynchronizer.IntentKey; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 53 | import org.onlab.onos.sdnip.config.Interface; |
| 54 | import org.onlab.packet.Ethernet; |
| 55 | import org.onlab.packet.IpAddress; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 56 | import org.onlab.packet.Ip4Address; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 57 | import org.onlab.packet.IpPrefix; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 58 | import org.onlab.packet.Ip4Prefix; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 59 | import org.onlab.packet.MacAddress; |
| 60 | import org.onlab.packet.VlanId; |
| 61 | |
| 62 | import com.google.common.collect.Sets; |
| 63 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 64 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 65 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
| 66 | |
| 67 | /** |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 68 | * This class tests the intent synchronization function in the |
| 69 | * IntentSynchronizer class. |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 70 | */ |
| 71 | public class IntentSyncTest { |
| 72 | |
| 73 | private InterfaceService interfaceService; |
| 74 | private IntentService intentService; |
| 75 | private HostService hostService; |
| 76 | |
| 77 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 78 | DeviceId.deviceId("of:0000000000000001"), |
| 79 | PortNumber.portNumber(1)); |
| 80 | |
| 81 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 82 | DeviceId.deviceId("of:0000000000000002"), |
| 83 | PortNumber.portNumber(1)); |
| 84 | |
| 85 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 86 | DeviceId.deviceId("of:0000000000000003"), |
| 87 | PortNumber.portNumber(1)); |
| 88 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 89 | private IntentSynchronizer intentSynchronizer; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 90 | private Router router; |
| 91 | |
| 92 | private static final ApplicationId APPID = new ApplicationId() { |
| 93 | @Override |
| 94 | public short id() { |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String name() { |
| 100 | return "SDNIP"; |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | @Before |
| 105 | public void setUp() throws Exception { |
| 106 | setUpInterfaceService(); |
| 107 | setUpHostService(); |
| 108 | intentService = createMock(IntentService.class); |
| 109 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 110 | intentSynchronizer = new IntentSynchronizer(APPID, intentService); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 111 | router = new Router(APPID, intentSynchronizer, null, interfaceService, |
| 112 | hostService); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Sets up InterfaceService. |
| 117 | */ |
| 118 | private void setUpInterfaceService() { |
| 119 | |
| 120 | interfaceService = createMock(InterfaceService.class); |
| 121 | |
| 122 | Set<Interface> interfaces = Sets.newHashSet(); |
| 123 | |
| 124 | Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet(); |
| 125 | interfaceIpAddresses1.add(new InterfaceIpAddress( |
| 126 | IpAddress.valueOf("192.168.10.101"), |
| 127 | IpPrefix.valueOf("192.168.10.0/24"))); |
| 128 | Interface sw1Eth1 = new Interface(SW1_ETH1, |
| 129 | interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01")); |
| 130 | interfaces.add(sw1Eth1); |
| 131 | |
| 132 | Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet(); |
| 133 | interfaceIpAddresses2.add(new InterfaceIpAddress( |
| 134 | IpAddress.valueOf("192.168.20.101"), |
| 135 | IpPrefix.valueOf("192.168.20.0/24"))); |
| 136 | Interface sw2Eth1 = new Interface(SW2_ETH1, |
| 137 | interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02")); |
| 138 | interfaces.add(sw2Eth1); |
| 139 | |
| 140 | Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet(); |
| 141 | interfaceIpAddresses3.add(new InterfaceIpAddress( |
| 142 | IpAddress.valueOf("192.168.30.101"), |
| 143 | IpPrefix.valueOf("192.168.30.0/24"))); |
| 144 | Interface sw3Eth1 = new Interface(SW3_ETH1, |
| 145 | interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03")); |
| 146 | interfaces.add(sw3Eth1); |
| 147 | |
| 148 | expect(interfaceService.getInterface(SW1_ETH1)).andReturn( |
| 149 | sw1Eth1).anyTimes(); |
| 150 | expect(interfaceService.getInterface(SW2_ETH1)).andReturn( |
| 151 | sw2Eth1).anyTimes(); |
| 152 | expect(interfaceService.getInterface(SW3_ETH1)).andReturn( |
| 153 | sw3Eth1).anyTimes(); |
| 154 | expect(interfaceService.getInterfaces()).andReturn( |
| 155 | interfaces).anyTimes(); |
| 156 | replay(interfaceService); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets up the host service with details of hosts. |
| 161 | */ |
| 162 | private void setUpHostService() { |
| 163 | hostService = createMock(HostService.class); |
| 164 | |
| 165 | hostService.addListener(anyObject(HostListener.class)); |
| 166 | expectLastCall().anyTimes(); |
| 167 | |
| 168 | IpAddress host1Address = IpAddress.valueOf("192.168.10.1"); |
| 169 | Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 170 | MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE, |
| 171 | new HostLocation(SW1_ETH1, 1), |
| 172 | Sets.newHashSet(host1Address)); |
| 173 | |
| 174 | expect(hostService.getHostsByIp(host1Address)) |
| 175 | .andReturn(Sets.newHashSet(host1)).anyTimes(); |
| 176 | hostService.startMonitoringIp(host1Address); |
| 177 | expectLastCall().anyTimes(); |
| 178 | |
| 179 | |
| 180 | IpAddress host2Address = IpAddress.valueOf("192.168.20.1"); |
| 181 | Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 182 | MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE, |
| 183 | new HostLocation(SW2_ETH1, 1), |
| 184 | Sets.newHashSet(host2Address)); |
| 185 | |
| 186 | expect(hostService.getHostsByIp(host2Address)) |
| 187 | .andReturn(Sets.newHashSet(host2)).anyTimes(); |
| 188 | hostService.startMonitoringIp(host2Address); |
| 189 | expectLastCall().anyTimes(); |
| 190 | |
| 191 | |
| 192 | IpAddress host3Address = IpAddress.valueOf("192.168.30.1"); |
| 193 | Host host3 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 194 | MacAddress.valueOf("00:00:00:00:00:03"), VlanId.NONE, |
| 195 | new HostLocation(SW3_ETH1, 1), |
| 196 | Sets.newHashSet(host3Address)); |
| 197 | |
| 198 | expect(hostService.getHostsByIp(host3Address)) |
| 199 | .andReturn(Sets.newHashSet(host3)).anyTimes(); |
| 200 | hostService.startMonitoringIp(host3Address); |
| 201 | expectLastCall().anyTimes(); |
| 202 | |
| 203 | |
| 204 | replay(hostService); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * This method tests the behavior of intent Synchronizer. |
| 209 | * |
| 210 | * @throws TestUtilsException |
| 211 | */ |
| 212 | @Test |
| 213 | public void testIntentSync() throws TestUtilsException { |
| 214 | |
| 215 | // |
| 216 | // Construct routes and intents. |
| 217 | // This test simulates the following cases during the master change |
| 218 | // time interval: |
| 219 | // 1. RouteEntry1 did not change and the intent also did not change. |
| 220 | // 2. RouteEntry2 was deleted, but the intent was not deleted. |
| 221 | // 3. RouteEntry3 was newly added, and the intent was also submitted. |
| 222 | // 4. RouteEntry4 was updated to RouteEntry4Update, and the intent was |
| 223 | // also updated to a new one. |
| 224 | // 5. RouteEntry5 did not change, but its intent id changed. |
| 225 | // 6. RouteEntry6 was newly added, but the intent was not submitted. |
| 226 | // |
| 227 | RouteEntry routeEntry1 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 228 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 229 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 230 | |
| 231 | RouteEntry routeEntry2 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 232 | Ip4Prefix.valueOf("2.2.2.0/24"), |
| 233 | Ip4Address.valueOf("192.168.20.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 234 | |
| 235 | RouteEntry routeEntry3 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 236 | Ip4Prefix.valueOf("3.3.3.0/24"), |
| 237 | Ip4Address.valueOf("192.168.30.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 238 | |
| 239 | RouteEntry routeEntry4 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 240 | Ip4Prefix.valueOf("4.4.4.0/24"), |
| 241 | Ip4Address.valueOf("192.168.30.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 242 | |
| 243 | RouteEntry routeEntry4Update = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 244 | Ip4Prefix.valueOf("4.4.4.0/24"), |
| 245 | Ip4Address.valueOf("192.168.20.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 246 | |
| 247 | RouteEntry routeEntry5 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 248 | Ip4Prefix.valueOf("5.5.5.0/24"), |
| 249 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 250 | |
| 251 | RouteEntry routeEntry6 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 252 | Ip4Prefix.valueOf("6.6.6.0/24"), |
| 253 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 254 | |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 255 | RouteEntry routeEntry7 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 256 | Ip4Prefix.valueOf("7.7.7.0/24"), |
| 257 | Ip4Address.valueOf("192.168.10.1")); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 258 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 259 | MultiPointToSinglePointIntent intent1 = intentBuilder( |
| 260 | routeEntry1.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
| 261 | MultiPointToSinglePointIntent intent2 = intentBuilder( |
| 262 | routeEntry2.prefix(), "00:00:00:00:00:02", SW2_ETH1); |
| 263 | MultiPointToSinglePointIntent intent3 = intentBuilder( |
| 264 | routeEntry3.prefix(), "00:00:00:00:00:03", SW3_ETH1); |
| 265 | MultiPointToSinglePointIntent intent4 = intentBuilder( |
| 266 | routeEntry4.prefix(), "00:00:00:00:00:03", SW3_ETH1); |
| 267 | MultiPointToSinglePointIntent intent4Update = intentBuilder( |
| 268 | routeEntry4Update.prefix(), "00:00:00:00:00:02", SW2_ETH1); |
| 269 | MultiPointToSinglePointIntent intent5 = intentBuilder( |
| 270 | routeEntry5.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 271 | MultiPointToSinglePointIntent intent7 = intentBuilder( |
| 272 | routeEntry7.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 273 | |
| 274 | // Compose a intent, which is equal to intent5 but the id is different. |
| 275 | MultiPointToSinglePointIntent intent5New = |
| 276 | staticIntentBuilder(intent5, routeEntry5, "00:00:00:00:00:01"); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 277 | assertThat(IntentSynchronizer.IntentKey.equalIntents( |
| 278 | intent5, intent5New), |
| 279 | is(true)); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 280 | assertFalse(intent5.equals(intent5New)); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 281 | |
| 282 | MultiPointToSinglePointIntent intent6 = intentBuilder( |
| 283 | routeEntry6.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
| 284 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 285 | // Set up the bgpRoutes field in Router class and routeIntents fields |
| 286 | // in IntentSynchronizer class |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 287 | InvertedRadixTree<RouteEntry> bgpRoutes = |
| 288 | new ConcurrentInvertedRadixTree<>( |
| 289 | new DefaultByteArrayNodeFactory()); |
| 290 | bgpRoutes.put(RouteEntry.createBinaryString(routeEntry1.prefix()), |
| 291 | routeEntry1); |
| 292 | bgpRoutes.put(RouteEntry.createBinaryString(routeEntry3.prefix()), |
| 293 | routeEntry3); |
| 294 | bgpRoutes.put(RouteEntry.createBinaryString(routeEntry4Update.prefix()), |
| 295 | routeEntry4Update); |
| 296 | bgpRoutes.put(RouteEntry.createBinaryString(routeEntry5.prefix()), |
| 297 | routeEntry5); |
| 298 | bgpRoutes.put(RouteEntry.createBinaryString(routeEntry6.prefix()), |
| 299 | routeEntry6); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 300 | bgpRoutes.put(RouteEntry.createBinaryString(routeEntry7.prefix()), |
| 301 | routeEntry7); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 302 | TestUtils.setField(router, "bgpRoutes", bgpRoutes); |
| 303 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 304 | ConcurrentHashMap<Ip4Prefix, MultiPointToSinglePointIntent> |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 305 | routeIntents = new ConcurrentHashMap<>(); |
| 306 | routeIntents.put(routeEntry1.prefix(), intent1); |
| 307 | routeIntents.put(routeEntry3.prefix(), intent3); |
| 308 | routeIntents.put(routeEntry4Update.prefix(), intent4Update); |
| 309 | routeIntents.put(routeEntry5.prefix(), intent5New); |
| 310 | routeIntents.put(routeEntry6.prefix(), intent6); |
| 311 | routeIntents.put(routeEntry7.prefix(), intent7); |
| 312 | TestUtils.setField(intentSynchronizer, "routeIntents", routeIntents); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 313 | |
| 314 | // Set up expectation |
| 315 | reset(intentService); |
| 316 | Set<Intent> intents = new HashSet<Intent>(); |
| 317 | intents.add(intent1); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 318 | expect(intentService.getIntentState(intent1.id())) |
| 319 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 320 | intents.add(intent2); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 321 | expect(intentService.getIntentState(intent2.id())) |
| 322 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 323 | intents.add(intent4); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 324 | expect(intentService.getIntentState(intent4.id())) |
| 325 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 326 | intents.add(intent5); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 327 | expect(intentService.getIntentState(intent5.id())) |
| 328 | .andReturn(IntentState.INSTALLED).anyTimes(); |
| 329 | intents.add(intent7); |
| 330 | expect(intentService.getIntentState(intent7.id())) |
| 331 | .andReturn(IntentState.WITHDRAWING).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 332 | expect(intentService.getIntents()).andReturn(intents).anyTimes(); |
| 333 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 334 | IntentOperations.Builder builder = IntentOperations.builder(); |
| 335 | builder.addWithdrawOperation(intent2.id()); |
| 336 | builder.addWithdrawOperation(intent4.id()); |
| 337 | intentService.execute(eqExceptId(builder.build())); |
| 338 | |
| 339 | builder = IntentOperations.builder(); |
| 340 | builder.addSubmitOperation(intent3); |
| 341 | builder.addSubmitOperation(intent4Update); |
| 342 | builder.addSubmitOperation(intent6); |
| 343 | builder.addSubmitOperation(intent7); |
| 344 | intentService.execute(eqExceptId(builder.build())); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 345 | replay(intentService); |
| 346 | |
| 347 | // Start the test |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 348 | intentSynchronizer.leaderChanged(true); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 349 | /* |
| 350 | TestUtils.callMethod(intentSynchronizer, "synchronizeIntents", |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 351 | new Class<?>[] {}); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 352 | */ |
| 353 | intentSynchronizer.synchronizeIntents(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 354 | |
| 355 | // Verify |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 356 | assertEquals(router.getRoutes().size(), 6); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 357 | assertTrue(router.getRoutes().contains(routeEntry1)); |
| 358 | assertTrue(router.getRoutes().contains(routeEntry3)); |
| 359 | assertTrue(router.getRoutes().contains(routeEntry4Update)); |
| 360 | assertTrue(router.getRoutes().contains(routeEntry5)); |
| 361 | assertTrue(router.getRoutes().contains(routeEntry6)); |
| 362 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 363 | assertEquals(intentSynchronizer.getRouteIntents().size(), 6); |
| 364 | assertTrue(intentSynchronizer.getRouteIntents().contains(intent1)); |
| 365 | assertTrue(intentSynchronizer.getRouteIntents().contains(intent3)); |
| 366 | assertTrue(intentSynchronizer.getRouteIntents().contains(intent4Update)); |
| 367 | assertTrue(intentSynchronizer.getRouteIntents().contains(intent5)); |
| 368 | assertTrue(intentSynchronizer.getRouteIntents().contains(intent6)); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 369 | |
| 370 | verify(intentService); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * MultiPointToSinglePointIntent builder. |
| 375 | * |
| 376 | * @param ipPrefix the ipPrefix to match |
| 377 | * @param nextHopMacAddress to which the destination MAC address in packet |
| 378 | * should be rewritten |
| 379 | * @param egressPoint to which packets should be sent |
| 380 | * @return the constructed MultiPointToSinglePointIntent |
| 381 | */ |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 382 | private MultiPointToSinglePointIntent intentBuilder(Ip4Prefix ipPrefix, |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 383 | String nextHopMacAddress, ConnectPoint egressPoint) { |
| 384 | |
| 385 | TrafficSelector.Builder selectorBuilder = |
| 386 | DefaultTrafficSelector.builder(); |
| 387 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipPrefix); |
| 388 | |
| 389 | TrafficTreatment.Builder treatmentBuilder = |
| 390 | DefaultTrafficTreatment.builder(); |
| 391 | treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress)); |
| 392 | |
| 393 | Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>(); |
| 394 | for (Interface intf : interfaceService.getInterfaces()) { |
| 395 | if (!intf.equals(interfaceService.getInterface(egressPoint))) { |
| 396 | ConnectPoint srcPort = intf.connectPoint(); |
| 397 | ingressPoints.add(srcPort); |
| 398 | } |
| 399 | } |
| 400 | MultiPointToSinglePointIntent intent = |
| 401 | new MultiPointToSinglePointIntent(APPID, |
| 402 | selectorBuilder.build(), treatmentBuilder.build(), |
| 403 | ingressPoints, egressPoint); |
| 404 | return intent; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * A static MultiPointToSinglePointIntent builder, the returned intent is |
| 409 | * equal to the input intent except that the id is different. |
| 410 | * |
| 411 | * |
| 412 | * @param intent the intent to be used for building a new intent |
| 413 | * @param routeEntry the relative routeEntry of the intent |
| 414 | * @return the newly constructed MultiPointToSinglePointIntent |
| 415 | * @throws TestUtilsException |
| 416 | */ |
| 417 | private MultiPointToSinglePointIntent staticIntentBuilder( |
| 418 | MultiPointToSinglePointIntent intent, RouteEntry routeEntry, |
| 419 | String nextHopMacAddress) throws TestUtilsException { |
| 420 | |
| 421 | // Use a different egress ConnectPoint with that in intent |
| 422 | // to generate a different id |
| 423 | MultiPointToSinglePointIntent intentNew = intentBuilder( |
| 424 | routeEntry.prefix(), nextHopMacAddress, SW2_ETH1); |
| 425 | TestUtils.setField(intentNew, "egressPoint", intent.egressPoint()); |
| 426 | TestUtils.setField(intentNew, |
| 427 | "ingressPoints", intent.ingressPoints()); |
| 428 | return intentNew; |
| 429 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame^] | 430 | |
| 431 | /* |
| 432 | * EasyMock matcher that matches {@link IntenOperations} but |
| 433 | * ignores the {@link IntentId} when matching. |
| 434 | * <p/> |
| 435 | * The normal intent equals method tests that the intent IDs are equal, |
| 436 | * however in these tests we can't know what the intent IDs will be in |
| 437 | * advance, so we can't set up expected intents with the correct IDs. Thus, |
| 438 | * the solution is to use an EasyMock matcher that verifies that all the |
| 439 | * value properties of the provided intent match the expected values, but |
| 440 | * ignores the intent ID when testing equality. |
| 441 | */ |
| 442 | private static final class IdAgnosticIntentOperationsMatcher implements |
| 443 | IArgumentMatcher { |
| 444 | |
| 445 | private final IntentOperations intentOperations; |
| 446 | private String providedString; |
| 447 | |
| 448 | /** |
| 449 | * Constructor taking the expected intent operations to match against. |
| 450 | * |
| 451 | * @param intentOperations the expected intent operations |
| 452 | */ |
| 453 | public IdAgnosticIntentOperationsMatcher( |
| 454 | IntentOperations intentOperations) { |
| 455 | this.intentOperations = intentOperations; |
| 456 | } |
| 457 | |
| 458 | @Override |
| 459 | public void appendTo(StringBuffer strBuffer) { |
| 460 | strBuffer.append("IntentOperationsMatcher unable to match: " |
| 461 | + providedString); |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public boolean matches(Object object) { |
| 466 | if (!(object instanceof IntentOperations)) { |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | IntentOperations providedIntentOperations = |
| 471 | (IntentOperations) object; |
| 472 | providedString = providedIntentOperations.toString(); |
| 473 | |
| 474 | List<IntentKey> thisSubmitIntents = new LinkedList<>(); |
| 475 | List<IntentId> thisWithdrawIntentIds = new LinkedList<>(); |
| 476 | List<IntentKey> thisReplaceIntents = new LinkedList<>(); |
| 477 | List<IntentKey> thisUpdateIntents = new LinkedList<>(); |
| 478 | List<IntentKey> providedSubmitIntents = new LinkedList<>(); |
| 479 | List<IntentId> providedWithdrawIntentIds = new LinkedList<>(); |
| 480 | List<IntentKey> providedReplaceIntents = new LinkedList<>(); |
| 481 | List<IntentKey> providedUpdateIntents = new LinkedList<>(); |
| 482 | |
| 483 | extractIntents(intentOperations, thisSubmitIntents, |
| 484 | thisWithdrawIntentIds, thisReplaceIntents, |
| 485 | thisUpdateIntents); |
| 486 | extractIntents(providedIntentOperations, providedSubmitIntents, |
| 487 | providedWithdrawIntentIds, providedReplaceIntents, |
| 488 | providedUpdateIntents); |
| 489 | |
| 490 | return CollectionUtils.isEqualCollection(thisSubmitIntents, |
| 491 | providedSubmitIntents) && |
| 492 | CollectionUtils.isEqualCollection(thisWithdrawIntentIds, |
| 493 | providedWithdrawIntentIds) && |
| 494 | CollectionUtils.isEqualCollection(thisUpdateIntents, |
| 495 | providedUpdateIntents) && |
| 496 | CollectionUtils.isEqualCollection(thisReplaceIntents, |
| 497 | providedReplaceIntents); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Extracts the intents per operation type. Each intent is encapsulated |
| 502 | * in IntentKey so it can be compared by excluding the Intent ID. |
| 503 | * |
| 504 | * @param intentOperations the container with the intent operations |
| 505 | * to extract the intents from |
| 506 | * @param submitIntents the SUBMIT intents |
| 507 | * @param withdrawIntentIds the WITHDRAW intents IDs |
| 508 | * @param replaceIntents the REPLACE intents |
| 509 | * @param updateIntents the UPDATE intens |
| 510 | */ |
| 511 | private void extractIntents(IntentOperations intentOperations, |
| 512 | List<IntentKey> submitIntents, |
| 513 | List<IntentId> withdrawIntentIds, |
| 514 | List<IntentKey> replaceIntents, |
| 515 | List<IntentKey> updateIntents) { |
| 516 | for (IntentOperation oper : intentOperations.operations()) { |
| 517 | IntentId intentId; |
| 518 | IntentKey intentKey; |
| 519 | switch (oper.type()) { |
| 520 | case SUBMIT: |
| 521 | intentKey = new IntentKey(oper.intent()); |
| 522 | submitIntents.add(intentKey); |
| 523 | break; |
| 524 | case WITHDRAW: |
| 525 | intentId = oper.intentId(); |
| 526 | withdrawIntentIds.add(intentId); |
| 527 | break; |
| 528 | case REPLACE: |
| 529 | intentKey = new IntentKey(oper.intent()); |
| 530 | replaceIntents.add(intentKey); |
| 531 | break; |
| 532 | case UPDATE: |
| 533 | intentKey = new IntentKey(oper.intent()); |
| 534 | updateIntents.add(intentKey); |
| 535 | break; |
| 536 | default: |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Matcher method to set an expected intent to match against (ignoring the |
| 545 | * the intent ID). |
| 546 | * |
| 547 | * @param intent the expected intent |
| 548 | * @return something of type IntentOperations |
| 549 | */ |
| 550 | private static IntentOperations eqExceptId( |
| 551 | IntentOperations intentOperations) { |
| 552 | reportMatcher(new IdAgnosticIntentOperationsMatcher(intentOperations)); |
| 553 | return intentOperations; |
| 554 | } |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 555 | } |