blob: dd6c8a58e8e0f0237444610d8bd511fc5204504b [file] [log] [blame]
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Srikanth Vavilapalli0599d512015-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 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080017
18import static org.junit.Assert.assertEquals;
19import static org.onosproject.net.DeviceId.deviceId;
20
21import java.util.ArrayList;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080022import java.util.Arrays;
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070023import java.util.Collections;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080024import java.util.List;
Sho SHIMIZU30d639b2015-05-05 09:30:35 -070025import java.util.Optional;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080026
27import org.junit.After;
28import org.junit.Before;
29import org.junit.Test;
30import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010031import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.DefaultApplicationId;
34import org.onosproject.core.GroupId;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070039import org.onosproject.net.group.DefaultGroup;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080040import org.onosproject.net.group.DefaultGroupBucket;
41import org.onosproject.net.group.DefaultGroupDescription;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070042import org.onosproject.net.group.DefaultGroupKey;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080043import org.onosproject.net.group.Group;
44import org.onosproject.net.group.GroupBucket;
45import org.onosproject.net.group.GroupBuckets;
46import org.onosproject.net.group.GroupDescription;
47import org.onosproject.net.group.GroupEvent;
48import org.onosproject.net.group.GroupKey;
sangho7ff01812015-02-09 16:21:53 -080049import org.onosproject.net.group.GroupOperation;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080050import org.onosproject.net.group.GroupStore.UpdateType;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080051import org.onosproject.net.group.GroupStoreDelegate;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070052import org.onosproject.net.group.StoredGroupBucketEntry;
53import org.onosproject.net.group.StoredGroupEntry;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080054
55import com.google.common.collect.Iterables;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080056
57/**
58 * Test of the simple DeviceStore implementation.
59 */
60public class SimpleGroupStoreTest {
61
62 private SimpleGroupStore simpleGroupStore;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080063 private final ApplicationId appId =
64 new DefaultApplicationId(2, "org.groupstore.test");
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080065
66 public static final DeviceId D1 = deviceId("of:1");
67
68 @Before
69 public void setUp() throws Exception {
70 simpleGroupStore = new SimpleGroupStore();
71 simpleGroupStore.activate();
72 }
73
74 @After
75 public void tearDown() throws Exception {
76 simpleGroupStore.deactivate();
77 }
78
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080079 private class InternalGroupStoreDelegate
80 implements GroupStoreDelegate {
81 private GroupId createdGroupId = null;
82 private GroupKey createdGroupKey;
83 private GroupBuckets createdBuckets;
84 private GroupEvent.Type expectedEvent;
85
86 public InternalGroupStoreDelegate(GroupKey key,
87 GroupBuckets buckets,
88 GroupEvent.Type expectedEvent) {
89 this.createdBuckets = buckets;
90 this.createdGroupKey = key;
91 this.expectedEvent = expectedEvent;
92 }
93 @Override
94 public void notify(GroupEvent event) {
95 assertEquals(expectedEvent, event.type());
96 assertEquals(Group.Type.SELECT, event.subject().type());
97 assertEquals(D1, event.subject().deviceId());
98 assertEquals(createdGroupKey, event.subject().appCookie());
99 assertEquals(createdBuckets.buckets(), event.subject().buckets().buckets());
100 if (expectedEvent == GroupEvent.Type.GROUP_ADD_REQUESTED) {
101 createdGroupId = event.subject().id();
102 assertEquals(Group.GroupState.PENDING_ADD,
103 event.subject().state());
104 } else if (expectedEvent == GroupEvent.Type.GROUP_ADDED) {
105 createdGroupId = event.subject().id();
106 assertEquals(Group.GroupState.ADDED,
107 event.subject().state());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700108 } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATED) {
109 createdGroupId = event.subject().id();
110 assertEquals(true,
111 event.subject().buckets().
112 buckets().containsAll(createdBuckets.buckets()));
113 assertEquals(true,
114 createdBuckets.buckets().
115 containsAll(event.subject().buckets().buckets()));
116 for (GroupBucket bucket:event.subject().buckets().buckets()) {
Sho SHIMIZU30d639b2015-05-05 09:30:35 -0700117 Optional<GroupBucket> matched = createdBuckets.buckets()
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700118 .stream()
119 .filter((expected) -> expected.equals(bucket))
120 .findFirst();
121 assertEquals(matched.get().packets(),
122 bucket.packets());
123 assertEquals(matched.get().bytes(),
124 bucket.bytes());
125 }
126 assertEquals(Group.GroupState.ADDED,
127 event.subject().state());
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800128 } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATE_REQUESTED) {
129 assertEquals(Group.GroupState.PENDING_UPDATE,
130 event.subject().state());
131 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVE_REQUESTED) {
132 assertEquals(Group.GroupState.PENDING_DELETE,
133 event.subject().state());
134 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVED) {
135 createdGroupId = event.subject().id();
136 assertEquals(Group.GroupState.PENDING_DELETE,
137 event.subject().state());
sangho7ff01812015-02-09 16:21:53 -0800138 } else if (expectedEvent == GroupEvent.Type.GROUP_ADD_FAILED) {
139 createdGroupId = event.subject().id();
140 assertEquals(Group.GroupState.PENDING_ADD,
141 event.subject().state());
142 } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATE_FAILED) {
143 createdGroupId = event.subject().id();
144 assertEquals(Group.GroupState.PENDING_UPDATE,
145 event.subject().state());
146 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVE_FAILED) {
147 createdGroupId = event.subject().id();
148 assertEquals(Group.GroupState.PENDING_DELETE,
149 event.subject().state());
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800150 }
151 }
152
153 public void verifyGroupId(GroupId id) {
154 assertEquals(createdGroupId, id);
155 }
156 }
157
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800158 /**
159 * Tests group store operations. The following operations are tested:
160 * a)Tests device group audit completion status change
161 * b)Tests storeGroup operation
162 * c)Tests getGroupCount operation
163 * d)Tests getGroup operation
164 * e)Tests getGroups operation
165 * f)Tests addOrUpdateGroupEntry operation from southbound
166 * g)Tests updateGroupDescription for ADD operation from northbound
167 * h)Tests updateGroupDescription for REMOVE operation from northbound
168 * i)Tests deleteGroupDescription operation from northbound
169 * j)Tests removeGroupEntry operation from southbound
170 */
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800171 @Test
172 public void testGroupStoreOperations() {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800173 // Set the Device AUDIT completed in the store
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800174 simpleGroupStore.deviceInitialAuditCompleted(D1, true);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800175
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800176 // Testing storeGroup operation
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700177 GroupKey newKey = new DefaultGroupKey("group1".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800178 testStoreAndGetGroup(newKey);
179
180 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700181 GroupKey currKey = newKey;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800182 testAddGroupEntryFromSB(currKey);
183
184 // Testing updateGroupDescription for ADD operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700185 newKey = new DefaultGroupKey("group1AddBuckets".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800186 testAddBuckets(currKey, newKey);
187
188 // Testing updateGroupDescription for REMOVE operation from northbound
189 currKey = newKey;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700190 newKey = new DefaultGroupKey("group1RemoveBuckets".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800191 testRemoveBuckets(currKey, newKey);
192
193 // Testing addOrUpdateGroupEntry operation from southbound
194 currKey = newKey;
195 testUpdateGroupEntryFromSB(currKey);
196
197 // Testing deleteGroupDescription operation from northbound
198 testDeleteGroup(currKey);
199
200 // Testing removeGroupEntry operation from southbound
201 testRemoveGroupFromSB(currKey);
202 }
203
204 // Testing storeGroup operation
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700205 private void testStoreAndGetGroup(GroupKey key) {
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800206 PortNumber[] ports = {PortNumber.portNumber(31),
207 PortNumber.portNumber(32)};
208 List<PortNumber> outPorts = new ArrayList<PortNumber>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800209 outPorts.addAll(Arrays.asList(ports));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800210
211 List<GroupBucket> buckets = new ArrayList<GroupBucket>();
212 for (PortNumber portNumber: outPorts) {
213 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
214 tBuilder.setOutput(portNumber)
215 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
216 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
217 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100218 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800219 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
220 tBuilder.build()));
221 }
222 GroupBuckets groupBuckets = new GroupBuckets(buckets);
223 GroupDescription groupDesc = new DefaultGroupDescription(
224 D1,
225 Group.Type.SELECT,
226 groupBuckets,
227 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700228 null,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800229 appId);
230 InternalGroupStoreDelegate checkStoreGroupDelegate =
231 new InternalGroupStoreDelegate(key,
232 groupBuckets,
233 GroupEvent.Type.GROUP_ADD_REQUESTED);
234 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800235 // Testing storeGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800236 simpleGroupStore.storeGroupDescription(groupDesc);
237
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800238 // Testing getGroupCount operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800239 assertEquals(1, simpleGroupStore.getGroupCount(D1));
240
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800241 // Testing getGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800242 Group createdGroup = simpleGroupStore.getGroup(D1, key);
243 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
244
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800245 // Testing getGroups operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800246 Iterable<Group> createdGroups = simpleGroupStore.getGroups(D1);
247 int groupCount = 0;
248 for (Group group:createdGroups) {
249 checkStoreGroupDelegate.verifyGroupId(group.id());
250 groupCount++;
251 }
252 assertEquals(1, groupCount);
253 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800254 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800255
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800256 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700257 private void testAddGroupEntryFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800258 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
259
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800260 InternalGroupStoreDelegate addGroupEntryDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800261 new InternalGroupStoreDelegate(currKey,
262 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800263 GroupEvent.Type.GROUP_ADDED);
264 simpleGroupStore.setDelegate(addGroupEntryDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800265 simpleGroupStore.addOrUpdateGroupEntry(existingGroup);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800266 simpleGroupStore.unsetDelegate(addGroupEntryDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800267 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800268
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800269 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700270 private void testUpdateGroupEntryFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800271 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700272 int totalPkts = 0;
273 int totalBytes = 0;
274 List<GroupBucket> newBucketList = new ArrayList<GroupBucket>();
275 for (GroupBucket bucket:existingGroup.buckets().buckets()) {
276 StoredGroupBucketEntry newBucket =
277 (StoredGroupBucketEntry)
278 DefaultGroupBucket.createSelectGroupBucket(bucket.treatment());
279 newBucket.setPackets(10);
280 newBucket.setBytes(10 * 256 * 8);
281 totalPkts += 10;
282 totalBytes += 10 * 256 * 8;
283 newBucketList.add(newBucket);
284 }
285 GroupBuckets updatedBuckets = new GroupBuckets(newBucketList);
286 Group updatedGroup = new DefaultGroup(existingGroup.id(),
287 existingGroup.deviceId(),
288 existingGroup.type(),
289 updatedBuckets);
290 ((StoredGroupEntry) updatedGroup).setPackets(totalPkts);
291 ((StoredGroupEntry) updatedGroup).setBytes(totalBytes);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800292
293 InternalGroupStoreDelegate updateGroupEntryDelegate =
294 new InternalGroupStoreDelegate(currKey,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700295 updatedBuckets,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800296 GroupEvent.Type.GROUP_UPDATED);
297 simpleGroupStore.setDelegate(updateGroupEntryDelegate);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700298 simpleGroupStore.addOrUpdateGroupEntry(updatedGroup);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800299 simpleGroupStore.unsetDelegate(updateGroupEntryDelegate);
300 }
301
302 // Testing updateGroupDescription for ADD operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700303 private void testAddBuckets(GroupKey currKey, GroupKey addKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800304 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
305 List<GroupBucket> buckets = new ArrayList<GroupBucket>();
306 buckets.addAll(existingGroup.buckets().buckets());
307
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800308 PortNumber[] newNeighborPorts = {PortNumber.portNumber(41),
309 PortNumber.portNumber(42)};
310 List<PortNumber> newOutPorts = new ArrayList<PortNumber>();
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700311 newOutPorts.addAll(Collections.singletonList(newNeighborPorts[0]));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800312
313 List<GroupBucket> toAddBuckets = new ArrayList<GroupBucket>();
314 for (PortNumber portNumber: newOutPorts) {
315 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
316 tBuilder.setOutput(portNumber)
317 .setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
318 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
319 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100320 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800321 toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
322 tBuilder.build()));
323 }
324 GroupBuckets toAddGroupBuckets = new GroupBuckets(toAddBuckets);
325 buckets.addAll(toAddBuckets);
326 GroupBuckets updatedGroupBuckets = new GroupBuckets(buckets);
327 InternalGroupStoreDelegate updateGroupDescDelegate =
328 new InternalGroupStoreDelegate(addKey,
329 updatedGroupBuckets,
330 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
331 simpleGroupStore.setDelegate(updateGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800332 simpleGroupStore.updateGroupDescription(D1,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800333 currKey,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800334 UpdateType.ADD,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800335 toAddGroupBuckets,
336 addKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800337 simpleGroupStore.unsetDelegate(updateGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800338 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800339
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800340 // Testing updateGroupDescription for REMOVE operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700341 private void testRemoveBuckets(GroupKey currKey, GroupKey removeKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800342 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
343 List<GroupBucket> buckets = new ArrayList<GroupBucket>();
344 buckets.addAll(existingGroup.buckets().buckets());
345
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800346 List<GroupBucket> toRemoveBuckets = new ArrayList<GroupBucket>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800347
348 // There should be 4 buckets in the current group
349 toRemoveBuckets.add(buckets.remove(0));
350 toRemoveBuckets.add(buckets.remove(1));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800351 GroupBuckets toRemoveGroupBuckets = new GroupBuckets(toRemoveBuckets);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800352
353 GroupBuckets remainingGroupBuckets = new GroupBuckets(buckets);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800354 InternalGroupStoreDelegate removeGroupDescDelegate =
355 new InternalGroupStoreDelegate(removeKey,
356 remainingGroupBuckets,
357 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
358 simpleGroupStore.setDelegate(removeGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800359 simpleGroupStore.updateGroupDescription(D1,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800360 currKey,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800361 UpdateType.REMOVE,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800362 toRemoveGroupBuckets,
363 removeKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800364 simpleGroupStore.unsetDelegate(removeGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800365 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800366
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800367 // Testing deleteGroupDescription operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700368 private void testDeleteGroup(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800369 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800370 InternalGroupStoreDelegate deleteGroupDescDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800371 new InternalGroupStoreDelegate(currKey,
372 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800373 GroupEvent.Type.GROUP_REMOVE_REQUESTED);
374 simpleGroupStore.setDelegate(deleteGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800375 simpleGroupStore.deleteGroupDescription(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800376 simpleGroupStore.unsetDelegate(deleteGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800377 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800378
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800379 // Testing removeGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700380 private void testRemoveGroupFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800381 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800382 InternalGroupStoreDelegate removeGroupEntryDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800383 new InternalGroupStoreDelegate(currKey,
384 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800385 GroupEvent.Type.GROUP_REMOVED);
386 simpleGroupStore.setDelegate(removeGroupEntryDelegate);
387 simpleGroupStore.removeGroupEntry(existingGroup);
388
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800389 // Testing getGroup operation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800390 existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800391 assertEquals(null, existingGroup);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800392 assertEquals(0, Iterables.size(simpleGroupStore.getGroups(D1)));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800393 assertEquals(0, simpleGroupStore.getGroupCount(D1));
394
395 simpleGroupStore.unsetDelegate(removeGroupEntryDelegate);
sangho7ff01812015-02-09 16:21:53 -0800396 }
397
398 @Test
399 public void testGroupOperationFailure() {
400
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800401 simpleGroupStore.deviceInitialAuditCompleted(D1, true);
sangho7ff01812015-02-09 16:21:53 -0800402
403 ApplicationId appId =
404 new DefaultApplicationId(2, "org.groupstore.test");
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700405 GroupKey key = new DefaultGroupKey("group1".getBytes());
sangho7ff01812015-02-09 16:21:53 -0800406 PortNumber[] ports = {PortNumber.portNumber(31),
407 PortNumber.portNumber(32)};
408 List<PortNumber> outPorts = new ArrayList<PortNumber>();
409 outPorts.add(ports[0]);
410 outPorts.add(ports[1]);
411
412 List<GroupBucket> buckets = new ArrayList<GroupBucket>();
413 for (PortNumber portNumber: outPorts) {
414 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
415 tBuilder.setOutput(portNumber)
416 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
417 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
418 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100419 .setMpls(MplsLabel.mplsLabel(106));
sangho7ff01812015-02-09 16:21:53 -0800420 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
421 tBuilder.build()));
422 }
423 GroupBuckets groupBuckets = new GroupBuckets(buckets);
424 GroupDescription groupDesc = new DefaultGroupDescription(
425 D1,
426 Group.Type.SELECT,
427 groupBuckets,
428 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700429 null,
sangho7ff01812015-02-09 16:21:53 -0800430 appId);
431 InternalGroupStoreDelegate checkStoreGroupDelegate =
432 new InternalGroupStoreDelegate(key,
433 groupBuckets,
434 GroupEvent.Type.GROUP_ADD_REQUESTED);
435 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
436 // Testing storeGroup operation
437 simpleGroupStore.storeGroupDescription(groupDesc);
438 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
439
440 // Testing Group add operation failure
441 Group createdGroup = simpleGroupStore.getGroup(D1, key);
442 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
443
444 GroupOperation groupAddOp = GroupOperation.
445 createAddGroupOperation(createdGroup.id(),
446 createdGroup.type(),
447 createdGroup.buckets());
448 InternalGroupStoreDelegate checkGroupAddFailureDelegate =
449 new InternalGroupStoreDelegate(key,
450 groupBuckets,
451 GroupEvent.Type.GROUP_ADD_FAILED);
452 simpleGroupStore.setDelegate(checkGroupAddFailureDelegate);
453 simpleGroupStore.groupOperationFailed(D1, groupAddOp);
454
455
456 // Testing Group modify operation failure
457 simpleGroupStore.unsetDelegate(checkGroupAddFailureDelegate);
458 GroupOperation groupModOp = GroupOperation.
459 createModifyGroupOperation(createdGroup.id(),
460 createdGroup.type(),
461 createdGroup.buckets());
462 InternalGroupStoreDelegate checkGroupModFailureDelegate =
463 new InternalGroupStoreDelegate(key,
464 groupBuckets,
465 GroupEvent.Type.GROUP_UPDATE_FAILED);
466 simpleGroupStore.setDelegate(checkGroupModFailureDelegate);
467 simpleGroupStore.groupOperationFailed(D1, groupModOp);
468
469 // Testing Group modify operation failure
470 simpleGroupStore.unsetDelegate(checkGroupModFailureDelegate);
471 GroupOperation groupDelOp = GroupOperation.
472 createDeleteGroupOperation(createdGroup.id(),
473 createdGroup.type());
474 InternalGroupStoreDelegate checkGroupDelFailureDelegate =
475 new InternalGroupStoreDelegate(key,
476 groupBuckets,
477 GroupEvent.Type.GROUP_REMOVE_FAILED);
478 simpleGroupStore.setDelegate(checkGroupDelFailureDelegate);
479 simpleGroupStore.groupOperationFailed(D1, groupDelOp);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800480 }
481}
482