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