blob: d1777bacec8c374067e52d51f34f9911873af612 [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;
Carmelo Cascone0761cd32018-08-29 19:22:50 -070040import org.onosproject.net.config.NetworkConfigServiceAdapter;
Andrea Campanella6ee73922016-02-03 18:00:00 -080041import org.onosproject.net.device.DeviceServiceAdapter;
42import org.onosproject.net.driver.AbstractHandlerBehaviour;
43import org.onosproject.net.driver.DefaultDriver;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070044import org.onosproject.net.driver.DriverRegistry;
Andrea Campanella6ee73922016-02-03 18:00:00 -080045import org.onosproject.net.driver.impl.DriverManager;
Thomas Vachuska11b99fc2017-04-27 12:51:04 -070046import org.onosproject.net.driver.impl.DriverRegistryManager;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080047import 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;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070052import org.onosproject.net.group.DefaultGroupKey;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080053import 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;
Andrea Campanella6ee73922016-02-03 18:00:00 -080062import org.onosproject.net.group.GroupProgrammable;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080063import org.onosproject.net.group.GroupProvider;
64import org.onosproject.net.group.GroupProviderRegistry;
65import org.onosproject.net.group.GroupProviderService;
66import org.onosproject.net.group.GroupService;
67import org.onosproject.net.group.StoredGroupEntry;
Carmelo Cascone0761cd32018-08-29 19:22:50 -070068import org.onosproject.net.pi.PiPipeconfServiceAdapter;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080069import org.onosproject.net.provider.AbstractProvider;
70import org.onosproject.net.provider.ProviderId;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070071import org.onosproject.store.trivial.SimpleGroupStore;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080072
Andrea Campanella6ee73922016-02-03 18:00:00 -080073import java.util.ArrayList;
74import java.util.Arrays;
Andrea Campanella1ea15102017-09-04 16:00:09 +020075import java.util.Collection;
Andrea Campanella6ee73922016-02-03 18:00:00 -080076import java.util.Collections;
77import java.util.List;
78
Carmelo Cascone0761cd32018-08-29 19:22:50 -070079import static org.junit.Assert.assertEquals;
80import static org.junit.Assert.assertFalse;
81import static org.junit.Assert.assertNotEquals;
82import static org.junit.Assert.assertNotNull;
83import static org.junit.Assert.assertTrue;
Andrea Campanella6ee73922016-02-03 18:00:00 -080084import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080085
86/**
87 * Test codifying the group service & group provider service contracts.
88 */
89public class GroupManagerTest {
90
91 private static final ProviderId PID = new ProviderId("of", "groupfoo");
92 private static final DeviceId DID = DeviceId.deviceId("of:001");
Andrea Campanella6ee73922016-02-03 18:00:00 -080093 private static final ProviderId FOO_PID = new ProviderId("foo", "foo");
94 private static final DeviceId FOO_DID = DeviceId.deviceId("foo:002");
95
96 private static final DefaultAnnotations ANNOTATIONS =
97 DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, "foo").build();
98
99 private static final Device FOO_DEV =
100 new DefaultDevice(FOO_PID, FOO_DID, Device.Type.SWITCH, "", "", "", "", null, ANNOTATIONS);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800101
Andrea Campanella1ea15102017-09-04 16:00:09 +0200102 private static GroupManager mgr;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800103 private GroupService groupService;
104 private GroupProviderRegistry providerRegistry;
105 private TestGroupListener internalListener = new TestGroupListener();
106 private GroupListener listener = internalListener;
107 private TestGroupProvider internalProvider;
108 private GroupProvider provider;
109 private GroupProviderService providerService;
110 private ApplicationId appId;
Andrea Campanella6ee73922016-02-03 18:00:00 -0800111 private TestDriverManager driverService;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800112
113 @Before
114 public void setUp() {
115 mgr = new GroupManager();
116 groupService = mgr;
Andrea Campanella6ee73922016-02-03 18:00:00 -0800117 //mgr.deviceService = new DeviceManager();
118 mgr.deviceService = new TestDeviceService();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800119 mgr.cfgService = new ComponentConfigAdapter();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800120 mgr.store = new SimpleGroupStore();
Andrea Campanella1ea15102017-09-04 16:00:09 +0200121 mgr.mastershipService = new TestMastershipService();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700122 injectEventDispatcher(mgr, new TestEventDispatcher());
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800123 providerRegistry = mgr;
124
Charles Chan0c7c43b2016-01-14 17:39:20 -0800125 mgr.activate(null);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800126 mgr.addListener(listener);
127
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700128 DriverRegistryManager driverRegistry = new DriverRegistryManager();
129 driverService = new TestDriverManager(driverRegistry);
130 driverRegistry.addDriver(new DefaultDriver("foo", ImmutableList.of(), "", "", "",
131 ImmutableMap.of(GroupProgrammable.class,
132 TestGroupProgrammable.class),
133 ImmutableMap.of()));
Andrea Campanella6ee73922016-02-03 18:00:00 -0800134
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800135 internalProvider = new TestGroupProvider(PID);
136 provider = internalProvider;
137 providerService = providerRegistry.register(provider);
138 appId = new DefaultApplicationId(2, "org.groupmanager.test");
139 assertTrue("provider should be registered",
140 providerRegistry.getProviders().contains(provider.id()));
141 }
142
143 @After
144 public void tearDown() {
145 providerRegistry.unregister(provider);
146 assertFalse("provider should not be registered",
147 providerRegistry.getProviders().contains(provider.id()));
148 mgr.removeListener(listener);
149 mgr.deactivate();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700150 injectEventDispatcher(mgr, null);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800151 }
152
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800153 /**
Andrea Campanella6ee73922016-02-03 18:00:00 -0800154 * Tests group creation before the device group AUDIT completes.
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800155 */
156 @Test
Andrea Campanella6ee73922016-02-03 18:00:00 -0800157 public void testGroupServiceBasics() {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800158 // Test Group creation before AUDIT process
Andrea Campanella6ee73922016-02-03 18:00:00 -0800159 testGroupCreationBeforeAudit(DID);
160 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800161
Andrea Campanella6ee73922016-02-03 18:00:00 -0800162 /**
163 * Tests initial device group AUDIT process.
164 */
165 @Test
166 public void testGroupServiceInitialAudit() {
167 // Test Group creation before AUDIT process
168 testGroupCreationBeforeAudit(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800169 // Test initial group audit process
Andrea Campanella6ee73922016-02-03 18:00:00 -0800170 testInitialAuditWithPendingGroupRequests(DID);
171 }
172
173 /**
174 * Tests deletion process of any extraneous groups.
175 */
176 @Test
177 public void testGroupServiceAuditExtraneous() {
178 // Test Group creation before AUDIT process
179 testGroupCreationBeforeAudit(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800180
181 // Test audit with extraneous and missing groups
Andrea Campanella6ee73922016-02-03 18:00:00 -0800182 testAuditWithExtraneousMissingGroups(DID);
183 }
184
185 /**
186 * Tests re-apply process of any missing groups tests execution of
187 * any pending group creation request after the device group AUDIT completes
188 * and tests event notifications after receiving confirmation for any
189 * operations from data plane.
190 */
191 @Test
192 public void testGroupServiceAuditConfirmed() {
193 // Test Group creation before AUDIT process
194 testGroupCreationBeforeAudit(DID);
195
196 // Test audit with extraneous and missing groups
197 testAuditWithExtraneousMissingGroups(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800198
199 // Test audit with confirmed groups
Andrea Campanella6ee73922016-02-03 18:00:00 -0800200 testAuditWithConfirmedGroups(DID);
201 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800202
Andrea Campanella6ee73922016-02-03 18:00:00 -0800203 /**
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530204 * Tests group Purge Operation.
205 */
206 @Test
207 public void testPurgeGroups() {
208 //Test Group creation before AUDIT process
209 testGroupCreationBeforeAudit(DID);
210 programmableTestCleanUp();
211 testAuditWithExtraneousMissingGroups(DID);
212 // Test group add bucket operations
213 testAddBuckets(DID);
214 // Test group Purge operations
215 testPurgeGroupEntry(DID);
216 }
217
218 /**
Andrea Campanella6ee73922016-02-03 18:00:00 -0800219 * Tests group bucket modifications (additions and deletions) and
220 * Tests group deletion.
221 */
222 @Test
223 public void testGroupServiceBuckets() {
224 // Test Group creation before AUDIT process
225 testGroupCreationBeforeAudit(DID);
226 programmableTestCleanUp();
227
228 testAuditWithExtraneousMissingGroups(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800229 // Test group add bucket operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800230 testAddBuckets(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800231
232 // Test group remove bucket operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800233 testRemoveBuckets(DID);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800234
235 // Test group remove operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800236 testRemoveGroup(DID);
237 }
238
239 /**
240 * Tests group creation before the device group AUDIT completes with fallback
241 * provider.
242 */
243 @Test
244 public void testGroupServiceFallbackBasics() {
245 // Test Group creation before AUDIT process
246 testGroupCreationBeforeAudit(FOO_DID);
247 programmableTestCleanUp();
248
249 }
250
251 /**
252 * Tests initial device group AUDIT process with fallback provider.
253 */
254 @Test
255 public void testGroupServiceFallbackInitialAudit() {
256 // Test Group creation before AUDIT process
257 testGroupCreationBeforeAudit(FOO_DID);
258 programmableTestCleanUp();
259 // Test initial group audit process
260 testInitialAuditWithPendingGroupRequests(FOO_DID);
261 }
262
263 /**
264 * Tests deletion process of any extraneous groups with fallback provider.
265 */
266 @Test
267 public void testGroupServiceFallbackAuditExtraneous() {
268 // Test Group creation before AUDIT process
269 testGroupCreationBeforeAudit(FOO_DID);
270 programmableTestCleanUp();
271
272 // Test audit with extraneous and missing groups
273 testAuditWithExtraneousMissingGroups(FOO_DID);
274 }
275
276 /**
277 * Tests re-apply process of any missing groups tests execution of
278 * any pending group creation request after the device group AUDIT completes
279 * and tests event notifications after receiving confirmation for any
280 * operations from data plane with fallback provider.
281 */
282 @Test
283 public void testGroupServiceFallbackAuditConfirmed() {
284 // Test Group creation before AUDIT process
285 testGroupCreationBeforeAudit(FOO_DID);
286 programmableTestCleanUp();
287
288 // Test audit with extraneous and missing groups
289 testAuditWithExtraneousMissingGroups(FOO_DID);
290
291 // Test audit with confirmed groups
292 testAuditWithConfirmedGroups(FOO_DID);
293 }
294
295 /**
296 * Tests group bucket modifications (additions and deletions) and
297 * Tests group deletion with fallback provider.
298 */
299 @Test
300 public void testGroupServiceFallbackBuckets() {
301 // Test Group creation before AUDIT process
302 testGroupCreationBeforeAudit(FOO_DID);
303 programmableTestCleanUp();
304
305 testAuditWithExtraneousMissingGroups(FOO_DID);
306 // Test group add bucket operations
307 testAddBuckets(FOO_DID);
308
309 // Test group remove bucket operations
310 testRemoveBuckets(FOO_DID);
311
312 // Test group remove operations
313 testRemoveGroup(FOO_DID);
314 }
315
Andrea Campanella1ea15102017-09-04 16:00:09 +0200316 @Test
317 public void fallbackPoll() {
318 // Test Group creation before AUDIT process
319 testGroupCreationBeforeAudit(FOO_DID);
320 programmableTestCleanUp();
321
322 // Test audit with extraneous and missing groups
323 testAuditWithExtraneousMissingGroups(FOO_DID);
324
325 // Test audit with confirmed groups
326 Group createdGroup = testAuditWithConfirmedGroups(FOO_DID);
327 GroupDriverProvider fallback = (GroupDriverProvider) mgr.defaultProvider();
328
329 fallback.init(mgr.deviceService, fallback.groupProviderService, mgr.mastershipService, 1);
330
331 TestTools.assertAfter(2000, () -> {
332 Group e = mgr.getGroups(FOO_DID).iterator().next();
333 assertEquals("incorrect group", createdGroup, e);
334 });
335 }
336
Andrea Campanella6ee73922016-02-03 18:00:00 -0800337 private void programmableTestCleanUp() {
338 groupOperations.clear();
339 lastDeviceIdProgrammable = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800340 }
341
342 // Test Group creation before AUDIT process
Andrea Campanella6ee73922016-02-03 18:00:00 -0800343 private void testGroupCreationBeforeAudit(DeviceId deviceId) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800344 PortNumber[] ports1 = {PortNumber.portNumber(31),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800345 PortNumber.portNumber(32)};
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800346 PortNumber[] ports2 = {PortNumber.portNumber(41),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800347 PortNumber.portNumber(42)};
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700348 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700349 List<GroupBucket> buckets = new ArrayList<>();
350 List<PortNumber> outPorts = new ArrayList<>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800351 outPorts.addAll(Arrays.asList(ports1));
352 outPorts.addAll(Arrays.asList(ports2));
Andrea Campanella6ee73922016-02-03 18:00:00 -0800353 for (PortNumber portNumber : outPorts) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800354 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
355 tBuilder.setOutput(portNumber)
356 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
357 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
358 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100359 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800360 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800361 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800362 }
363 GroupBuckets groupBuckets = new GroupBuckets(buckets);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800364 GroupDescription newGroupDesc = new DefaultGroupDescription(deviceId,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800365 Group.Type.SELECT,
366 groupBuckets,
367 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700368 null,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800369 appId);
370 groupService.addGroup(newGroupDesc);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800371 assertEquals(null, groupService.getGroup(deviceId, key));
372 assertEquals(0, Iterables.size(groupService.getGroups(deviceId, appId)));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800373 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800374
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800375 // Test initial AUDIT process with pending group requests
Andrea Campanella6ee73922016-02-03 18:00:00 -0800376 private void testInitialAuditWithPendingGroupRequests(DeviceId deviceId) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800377 PortNumber[] ports1 = {PortNumber.portNumber(31),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800378 PortNumber.portNumber(32)};
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800379 PortNumber[] ports2 = {PortNumber.portNumber(41),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800380 PortNumber.portNumber(42)};
Yi Tsengfa394de2017-02-01 11:26:40 -0800381 GroupId gId1 = new GroupId(1);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800382 Group group1 = createSouthboundGroupEntry(gId1,
383 Arrays.asList(ports1),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800384 0, deviceId);
Yi Tsengfa394de2017-02-01 11:26:40 -0800385 GroupId gId2 = new GroupId(2);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800386 // Non zero reference count will make the group manager to queue
387 // the extraneous groups until reference count is zero.
388 Group group2 = createSouthboundGroupEntry(gId2,
389 Arrays.asList(ports2),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800390 2, deviceId);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800391 List<Group> groupEntries = Arrays.asList(group1, group2);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800392 providerService.pushGroupMetrics(deviceId, groupEntries);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800393 // First group metrics would trigger the device audit completion
394 // post which all pending group requests are also executed.
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700395 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800396 Group createdGroup = groupService.getGroup(deviceId, key);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800397 int createdGroupId = createdGroup.id().id();
Prince Pereira3ff504c2016-08-30 14:23:43 +0530398 assertNotEquals(gId1.id().intValue(), createdGroupId);
399 assertNotEquals(gId2.id().intValue(), createdGroupId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800400
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800401 List<GroupOperation> expectedGroupOps = Arrays.asList(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800402 GroupOperation.createDeleteGroupOperation(gId1,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800403 Group.Type.SELECT),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800404 GroupOperation.createAddGroupOperation(
405 createdGroup.id(),
406 Group.Type.SELECT,
407 createdGroup.buckets()));
408 if (deviceId.equals(DID)) {
409 internalProvider.validate(deviceId, expectedGroupOps);
410 } else {
411 this.validate(deviceId, expectedGroupOps);
412 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800413 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800414
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800415 // Test AUDIT process with extraneous groups and missing groups
Andrea Campanella6ee73922016-02-03 18:00:00 -0800416 private void testAuditWithExtraneousMissingGroups(DeviceId deviceId) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800417 PortNumber[] ports1 = {PortNumber.portNumber(31),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800418 PortNumber.portNumber(32)};
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800419 PortNumber[] ports2 = {PortNumber.portNumber(41),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800420 PortNumber.portNumber(42)};
Yi Tsengfa394de2017-02-01 11:26:40 -0800421 GroupId gId1 = new GroupId(1);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800422 Group group1 = createSouthboundGroupEntry(gId1,
Andrea Campanella6ee73922016-02-03 18:00:00 -0800423 Arrays.asList(ports1),
424 0, deviceId);
Yi Tsengfa394de2017-02-01 11:26:40 -0800425 GroupId gId2 = new GroupId(2);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800426 Group group2 = createSouthboundGroupEntry(gId2,
Andrea Campanella6ee73922016-02-03 18:00:00 -0800427 Arrays.asList(ports2),
428 0, deviceId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800429 List<Group> groupEntries = Arrays.asList(group1, group2);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800430 providerService.pushGroupMetrics(deviceId, groupEntries);
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700431 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800432 Group createdGroup = groupService.getGroup(deviceId, key);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800433 List<GroupOperation> expectedGroupOps = Arrays.asList(
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800434 GroupOperation.createDeleteGroupOperation(gId1,
435 Group.Type.SELECT),
436 GroupOperation.createDeleteGroupOperation(gId2,
437 Group.Type.SELECT),
438 GroupOperation.createAddGroupOperation(createdGroup.id(),
439 Group.Type.SELECT,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800440 createdGroup.buckets()));
Andrea Campanella6ee73922016-02-03 18:00:00 -0800441 if (deviceId.equals(DID)) {
442 internalProvider.validate(deviceId, expectedGroupOps);
443 } else {
444 this.validate(deviceId, expectedGroupOps);
445 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800446 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800447
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800448 // Test AUDIT with confirmed groups
Andrea Campanella1ea15102017-09-04 16:00:09 +0200449 private Group testAuditWithConfirmedGroups(DeviceId deviceId) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700450 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800451 Group createdGroup = groupService.getGroup(deviceId, key);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800452 createdGroup = new DefaultGroup(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800453 deviceId,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800454 Group.Type.SELECT,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800455 createdGroup.buckets());
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700456 List<Group> groupEntries = Collections.singletonList(createdGroup);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800457 providerService.pushGroupMetrics(deviceId, groupEntries);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700458 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_ADDED));
Andrea Campanella1ea15102017-09-04 16:00:09 +0200459 return createdGroup;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800460 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800461
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800462 // Test group add bucket operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800463 private void testAddBuckets(DeviceId deviceId) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700464 GroupKey addKey = new DefaultGroupKey("group1AddBuckets".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800465
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700466 GroupKey prevKey = new DefaultGroupKey("group1BeforeAudit".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800467 Group createdGroup = groupService.getGroup(deviceId, prevKey);
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700468 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800469 buckets.addAll(createdGroup.buckets().buckets());
470
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800471 PortNumber[] addPorts = {PortNumber.portNumber(51),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800472 PortNumber.portNumber(52)};
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700473 List<PortNumber> outPorts;
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700474 outPorts = new ArrayList<>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800475 outPorts.addAll(Arrays.asList(addPorts));
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700476 List<GroupBucket> addBuckets;
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700477 addBuckets = new ArrayList<>();
Andrea Campanella6ee73922016-02-03 18:00:00 -0800478 for (PortNumber portNumber : outPorts) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800479 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
480 tBuilder.setOutput(portNumber)
481 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
482 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
483 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100484 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800485 addBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800486 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800487 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800488 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800489 }
490 GroupBuckets groupAddBuckets = new GroupBuckets(addBuckets);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800491 groupService.addBucketsToGroup(deviceId,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800492 prevKey,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800493 groupAddBuckets,
494 addKey,
495 appId);
496 GroupBuckets updatedBuckets = new GroupBuckets(buckets);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700497 List<GroupOperation> expectedGroupOps = Collections.singletonList(
498 GroupOperation.createModifyGroupOperation(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800499 Group.Type.SELECT,
500 updatedBuckets));
501 if (deviceId.equals(DID)) {
502 internalProvider.validate(deviceId, expectedGroupOps);
503 } else {
504 this.validate(deviceId, expectedGroupOps);
505 }
506 Group existingGroup = groupService.getGroup(deviceId, addKey);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700507 List<Group> groupEntries = Collections.singletonList(existingGroup);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800508 providerService.pushGroupMetrics(deviceId, groupEntries);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700509 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800510 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800511
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800512 // Test group remove bucket operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800513 private void testRemoveBuckets(DeviceId deviceId) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700514 GroupKey removeKey = new DefaultGroupKey("group1RemoveBuckets".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800515
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700516 GroupKey prevKey = new DefaultGroupKey("group1AddBuckets".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800517 Group createdGroup = groupService.getGroup(deviceId, prevKey);
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700518 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800519 buckets.addAll(createdGroup.buckets().buckets());
520
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800521 PortNumber[] removePorts = {PortNumber.portNumber(31),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800522 PortNumber.portNumber(32)};
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700523 List<PortNumber> outPorts = new ArrayList<>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800524 outPorts.addAll(Arrays.asList(removePorts));
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700525 List<GroupBucket> removeBuckets = new ArrayList<>();
Andrea Campanella6ee73922016-02-03 18:00:00 -0800526 for (PortNumber portNumber : outPorts) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800527 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
528 tBuilder.setOutput(portNumber)
529 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
530 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
531 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100532 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800533 removeBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800534 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800535 buckets.remove(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800536 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800537 }
538 GroupBuckets groupRemoveBuckets = new GroupBuckets(removeBuckets);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800539 groupService.removeBucketsFromGroup(deviceId,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800540 prevKey,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800541 groupRemoveBuckets,
542 removeKey,
543 appId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800544 GroupBuckets updatedBuckets = new GroupBuckets(buckets);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700545 List<GroupOperation> expectedGroupOps = Collections.singletonList(
546 GroupOperation.createModifyGroupOperation(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800547 Group.Type.SELECT,
548 updatedBuckets));
549 if (deviceId.equals(DID)) {
550 internalProvider.validate(deviceId, expectedGroupOps);
551 } else {
552 this.validate(deviceId, expectedGroupOps);
553 }
554 Group existingGroup = groupService.getGroup(deviceId, removeKey);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700555 List<Group> groupEntries = Collections.singletonList(existingGroup);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800556 providerService.pushGroupMetrics(deviceId, groupEntries);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700557 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800558 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800559
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530560 // Test purge group entry operations
561 private void testPurgeGroupEntry(DeviceId deviceId) {
562 assertEquals(1, Iterables.size(groupService.getGroups(deviceId, appId)));
563 groupService.purgeGroupEntries(deviceId);
564 assertEquals(0, Iterables.size(groupService.getGroups(deviceId, appId)));
565 }
566
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800567 // Test group remove operations
Andrea Campanella6ee73922016-02-03 18:00:00 -0800568 private void testRemoveGroup(DeviceId deviceId) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700569 GroupKey currKey = new DefaultGroupKey("group1RemoveBuckets".getBytes());
Andrea Campanella6ee73922016-02-03 18:00:00 -0800570 Group existingGroup = groupService.getGroup(deviceId, currKey);
571 groupService.removeGroup(deviceId, currKey, appId);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700572 List<GroupOperation> expectedGroupOps = Collections.singletonList(
573 GroupOperation.createDeleteGroupOperation(existingGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800574 Group.Type.SELECT));
575 if (deviceId.equals(DID)) {
576 internalProvider.validate(deviceId, expectedGroupOps);
577 } else {
578 this.validate(deviceId, expectedGroupOps);
579 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800580 List<Group> groupEntries = Collections.emptyList();
Andrea Campanella6ee73922016-02-03 18:00:00 -0800581 providerService.pushGroupMetrics(deviceId, groupEntries);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700582 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_REMOVED));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800583 }
584
sangho7ff01812015-02-09 16:21:53 -0800585 /**
586 * Test GroupOperationFailure function in Group Manager.
587 * a)GroupAddFailure
588 * b)GroupUpdateFailure
589 * c)GroupRemoteFailure
590 */
591 @Test
592 public void testGroupOperationFailure() {
Andrea Campanella6ee73922016-02-03 18:00:00 -0800593 groupOperationFaliure(DID);
594 }
595
596 /**
597 * Test GroupOperationFailure function in Group Manager
598 * with fallback provider.
599 * a)GroupAddFailure
600 * b)GroupUpdateFailure
601 * c)GroupRemoteFailure
602 */
603 @Test
604 public void testGroupOperationFailureFallBack() {
605 groupOperationFaliure(FOO_DID);
606 }
607
608 private void groupOperationFaliure(DeviceId deviceId) {
sangho7ff01812015-02-09 16:21:53 -0800609 PortNumber[] ports1 = {PortNumber.portNumber(31),
610 PortNumber.portNumber(32)};
611 PortNumber[] ports2 = {PortNumber.portNumber(41),
612 PortNumber.portNumber(42)};
613 // Test Group creation before AUDIT process
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700614 GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes());
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700615 List<GroupBucket> buckets = new ArrayList<>();
616 List<PortNumber> outPorts = new ArrayList<>();
sangho7ff01812015-02-09 16:21:53 -0800617 outPorts.addAll(Arrays.asList(ports1));
618 outPorts.addAll(Arrays.asList(ports2));
Andrea Campanella6ee73922016-02-03 18:00:00 -0800619 for (PortNumber portNumber : outPorts) {
sangho7ff01812015-02-09 16:21:53 -0800620 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
621 tBuilder.setOutput(portNumber)
622 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
623 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
624 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100625 .setMpls(MplsLabel.mplsLabel(106));
sangho7ff01812015-02-09 16:21:53 -0800626 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
627 tBuilder.build()));
628 }
629 GroupBuckets groupBuckets = new GroupBuckets(buckets);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800630 GroupDescription newGroupDesc = new DefaultGroupDescription(deviceId,
631 Group.Type.SELECT,
632 groupBuckets,
633 key,
634 null,
635 appId);
sangho7ff01812015-02-09 16:21:53 -0800636 groupService.addGroup(newGroupDesc);
637
638 // Test initial group audit process
Yi Tsengfa394de2017-02-01 11:26:40 -0800639 GroupId gId1 = new GroupId(1);
sangho7ff01812015-02-09 16:21:53 -0800640 Group group1 = createSouthboundGroupEntry(gId1,
Andrea Campanella6ee73922016-02-03 18:00:00 -0800641 Arrays.asList(ports1),
642 0, deviceId);
Yi Tsengfa394de2017-02-01 11:26:40 -0800643 GroupId gId2 = new GroupId(2);
sangho7ff01812015-02-09 16:21:53 -0800644 // Non zero reference count will make the group manager to queue
645 // the extraneous groups until reference count is zero.
646 Group group2 = createSouthboundGroupEntry(gId2,
Andrea Campanella6ee73922016-02-03 18:00:00 -0800647 Arrays.asList(ports2),
648 2, deviceId);
sangho7ff01812015-02-09 16:21:53 -0800649 List<Group> groupEntries = Arrays.asList(group1, group2);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800650 providerService.pushGroupMetrics(deviceId, groupEntries);
651 Group createdGroup = groupService.getGroup(deviceId, key);
sangho7ff01812015-02-09 16:21:53 -0800652
653 // Group Add failure test
654 GroupOperation groupAddOp = GroupOperation.
655 createAddGroupOperation(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800656 createdGroup.type(),
657 createdGroup.buckets());
658 providerService.groupOperationFailed(deviceId, groupAddOp);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700659 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_ADD_FAILED));
sangho7ff01812015-02-09 16:21:53 -0800660
661 // Group Mod failure test
662 groupService.addGroup(newGroupDesc);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800663 createdGroup = groupService.getGroup(deviceId, key);
sangho7ff01812015-02-09 16:21:53 -0800664 assertNotNull(createdGroup);
665
666 GroupOperation groupModOp = GroupOperation.
667 createModifyGroupOperation(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800668 createdGroup.type(),
669 createdGroup.buckets());
670 providerService.groupOperationFailed(deviceId, groupModOp);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700671 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATE_FAILED));
sangho7ff01812015-02-09 16:21:53 -0800672
673 // Group Delete failure test
674 groupService.addGroup(newGroupDesc);
Andrea Campanella6ee73922016-02-03 18:00:00 -0800675 createdGroup = groupService.getGroup(deviceId, key);
sangho7ff01812015-02-09 16:21:53 -0800676 assertNotNull(createdGroup);
677
678 GroupOperation groupDelOp = GroupOperation.
679 createDeleteGroupOperation(createdGroup.id(),
Andrea Campanella6ee73922016-02-03 18:00:00 -0800680 createdGroup.type());
681 providerService.groupOperationFailed(deviceId, groupDelOp);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700682 internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_REMOVE_FAILED));
sangho7ff01812015-02-09 16:21:53 -0800683 }
684
Andrea Campanella1ea15102017-09-04 16:00:09 +0200685 private static Group createSouthboundGroupEntry(GroupId gId,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800686 List<PortNumber> ports,
Andrea Campanella6ee73922016-02-03 18:00:00 -0800687 long referenceCount, DeviceId deviceId) {
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700688 List<PortNumber> outPorts = new ArrayList<>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800689 outPorts.addAll(ports);
690
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700691 List<GroupBucket> buckets = new ArrayList<>();
Andrea Campanella6ee73922016-02-03 18:00:00 -0800692 for (PortNumber portNumber : outPorts) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800693 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
694 tBuilder.setOutput(portNumber)
695 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
696 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
697 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100698 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800699 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800700 tBuilder.build()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800701 }
702 GroupBuckets groupBuckets = new GroupBuckets(buckets);
703 StoredGroupEntry group = new DefaultGroup(
Andrea Campanella6ee73922016-02-03 18:00:00 -0800704 gId, deviceId, Group.Type.SELECT, groupBuckets);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800705 group.setReferenceCount(referenceCount);
706 return group;
707 }
708
709 private static class TestGroupListener implements GroupListener {
710 final List<GroupEvent> events = new ArrayList<>();
711
712 @Override
713 public void event(GroupEvent event) {
714 events.add(event);
715 }
716
717 public void validateEvent(List<GroupEvent.Type> expectedEvents) {
718 int i = 0;
719 System.err.println("events :" + events);
720 for (GroupEvent e : events) {
721 assertEquals("unexpected event", expectedEvents.get(i), e.type());
722 i++;
723 }
724 assertEquals("mispredicted number of events",
725 expectedEvents.size(), events.size());
726 events.clear();
727 }
728 }
729
730 private class TestGroupProvider
Andrea Campanella6ee73922016-02-03 18:00:00 -0800731 extends AbstractProvider implements GroupProvider {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800732 DeviceId lastDeviceId;
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700733 List<GroupOperation> groupOperations = new ArrayList<>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800734
735 protected TestGroupProvider(ProviderId id) {
736 super(id);
737 }
738
739 @Override
740 public void performGroupOperation(DeviceId deviceId,
741 GroupOperations groupOps) {
742 lastDeviceId = deviceId;
743 groupOperations.addAll(groupOps.operations());
744 }
745
746 public void validate(DeviceId expectedDeviceId,
747 List<GroupOperation> expectedGroupOps) {
748 if (expectedGroupOps == null) {
749 assertTrue("events generated", groupOperations.isEmpty());
750 return;
751 }
752
753 assertEquals(lastDeviceId, expectedDeviceId);
754 assertTrue((this.groupOperations.containsAll(expectedGroupOps) &&
755 expectedGroupOps.containsAll(groupOperations)));
756
757 groupOperations.clear();
758 lastDeviceId = null;
759 }
760
761 }
762
Andrea Campanella6ee73922016-02-03 18:00:00 -0800763 private static class TestDeviceService extends DeviceServiceAdapter {
764 @Override
765 public int getDeviceCount() {
766 return 1;
767 }
768
769 @Override
770 public Iterable<Device> getDevices() {
771 return ImmutableList.of(FOO_DEV);
772 }
773
774 @Override
775 public Iterable<Device> getAvailableDevices() {
776 return getDevices();
777 }
778
779 @Override
780 public Device getDevice(DeviceId deviceId) {
781 return FOO_DEV;
782 }
783 }
784
Andrea Campanella1ea15102017-09-04 16:00:09 +0200785 private class TestMastershipService extends MastershipServiceAdapter {
786 @Override
787 public MastershipRole getLocalRole(DeviceId deviceId) {
788 return MastershipRole.MASTER;
789 }
790 }
791
Andrea Campanella6ee73922016-02-03 18:00:00 -0800792 private class TestDriverManager extends DriverManager {
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700793 TestDriverManager(DriverRegistry registry) {
794 this.registry = registry;
Andrea Campanella6ee73922016-02-03 18:00:00 -0800795 this.deviceService = mgr.deviceService;
Carmelo Cascone0761cd32018-08-29 19:22:50 -0700796 this.pipeconfService = new PiPipeconfServiceAdapter();
797 this.networkConfigService = new NetworkConfigServiceAdapter();
Andrea Campanella6ee73922016-02-03 18:00:00 -0800798 activate();
799 }
800 }
801
802 private static DeviceId lastDeviceIdProgrammable;
803 private static List<GroupOperation> groupOperations = new ArrayList<>();
804
805 public static class TestGroupProgrammable extends AbstractHandlerBehaviour implements GroupProgrammable {
806 @Override
807 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
808 lastDeviceIdProgrammable = deviceId;
809 groupOperations.addAll(groupOps.operations());
810 }
Andrea Campanella1ea15102017-09-04 16:00:09 +0200811
812 @Override
813 public Collection<Group> getGroups() {
814 return ImmutableList.of(mgr.getGroups(FOO_DID).iterator().next());
815 }
Andrea Campanella6ee73922016-02-03 18:00:00 -0800816 }
817
818 public void validate(DeviceId expectedDeviceId,
819 List<GroupOperation> expectedGroupOps) {
820 if (expectedGroupOps == null) {
821 assertTrue("events generated", groupOperations.isEmpty());
822 return;
823 }
824
825 assertEquals(lastDeviceIdProgrammable, expectedDeviceId);
Sho SHIMIZU970d6e22016-08-12 15:13:22 -0700826 assertTrue(groupOperations.containsAll(expectedGroupOps) &&
Thomas Vachuska11b99fc2017-04-27 12:51:04 -0700827 expectedGroupOps.containsAll(groupOperations));
Andrea Campanella6ee73922016-02-03 18:00:00 -0800828
829 groupOperations.clear();
830 lastDeviceIdProgrammable = null;
831 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800832}