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