blob: 7aa0d9ca643687355933680dfc81d9c1577ac377 [file] [log] [blame]
yoonseondc3210d2017-01-25 16:03:10 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
yoonseondc3210d2017-01-25 16:03:10 -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 */
16
17package org.onosproject.incubator.net.virtual.impl;
18
19import com.google.common.collect.Iterables;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.junit.TestUtils;
24import org.onlab.osgi.ServiceDirectory;
25import org.onlab.osgi.TestServiceDirectory;
26import org.onlab.packet.MacAddress;
27import org.onlab.packet.MplsLabel;
28import org.onosproject.TestApplicationId;
29import org.onosproject.common.event.impl.TestEventDispatcher;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
yoonseondc3210d2017-01-25 16:03:10 -080032import org.onosproject.core.GroupId;
33import org.onosproject.event.EventDeliveryService;
34import org.onosproject.incubator.net.virtual.NetworkId;
35import org.onosproject.incubator.net.virtual.VirtualNetwork;
36import org.onosproject.incubator.net.virtual.VirtualNetworkGroupStore;
37import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
yoonseondc3210d2017-01-25 16:03:10 -080038import org.onosproject.incubator.net.virtual.impl.provider.VirtualProviderManager;
39import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProvider;
40import org.onosproject.incubator.net.virtual.provider.VirtualGroupProvider;
41import org.onosproject.incubator.net.virtual.provider.VirtualGroupProviderService;
42import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080043import org.onosproject.incubator.net.virtual.store.impl.DistributedVirtualNetworkStore;
44import org.onosproject.incubator.net.virtual.store.impl.SimpleVirtualGroupStore;
yoonseondc3210d2017-01-25 16:03:10 -080045import org.onosproject.net.DeviceId;
46import org.onosproject.net.PortNumber;
47import org.onosproject.net.flow.DefaultTrafficTreatment;
48import org.onosproject.net.flow.TrafficTreatment;
49import org.onosproject.net.group.DefaultGroup;
50import org.onosproject.net.group.DefaultGroupBucket;
51import org.onosproject.net.group.DefaultGroupDescription;
52import org.onosproject.net.group.DefaultGroupKey;
53import org.onosproject.net.group.Group;
54import org.onosproject.net.group.GroupBucket;
55import org.onosproject.net.group.GroupBuckets;
56import org.onosproject.net.group.GroupDescription;
57import org.onosproject.net.group.GroupEvent;
58import org.onosproject.net.group.GroupKey;
59import org.onosproject.net.group.GroupListener;
60import org.onosproject.net.group.GroupOperation;
61import org.onosproject.net.group.GroupOperations;
62import org.onosproject.net.group.StoredGroupEntry;
yoonseondc3210d2017-01-25 16:03:10 -080063import org.onosproject.net.provider.ProviderId;
64import org.onosproject.store.service.TestStorageService;
65
66import java.util.ArrayList;
67import java.util.Arrays;
68import java.util.Collections;
69import java.util.List;
70
71import static org.junit.Assert.*;
72import static org.onosproject.incubator.net.virtual.impl.VirtualNetworkTestUtil.*;
73import static org.onosproject.net.NetTestTools.injectEventDispatcher;
74
75/**
76 * Test codifying the virtual group service & group provider service contracts.
77 */
78public class VirtualNetworkGroupManagerTest {
79
80 private VirtualNetworkManager manager;
81 private DistributedVirtualNetworkStore virtualNetworkManagerStore;
yoonseondc3210d2017-01-25 16:03:10 -080082 private ServiceDirectory testDirectory;
83 private VirtualProviderManager providerRegistryService;
84
85 private EventDeliveryService eventDeliveryService;
yoonseondc3210d2017-01-25 16:03:10 -080086
87 private VirtualNetworkGroupManager groupManager1;
88 private VirtualNetworkGroupManager groupManager2;
89
90 private VirtualNetworkGroupStore groupStore;
91
92 private TestGroupProvider provider = new TestGroupProvider();
93 private VirtualGroupProviderService providerService1;
94 private VirtualGroupProviderService providerService2;
95
96 protected TestGroupListener listener1 = new TestGroupListener();
97 protected TestGroupListener listener2 = new TestGroupListener();
98
99 private VirtualNetwork vnet1;
100 private VirtualNetwork vnet2;
101
102 private ApplicationId appId;
103
104 @Before
105 public void setUp() throws Exception {
106 virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
107
108 CoreService coreService = new TestCoreService();
109 TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
110 TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
111 virtualNetworkManagerStore.activate();
112
113 groupStore = new SimpleVirtualGroupStore();
114
115 providerRegistryService = new VirtualProviderManager();
116 providerRegistryService.registerProvider(provider);
117
118 manager = new VirtualNetworkManager();
119 manager.store = virtualNetworkManagerStore;
yoonseondc3210d2017-01-25 16:03:10 -0800120 TestUtils.setField(manager, "coreService", coreService);
121
122 eventDeliveryService = new TestEventDispatcher();
123 injectEventDispatcher(manager, eventDeliveryService);
yoonseondc3210d2017-01-25 16:03:10 -0800124
125 appId = new TestApplicationId("VirtualGroupManagerTest");
126
127 testDirectory = new TestServiceDirectory()
128 .add(VirtualNetworkStore.class, virtualNetworkManagerStore)
129 .add(CoreService.class, coreService)
130 .add(VirtualProviderRegistryService.class, providerRegistryService)
131 .add(EventDeliveryService.class, eventDeliveryService)
132 .add(VirtualNetworkGroupStore.class, groupStore);
133 TestUtils.setField(manager, "serviceDirectory", testDirectory);
134
135 manager.activate();
136
137 vnet1 = setupVirtualNetworkTopology(manager, TID1);
138 vnet2 = setupVirtualNetworkTopology(manager, TID2);
139
140 groupManager1 = new VirtualNetworkGroupManager(manager, vnet1.id());
141 groupManager2 = new VirtualNetworkGroupManager(manager, vnet2.id());
142 groupManager1.addListener(listener1);
143 groupManager2.addListener(listener2);
144
145 providerService1 = (VirtualGroupProviderService)
146 providerRegistryService.getProviderService(vnet1.id(),
147 VirtualGroupProvider.class);
148 providerService2 = (VirtualGroupProviderService)
149 providerRegistryService.getProviderService(vnet2.id(),
150 VirtualGroupProvider.class);
151 }
152
153 @After
154 public void tearDown() {
155 providerRegistryService.unregisterProvider(provider);
156 assertFalse("provider should not be registered",
157 providerRegistryService.getProviders().contains(provider.id()));
158 groupManager1.removeListener(listener1);
159 groupManager2.removeListener(listener2);
160
161 manager.deactivate();
162 virtualNetworkManagerStore.deactivate();
163 }
164
165 /**
166 * Tests group creation before the device group AUDIT completes.
167 */
168 @Test
169 public void testGroupServiceBasics() {
170 // Test Group creation before AUDIT process
171 testGroupCreationBeforeAudit(vnet1.id(), VDID1);
172 testGroupCreationBeforeAudit(vnet2.id(), VDID1);
173 }
174
175 /**
176 * Tests initial device group AUDIT process.
177 */
178 @Test
179 public void testGroupServiceInitialAudit() {
180 // Test Group creation before AUDIT process
181 testGroupCreationBeforeAudit(vnet1.id(), VDID1);
182 testGroupCreationBeforeAudit(vnet2.id(), VDID1);
183 // Test initial group audit process
184 testInitialAuditWithPendingGroupRequests(vnet1.id(), VDID1);
185 testInitialAuditWithPendingGroupRequests(vnet2.id(), VDID1);
186 }
187
188 /**
189 * Tests deletion process of any extraneous groups.
190 */
191 @Test
192 public void testGroupServiceAuditExtraneous() {
193 // Test Group creation before AUDIT process
194 testGroupCreationBeforeAudit(vnet1.id(), VDID1);
195 testGroupCreationBeforeAudit(vnet2.id(), VDID1);
196
197 // Test audit with extraneous and missing groups
198 testAuditWithExtraneousMissingGroups(vnet1.id(), VDID1);
199 testAuditWithExtraneousMissingGroups(vnet2.id(), VDID1);
200 }
201
202 /**
203 * Tests re-apply process of any missing groups tests execution of
204 * any pending group creation request after the device group AUDIT completes
205 * and tests event notifications after receiving confirmation for any
206 * operations from data plane.
207 */
208 @Test
209 public void testGroupServiceAuditConfirmed() {
210 // Test Group creation before AUDIT process
211 testGroupCreationBeforeAudit(vnet1.id(), VDID1);
212 testGroupCreationBeforeAudit(vnet2.id(), VDID1);
213
214 // Test audit with extraneous and missing groups
215 testAuditWithExtraneousMissingGroups(vnet1.id(), VDID1);
216 testAuditWithExtraneousMissingGroups(vnet2.id(), VDID1);
217
218 // Test audit with confirmed groups
219 testAuditWithConfirmedGroups(vnet1.id(), VDID1);
220 testAuditWithConfirmedGroups(vnet2.id(), VDID1);
221 }
222
223 /**
224 * Tests group Purge Operation.
225 */
226 @Test
227 public void testPurgeGroups() {
228 // Tests for virtual network 1
229 // Test Group creation before AUDIT process
230 testGroupCreationBeforeAudit(vnet1.id(), VDID1);
231 testAuditWithExtraneousMissingGroups(vnet1.id(), VDID1);
232 // Test group add bucket operations
233 testAddBuckets(vnet1.id(), VDID1);
234 // Test group Purge operations
235 testPurgeGroupEntry(vnet1.id(), VDID1);
236
237 // Tests for virtual network 2
238 // Test Group creation before AUDIT process
239 testGroupCreationBeforeAudit(vnet2.id(), VDID1);
240 testAuditWithExtraneousMissingGroups(vnet2.id(), VDID1);
241 // Test group add bucket operations
242 testAddBuckets(vnet2.id(), VDID1);
243 // Test group Purge operations
244 testPurgeGroupEntry(vnet2.id(), VDID1);
245 }
246
247 /**
248 * Tests group bucket modifications (additions and deletions) and
249 * Tests group deletion.
250 */
251 @Test
252 public void testGroupServiceBuckets() {
253 // Tests for virtual network 1
254 // Test Group creation before AUDIT process
255 testGroupCreationBeforeAudit(vnet1.id(), VDID1);
256
257 testAuditWithExtraneousMissingGroups(vnet1.id(), VDID1);
258 // Test group add bucket operations
259 testAddBuckets(vnet1.id(), VDID1);
260
261 // Test group remove bucket operations
262 testRemoveBuckets(vnet1.id(), VDID1);
263
264 // Test group remove operations
265 testRemoveGroup(vnet1.id(), VDID1);
266
267 // Tests for virtual network 2
268 // Test Group creation before AUDIT process
269 testGroupCreationBeforeAudit(vnet2.id(), VDID1);
270
271 testAuditWithExtraneousMissingGroups(vnet2.id(), VDID1);
272 // Test group add bucket operations
273 testAddBuckets(vnet2.id(), VDID1);
274
275 // Test group remove bucket operations
276 testRemoveBuckets(vnet2.id(), VDID1);
277
278 // Test group remove operations
279 testRemoveGroup(vnet2.id(), VDID1);
280 }
281
282 /**
283 * Tests group creation before the device group AUDIT completes with fallback
284 * provider.
285 */
286 @Test
287 public void testGroupServiceFallbackBasics() {
288 // Test Group creation before AUDIT process
289 testGroupCreationBeforeAudit(vnet1.id(), VDID2);
290 testGroupCreationBeforeAudit(vnet2.id(), VDID2);
291 }
292
293 // Test Group creation before AUDIT process
294 private void testGroupCreationBeforeAudit(NetworkId networkId, DeviceId deviceId) {
295 PortNumber[] ports1 = {PortNumber.portNumber(31),
296 PortNumber.portNumber(32)};
297 PortNumber[] ports2 = {PortNumber.portNumber(41),
298 PortNumber.portNumber(42)};
299 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
300 List<GroupBucket> buckets = new ArrayList<>();
301 List<PortNumber> outPorts = new ArrayList<>();
302 outPorts.addAll(Arrays.asList(ports1));
303 outPorts.addAll(Arrays.asList(ports2));
304 for (PortNumber portNumber : outPorts) {
305 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
306 tBuilder.setOutput(portNumber)
307 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
308 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
309 .pushMpls()
310 .setMpls(MplsLabel.mplsLabel(106));
311 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
312 tBuilder.build()));
313 }
314 GroupBuckets groupBuckets = new GroupBuckets(buckets);
315 GroupDescription newGroupDesc = new DefaultGroupDescription(deviceId,
316 Group.Type.SELECT,
317 groupBuckets,
318 key,
319 null,
320 appId);
321 VirtualNetworkGroupManager groupManager;
322 if (networkId.id() == 1) {
323 groupManager = groupManager1;
324 } else {
325 groupManager = groupManager2;
326 }
327
328 groupManager.addGroup(newGroupDesc);
329 assertEquals(null, groupManager.getGroup(deviceId, key));
330 assertEquals(0, Iterables.size(groupManager.getGroups(deviceId, appId)));
331 }
332
333
334 // Test initial AUDIT process with pending group requests
335 private void testInitialAuditWithPendingGroupRequests(NetworkId networkId,
336 DeviceId deviceId) {
337 VirtualNetworkGroupManager groupManager;
338 VirtualGroupProviderService providerService;
339 if (networkId.id() == 1) {
340 groupManager = groupManager1;
341 providerService = providerService1;
342 } else {
343 groupManager = groupManager2;
344 providerService = providerService2;
345 }
346
347 PortNumber[] ports1 = {PortNumber.portNumber(31),
348 PortNumber.portNumber(32)};
349 PortNumber[] ports2 = {PortNumber.portNumber(41),
350 PortNumber.portNumber(42)};
Yi Tsengfa394de2017-02-01 11:26:40 -0800351 GroupId gId1 = new GroupId(1);
yoonseondc3210d2017-01-25 16:03:10 -0800352 Group group1 = createSouthboundGroupEntry(gId1,
353 Arrays.asList(ports1),
354 0, deviceId);
Yi Tsengfa394de2017-02-01 11:26:40 -0800355 GroupId gId2 = new GroupId(2);
yoonseondc3210d2017-01-25 16:03:10 -0800356 // Non zero reference count will make the group manager to queue
357 // the extraneous groups until reference count is zero.
358 Group group2 = createSouthboundGroupEntry(gId2,
359 Arrays.asList(ports2),
360 2, deviceId);
361 List<Group> groupEntries = Arrays.asList(group1, group2);
362 providerService.pushGroupMetrics(deviceId, groupEntries);
363 // First group metrics would trigger the device audit completion
364 // post which all pending group requests are also executed.
365 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
366 Group createdGroup = groupManager.getGroup(deviceId, key);
367 int createdGroupId = createdGroup.id().id();
368 assertNotEquals(gId1.id().intValue(), createdGroupId);
369 assertNotEquals(gId2.id().intValue(), createdGroupId);
370
371 List<GroupOperation> expectedGroupOps = Arrays.asList(
372 GroupOperation.createDeleteGroupOperation(gId1,
373 Group.Type.SELECT),
374 GroupOperation.createAddGroupOperation(
375 createdGroup.id(),
376 Group.Type.SELECT,
377 createdGroup.buckets()));
378 if (deviceId.equals(VDID1)) {
379 provider.validate(networkId, deviceId, expectedGroupOps);
380 }
381 }
382
383 // Test AUDIT process with extraneous groups and missing groups
384 private void testAuditWithExtraneousMissingGroups(NetworkId networkId,
385 DeviceId deviceId) {
386 VirtualNetworkGroupManager groupManager;
387 VirtualGroupProviderService providerService;
388 if (networkId.id() == 1) {
389 groupManager = groupManager1;
390 providerService = providerService1;
391 } else {
392 groupManager = groupManager2;
393 providerService = providerService2;
394 }
395
396 PortNumber[] ports1 = {PortNumber.portNumber(31),
397 PortNumber.portNumber(32)};
398 PortNumber[] ports2 = {PortNumber.portNumber(41),
399 PortNumber.portNumber(42)};
Yi Tsengfa394de2017-02-01 11:26:40 -0800400 GroupId gId1 = new GroupId(1);
yoonseondc3210d2017-01-25 16:03:10 -0800401 Group group1 = createSouthboundGroupEntry(gId1,
402 Arrays.asList(ports1),
403 0, deviceId);
Yi Tsengfa394de2017-02-01 11:26:40 -0800404 GroupId gId2 = new GroupId(2);
yoonseondc3210d2017-01-25 16:03:10 -0800405 Group group2 = createSouthboundGroupEntry(gId2,
406 Arrays.asList(ports2),
407 0, deviceId);
408 List<Group> groupEntries = Arrays.asList(group1, group2);
409 providerService.pushGroupMetrics(deviceId, groupEntries);
410 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
411 Group createdGroup = groupManager.getGroup(deviceId, key);
412 List<GroupOperation> expectedGroupOps = Arrays.asList(
413 GroupOperation.createDeleteGroupOperation(gId1,
414 Group.Type.SELECT),
415 GroupOperation.createDeleteGroupOperation(gId2,
416 Group.Type.SELECT),
417 GroupOperation.createAddGroupOperation(createdGroup.id(),
418 Group.Type.SELECT,
419 createdGroup.buckets()));
420 if (deviceId.equals(VDID1)) {
421 provider.validate(networkId, deviceId, expectedGroupOps);
422 }
423 }
424
425 // Test AUDIT with confirmed groups
426 private void testAuditWithConfirmedGroups(NetworkId networkId,
427 DeviceId deviceId) {
428 VirtualNetworkGroupManager groupManager;
429 VirtualGroupProviderService providerService;
430 TestGroupListener listener;
431
432 if (networkId.id() == 1) {
433 groupManager = groupManager1;
434 providerService = providerService1;
435 listener = listener1;
436 } else {
437 groupManager = groupManager2;
438 providerService = providerService2;
439 listener = listener2;
440 }
441
442 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
443 Group createdGroup = groupManager.getGroup(deviceId, key);
444 createdGroup = new DefaultGroup(createdGroup.id(),
445 deviceId,
446 Group.Type.SELECT,
447 createdGroup.buckets());
448 List<Group> groupEntries = Collections.singletonList(createdGroup);
449 providerService.pushGroupMetrics(deviceId, groupEntries);
450 listener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_ADDED));
451 }
452
453 private Group createSouthboundGroupEntry(GroupId gId,
454 List<PortNumber> ports,
455 long referenceCount, DeviceId deviceId) {
456 List<PortNumber> outPorts = new ArrayList<>();
457 outPorts.addAll(ports);
458
459 List<GroupBucket> buckets = new ArrayList<>();
460 for (PortNumber portNumber : outPorts) {
461 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
462 tBuilder.setOutput(portNumber)
463 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
464 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
465 .pushMpls()
466 .setMpls(MplsLabel.mplsLabel(106));
467 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
468 tBuilder.build()));
469 }
470 GroupBuckets groupBuckets = new GroupBuckets(buckets);
471 StoredGroupEntry group = new DefaultGroup(
472 gId, deviceId, Group.Type.SELECT, groupBuckets);
473 group.setReferenceCount(referenceCount);
474 return group;
475 }
476
477 // Test group add bucket operations
478 private void testAddBuckets(NetworkId networkId, DeviceId deviceId) {
479 VirtualNetworkGroupManager groupManager;
480 VirtualGroupProviderService providerService;
481 TestGroupListener listener;
482
483 if (networkId.id() == 1) {
484 groupManager = groupManager1;
485 providerService = providerService1;
486 listener = listener1;
487 } else {
488 groupManager = groupManager2;
489 providerService = providerService2;
490 listener = listener2;
491 }
492
493 GroupKey addKey = new DefaultGroupKey("group1AddBuckets".getBytes());
494
495 GroupKey prevKey = new DefaultGroupKey("group1BeforeAudit".getBytes());
496 Group createdGroup = groupManager.getGroup(deviceId, prevKey);
497 List<GroupBucket> buckets = new ArrayList<>();
498 buckets.addAll(createdGroup.buckets().buckets());
499
500 PortNumber[] addPorts = {PortNumber.portNumber(51),
501 PortNumber.portNumber(52)};
502 List<PortNumber> outPorts;
503 outPorts = new ArrayList<>();
504 outPorts.addAll(Arrays.asList(addPorts));
505 List<GroupBucket> addBuckets;
506 addBuckets = new ArrayList<>();
507 for (PortNumber portNumber : outPorts) {
508 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
509 tBuilder.setOutput(portNumber)
510 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
511 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
512 .pushMpls()
513 .setMpls(MplsLabel.mplsLabel(106));
514 addBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
515 tBuilder.build()));
516 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
517 tBuilder.build()));
518 }
519 GroupBuckets groupAddBuckets = new GroupBuckets(addBuckets);
520 groupManager.addBucketsToGroup(deviceId,
521 prevKey,
522 groupAddBuckets,
523 addKey,
524 appId);
525 GroupBuckets updatedBuckets = new GroupBuckets(buckets);
526 List<GroupOperation> expectedGroupOps = Collections.singletonList(
527 GroupOperation.createModifyGroupOperation(createdGroup.id(),
528 Group.Type.SELECT,
529 updatedBuckets));
530 if (deviceId.equals(VDID1)) {
531 provider.validate(networkId, deviceId, expectedGroupOps);
532 }
533
534 Group existingGroup = groupManager.getGroup(deviceId, addKey);
535 List<Group> groupEntries = Collections.singletonList(existingGroup);
536 providerService.pushGroupMetrics(deviceId, groupEntries);
537 listener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED));
538 }
539
540 // Test purge group entry operations
541 private void testPurgeGroupEntry(NetworkId networkId, DeviceId deviceId) {
542 VirtualNetworkGroupManager groupManager;
543 if (networkId.id() == 1) {
544 groupManager = groupManager1;
545 } else {
546 groupManager = groupManager2;
547 }
548
549 assertEquals(1, Iterables.size(groupManager.getGroups(deviceId, appId)));
550 groupManager.purgeGroupEntries(deviceId);
551 assertEquals(0, Iterables.size(groupManager.getGroups(deviceId, appId)));
552 }
553
554 // Test group remove bucket operations
555 private void testRemoveBuckets(NetworkId networkId, DeviceId deviceId) {
556 VirtualNetworkGroupManager groupManager;
557 VirtualGroupProviderService providerService;
558 TestGroupListener listener;
559
560 if (networkId.id() == 1) {
561 groupManager = groupManager1;
562 providerService = providerService1;
563 listener = listener1;
564 } else {
565 groupManager = groupManager2;
566 providerService = providerService2;
567 listener = listener2;
568 }
569
570 GroupKey removeKey = new DefaultGroupKey("group1RemoveBuckets".getBytes());
571
572 GroupKey prevKey = new DefaultGroupKey("group1AddBuckets".getBytes());
573 Group createdGroup = groupManager.getGroup(deviceId, prevKey);
574 List<GroupBucket> buckets = new ArrayList<>();
575 buckets.addAll(createdGroup.buckets().buckets());
576
577 PortNumber[] removePorts = {PortNumber.portNumber(31),
578 PortNumber.portNumber(32)};
579 List<PortNumber> outPorts = new ArrayList<>();
580 outPorts.addAll(Arrays.asList(removePorts));
581 List<GroupBucket> removeBuckets = new ArrayList<>();
582 for (PortNumber portNumber : outPorts) {
583 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
584 tBuilder.setOutput(portNumber)
585 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
586 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
587 .pushMpls()
588 .setMpls(MplsLabel.mplsLabel(106));
589 removeBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
590 tBuilder.build()));
591 buckets.remove(DefaultGroupBucket.createSelectGroupBucket(
592 tBuilder.build()));
593 }
594 GroupBuckets groupRemoveBuckets = new GroupBuckets(removeBuckets);
595 groupManager.removeBucketsFromGroup(deviceId,
596 prevKey,
597 groupRemoveBuckets,
598 removeKey,
599 appId);
600 GroupBuckets updatedBuckets = new GroupBuckets(buckets);
601 List<GroupOperation> expectedGroupOps = Collections.singletonList(
602 GroupOperation.createModifyGroupOperation(createdGroup.id(),
603 Group.Type.SELECT,
604 updatedBuckets));
605 if (deviceId.equals(VDID1)) {
606 provider.validate(networkId, deviceId, expectedGroupOps);
607 }
608
609 Group existingGroup = groupManager.getGroup(deviceId, removeKey);
610 List<Group> groupEntries = Collections.singletonList(existingGroup);
611 providerService.pushGroupMetrics(deviceId, groupEntries);
612 listener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED));
613 }
614
615 // Test group remove operations
616 private void testRemoveGroup(NetworkId networkId, DeviceId deviceId) {
617 VirtualNetworkGroupManager groupManager;
618 VirtualGroupProviderService providerService;
619 TestGroupListener listener;
620
621 if (networkId.id() == 1) {
622 groupManager = groupManager1;
623 providerService = providerService1;
624 listener = listener1;
625 } else {
626 groupManager = groupManager2;
627 providerService = providerService2;
628 listener = listener2;
629 }
630
631 GroupKey currKey = new DefaultGroupKey("group1RemoveBuckets".getBytes());
632 Group existingGroup = groupManager.getGroup(deviceId, currKey);
633 groupManager.removeGroup(deviceId, currKey, appId);
634 List<GroupOperation> expectedGroupOps = Collections.singletonList(
635 GroupOperation.createDeleteGroupOperation(existingGroup.id(),
636 Group.Type.SELECT));
637 if (deviceId.equals(VDID1)) {
638 provider.validate(networkId, deviceId, expectedGroupOps);
639 }
640
641 List<Group> groupEntries = Collections.emptyList();
642 providerService.pushGroupMetrics(deviceId, groupEntries);
643 listener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_REMOVED));
644 }
645
646 private class TestGroupProvider extends AbstractVirtualProvider
647 implements VirtualGroupProvider {
648 NetworkId lastNetworkId;
649 DeviceId lastDeviceId;
650 List<GroupOperation> groupOperations = new ArrayList<>();
651
652 protected TestGroupProvider() {
653 super(new ProviderId("test", "org.onosproject.virtual.testprovider"));
654 }
655
656 @Override
657 public void performGroupOperation(NetworkId networkId, DeviceId deviceId,
658 GroupOperations groupOps) {
659 lastNetworkId = networkId;
660 lastDeviceId = deviceId;
661 groupOperations.addAll(groupOps.operations());
662 }
663
664 public void validate(NetworkId expectedNetworkId, DeviceId expectedDeviceId,
665 List<GroupOperation> expectedGroupOps) {
666 if (expectedGroupOps == null) {
667 assertTrue("events generated", groupOperations.isEmpty());
668 return;
669 }
670
671 assertEquals(lastNetworkId, expectedNetworkId);
672 assertEquals(lastDeviceId, expectedDeviceId);
673 assertTrue((this.groupOperations.containsAll(expectedGroupOps) &&
674 expectedGroupOps.containsAll(groupOperations)));
675
676 groupOperations.clear();
677 lastDeviceId = null;
678 lastNetworkId = null;
679 }
680 }
681
682 private static class TestGroupListener implements GroupListener {
683 final List<GroupEvent> events = new ArrayList<>();
684
685 @Override
686 public void event(GroupEvent event) {
687 events.add(event);
688 }
689
690 public void validateEvent(List<GroupEvent.Type> expectedEvents) {
691 int i = 0;
692 System.err.println("events :" + events);
693 for (GroupEvent e : events) {
694 assertEquals("unexpected event", expectedEvents.get(i), e.type());
695 i++;
696 }
697 assertEquals("mispredicted number of events",
698 expectedEvents.size(), events.size());
699 events.clear();
700 }
701 }
702}