blob: 7135c49524b218a18d7ad8703ed5c573f65f2aa1 [file] [log] [blame]
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -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.net.group.impl;
17
Andrea Campanella6ee73922016-02-03 18:00:00 -080018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.Iterables;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080021import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
Andrea Campanella1ea15102017-09-04 16:00:09 +020024import org.onlab.junit.TestTools;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080025import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010026import org.onlab.packet.MplsLabel;
Charles Chan0c7c43b2016-01-14 17:39:20 -080027import org.onosproject.cfg.ComponentConfigAdapter;
Andrea Campanella6ee73922016-02-03 18:00:00 -080028import org.onosproject.common.event.impl.TestEventDispatcher;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.DefaultApplicationId;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080031import org.onosproject.core.GroupId;
Andrea Campanella1ea15102017-09-04 16:00:09 +020032import org.onosproject.mastership.MastershipServiceAdapter;
Andrea Campanella6ee73922016-02-03 18:00:00 -080033import org.onosproject.net.AnnotationKeys;
34import org.onosproject.net.DefaultAnnotations;
35import org.onosproject.net.DefaultDevice;
36import org.onosproject.net.Device;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080037import org.onosproject.net.DeviceId;
Andrea Campanella1ea15102017-09-04 16:00:09 +020038import org.onosproject.net.MastershipRole;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080039import org.onosproject.net.PortNumber;
Andrea Campanella6ee73922016-02-03 18:00:00 -080040import org.onosproject.net.device.DeviceServiceAdapter;
41import org.onosproject.net.driver.AbstractHandlerBehaviour;
42import org.onosproject.net.driver.DefaultDriver;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070043import org.onosproject.net.driver.DriverRegistry;
Andrea Campanella6ee73922016-02-03 18:00:00 -080044import org.onosproject.net.driver.impl.DriverManager;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070045import org.onosproject.net.driver.impl.DriverRegistryManager;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080046import org.onosproject.net.flow.DefaultTrafficTreatment;
47import org.onosproject.net.flow.TrafficTreatment;
48import org.onosproject.net.group.DefaultGroup;
49import org.onosproject.net.group.DefaultGroupBucket;
50import org.onosproject.net.group.DefaultGroupDescription;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070051import org.onosproject.net.group.DefaultGroupKey;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080052import org.onosproject.net.group.Group;
53import org.onosproject.net.group.GroupBucket;
54import org.onosproject.net.group.GroupBuckets;
55import org.onosproject.net.group.GroupDescription;
56import org.onosproject.net.group.GroupEvent;
57import org.onosproject.net.group.GroupKey;
58import org.onosproject.net.group.GroupListener;
59import org.onosproject.net.group.GroupOperation;
60import org.onosproject.net.group.GroupOperations;
Andrea Campanella6ee73922016-02-03 18:00:00 -080061import org.onosproject.net.group.GroupProgrammable;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080062import org.onosproject.net.group.GroupProvider;
63import org.onosproject.net.group.GroupProviderRegistry;
64import org.onosproject.net.group.GroupProviderService;
65import org.onosproject.net.group.GroupService;
66import org.onosproject.net.group.StoredGroupEntry;
67import org.onosproject.net.provider.AbstractProvider;
68import org.onosproject.net.provider.ProviderId;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070069import org.onosproject.store.trivial.SimpleGroupStore;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080070
Andrea Campanella6ee73922016-02-03 18:00:00 -080071import java.util.ArrayList;
72import java.util.Arrays;
Andrea Campanella1ea15102017-09-04 16:00:09 +020073import java.util.Collection;
Andrea Campanella6ee73922016-02-03 18:00:00 -080074import java.util.Collections;
75import java.util.List;
76
77import static org.junit.Assert.*;
78import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080079
80/**
81 * Test codifying the group service & group provider service contracts.
82 */
83public class GroupManagerTest {
84
85 private static final ProviderId PID = new ProviderId("of", "groupfoo");
86 private static final DeviceId DID = DeviceId.deviceId("of:001");
Andrea Campanella6ee73922016-02-03 18:00:00 -080087 private static final ProviderId FOO_PID = new ProviderId("foo", "foo");
88 private static final DeviceId FOO_DID = DeviceId.deviceId("foo:002");
89
90 private static final DefaultAnnotations ANNOTATIONS =
91 DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, "foo").build();
92
93 private static final Device FOO_DEV =
94 new DefaultDevice(FOO_PID, FOO_DID, Device.Type.SWITCH, "", "", "", "", null, ANNOTATIONS);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080095
Andrea Campanella1ea15102017-09-04 16:00:09 +020096 private static GroupManager mgr;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080097 private GroupService groupService;
98 private GroupProviderRegistry providerRegistry;
99 private TestGroupListener internalListener = new TestGroupListener();
100 private GroupListener listener = internalListener;
101 private TestGroupProvider internalProvider;
102 private GroupProvider provider;
103 private GroupProviderService providerService;
104 private ApplicationId appId;
Andrea Campanella6ee73922016-02-03 18:00:00 -0800105 private TestDriverManager driverService;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800106
107 @Before
108 public void setUp() {
109 mgr = new GroupManager();
110 groupService = mgr;
Andrea Campanella6ee73922016-02-03 18:00:00 -0800111 //mgr.deviceService = new DeviceManager();
112 mgr.deviceService = new TestDeviceService();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800113 mgr.cfgService = new ComponentConfigAdapter();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800114 mgr.store = new SimpleGroupStore();
Andrea Campanella1ea15102017-09-04 16:00:09 +0200115 mgr.mastershipService = new TestMastershipService();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700116 injectEventDispatcher(mgr, new TestEventDispatcher());
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800117 providerRegistry = mgr;
118
Charles Chan0c7c43b2016-01-14 17:39:20 -0800119 mgr.activate(null);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800120 mgr.addListener(listener);
121
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700122 DriverRegistryManager driverRegistry = new DriverRegistryManager();
123 driverService = new TestDriverManager(driverRegistry);
124 driverRegistry.addDriver(new DefaultDriver("foo", ImmutableList.of(), "", "", "",
125 ImmutableMap.of(GroupProgrammable.class,
126 TestGroupProgrammable.class),
127 ImmutableMap.of()));
Andrea Campanella6ee73922016-02-03 18:00:00 -0800128
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800129 internalProvider = new TestGroupProvider(PID);
130 provider = internalProvider;
131 providerService = providerRegistry.register(provider);
132 appId = new DefaultApplicationId(2, "org.groupmanager.test");
133 assertTrue("provider should be registered",
134 providerRegistry.getProviders().contains(provider.id()));
135 }
136
137 @After
138 public void tearDown() {
139 providerRegistry.unregister(provider);
140 assertFalse("provider should not be registered",
141 providerRegistry.getProviders().contains(provider.id()));
142 mgr.removeListener(listener);
143 mgr.deactivate();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700144 injectEventDispatcher(mgr, null);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800145 }
146
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800147 /**
Andrea Campanella6ee73922016-02-03 18:00:00 -0800148 * Tests group creation before the device group AUDIT completes.
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800149 */
150 @Test
Andrea Campanella6ee73922016-02-03 18:00:00 -0800151 public void testGroupServiceBasics() {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800152 // Test Group creation before AUDIT process
Andrea Campanella6ee73922016-02-03 18:00:00 -0800153 testGroupCreationBeforeAudit(DID);
154 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800155
Andrea Campanella6ee73922016-02-03 18:00:00 -0800156 /**
157 * Tests initial device group AUDIT process.
158 */
159 @Test
160 public void testGroupServiceInitialAudit() {
161 // Test Group creation before AUDIT process
162 testGroupCreationBeforeAudit(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800163 // Test initial group audit process
Andrea Campanella6ee73922016-02-03 18:00:00 -0800164 testInitialAuditWithPendingGroupRequests(DID);
165 }
166
167 /**
168 * Tests deletion process of any extraneous groups.
169 */
170 @Test
171 public void testGroupServiceAuditExtraneous() {
172 // Test Group creation before AUDIT process
173 testGroupCreationBeforeAudit(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800174
175 // Test audit with extraneous and missing groups
Andrea Campanella6ee73922016-02-03 18:00:00 -0800176 testAuditWithExtraneousMissingGroups(DID);
177 }
178
179 /**
180 * Tests re-apply process of any missing groups tests execution of
181 * any pending group creation request after the device group AUDIT completes
182 * and tests event notifications after receiving confirmation for any
183 * operations from data plane.
184 */
185 @Test
186 public void testGroupServiceAuditConfirmed() {
187 // Test Group creation before AUDIT process
188 testGroupCreationBeforeAudit(DID);
189
190 // Test audit with extraneous and missing groups
191 testAuditWithExtraneousMissingGroups(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800192
193 // Test audit with confirmed groups
Andrea Campanella6ee73922016-02-03 18:00:00 -0800194 testAuditWithConfirmedGroups(DID);
195 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800196
Andrea Campanella6ee73922016-02-03 18:00:00 -0800197 /**
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530198 * Tests group Purge Operation.
199 */
200 @Test
201 public void testPurgeGroups() {
202 //Test Group creation before AUDIT process
203 testGroupCreationBeforeAudit(DID);
204 programmableTestCleanUp();
205 testAuditWithExtraneousMissingGroups(DID);
206 // Test group add bucket operations
207 testAddBuckets(DID);
208 // Test group Purge operations
209 testPurgeGroupEntry(DID);
210 }
211
212 /**
Andrea Campanella6ee73922016-02-03 18:00:00 -0800213 * Tests group bucket modifications (additions and deletions) and
214 * Tests group deletion.
215 */
216 @Test
217 public void testGroupServiceBuckets() {
218 // Test Group creation before AUDIT process
219 testGroupCreationBeforeAudit(DID);
220 programmableTestCleanUp();
221
222 testAuditWithExtraneousMissingGroups(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800223 // Test group add bucket operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800224 testAddBuckets(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800225
226 // Test group remove bucket operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800227 testRemoveBuckets(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800228
229 // Test group remove operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800230 testRemoveGroup(DID);
231 }
232
233 /**
234 * Tests group creation before the device group AUDIT completes with fallback
235 * provider.
236 */
237 @Test
238 public void testGroupServiceFallbackBasics() {
239 // Test Group creation before AUDIT process
240 testGroupCreationBeforeAudit(FOO_DID);
241 programmableTestCleanUp();
242
243 }
244
245 /**
246 * Tests initial device group AUDIT process with fallback provider.
247 */
248 @Test
249 public void testGroupServiceFallbackInitialAudit() {
250 // Test Group creation before AUDIT process
251 testGroupCreationBeforeAudit(FOO_DID);
252 programmableTestCleanUp();
253 // Test initial group audit process
254 testInitialAuditWithPendingGroupRequests(FOO_DID);
255 }
256
257 /**
258 * Tests deletion process of any extraneous groups with fallback provider.
259 */
260 @Test
261 public void testGroupServiceFallbackAuditExtraneous() {
262 // Test Group creation before AUDIT process
263 testGroupCreationBeforeAudit(FOO_DID);
264 programmableTestCleanUp();
265
266 // Test audit with extraneous and missing groups
267 testAuditWithExtraneousMissingGroups(FOO_DID);
268 }
269
270 /**
271 * Tests re-apply process of any missing groups tests execution of
272 * any pending group creation request after the device group AUDIT completes
273 * and tests event notifications after receiving confirmation for any
274 * operations from data plane with fallback provider.
275 */
276 @Test
277 public void testGroupServiceFallbackAuditConfirmed() {
278 // Test Group creation before AUDIT process
279 testGroupCreationBeforeAudit(FOO_DID);
280 programmableTestCleanUp();
281
282 // Test audit with extraneous and missing groups
283 testAuditWithExtraneousMissingGroups(FOO_DID);
284
285 // Test audit with confirmed groups
286 testAuditWithConfirmedGroups(FOO_DID);
287 }
288
289 /**
290 * Tests group bucket modifications (additions and deletions) and
291 * Tests group deletion with fallback provider.
292 */
293 @Test
294 public void testGroupServiceFallbackBuckets() {
295 // Test Group creation before AUDIT process
296 testGroupCreationBeforeAudit(FOO_DID);
297 programmableTestCleanUp();
298
299 testAuditWithExtraneousMissingGroups(FOO_DID);
300 // Test group add bucket operations
301 testAddBuckets(FOO_DID);
302
303 // Test group remove bucket operations
304 testRemoveBuckets(FOO_DID);
305
306 // Test group remove operations
307 testRemoveGroup(FOO_DID);
308 }
309
Andrea Campanella1ea15102017-09-04 16:00:09 +0200310 @Test
311 public void fallbackPoll() {
312 // Test Group creation before AUDIT process
313 testGroupCreationBeforeAudit(FOO_DID);
314 programmableTestCleanUp();
315
316 // Test audit with extraneous and missing groups
317 testAuditWithExtraneousMissingGroups(FOO_DID);
318
319 // Test audit with confirmed groups
320 Group createdGroup = testAuditWithConfirmedGroups(FOO_DID);
321 GroupDriverProvider fallback = (GroupDriverProvider) mgr.defaultProvider();
322
323 fallback.init(mgr.deviceService, fallback.groupProviderService, mgr.mastershipService, 1);
324
325 TestTools.assertAfter(2000, () -> {
326 Group e = mgr.getGroups(FOO_DID).iterator().next();
327 assertEquals("incorrect group", createdGroup, e);
328 });
329 }
330
Andrea Campanella6ee73922016-02-03 18:00:00 -0800331 private void programmableTestCleanUp() {
332 groupOperations.clear();
333 lastDeviceIdProgrammable = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800334 }
335
336 // Test Group creation before AUDIT process
Andrea Campanella6ee73922016-02-03 18:00:00 -0800337 private void testGroupCreationBeforeAudit(DeviceId deviceId) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800338 PortNumber[] ports1 = {PortNumber.portNumber(31),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800339 PortNumber.portNumber(32)};
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800340 PortNumber[] ports2 = {PortNumber.portNumber(41),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800341 PortNumber.portNumber(42)};
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700342 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700343 List<GroupBucket> buckets = new ArrayList<>();
344 List<PortNumber> outPorts = new ArrayList<>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800345 outPorts.addAll(Arrays.asList(ports1));
346 outPorts.addAll(Arrays.asList(ports2));
Andrea Campanella6ee73922016-02-03 18:00:00 -0800347 for (PortNumber portNumber : outPorts) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800348 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
349 tBuilder.setOutput(portNumber)
350 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
351 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
352 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100353 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800354 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800355 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800356 }
357 GroupBuckets groupBuckets = new GroupBuckets(buckets);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800358 GroupDescription newGroupDesc = new DefaultGroupDescription(deviceId,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800359 Group.Type.SELECT,
360 groupBuckets,
361 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700362 null,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800363 appId);
364 groupService.addGroup(newGroupDesc);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800365 assertEquals(null, groupService.getGroup(deviceId, key));
366 assertEquals(0, Iterables.size(groupService.getGroups(deviceId, appId)));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800367 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800368
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800369 // Test initial AUDIT process with pending group requests
Andrea Campanella6ee73922016-02-03 18:00:00 -0800370 private void testInitialAuditWithPendingGroupRequests(DeviceId deviceId) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800371 PortNumber[] ports1 = {PortNumber.portNumber(31),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800372 PortNumber.portNumber(32)};
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800373 PortNumber[] ports2 = {PortNumber.portNumber(41),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800374 PortNumber.portNumber(42)};
Yi Tsengfa394de2017-02-01 11:26:40 -0800375 GroupId gId1 = new GroupId(1);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800376 Group group1 = createSouthboundGroupEntry(gId1,
377 Arrays.asList(ports1),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800378 0, deviceId);
Yi Tsengfa394de2017-02-01 11:26:40 -0800379 GroupId gId2 = new GroupId(2);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800380 // Non zero reference count will make the group manager to queue
381 // the extraneous groups until reference count is zero.
382 Group group2 = createSouthboundGroupEntry(gId2,
383 Arrays.asList(ports2),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800384 2, deviceId);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800385 List<Group> groupEntries = Arrays.asList(group1, group2);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800386 providerService.pushGroupMetrics(deviceId, groupEntries);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800387 // First group metrics would trigger the device audit completion
388 // post which all pending group requests are also executed.
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700389 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800390 Group createdGroup = groupService.getGroup(deviceId, key);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800391 int createdGroupId = createdGroup.id().id();
Prince Pereira3ff504c2016-08-30 14:23:43 +0530392 assertNotEquals(gId1.id().intValue(), createdGroupId);
393 assertNotEquals(gId2.id().intValue(), createdGroupId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800394
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800395 List<GroupOperation> expectedGroupOps = Arrays.asList(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800396 GroupOperation.createDeleteGroupOperation(gId1,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800397 Group.Type.SELECT),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800398 GroupOperation.createAddGroupOperation(
399 createdGroup.id(),
400 Group.Type.SELECT,
401 createdGroup.buckets()));
402 if (deviceId.equals(DID)) {
403 internalProvider.validate(deviceId, expectedGroupOps);
404 } else {
405 this.validate(deviceId, expectedGroupOps);
406 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800407 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800408
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800409 // Test AUDIT process with extraneous groups and missing groups
Andrea Campanella6ee73922016-02-03 18:00:00 -0800410 private void testAuditWithExtraneousMissingGroups(DeviceId deviceId) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800411 PortNumber[] ports1 = {PortNumber.portNumber(31),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800412 PortNumber.portNumber(32)};
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800413 PortNumber[] ports2 = {PortNumber.portNumber(41),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800414 PortNumber.portNumber(42)};
Yi Tsengfa394de2017-02-01 11:26:40 -0800415 GroupId gId1 = new GroupId(1);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800416 Group group1 = createSouthboundGroupEntry(gId1,
Andrea Campanella6ee73922016-02-03 18:00:00 -0800417 Arrays.asList(ports1),
418 0, deviceId);
Yi Tsengfa394de2017-02-01 11:26:40 -0800419 GroupId gId2 = new GroupId(2);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800420 Group group2 = createSouthboundGroupEntry(gId2,
Andrea Campanella6ee73922016-02-03 18:00:00 -0800421 Arrays.asList(ports2),
422 0, deviceId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800423 List<Group> groupEntries = Arrays.asList(group1, group2);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800424 providerService.pushGroupMetrics(deviceId, groupEntries);
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700425 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800426 Group createdGroup = groupService.getGroup(deviceId, key);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800427 List<GroupOperation> expectedGroupOps = Arrays.asList(
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800428 GroupOperation.createDeleteGroupOperation(gId1,
429 Group.Type.SELECT),
430 GroupOperation.createDeleteGroupOperation(gId2,
431 Group.Type.SELECT),
432 GroupOperation.createAddGroupOperation(createdGroup.id(),
433 Group.Type.SELECT,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800434 createdGroup.buckets()));
Andrea Campanella6ee73922016-02-03 18:00:00 -0800435 if (deviceId.equals(DID)) {
436 internalProvider.validate(deviceId, expectedGroupOps);
437 } else {
438 this.validate(deviceId, expectedGroupOps);
439 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800440 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800441
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800442 // Test AUDIT with confirmed groups
Andrea Campanella1ea15102017-09-04 16:00:09 +0200443 private Group testAuditWithConfirmedGroups(DeviceId deviceId) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700444 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800445 Group createdGroup = groupService.getGroup(deviceId, key);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800446 createdGroup = new DefaultGroup(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800447 deviceId,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800448 Group.Type.SELECT,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800449 createdGroup.buckets());
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700450 List<Group> groupEntries = Collections.singletonList(createdGroup);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800451 providerService.pushGroupMetrics(deviceId, groupEntries);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700452 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_ADDED));
Andrea Campanella1ea15102017-09-04 16:00:09 +0200453 return createdGroup;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800454 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800455
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800456 // Test group add bucket operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800457 private void testAddBuckets(DeviceId deviceId) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700458 GroupKey addKey = new DefaultGroupKey("group1AddBuckets".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800459
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700460 GroupKey prevKey = new DefaultGroupKey("group1BeforeAudit".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800461 Group createdGroup = groupService.getGroup(deviceId, prevKey);
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700462 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800463 buckets.addAll(createdGroup.buckets().buckets());
464
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800465 PortNumber[] addPorts = {PortNumber.portNumber(51),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800466 PortNumber.portNumber(52)};
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700467 List<PortNumber> outPorts;
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700468 outPorts = new ArrayList<>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800469 outPorts.addAll(Arrays.asList(addPorts));
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700470 List<GroupBucket> addBuckets;
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700471 addBuckets = new ArrayList<>();
Andrea Campanella6ee73922016-02-03 18:00:00 -0800472 for (PortNumber portNumber : outPorts) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800473 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
474 tBuilder.setOutput(portNumber)
475 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
476 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
477 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100478 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800479 addBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800480 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800481 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800482 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800483 }
484 GroupBuckets groupAddBuckets = new GroupBuckets(addBuckets);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800485 groupService.addBucketsToGroup(deviceId,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800486 prevKey,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800487 groupAddBuckets,
488 addKey,
489 appId);
490 GroupBuckets updatedBuckets = new GroupBuckets(buckets);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700491 List<GroupOperation> expectedGroupOps = Collections.singletonList(
492 GroupOperation.createModifyGroupOperation(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800493 Group.Type.SELECT,
494 updatedBuckets));
495 if (deviceId.equals(DID)) {
496 internalProvider.validate(deviceId, expectedGroupOps);
497 } else {
498 this.validate(deviceId, expectedGroupOps);
499 }
500 Group existingGroup = groupService.getGroup(deviceId, addKey);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700501 List<Group> groupEntries = Collections.singletonList(existingGroup);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800502 providerService.pushGroupMetrics(deviceId, groupEntries);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700503 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800504 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800505
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800506 // Test group remove bucket operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800507 private void testRemoveBuckets(DeviceId deviceId) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700508 GroupKey removeKey = new DefaultGroupKey("group1RemoveBuckets".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800509
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700510 GroupKey prevKey = new DefaultGroupKey("group1AddBuckets".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800511 Group createdGroup = groupService.getGroup(deviceId, prevKey);
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700512 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800513 buckets.addAll(createdGroup.buckets().buckets());
514
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800515 PortNumber[] removePorts = {PortNumber.portNumber(31),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800516 PortNumber.portNumber(32)};
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700517 List<PortNumber> outPorts = new ArrayList<>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800518 outPorts.addAll(Arrays.asList(removePorts));
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700519 List<GroupBucket> removeBuckets = new ArrayList<>();
Andrea Campanella6ee73922016-02-03 18:00:00 -0800520 for (PortNumber portNumber : outPorts) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800521 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
522 tBuilder.setOutput(portNumber)
523 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
524 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
525 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100526 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800527 removeBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800528 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800529 buckets.remove(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800530 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800531 }
532 GroupBuckets groupRemoveBuckets = new GroupBuckets(removeBuckets);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800533 groupService.removeBucketsFromGroup(deviceId,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800534 prevKey,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800535 groupRemoveBuckets,
536 removeKey,
537 appId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800538 GroupBuckets updatedBuckets = new GroupBuckets(buckets);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700539 List<GroupOperation> expectedGroupOps = Collections.singletonList(
540 GroupOperation.createModifyGroupOperation(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800541 Group.Type.SELECT,
542 updatedBuckets));
543 if (deviceId.equals(DID)) {
544 internalProvider.validate(deviceId, expectedGroupOps);
545 } else {
546 this.validate(deviceId, expectedGroupOps);
547 }
548 Group existingGroup = groupService.getGroup(deviceId, removeKey);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700549 List<Group> groupEntries = Collections.singletonList(existingGroup);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800550 providerService.pushGroupMetrics(deviceId, groupEntries);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700551 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800552 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800553
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530554 // Test purge group entry operations
555 private void testPurgeGroupEntry(DeviceId deviceId) {
556 assertEquals(1, Iterables.size(groupService.getGroups(deviceId, appId)));
557 groupService.purgeGroupEntries(deviceId);
558 assertEquals(0, Iterables.size(groupService.getGroups(deviceId, appId)));
559 }
560
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800561 // Test group remove operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800562 private void testRemoveGroup(DeviceId deviceId) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700563 GroupKey currKey = new DefaultGroupKey("group1RemoveBuckets".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800564 Group existingGroup = groupService.getGroup(deviceId, currKey);
565 groupService.removeGroup(deviceId, currKey, appId);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700566 List<GroupOperation> expectedGroupOps = Collections.singletonList(
567 GroupOperation.createDeleteGroupOperation(existingGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800568 Group.Type.SELECT));
569 if (deviceId.equals(DID)) {
570 internalProvider.validate(deviceId, expectedGroupOps);
571 } else {
572 this.validate(deviceId, expectedGroupOps);
573 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800574 List<Group> groupEntries = Collections.emptyList();
Andrea Campanella6ee73922016-02-03 18:00:00 -0800575 providerService.pushGroupMetrics(deviceId, groupEntries);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700576 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_REMOVED));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800577 }
578
sangho7ff01812015-02-09 16:21:53 -0800579 /**
580 * Test GroupOperationFailure function in Group Manager.
581 * a)GroupAddFailure
582 * b)GroupUpdateFailure
583 * c)GroupRemoteFailure
584 */
585 @Test
586 public void testGroupOperationFailure() {
Andrea Campanella6ee73922016-02-03 18:00:00 -0800587 groupOperationFaliure(DID);
588 }
589
590 /**
591 * Test GroupOperationFailure function in Group Manager
592 * with fallback provider.
593 * a)GroupAddFailure
594 * b)GroupUpdateFailure
595 * c)GroupRemoteFailure
596 */
597 @Test
598 public void testGroupOperationFailureFallBack() {
599 groupOperationFaliure(FOO_DID);
600 }
601
602 private void groupOperationFaliure(DeviceId deviceId) {
sangho7ff01812015-02-09 16:21:53 -0800603 PortNumber[] ports1 = {PortNumber.portNumber(31),
604 PortNumber.portNumber(32)};
605 PortNumber[] ports2 = {PortNumber.portNumber(41),
606 PortNumber.portNumber(42)};
607 // Test Group creation before AUDIT process
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700608 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700609 List<GroupBucket> buckets = new ArrayList<>();
610 List<PortNumber> outPorts = new ArrayList<>();
sangho7ff01812015-02-09 16:21:53 -0800611 outPorts.addAll(Arrays.asList(ports1));
612 outPorts.addAll(Arrays.asList(ports2));
Andrea Campanella6ee73922016-02-03 18:00:00 -0800613 for (PortNumber portNumber : outPorts) {
sangho7ff01812015-02-09 16:21:53 -0800614 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
615 tBuilder.setOutput(portNumber)
616 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
617 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
618 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100619 .setMpls(MplsLabel.mplsLabel(106));
sangho7ff01812015-02-09 16:21:53 -0800620 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
621 tBuilder.build()));
622 }
623 GroupBuckets groupBuckets = new GroupBuckets(buckets);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800624 GroupDescription newGroupDesc = new DefaultGroupDescription(deviceId,
625 Group.Type.SELECT,
626 groupBuckets,
627 key,
628 null,
629 appId);
sangho7ff01812015-02-09 16:21:53 -0800630 groupService.addGroup(newGroupDesc);
631
632 // Test initial group audit process
Yi Tsengfa394de2017-02-01 11:26:40 -0800633 GroupId gId1 = new GroupId(1);
sangho7ff01812015-02-09 16:21:53 -0800634 Group group1 = createSouthboundGroupEntry(gId1,
Andrea Campanella6ee73922016-02-03 18:00:00 -0800635 Arrays.asList(ports1),
636 0, deviceId);
Yi Tsengfa394de2017-02-01 11:26:40 -0800637 GroupId gId2 = new GroupId(2);
sangho7ff01812015-02-09 16:21:53 -0800638 // Non zero reference count will make the group manager to queue
639 // the extraneous groups until reference count is zero.
640 Group group2 = createSouthboundGroupEntry(gId2,
Andrea Campanella6ee73922016-02-03 18:00:00 -0800641 Arrays.asList(ports2),
642 2, deviceId);
sangho7ff01812015-02-09 16:21:53 -0800643 List<Group> groupEntries = Arrays.asList(group1, group2);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800644 providerService.pushGroupMetrics(deviceId, groupEntries);
645 Group createdGroup = groupService.getGroup(deviceId, key);
sangho7ff01812015-02-09 16:21:53 -0800646
647 // Group Add failure test
648 GroupOperation groupAddOp = GroupOperation.
649 createAddGroupOperation(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800650 createdGroup.type(),
651 createdGroup.buckets());
652 providerService.groupOperationFailed(deviceId, groupAddOp);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700653 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_ADD_FAILED));
sangho7ff01812015-02-09 16:21:53 -0800654
655 // Group Mod failure test
656 groupService.addGroup(newGroupDesc);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800657 createdGroup = groupService.getGroup(deviceId, key);
sangho7ff01812015-02-09 16:21:53 -0800658 assertNotNull(createdGroup);
659
660 GroupOperation groupModOp = GroupOperation.
661 createModifyGroupOperation(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800662 createdGroup.type(),
663 createdGroup.buckets());
664 providerService.groupOperationFailed(deviceId, groupModOp);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700665 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATE_FAILED));
sangho7ff01812015-02-09 16:21:53 -0800666
667 // Group Delete failure test
668 groupService.addGroup(newGroupDesc);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800669 createdGroup = groupService.getGroup(deviceId, key);
sangho7ff01812015-02-09 16:21:53 -0800670 assertNotNull(createdGroup);
671
672 GroupOperation groupDelOp = GroupOperation.
673 createDeleteGroupOperation(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800674 createdGroup.type());
675 providerService.groupOperationFailed(deviceId, groupDelOp);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700676 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_REMOVE_FAILED));
sangho7ff01812015-02-09 16:21:53 -0800677 }
678
Andrea Campanella1ea15102017-09-04 16:00:09 +0200679 private static Group createSouthboundGroupEntry(GroupId gId,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800680 List<PortNumber> ports,
Andrea Campanella6ee73922016-02-03 18:00:00 -0800681 long referenceCount, DeviceId deviceId) {
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700682 List<PortNumber> outPorts = new ArrayList<>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800683 outPorts.addAll(ports);
684
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700685 List<GroupBucket> buckets = new ArrayList<>();
Andrea Campanella6ee73922016-02-03 18:00:00 -0800686 for (PortNumber portNumber : outPorts) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800687 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
688 tBuilder.setOutput(portNumber)
689 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
690 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
691 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100692 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800693 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800694 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800695 }
696 GroupBuckets groupBuckets = new GroupBuckets(buckets);
697 StoredGroupEntry group = new DefaultGroup(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800698 gId, deviceId, Group.Type.SELECT, groupBuckets);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800699 group.setReferenceCount(referenceCount);
700 return group;
701 }
702
703 private static class TestGroupListener implements GroupListener {
704 final List<GroupEvent> events = new ArrayList<>();
705
706 @Override
707 public void event(GroupEvent event) {
708 events.add(event);
709 }
710
711 public void validateEvent(List<GroupEvent.Type> expectedEvents) {
712 int i = 0;
713 System.err.println("events :" + events);
714 for (GroupEvent e : events) {
715 assertEquals("unexpected event", expectedEvents.get(i), e.type());
716 i++;
717 }
718 assertEquals("mispredicted number of events",
719 expectedEvents.size(), events.size());
720 events.clear();
721 }
722 }
723
724 private class TestGroupProvider
Andrea Campanella6ee73922016-02-03 18:00:00 -0800725 extends AbstractProvider implements GroupProvider {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800726 DeviceId lastDeviceId;
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700727 List<GroupOperation> groupOperations = new ArrayList<>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800728
729 protected TestGroupProvider(ProviderId id) {
730 super(id);
731 }
732
733 @Override
734 public void performGroupOperation(DeviceId deviceId,
735 GroupOperations groupOps) {
736 lastDeviceId = deviceId;
737 groupOperations.addAll(groupOps.operations());
738 }
739
740 public void validate(DeviceId expectedDeviceId,
741 List<GroupOperation> expectedGroupOps) {
742 if (expectedGroupOps == null) {
743 assertTrue("events generated", groupOperations.isEmpty());
744 return;
745 }
746
747 assertEquals(lastDeviceId, expectedDeviceId);
748 assertTrue((this.groupOperations.containsAll(expectedGroupOps) &&
749 expectedGroupOps.containsAll(groupOperations)));
750
751 groupOperations.clear();
752 lastDeviceId = null;
753 }
754
755 }
756
Andrea Campanella6ee73922016-02-03 18:00:00 -0800757 private static class TestDeviceService extends DeviceServiceAdapter {
758 @Override
759 public int getDeviceCount() {
760 return 1;
761 }
762
763 @Override
764 public Iterable<Device> getDevices() {
765 return ImmutableList.of(FOO_DEV);
766 }
767
768 @Override
769 public Iterable<Device> getAvailableDevices() {
770 return getDevices();
771 }
772
773 @Override
774 public Device getDevice(DeviceId deviceId) {
775 return FOO_DEV;
776 }
777 }
778
Andrea Campanella1ea15102017-09-04 16:00:09 +0200779 private class TestMastershipService extends MastershipServiceAdapter {
780 @Override
781 public MastershipRole getLocalRole(DeviceId deviceId) {
782 return MastershipRole.MASTER;
783 }
784 }
785
Andrea Campanella6ee73922016-02-03 18:00:00 -0800786 private class TestDriverManager extends DriverManager {
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700787 TestDriverManager(DriverRegistry registry) {
788 this.registry = registry;
Andrea Campanella6ee73922016-02-03 18:00:00 -0800789 this.deviceService = mgr.deviceService;
790 activate();
791 }
792 }
793
794 private static DeviceId lastDeviceIdProgrammable;
795 private static List<GroupOperation> groupOperations = new ArrayList<>();
796
797 public static class TestGroupProgrammable extends AbstractHandlerBehaviour implements GroupProgrammable {
798 @Override
799 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
800 lastDeviceIdProgrammable = deviceId;
801 groupOperations.addAll(groupOps.operations());
802 }
Andrea Campanella1ea15102017-09-04 16:00:09 +0200803
804 @Override
805 public Collection<Group> getGroups() {
806 return ImmutableList.of(mgr.getGroups(FOO_DID).iterator().next());
807 }
Andrea Campanella6ee73922016-02-03 18:00:00 -0800808 }
809
810 public void validate(DeviceId expectedDeviceId,
811 List<GroupOperation> expectedGroupOps) {
812 if (expectedGroupOps == null) {
813 assertTrue("events generated", groupOperations.isEmpty());
814 return;
815 }
816
817 assertEquals(lastDeviceIdProgrammable, expectedDeviceId);
Sho SHIMIZU970d6e22016-08-12 15:13:22 -0700818 assertTrue(groupOperations.containsAll(expectedGroupOps) &&
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700819 expectedGroupOps.containsAll(groupOperations));
Andrea Campanella6ee73922016-02-03 18:00:00 -0800820
821 groupOperations.clear();
822 lastDeviceIdProgrammable = null;
823 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800824}