Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.vpls; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.BeforeClass; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.TestApplicationId; |
| 26 | import org.onosproject.app.ApplicationService; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.core.IdGenerator; |
| 30 | import org.onosproject.incubator.net.intf.Interface; |
| 31 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.DefaultHost; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Host; |
| 36 | import org.onosproject.net.HostId; |
| 37 | import org.onosproject.net.HostLocation; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
| 43 | import org.onosproject.net.host.HostEvent; |
| 44 | import org.onosproject.net.host.HostListener; |
| 45 | import org.onosproject.net.host.HostService; |
| 46 | import org.onosproject.net.host.HostServiceAdapter; |
| 47 | import org.onosproject.net.intent.Intent; |
| 48 | import org.onosproject.net.intent.IntentService; |
| 49 | import org.onosproject.net.intent.IntentServiceAdapter; |
| 50 | import org.onosproject.net.intent.IntentUtils; |
| 51 | import org.onosproject.net.intent.Key; |
| 52 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 53 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
| 54 | import org.onosproject.net.provider.ProviderId; |
| 55 | import org.onosproject.routing.IntentSynchronizationAdminService; |
| 56 | import org.onosproject.routing.IntentSynchronizationService; |
| 57 | |
| 58 | import java.util.ArrayList; |
| 59 | import java.util.Collections; |
| 60 | import java.util.HashSet; |
| 61 | import java.util.List; |
| 62 | import java.util.Set; |
| 63 | import java.util.concurrent.atomic.AtomicLong; |
| 64 | import java.util.stream.Collectors; |
| 65 | |
| 66 | import static java.lang.String.format; |
| 67 | import static org.easymock.EasyMock.createMock; |
| 68 | import static org.easymock.EasyMock.expect; |
| 69 | import static org.easymock.EasyMock.replay; |
| 70 | import static org.junit.Assert.assertEquals; |
| 71 | import static org.junit.Assert.assertTrue; |
| 72 | |
| 73 | /** |
| 74 | * Tests for the {@link Vpls} class. |
| 75 | */ |
| 76 | public class VplsTest { |
| 77 | |
| 78 | private static final int NUM_DEVICES = 7; |
| 79 | |
| 80 | private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 81 | private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 82 | private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03"); |
| 83 | private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04"); |
| 84 | private static final MacAddress MAC5 = MacAddress.valueOf("00:00:00:00:00:05"); |
| 85 | private static final MacAddress MAC6 = MacAddress.valueOf("00:00:00:00:00:06"); |
| 86 | private static final MacAddress MAC7 = MacAddress.valueOf("00:00:00:00:00:07"); |
| 87 | |
| 88 | private static final Ip4Address IP1 = Ip4Address.valueOf("192.168.1.1"); |
| 89 | private static final Ip4Address IP2 = Ip4Address.valueOf("192.168.1.2"); |
| 90 | |
| 91 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 92 | |
| 93 | private static final VlanId VLAN1 = VlanId.vlanId((short) 1); |
| 94 | private static final VlanId VLAN2 = VlanId.vlanId((short) 2); |
| 95 | |
| 96 | private static final int PRIORITY_OFFSET = 1000; |
| 97 | private static final String PREFIX_BROADCAST = "brc"; |
| 98 | private static final String PREFIX_UNICAST = "uni"; |
| 99 | |
| 100 | private static final DeviceId DID1 = getDeviceId(1); |
| 101 | private static final DeviceId DID2 = getDeviceId(2); |
| 102 | private static final DeviceId DID3 = getDeviceId(3); |
| 103 | private static final DeviceId DID4 = getDeviceId(4); |
| 104 | private static final DeviceId DID5 = getDeviceId(5); |
| 105 | private static final DeviceId DID6 = getDeviceId(6); |
| 106 | |
| 107 | private static final ConnectPoint C1 = new ConnectPoint(DID1, P1); |
| 108 | private static final ConnectPoint C2 = new ConnectPoint(DID2, P1); |
| 109 | private static final ConnectPoint C3 = new ConnectPoint(DID3, P1); |
| 110 | private static final ConnectPoint C4 = new ConnectPoint(DID4, P1); |
| 111 | private static final ConnectPoint C5 = new ConnectPoint(DID5, P1); |
| 112 | private static final ConnectPoint C6 = new ConnectPoint(DID6, P1); |
| 113 | |
| 114 | private static final HostId HID1 = HostId.hostId(MAC1, VLAN1); |
| 115 | private static final HostId HID2 = HostId.hostId(MAC2, VLAN1); |
| 116 | private static final HostId HID3 = HostId.hostId(MAC3, VLAN1); |
| 117 | private static final HostId HID4 = HostId.hostId(MAC4, VLAN2); |
| 118 | private static final HostId HID5 = HostId.hostId(MAC5, VLAN2); |
| 119 | private static final HostId HID6 = HostId.hostId(MAC6, VLAN2); |
| 120 | private static final HostId HID7 = HostId.hostId(MAC7, VlanId.NONE); |
| 121 | |
| 122 | private ApplicationService applicationService; |
| 123 | private CoreService coreService; |
| 124 | private HostListener hostListener; |
| 125 | private Set<Host> hostsAvailable; |
| 126 | private HostService hostService; |
| 127 | private IntentService intentService; |
| 128 | private InterfaceService interfaceService; |
| 129 | private Vpls vpls; |
| 130 | |
| 131 | private static final String APP_NAME = "org.onosproject.vpls"; |
| 132 | private static final ApplicationId APPID = TestApplicationId.create(APP_NAME); |
| 133 | |
| 134 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 135 | |
| 136 | @BeforeClass |
| 137 | public static void setUpClass() { |
| 138 | IdGenerator idGenerator = new TestIdGenerator(); |
| 139 | Intent.bindIdGenerator(idGenerator); |
| 140 | } |
| 141 | |
| 142 | @Before |
| 143 | public void setUp() throws Exception { |
| 144 | applicationService = createMock(ApplicationService.class); |
| 145 | |
| 146 | coreService = createMock(CoreService.class); |
| 147 | expect(coreService.registerApplication(APP_NAME)) |
| 148 | .andReturn(APPID); |
| 149 | replay(coreService); |
| 150 | |
| 151 | hostsAvailable = Sets.newHashSet(); |
| 152 | hostService = new TestHostService(hostsAvailable); |
| 153 | |
| 154 | intentService = new TestIntentService(); |
| 155 | |
| 156 | TestIntentSynchronizer intentSynchronizer = |
| 157 | new TestIntentSynchronizer(intentService); |
| 158 | |
| 159 | interfaceService = createMock(InterfaceService.class); |
| 160 | addIntfConfig(); |
| 161 | |
| 162 | vpls = new Vpls(); |
| 163 | vpls.applicationService = applicationService; |
| 164 | vpls.coreService = coreService; |
| 165 | vpls.hostService = hostService; |
| 166 | vpls.intentService = intentService; |
| 167 | vpls.interfaceService = interfaceService; |
| 168 | vpls.intentSynchronizer = intentSynchronizer; |
| 169 | vpls.intentSynchronizerAdmin = intentSynchronizer; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Creates the interface configuration. On devices 1, 2 and 3 is configured |
| 174 | * an interface on port 1 with vlan 1. On devices 4, 5 and 6 is configured |
| 175 | * an interface on port 1 with vlan 2. On device 5 no interfaces are |
| 176 | * configured. |
| 177 | */ |
| 178 | private void addIntfConfig() { |
| 179 | Set<Interface> interfaces = Sets.newHashSet(); |
| 180 | Set<Interface> vlanOneSet = new HashSet<>(); |
| 181 | Set<Interface> vlanTwoSet = new HashSet<>(); |
| 182 | |
| 183 | for (int i = 1; i <= NUM_DEVICES - 1; i++) { |
| 184 | ConnectPoint cp = new ConnectPoint(getDeviceId(i), P1); |
| 185 | |
| 186 | Interface intf = |
| 187 | new Interface(cp, Collections.emptySet(), null, VlanId.NONE); |
| 188 | |
| 189 | if (i <= 3) { |
| 190 | intf = new Interface(cp, Collections.emptySet(), null, VLAN1); |
| 191 | interfaces.add(intf); |
| 192 | vlanOneSet.add(intf); |
| 193 | } else if (i > 3 && i <= 6) { |
| 194 | intf = new Interface(cp, Collections.emptySet(), null, VLAN2); |
| 195 | interfaces.add(intf); |
| 196 | vlanTwoSet.add(intf); |
| 197 | } |
| 198 | expect(interfaceService.getInterfacesByPort(cp)) |
| 199 | .andReturn(Sets.newHashSet(intf)).anyTimes(); |
| 200 | } |
| 201 | expect(interfaceService.getInterfacesByVlan(VLAN1)) |
| 202 | .andReturn(vlanOneSet).anyTimes(); |
| 203 | expect(interfaceService.getInterfacesByVlan(VLAN2)) |
| 204 | .andReturn(vlanTwoSet).anyTimes(); |
| 205 | expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes(); |
| 206 | |
| 207 | replay(interfaceService); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Checks the case in which six ports are configured with VLANs but no |
| 212 | * hosts are registered by the HostService. The first three ports have an |
| 213 | * interface configured on VLAN1, the other three on VLAN2. The number of |
| 214 | * intents expected is six: three for VLAN1, three for VLAN2. three sp2mp |
| 215 | * intents, three mp2sp intents. |
| 216 | */ |
| 217 | @Test |
| 218 | public void testActivateNoHosts() { |
| 219 | vpls.activate(); |
| 220 | |
| 221 | List<Intent> expectedIntents = new ArrayList<>(); |
| 222 | expectedIntents.addAll(generateVlanOneBrc()); |
| 223 | expectedIntents.addAll(generateVlanTwoBrc()); |
| 224 | |
| 225 | checkIntents(expectedIntents); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Checks the case in which six ports are configured with VLANs and four |
| 230 | * hosts are registered by the HostService. The first three ports have an |
| 231 | * interface configured on VLAN1, the other three on VLAN2. The number of |
| 232 | * intents expected is twelve: six for VLAN1, six for VLAN2. six sp2mp |
| 233 | * intents, six mp2sp intents. For VLAN1 IPs are added to demonstrate it |
| 234 | * doesn't influence the number of intents created. |
| 235 | */ |
| 236 | @Test |
| 237 | public void testFourInterfacesConfiguredHostsPresent() { |
| 238 | Host h1 = new DefaultHost(PID, HID1, MAC1, VLAN1, getLocation(1), |
| 239 | Collections.singleton(IP1)); |
| 240 | Host h2 = new DefaultHost(PID, HID2, MAC2, VLAN1, getLocation(2), |
| 241 | Collections.singleton(IP2)); |
| 242 | Host h3 = new DefaultHost(PID, HID3, MAC3, VLAN1, getLocation(3), |
| 243 | Collections.EMPTY_SET); |
| 244 | Host h4 = new DefaultHost(PID, HID4, MAC4, VLAN2, getLocation(4), |
| 245 | Collections.EMPTY_SET); |
| 246 | Host h5 = new DefaultHost(PID, HID5, MAC5, VLAN2, getLocation(5), |
| 247 | Collections.EMPTY_SET); |
| 248 | Host h6 = new DefaultHost(PID, HID6, MAC6, VLAN2, getLocation(6), |
| 249 | Collections.EMPTY_SET); |
| 250 | hostsAvailable.addAll(Sets.newHashSet(h1, h2, h3, h4, h5, h6)); |
| 251 | |
| 252 | vpls.activate(); |
| 253 | |
| 254 | List<Intent> expectedIntents = new ArrayList<>(); |
| 255 | expectedIntents.addAll(generateVlanOneBrc()); |
| 256 | expectedIntents.addAll(generateVlanOneUni()); |
| 257 | expectedIntents.addAll(generateVlanTwoBrc()); |
| 258 | expectedIntents.addAll(generateVlanTwoUni()); |
| 259 | |
| 260 | checkIntents(expectedIntents); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Checks the case in which six ports are configured with VLANs and |
| 265 | * initially no hosts are registered by the HostService. The first three |
| 266 | * ports have an interface configured on VLAN1, the other three have an |
| 267 | * interface configured on VLAN2. When the module starts up, three hosts - |
| 268 | * on device one, two and three - port 1 (both on VLAN1), are registered by |
| 269 | * the HostService and events are sent to the application. sp2mp intents |
| 270 | * are created for all interfaces configured and mp2sp intents are created |
| 271 | * only for the hosts attached. |
| 272 | * The number of intents expected is nine: six for VLAN1, three for VLAN2. |
| 273 | * Six sp2mp intents, three mp2sp intents. IPs are added on the first two |
| 274 | * hosts only to demonstrate it doesn't influence the number of intents |
| 275 | * created. |
| 276 | * An additional host is added on device seven, port one to demonstrate |
| 277 | * that, even if it's on the same VLAN of other interfaces configured in |
| 278 | * the system, it doesn't let the application generate intents, since it's |
| 279 | * not connected to the interface configured. |
| 280 | */ |
| 281 | @Test |
| 282 | public void testFourInterfacesThreeHostEventsSameVlan() { |
| 283 | vpls.activate(); |
| 284 | |
| 285 | Host h1 = new DefaultHost(PID, HID1, MAC1, VLAN1, getLocation(1), |
| 286 | Collections.singleton(IP1)); |
| 287 | Host h2 = new DefaultHost(PID, HID2, MAC2, VLAN1, getLocation(2), |
| 288 | Collections.singleton(IP2)); |
| 289 | Host h3 = new DefaultHost(PID, HID3, MAC3, VLAN1, getLocation(3), |
| 290 | Collections.EMPTY_SET); |
| 291 | Host h7 = new DefaultHost(PID, HID7, MAC7, VLAN1, getLocation(7), |
| 292 | Collections.EMPTY_SET); |
| 293 | hostsAvailable.addAll(Sets.newHashSet(h1, h2, h3, h7)); |
| 294 | |
| 295 | hostsAvailable.forEach(host -> |
| 296 | hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host))); |
| 297 | |
| 298 | List<Intent> expectedIntents = new ArrayList<>(); |
| 299 | expectedIntents.addAll(generateVlanOneBrc()); |
| 300 | expectedIntents.addAll(generateVlanOneUni()); |
| 301 | expectedIntents.addAll(generateVlanTwoBrc()); |
| 302 | |
| 303 | checkIntents(expectedIntents); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Checks the case in which six ports are configured with VLANs and |
| 308 | * initially no hosts are registered by the HostService. The first three |
| 309 | * ports have an interface configured on VLAN1, the other three have an |
| 310 | * interface configured on VLAN2. When the module starts up, two hosts - |
| 311 | * on device one and four - port 1 (VLAN 1 and VLAN 2), are registered by |
| 312 | * the HostService and events are sent to the application. sp2mp intents |
| 313 | * are created for all interfaces configured and no mp2sp intents are created |
| 314 | * at all, since the minimum number of hosts needed on the same vlan to |
| 315 | * create mp2sp intents is 2. |
| 316 | * The number of intents expected is six: three for VLAN1, three for VLAN2. |
| 317 | * six sp2mp intents, zero mp2sp intents. IPs are added on the first host |
| 318 | * only to demonstrate it doesn't influence the number of intents created. |
| 319 | */ |
| 320 | @Test |
| 321 | public void testFourInterfacesTwoHostEventsDifferentVlan() { |
| 322 | vpls.activate(); |
| 323 | |
| 324 | Host h1 = new DefaultHost(PID, HID1, MAC1, VLAN1, getLocation(1), |
| 325 | Collections.singleton(IP1)); |
| 326 | Host h4 = new DefaultHost(PID, HID4, MAC4, VLAN2, getLocation(4), |
| 327 | Collections.EMPTY_SET); |
| 328 | hostsAvailable.addAll(Sets.newHashSet(h1, h4)); |
| 329 | |
| 330 | hostsAvailable.forEach(host -> { |
| 331 | hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host)); |
| 332 | }); |
| 333 | |
| 334 | List<Intent> expectedIntents = new ArrayList<>(); |
| 335 | expectedIntents.addAll(generateVlanOneBrc()); |
| 336 | expectedIntents.addAll(generateVlanTwoBrc()); |
| 337 | |
| 338 | checkIntents(expectedIntents); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Checks both that the number of intents in submitted in the intent |
| 343 | * framework it's equal to the number of intents expected and that all |
| 344 | * intents are equivalent. |
| 345 | * |
| 346 | * @param intents the list of intents expected |
| 347 | */ |
| 348 | private void checkIntents(List<Intent> intents) { |
| 349 | assertEquals(intents.size(), intentService.getIntentCount()); |
| 350 | |
| 351 | for (Intent intentOne : intents) { |
| 352 | boolean found = false; |
| 353 | for (Intent intentTwo : intentService.getIntents()) { |
| 354 | if (intentOne.key().equals(intentTwo.key())) { |
| 355 | found = true; |
| 356 | assertTrue(format("Comparing %s and %s", intentOne, intentTwo), |
| 357 | IntentUtils.intentsAreEqual(intentOne, intentTwo)); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | assertTrue(found); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Generates the list of the expected sp2mp intents for VLAN 1. |
| 367 | * |
| 368 | * @return the list of expected sp2mp intents for VLAN 1 |
| 369 | */ |
| 370 | private List<SinglePointToMultiPointIntent> generateVlanOneBrc() { |
| 371 | Key key = null; |
| 372 | |
| 373 | List<SinglePointToMultiPointIntent> intents = new ArrayList<>(); |
| 374 | |
| 375 | // Building sp2mp intent for H1 - VLAN1 |
| 376 | key = Key.of((PREFIX_BROADCAST + "-" + DID1 + "-" + P1 + "-" + VLAN1), |
| 377 | APPID); |
| 378 | intents.add(buildBrcIntent(key, C1, Sets.newHashSet(C2, C3), VLAN1)); |
| 379 | |
| 380 | // Building sp2mp intent for H2 - VLAN1 |
| 381 | key = Key.of((PREFIX_BROADCAST + "-" + DID2 + "-" + P1 + "-" + VLAN1), |
| 382 | APPID); |
| 383 | intents.add(buildBrcIntent(key, C2, Sets.newHashSet(C1, C3), VLAN1)); |
| 384 | |
| 385 | // Building sp2mp intent for H3 - VLAN1 |
| 386 | key = Key.of((PREFIX_BROADCAST + "-" + DID3 + "-" + P1 + "-" + VLAN1), |
| 387 | APPID); |
| 388 | intents.add(buildBrcIntent(key, C3, Sets.newHashSet(C1, C2), VLAN1)); |
| 389 | |
| 390 | return intents; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Generates the list of the expected mp2sp intents for VLAN 1. |
| 395 | * |
| 396 | * @return the list of expected mp2sp intents for VLAN 1 |
| 397 | */ |
| 398 | private List<MultiPointToSinglePointIntent> generateVlanOneUni() { |
| 399 | Key key = null; |
| 400 | |
| 401 | List<MultiPointToSinglePointIntent> intents = new ArrayList<>(); |
| 402 | |
| 403 | // Building mp2sp intent for H1 - VLAN1 |
| 404 | key = Key.of((PREFIX_UNICAST + "-" + DID1 + "-" + P1 + "-" + VLAN1), |
| 405 | APPID); |
| 406 | intents.add(buildUniIntent(key, Sets.newHashSet(C2, C3), C1, VLAN1, MAC1)); |
| 407 | |
| 408 | // Building mp2sp intent for H2 - VLAN1 |
| 409 | key = Key.of((PREFIX_UNICAST + "-" + DID2 + "-" + P1 + "-" + VLAN1), |
| 410 | APPID); |
| 411 | intents.add(buildUniIntent(key, Sets.newHashSet(C1, C3), C2, VLAN1, MAC2)); |
| 412 | |
| 413 | // Building mp2sp intent for H3 - VLAN1 |
| 414 | key = Key.of((PREFIX_UNICAST + "-" + DID3 + "-" + P1 + "-" + VLAN1), |
| 415 | APPID); |
| 416 | intents.add(buildUniIntent(key, Sets.newHashSet(C1, C2), C3, VLAN1, MAC3)); |
| 417 | |
| 418 | return intents; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Generates the list of the expected sp2mp intents for VLAN 2. |
| 423 | * |
| 424 | * @return the list of expected sp2mp intents for VLAN 2 |
| 425 | */ |
| 426 | private List<SinglePointToMultiPointIntent> generateVlanTwoBrc() { |
| 427 | Key key = null; |
| 428 | |
| 429 | List<SinglePointToMultiPointIntent> intents = new ArrayList<>(); |
| 430 | |
| 431 | // Building sp2mp intent for H4 - VLAN2 |
| 432 | key = Key.of((PREFIX_BROADCAST + "-" + DID4 + "-" + P1 + "-" + VLAN2), |
| 433 | APPID); |
| 434 | intents.add(buildBrcIntent(key, C4, Sets.newHashSet(C5, C6), VLAN2)); |
| 435 | |
| 436 | // Building sp2mp intent for H5 - VLAN2 |
| 437 | key = Key.of((PREFIX_BROADCAST + "-" + DID5 + "-" + P1 + "-" + VLAN2), |
| 438 | APPID); |
| 439 | intents.add(buildBrcIntent(key, C5, Sets.newHashSet(C4, C6), VLAN2)); |
| 440 | |
| 441 | // Building sp2mp intent for H6 - VLAN2 |
| 442 | key = Key.of((PREFIX_BROADCAST + "-" + DID6 + "-" + P1 + "-" + VLAN2), |
| 443 | APPID); |
| 444 | intents.add(buildBrcIntent(key, C6, Sets.newHashSet(C4, C5), VLAN2)); |
| 445 | |
| 446 | return intents; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Generates the list of the expected mp2sp intents for VLAN 2. |
| 451 | * |
| 452 | * @return the list of expected mp2sp intents for VLAN 2 |
| 453 | */ |
| 454 | private List<MultiPointToSinglePointIntent> generateVlanTwoUni() { |
| 455 | Key key = null; |
| 456 | |
| 457 | List<MultiPointToSinglePointIntent> intents = new ArrayList<>(); |
| 458 | |
| 459 | // Building mp2sp intent for H4 - VLAN2 |
| 460 | key = Key.of((PREFIX_UNICAST + "-" + DID4 + "-" + P1 + "-" + VLAN2), |
| 461 | APPID); |
| 462 | intents.add(buildUniIntent(key, Sets.newHashSet(C5, C6), C4, VLAN2, MAC4)); |
| 463 | |
| 464 | // Building mp2sp intent for H5 - VLAN2 |
| 465 | key = Key.of((PREFIX_UNICAST + "-" + DID5 + "-" + P1 + "-" + VLAN2), |
| 466 | APPID); |
| 467 | intents.add(buildUniIntent(key, Sets.newHashSet(C4, C6), C5, VLAN2, MAC5)); |
| 468 | |
| 469 | // Building mp2sp intent for H6 - VLAN2 |
| 470 | key = Key.of((PREFIX_UNICAST + "-" + DID6 + "-" + P1 + "-" + VLAN2), |
| 471 | APPID); |
| 472 | intents.add(buildUniIntent(key, Sets.newHashSet(C4, C5), C6, VLAN2, MAC6)); |
| 473 | |
| 474 | return intents; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Builds a Single Point to Multi Point intent. |
| 479 | * |
| 480 | * @param key The intent key |
| 481 | * @param src The source Connect Point |
| 482 | * @param dsts The destination Connect Points |
| 483 | * @return Single Point to Multi Point intent generated. |
| 484 | */ |
| 485 | private SinglePointToMultiPointIntent buildBrcIntent(Key key, |
| 486 | ConnectPoint src, |
| 487 | Set<ConnectPoint> dsts, |
| 488 | VlanId vlanId) { |
| 489 | SinglePointToMultiPointIntent intent; |
| 490 | |
| 491 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 492 | |
| 493 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 494 | .matchEthDst(MacAddress.BROADCAST) |
| 495 | .matchVlanId(vlanId) |
| 496 | .build(); |
| 497 | |
| 498 | intent = SinglePointToMultiPointIntent.builder() |
| 499 | .appId(APPID) |
| 500 | .key(key) |
| 501 | .selector(selector) |
| 502 | .treatment(treatment) |
| 503 | .ingressPoint(src) |
| 504 | .egressPoints(dsts) |
| 505 | .priority(PRIORITY_OFFSET) |
| 506 | .build(); |
| 507 | return intent; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Builds a Multi Point to Single Point intent. |
| 512 | * |
| 513 | * @param key The intent key |
| 514 | * @param srcs The source Connect Points |
| 515 | * @param dst The destination Connect Point |
| 516 | * @return Multi Point to Single Point intent generated. |
| 517 | */ |
| 518 | private MultiPointToSinglePointIntent buildUniIntent(Key key, |
| 519 | Set<ConnectPoint> srcs, |
| 520 | ConnectPoint dst, |
| 521 | VlanId vlanId, |
| 522 | MacAddress mac) { |
| 523 | MultiPointToSinglePointIntent intent; |
| 524 | |
| 525 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 526 | |
| 527 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder() |
| 528 | .matchEthDst(mac) |
| 529 | .matchVlanId(vlanId); |
| 530 | |
| 531 | TrafficSelector selector = builder.build(); |
| 532 | |
| 533 | intent = MultiPointToSinglePointIntent.builder() |
| 534 | .appId(APPID) |
| 535 | .key(key) |
| 536 | .selector(selector) |
| 537 | .treatment(treatment) |
| 538 | .ingressPoints(srcs) |
| 539 | .egressPoint(dst) |
| 540 | .priority(PRIORITY_OFFSET) |
| 541 | .build(); |
| 542 | return intent; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Returns the device ID of the ith device. |
| 547 | * |
| 548 | * @param i device to get the ID of |
| 549 | * @return the device ID |
| 550 | */ |
| 551 | private static DeviceId getDeviceId(int i) { |
| 552 | return DeviceId.deviceId("" + i); |
| 553 | } |
| 554 | |
| 555 | private static HostLocation getLocation(int i) { |
| 556 | return new HostLocation(new ConnectPoint(getDeviceId(i), P1), 123L); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Represents a fake IntentService class that easily allows to store and |
| 561 | * retrieve intents without implementing the IntentService logic. |
| 562 | */ |
| 563 | private class TestIntentService extends IntentServiceAdapter { |
| 564 | |
| 565 | private Set<Intent> intents; |
| 566 | |
| 567 | public TestIntentService() { |
| 568 | intents = Sets.newHashSet(); |
| 569 | } |
| 570 | |
| 571 | @Override |
| 572 | public void submit(Intent intent) { |
| 573 | intents.add(intent); |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public long getIntentCount() { |
| 578 | return intents.size(); |
| 579 | } |
| 580 | |
| 581 | @Override |
| 582 | public Iterable<Intent> getIntents() { |
| 583 | return intents; |
| 584 | } |
| 585 | |
| 586 | @Override |
| 587 | public Intent getIntent(Key intentKey) { |
| 588 | for (Intent intent : intents) { |
| 589 | if (intent.key().equals(intentKey)) { |
| 590 | return intent; |
| 591 | } |
| 592 | } |
| 593 | return null; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Represents a fake HostService class which allows to add hosts manually |
| 599 | * in each test, when needed. |
| 600 | */ |
| 601 | private class TestHostService extends HostServiceAdapter { |
| 602 | |
| 603 | private Set<Host> hosts; |
| 604 | |
| 605 | public TestHostService(Set<Host> hosts) { |
| 606 | this.hosts = hosts; |
| 607 | } |
| 608 | |
| 609 | @Override |
| 610 | public void addListener(HostListener listener) { |
| 611 | VplsTest.this.hostListener = listener; |
| 612 | } |
| 613 | |
| 614 | @Override |
| 615 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
| 616 | return hosts.stream() |
| 617 | .filter(h -> h.location().elementId().equals(connectPoint.elementId()) |
| 618 | && h.location().port().equals(connectPoint.port())) |
| 619 | .collect(Collectors.toSet()); |
| 620 | } |
| 621 | |
| 622 | } |
| 623 | |
| 624 | private static class TestIdGenerator implements IdGenerator { |
| 625 | |
| 626 | private final AtomicLong id = new AtomicLong(0); |
| 627 | |
| 628 | @Override |
| 629 | public long getNewId() { |
| 630 | return id.getAndIncrement(); |
| 631 | } |
| 632 | |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Test IntentSynchronizer that passes all intents straight through to the |
| 637 | * intent service. |
| 638 | */ |
| 639 | private class TestIntentSynchronizer implements IntentSynchronizationService, |
| 640 | IntentSynchronizationAdminService { |
| 641 | |
| 642 | private final IntentService intentService; |
| 643 | |
| 644 | /** |
| 645 | * Creates a new test intent synchronizer. |
| 646 | * |
| 647 | * @param intentService intent service |
| 648 | */ |
| 649 | public TestIntentSynchronizer(IntentService intentService) { |
| 650 | this.intentService = intentService; |
| 651 | } |
| 652 | |
| 653 | @Override |
| 654 | public void submit(Intent intent) { |
| 655 | intentService.submit(intent); |
| 656 | } |
| 657 | |
| 658 | @Override |
| 659 | public void withdraw(Intent intent) { |
| 660 | intentService.withdraw(intent); |
| 661 | } |
| 662 | |
| 663 | @Override |
| 664 | public void modifyPrimary(boolean isPrimary) { |
| 665 | |
| 666 | } |
| 667 | |
| 668 | @Override |
| 669 | public void removeIntents() { |
| 670 | |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | } |