blob: 2fe5e4b886fbda1540cad4b4f0721db34ee602c2 [file] [log] [blame]
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080018import java.util.ArrayList;
19import java.util.Arrays;
20import java.util.Collections;
21import java.util.List;
22
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010027import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080028import org.onosproject.core.ApplicationId;
29import org.onosproject.core.DefaultApplicationId;
30import org.onosproject.core.DefaultGroupId;
31import org.onosproject.core.GroupId;
32import org.onosproject.event.impl.TestEventDispatcher;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080035import org.onosproject.net.device.impl.DeviceManager;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080036import org.onosproject.net.flow.DefaultTrafficTreatment;
37import org.onosproject.net.flow.TrafficTreatment;
38import org.onosproject.net.group.DefaultGroup;
39import org.onosproject.net.group.DefaultGroupBucket;
40import org.onosproject.net.group.DefaultGroupDescription;
41import org.onosproject.net.group.Group;
42import org.onosproject.net.group.GroupBucket;
43import org.onosproject.net.group.GroupBuckets;
44import org.onosproject.net.group.GroupDescription;
45import org.onosproject.net.group.GroupEvent;
46import org.onosproject.net.group.GroupKey;
47import org.onosproject.net.group.GroupListener;
48import org.onosproject.net.group.GroupOperation;
49import org.onosproject.net.group.GroupOperations;
50import org.onosproject.net.group.GroupProvider;
51import org.onosproject.net.group.GroupProviderRegistry;
52import org.onosproject.net.group.GroupProviderService;
53import org.onosproject.net.group.GroupService;
54import org.onosproject.net.group.StoredGroupEntry;
55import org.onosproject.net.provider.AbstractProvider;
56import org.onosproject.net.provider.ProviderId;
57import org.onosproject.store.trivial.impl.SimpleGroupStore;
58
59import com.google.common.collect.Iterables;
60
sangho7ff01812015-02-09 16:21:53 -080061import static org.junit.Assert.*;
62
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080063/**
64 * Test codifying the group service & group provider service contracts.
65 */
66public class GroupManagerTest {
67
68 private static final ProviderId PID = new ProviderId("of", "groupfoo");
69 private static final DeviceId DID = DeviceId.deviceId("of:001");
70
71 private GroupManager mgr;
72 private GroupService groupService;
73 private GroupProviderRegistry providerRegistry;
74 private TestGroupListener internalListener = new TestGroupListener();
75 private GroupListener listener = internalListener;
76 private TestGroupProvider internalProvider;
77 private GroupProvider provider;
78 private GroupProviderService providerService;
79 private ApplicationId appId;
80
81 @Before
82 public void setUp() {
83 mgr = new GroupManager();
84 groupService = mgr;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080085 mgr.deviceService = new DeviceManager();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080086 mgr.store = new SimpleGroupStore();
87 mgr.eventDispatcher = new TestEventDispatcher();
88 providerRegistry = mgr;
89
90 mgr.activate();
91 mgr.addListener(listener);
92
93 internalProvider = new TestGroupProvider(PID);
94 provider = internalProvider;
95 providerService = providerRegistry.register(provider);
96 appId = new DefaultApplicationId(2, "org.groupmanager.test");
97 assertTrue("provider should be registered",
98 providerRegistry.getProviders().contains(provider.id()));
99 }
100
101 @After
102 public void tearDown() {
103 providerRegistry.unregister(provider);
104 assertFalse("provider should not be registered",
105 providerRegistry.getProviders().contains(provider.id()));
106 mgr.removeListener(listener);
107 mgr.deactivate();
108 mgr.eventDispatcher = null;
109 }
110
111 private class TestGroupKey implements GroupKey {
112 private String groupId;
113
114 public TestGroupKey(String id) {
115 this.groupId = id;
116 }
117
118 public String id() {
119 return this.groupId;
120 }
121
122 @Override
123 public int hashCode() {
124 return groupId.hashCode();
125 }
126
127 @Override
128 public boolean equals(Object obj) {
129 if (obj instanceof TestGroupKey) {
130 return this.groupId.equals(((TestGroupKey) obj).id());
131 }
132 return false;
133 }
134 }
135
136 /**
137 * Tests group service north bound and south bound interfaces.
138 * The following operations are tested:
139 * a)Tests group creation before the device group AUDIT completes
140 * b)Tests initial device group AUDIT process
141 * c)Tests deletion process of any extraneous groups
142 * d)Tests execution of any pending group creation requests
143 * after the device group AUDIT completes
144 * e)Tests re-apply process of any missing groups
145 * f)Tests event notifications after receiving confirmation for
146 * any operations from data plane
147 * g)Tests group bucket modifications (additions and deletions)
148 * h)Tests group deletion
149 */
150 @Test
151 public void testGroupService() {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800152 // Test Group creation before AUDIT process
153 testGroupCreationBeforeAudit();
154
155 // Test initial group audit process
156 testInitialAuditWithPendingGroupRequests();
157
158 // Test audit with extraneous and missing groups
159 testAuditWithExtraneousMissingGroups();
160
161 // Test audit with confirmed groups
162 testAuditWithConfirmedGroups();
163
164 // Test group add bucket operations
165 testAddBuckets();
166
167 // Test group remove bucket operations
168 testRemoveBuckets();
169
170 // Test group remove operations
171 testRemoveGroup();
172 }
173
174 // Test Group creation before AUDIT process
175 private void testGroupCreationBeforeAudit() {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800176 PortNumber[] ports1 = {PortNumber.portNumber(31),
177 PortNumber.portNumber(32)};
178 PortNumber[] ports2 = {PortNumber.portNumber(41),
179 PortNumber.portNumber(42)};
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800180 TestGroupKey key = new TestGroupKey("group1BeforeAudit");
181 List<GroupBucket> buckets = new ArrayList<GroupBucket>();
182 List<PortNumber> outPorts = new ArrayList<PortNumber>();
183 outPorts.addAll(Arrays.asList(ports1));
184 outPorts.addAll(Arrays.asList(ports2));
185 for (PortNumber portNumber: outPorts) {
186 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
187 tBuilder.setOutput(portNumber)
188 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
189 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
190 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100191 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800192 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
193 tBuilder.build()));
194 }
195 GroupBuckets groupBuckets = new GroupBuckets(buckets);
196 GroupDescription newGroupDesc = new DefaultGroupDescription(DID,
197 Group.Type.SELECT,
198 groupBuckets,
199 key,
200 appId);
201 groupService.addGroup(newGroupDesc);
202 internalProvider.validate(DID, null);
203 assertEquals(null, groupService.getGroup(DID, key));
204 assertEquals(0, Iterables.size(groupService.getGroups(DID, appId)));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800205 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800206
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800207 // Test initial AUDIT process with pending group requests
208 private void testInitialAuditWithPendingGroupRequests() {
209 PortNumber[] ports1 = {PortNumber.portNumber(31),
210 PortNumber.portNumber(32)};
211 PortNumber[] ports2 = {PortNumber.portNumber(41),
212 PortNumber.portNumber(42)};
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800213 GroupId gId1 = new DefaultGroupId(1);
214 Group group1 = createSouthboundGroupEntry(gId1,
215 Arrays.asList(ports1),
216 0);
217 GroupId gId2 = new DefaultGroupId(2);
218 // Non zero reference count will make the group manager to queue
219 // the extraneous groups until reference count is zero.
220 Group group2 = createSouthboundGroupEntry(gId2,
221 Arrays.asList(ports2),
222 2);
223 List<Group> groupEntries = Arrays.asList(group1, group2);
224 providerService.pushGroupMetrics(DID, groupEntries);
225 // First group metrics would trigger the device audit completion
226 // post which all pending group requests are also executed.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800227 TestGroupKey key = new TestGroupKey("group1BeforeAudit");
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800228 Group createdGroup = groupService.getGroup(DID, key);
229 int createdGroupId = createdGroup.id().id();
230 assertNotEquals(gId1.id(), createdGroupId);
231 assertNotEquals(gId2.id(), createdGroupId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800232
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800233 List<GroupOperation> expectedGroupOps = Arrays.asList(
234 GroupOperation.createDeleteGroupOperation(gId1,
235 Group.Type.SELECT),
236 GroupOperation.createAddGroupOperation(
237 createdGroup.id(),
238 Group.Type.SELECT,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800239 createdGroup.buckets()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800240 internalProvider.validate(DID, expectedGroupOps);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800241 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800242
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800243 // Test AUDIT process with extraneous groups and missing groups
244 private void testAuditWithExtraneousMissingGroups() {
245 PortNumber[] ports1 = {PortNumber.portNumber(31),
246 PortNumber.portNumber(32)};
247 PortNumber[] ports2 = {PortNumber.portNumber(41),
248 PortNumber.portNumber(42)};
249 GroupId gId1 = new DefaultGroupId(1);
250 Group group1 = createSouthboundGroupEntry(gId1,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800251 Arrays.asList(ports1),
252 0);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800253 GroupId gId2 = new DefaultGroupId(2);
254 Group group2 = createSouthboundGroupEntry(gId2,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800255 Arrays.asList(ports2),
256 0);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800257 List<Group> groupEntries = Arrays.asList(group1, group2);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800258 providerService.pushGroupMetrics(DID, groupEntries);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800259 TestGroupKey key = new TestGroupKey("group1BeforeAudit");
260 Group createdGroup = groupService.getGroup(DID, key);
261 List<GroupOperation> expectedGroupOps = Arrays.asList(
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800262 GroupOperation.createDeleteGroupOperation(gId1,
263 Group.Type.SELECT),
264 GroupOperation.createDeleteGroupOperation(gId2,
265 Group.Type.SELECT),
266 GroupOperation.createAddGroupOperation(createdGroup.id(),
267 Group.Type.SELECT,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800268 createdGroup.buckets()));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800269 internalProvider.validate(DID, expectedGroupOps);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800270 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800271
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800272 // Test AUDIT with confirmed groups
273 private void testAuditWithConfirmedGroups() {
274 TestGroupKey key = new TestGroupKey("group1BeforeAudit");
275 Group createdGroup = groupService.getGroup(DID, key);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800276 createdGroup = new DefaultGroup(createdGroup.id(),
277 DID,
278 Group.Type.SELECT,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800279 createdGroup.buckets());
280 List<Group> groupEntries = Arrays.asList(createdGroup);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800281 providerService.pushGroupMetrics(DID, groupEntries);
282 internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_ADDED));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800283 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800284
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800285 // Test group add bucket operations
286 private void testAddBuckets() {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800287 TestGroupKey addKey = new TestGroupKey("group1AddBuckets");
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800288
289 TestGroupKey prevKey = new TestGroupKey("group1BeforeAudit");
290 Group createdGroup = groupService.getGroup(DID, prevKey);
291 List<GroupBucket> buckets = new ArrayList<GroupBucket>();
292 buckets.addAll(createdGroup.buckets().buckets());
293
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800294 PortNumber[] addPorts = {PortNumber.portNumber(51),
295 PortNumber.portNumber(52)};
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800296 List<PortNumber> outPorts = new ArrayList<PortNumber>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800297 outPorts.addAll(Arrays.asList(addPorts));
298 List<GroupBucket> addBuckets = new ArrayList<GroupBucket>();
299 for (PortNumber portNumber: outPorts) {
300 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
301 tBuilder.setOutput(portNumber)
302 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
303 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
304 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100305 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800306 addBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
307 tBuilder.build()));
308 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
309 tBuilder.build()));
310 }
311 GroupBuckets groupAddBuckets = new GroupBuckets(addBuckets);
312 groupService.addBucketsToGroup(DID,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800313 prevKey,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800314 groupAddBuckets,
315 addKey,
316 appId);
317 GroupBuckets updatedBuckets = new GroupBuckets(buckets);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800318 List<GroupOperation> expectedGroupOps = Arrays.asList(
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800319 GroupOperation.createModifyGroupOperation(createdGroup.id(),
320 Group.Type.SELECT,
321 updatedBuckets));
322 internalProvider.validate(DID, expectedGroupOps);
323 Group existingGroup = groupService.getGroup(DID, addKey);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800324 List<Group> groupEntries = Arrays.asList(existingGroup);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800325 providerService.pushGroupMetrics(DID, groupEntries);
326 internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_UPDATED));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800327 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800328
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800329 // Test group remove bucket operations
330 private void testRemoveBuckets() {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800331 TestGroupKey removeKey = new TestGroupKey("group1RemoveBuckets");
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800332
333 TestGroupKey prevKey = new TestGroupKey("group1AddBuckets");
334 Group createdGroup = groupService.getGroup(DID, prevKey);
335 List<GroupBucket> buckets = new ArrayList<GroupBucket>();
336 buckets.addAll(createdGroup.buckets().buckets());
337
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800338 PortNumber[] removePorts = {PortNumber.portNumber(31),
339 PortNumber.portNumber(32)};
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800340 List<PortNumber> outPorts = new ArrayList<PortNumber>();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800341 outPorts.addAll(Arrays.asList(removePorts));
342 List<GroupBucket> removeBuckets = new ArrayList<GroupBucket>();
343 for (PortNumber portNumber: outPorts) {
344 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
345 tBuilder.setOutput(portNumber)
346 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
347 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
348 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100349 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800350 removeBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
351 tBuilder.build()));
352 buckets.remove(DefaultGroupBucket.createSelectGroupBucket(
353 tBuilder.build()));
354 }
355 GroupBuckets groupRemoveBuckets = new GroupBuckets(removeBuckets);
356 groupService.removeBucketsFromGroup(DID,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800357 prevKey,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800358 groupRemoveBuckets,
359 removeKey,
360 appId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800361 GroupBuckets updatedBuckets = new GroupBuckets(buckets);
362 List<GroupOperation> expectedGroupOps = Arrays.asList(
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800363 GroupOperation.createModifyGroupOperation(createdGroup.id(),
364 Group.Type.SELECT,
365 updatedBuckets));
366 internalProvider.validate(DID, expectedGroupOps);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800367 Group existingGroup = groupService.getGroup(DID, removeKey);
368 List<Group> groupEntries = Arrays.asList(existingGroup);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800369 providerService.pushGroupMetrics(DID, groupEntries);
370 internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_UPDATED));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800371 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800372
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800373 // Test group remove operations
374 private void testRemoveGroup() {
375 TestGroupKey currKey = new TestGroupKey("group1RemoveBuckets");
376 Group existingGroup = groupService.getGroup(DID, currKey);
377 groupService.removeGroup(DID, currKey, appId);
378 List<GroupOperation> expectedGroupOps = Arrays.asList(
379 GroupOperation.createDeleteGroupOperation(existingGroup.id(),
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800380 Group.Type.SELECT));
381 internalProvider.validate(DID, expectedGroupOps);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800382 List<Group> groupEntries = Collections.emptyList();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800383 providerService.pushGroupMetrics(DID, groupEntries);
384 internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_REMOVED));
385 }
386
sangho7ff01812015-02-09 16:21:53 -0800387 /**
388 * Test GroupOperationFailure function in Group Manager.
389 * a)GroupAddFailure
390 * b)GroupUpdateFailure
391 * c)GroupRemoteFailure
392 */
393 @Test
394 public void testGroupOperationFailure() {
395 PortNumber[] ports1 = {PortNumber.portNumber(31),
396 PortNumber.portNumber(32)};
397 PortNumber[] ports2 = {PortNumber.portNumber(41),
398 PortNumber.portNumber(42)};
399 // Test Group creation before AUDIT process
400 TestGroupKey key = new TestGroupKey("group1BeforeAudit");
401 List<GroupBucket> buckets = new ArrayList<GroupBucket>();
402 List<PortNumber> outPorts = new ArrayList<PortNumber>();
403 outPorts.addAll(Arrays.asList(ports1));
404 outPorts.addAll(Arrays.asList(ports2));
405 for (PortNumber portNumber: outPorts) {
406 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
407 tBuilder.setOutput(portNumber)
408 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
409 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
410 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100411 .setMpls(MplsLabel.mplsLabel(106));
sangho7ff01812015-02-09 16:21:53 -0800412 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
413 tBuilder.build()));
414 }
415 GroupBuckets groupBuckets = new GroupBuckets(buckets);
416 GroupDescription newGroupDesc = new DefaultGroupDescription(DID,
417 Group.Type.SELECT,
418 groupBuckets,
419 key,
420 appId);
421 groupService.addGroup(newGroupDesc);
422
423 // Test initial group audit process
424 GroupId gId1 = new DefaultGroupId(1);
425 Group group1 = createSouthboundGroupEntry(gId1,
426 Arrays.asList(ports1),
427 0);
428 GroupId gId2 = new DefaultGroupId(2);
429 // Non zero reference count will make the group manager to queue
430 // the extraneous groups until reference count is zero.
431 Group group2 = createSouthboundGroupEntry(gId2,
432 Arrays.asList(ports2),
433 2);
434 List<Group> groupEntries = Arrays.asList(group1, group2);
435 providerService.pushGroupMetrics(DID, groupEntries);
436 Group createdGroup = groupService.getGroup(DID, key);
437
438 // Group Add failure test
439 GroupOperation groupAddOp = GroupOperation.
440 createAddGroupOperation(createdGroup.id(),
441 createdGroup.type(),
442 createdGroup.buckets());
443 providerService.groupOperationFailed(DID, groupAddOp);
444 internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_ADD_FAILED));
445
446 // Group Mod failure test
447 groupService.addGroup(newGroupDesc);
448 createdGroup = groupService.getGroup(DID, key);
449 assertNotNull(createdGroup);
450
451 GroupOperation groupModOp = GroupOperation.
452 createModifyGroupOperation(createdGroup.id(),
453 createdGroup.type(),
454 createdGroup.buckets());
455 providerService.groupOperationFailed(DID, groupModOp);
456 internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_UPDATE_FAILED));
457
458 // Group Delete failure test
459 groupService.addGroup(newGroupDesc);
460 createdGroup = groupService.getGroup(DID, key);
461 assertNotNull(createdGroup);
462
463 GroupOperation groupDelOp = GroupOperation.
464 createDeleteGroupOperation(createdGroup.id(),
465 createdGroup.type());
466 providerService.groupOperationFailed(DID, groupDelOp);
467 internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_REMOVE_FAILED));
468
469 }
470
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800471 private Group createSouthboundGroupEntry(GroupId gId,
472 List<PortNumber> ports,
473 long referenceCount) {
474 List<PortNumber> outPorts = new ArrayList<PortNumber>();
475 outPorts.addAll(ports);
476
477 List<GroupBucket> buckets = new ArrayList<GroupBucket>();
478 for (PortNumber portNumber: outPorts) {
479 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 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
486 tBuilder.build()));
487 }
488 GroupBuckets groupBuckets = new GroupBuckets(buckets);
489 StoredGroupEntry group = new DefaultGroup(
490 gId, DID, Group.Type.SELECT, groupBuckets);
491 group.setReferenceCount(referenceCount);
492 return group;
493 }
494
495 private static class TestGroupListener implements GroupListener {
496 final List<GroupEvent> events = new ArrayList<>();
497
498 @Override
499 public void event(GroupEvent event) {
500 events.add(event);
501 }
502
503 public void validateEvent(List<GroupEvent.Type> expectedEvents) {
504 int i = 0;
505 System.err.println("events :" + events);
506 for (GroupEvent e : events) {
507 assertEquals("unexpected event", expectedEvents.get(i), e.type());
508 i++;
509 }
510 assertEquals("mispredicted number of events",
511 expectedEvents.size(), events.size());
512 events.clear();
513 }
514 }
515
516 private class TestGroupProvider
517 extends AbstractProvider implements GroupProvider {
518 DeviceId lastDeviceId;
519 List<GroupOperation> groupOperations = new ArrayList<GroupOperation>();
520
521 protected TestGroupProvider(ProviderId id) {
522 super(id);
523 }
524
525 @Override
526 public void performGroupOperation(DeviceId deviceId,
527 GroupOperations groupOps) {
528 lastDeviceId = deviceId;
529 groupOperations.addAll(groupOps.operations());
530 }
531
532 public void validate(DeviceId expectedDeviceId,
533 List<GroupOperation> expectedGroupOps) {
534 if (expectedGroupOps == null) {
535 assertTrue("events generated", groupOperations.isEmpty());
536 return;
537 }
538
539 assertEquals(lastDeviceId, expectedDeviceId);
540 assertTrue((this.groupOperations.containsAll(expectedGroupOps) &&
541 expectedGroupOps.containsAll(groupOperations)));
542
543 groupOperations.clear();
544 lastDeviceId = null;
545 }
546
547 }
548
549}
550
551