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