blob: 11ec79513b632b2b8dfb06f46f000cb56b59da11 [file] [log] [blame]
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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;
Charles Chan0c7c43b2016-01-14 17:39:20 -080019import static org.junit.Assert.assertThat;
20import static org.hamcrest.Matchers.is;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080021import static org.onosproject.net.DeviceId.deviceId;
22
23import java.util.ArrayList;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080024import java.util.Arrays;
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070025import java.util.Collections;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080026import java.util.List;
Sho SHIMIZU30d639b2015-05-05 09:30:35 -070027import java.util.Optional;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080028
29import org.junit.After;
30import org.junit.Before;
31import org.junit.Test;
32import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010033import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080034import org.onosproject.core.ApplicationId;
35import org.onosproject.core.DefaultApplicationId;
36import org.onosproject.core.GroupId;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.PortNumber;
39import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070041import org.onosproject.net.group.DefaultGroup;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080042import org.onosproject.net.group.DefaultGroupBucket;
43import org.onosproject.net.group.DefaultGroupDescription;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070044import org.onosproject.net.group.DefaultGroupKey;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080045import org.onosproject.net.group.Group;
46import org.onosproject.net.group.GroupBucket;
47import org.onosproject.net.group.GroupBuckets;
48import org.onosproject.net.group.GroupDescription;
49import org.onosproject.net.group.GroupEvent;
50import org.onosproject.net.group.GroupKey;
sangho7ff01812015-02-09 16:21:53 -080051import org.onosproject.net.group.GroupOperation;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080052import org.onosproject.net.group.GroupStore.UpdateType;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080053import org.onosproject.net.group.GroupStoreDelegate;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070054import org.onosproject.net.group.StoredGroupBucketEntry;
55import org.onosproject.net.group.StoredGroupEntry;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080056
57import com.google.common.collect.Iterables;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080058
59/**
60 * Test of the simple DeviceStore implementation.
61 */
62public class SimpleGroupStoreTest {
63
64 private SimpleGroupStore simpleGroupStore;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080065 private final ApplicationId appId =
66 new DefaultApplicationId(2, "org.groupstore.test");
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080067
68 public static final DeviceId D1 = deviceId("of:1");
69
70 @Before
71 public void setUp() throws Exception {
72 simpleGroupStore = new SimpleGroupStore();
73 simpleGroupStore.activate();
74 }
75
76 @After
77 public void tearDown() throws Exception {
78 simpleGroupStore.deactivate();
79 }
80
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080081 private class InternalGroupStoreDelegate
82 implements GroupStoreDelegate {
83 private GroupId createdGroupId = null;
84 private GroupKey createdGroupKey;
85 private GroupBuckets createdBuckets;
86 private GroupEvent.Type expectedEvent;
87
88 public InternalGroupStoreDelegate(GroupKey key,
89 GroupBuckets buckets,
90 GroupEvent.Type expectedEvent) {
91 this.createdBuckets = buckets;
92 this.createdGroupKey = key;
93 this.expectedEvent = expectedEvent;
94 }
95 @Override
96 public void notify(GroupEvent event) {
97 assertEquals(expectedEvent, event.type());
98 assertEquals(Group.Type.SELECT, event.subject().type());
99 assertEquals(D1, event.subject().deviceId());
100 assertEquals(createdGroupKey, event.subject().appCookie());
101 assertEquals(createdBuckets.buckets(), event.subject().buckets().buckets());
102 if (expectedEvent == GroupEvent.Type.GROUP_ADD_REQUESTED) {
103 createdGroupId = event.subject().id();
104 assertEquals(Group.GroupState.PENDING_ADD,
105 event.subject().state());
106 } else if (expectedEvent == GroupEvent.Type.GROUP_ADDED) {
107 createdGroupId = event.subject().id();
108 assertEquals(Group.GroupState.ADDED,
109 event.subject().state());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700110 } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATED) {
111 createdGroupId = event.subject().id();
112 assertEquals(true,
113 event.subject().buckets().
114 buckets().containsAll(createdBuckets.buckets()));
115 assertEquals(true,
116 createdBuckets.buckets().
117 containsAll(event.subject().buckets().buckets()));
118 for (GroupBucket bucket:event.subject().buckets().buckets()) {
Sho SHIMIZU30d639b2015-05-05 09:30:35 -0700119 Optional<GroupBucket> matched = createdBuckets.buckets()
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700120 .stream()
121 .filter((expected) -> expected.equals(bucket))
122 .findFirst();
123 assertEquals(matched.get().packets(),
124 bucket.packets());
125 assertEquals(matched.get().bytes(),
126 bucket.bytes());
127 }
128 assertEquals(Group.GroupState.ADDED,
129 event.subject().state());
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800130 } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATE_REQUESTED) {
131 assertEquals(Group.GroupState.PENDING_UPDATE,
132 event.subject().state());
133 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVE_REQUESTED) {
134 assertEquals(Group.GroupState.PENDING_DELETE,
135 event.subject().state());
136 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVED) {
137 createdGroupId = event.subject().id();
138 assertEquals(Group.GroupState.PENDING_DELETE,
139 event.subject().state());
sangho7ff01812015-02-09 16:21:53 -0800140 } else if (expectedEvent == GroupEvent.Type.GROUP_ADD_FAILED) {
141 createdGroupId = event.subject().id();
142 assertEquals(Group.GroupState.PENDING_ADD,
143 event.subject().state());
144 } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATE_FAILED) {
145 createdGroupId = event.subject().id();
146 assertEquals(Group.GroupState.PENDING_UPDATE,
147 event.subject().state());
148 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVE_FAILED) {
149 createdGroupId = event.subject().id();
150 assertEquals(Group.GroupState.PENDING_DELETE,
151 event.subject().state());
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800152 }
153 }
154
155 public void verifyGroupId(GroupId id) {
156 assertEquals(createdGroupId, id);
157 }
158 }
159
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800160 /**
161 * Tests group store operations. The following operations are tested:
162 * a)Tests device group audit completion status change
163 * b)Tests storeGroup operation
164 * c)Tests getGroupCount operation
165 * d)Tests getGroup operation
166 * e)Tests getGroups operation
167 * f)Tests addOrUpdateGroupEntry operation from southbound
168 * g)Tests updateGroupDescription for ADD operation from northbound
169 * h)Tests updateGroupDescription for REMOVE operation from northbound
170 * i)Tests deleteGroupDescription operation from northbound
171 * j)Tests removeGroupEntry operation from southbound
172 */
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800173 @Test
174 public void testGroupStoreOperations() {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800175 // Set the Device AUDIT completed in the store
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800176 simpleGroupStore.deviceInitialAuditCompleted(D1, true);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800177
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800178 // Testing storeGroup operation
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700179 GroupKey newKey = new DefaultGroupKey("group1".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800180 testStoreAndGetGroup(newKey);
181
182 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700183 GroupKey currKey = newKey;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800184 testAddGroupEntryFromSB(currKey);
185
186 // Testing updateGroupDescription for ADD operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700187 newKey = new DefaultGroupKey("group1AddBuckets".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800188 testAddBuckets(currKey, newKey);
189
190 // Testing updateGroupDescription for REMOVE operation from northbound
191 currKey = newKey;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700192 newKey = new DefaultGroupKey("group1RemoveBuckets".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800193 testRemoveBuckets(currKey, newKey);
194
195 // Testing addOrUpdateGroupEntry operation from southbound
196 currKey = newKey;
197 testUpdateGroupEntryFromSB(currKey);
198
199 // Testing deleteGroupDescription operation from northbound
200 testDeleteGroup(currKey);
201
202 // Testing removeGroupEntry operation from southbound
203 testRemoveGroupFromSB(currKey);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800204
205 // Testing removing all groups on the given device
206 newKey = new DefaultGroupKey("group1".getBytes());
207 testStoreAndGetGroup(newKey);
208 testDeleteGroupOnDevice(newKey);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800209 }
210
211 // Testing storeGroup operation
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700212 private void testStoreAndGetGroup(GroupKey key) {
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800213 PortNumber[] ports = {PortNumber.portNumber(31),
214 PortNumber.portNumber(32)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700215 List<PortNumber> outPorts = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800216 outPorts.addAll(Arrays.asList(ports));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800217
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700218 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800219 for (PortNumber portNumber: outPorts) {
220 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
221 tBuilder.setOutput(portNumber)
222 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
223 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
224 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100225 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800226 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
227 tBuilder.build()));
228 }
229 GroupBuckets groupBuckets = new GroupBuckets(buckets);
230 GroupDescription groupDesc = new DefaultGroupDescription(
231 D1,
232 Group.Type.SELECT,
233 groupBuckets,
234 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700235 null,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800236 appId);
237 InternalGroupStoreDelegate checkStoreGroupDelegate =
238 new InternalGroupStoreDelegate(key,
239 groupBuckets,
240 GroupEvent.Type.GROUP_ADD_REQUESTED);
241 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800242 // Testing storeGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800243 simpleGroupStore.storeGroupDescription(groupDesc);
244
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800245 // Testing getGroupCount operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800246 assertEquals(1, simpleGroupStore.getGroupCount(D1));
247
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800248 // Testing getGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800249 Group createdGroup = simpleGroupStore.getGroup(D1, key);
250 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
251
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800252 // Testing getGroups operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800253 Iterable<Group> createdGroups = simpleGroupStore.getGroups(D1);
254 int groupCount = 0;
255 for (Group group:createdGroups) {
256 checkStoreGroupDelegate.verifyGroupId(group.id());
257 groupCount++;
258 }
259 assertEquals(1, groupCount);
260 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800261 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800262
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800263 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700264 private void testAddGroupEntryFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800265 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
266
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800267 InternalGroupStoreDelegate addGroupEntryDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800268 new InternalGroupStoreDelegate(currKey,
269 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800270 GroupEvent.Type.GROUP_ADDED);
271 simpleGroupStore.setDelegate(addGroupEntryDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800272 simpleGroupStore.addOrUpdateGroupEntry(existingGroup);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800273 simpleGroupStore.unsetDelegate(addGroupEntryDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800274 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800275
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800276 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700277 private void testUpdateGroupEntryFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800278 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700279 int totalPkts = 0;
280 int totalBytes = 0;
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700281 List<GroupBucket> newBucketList = new ArrayList<>();
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700282 for (GroupBucket bucket:existingGroup.buckets().buckets()) {
283 StoredGroupBucketEntry newBucket =
284 (StoredGroupBucketEntry)
285 DefaultGroupBucket.createSelectGroupBucket(bucket.treatment());
286 newBucket.setPackets(10);
287 newBucket.setBytes(10 * 256 * 8);
288 totalPkts += 10;
289 totalBytes += 10 * 256 * 8;
290 newBucketList.add(newBucket);
291 }
292 GroupBuckets updatedBuckets = new GroupBuckets(newBucketList);
293 Group updatedGroup = new DefaultGroup(existingGroup.id(),
294 existingGroup.deviceId(),
295 existingGroup.type(),
296 updatedBuckets);
297 ((StoredGroupEntry) updatedGroup).setPackets(totalPkts);
298 ((StoredGroupEntry) updatedGroup).setBytes(totalBytes);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800299
300 InternalGroupStoreDelegate updateGroupEntryDelegate =
301 new InternalGroupStoreDelegate(currKey,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700302 updatedBuckets,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800303 GroupEvent.Type.GROUP_UPDATED);
304 simpleGroupStore.setDelegate(updateGroupEntryDelegate);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700305 simpleGroupStore.addOrUpdateGroupEntry(updatedGroup);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800306 simpleGroupStore.unsetDelegate(updateGroupEntryDelegate);
307 }
308
309 // Testing updateGroupDescription for ADD operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700310 private void testAddBuckets(GroupKey currKey, GroupKey addKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800311 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700312 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800313 buckets.addAll(existingGroup.buckets().buckets());
314
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800315 PortNumber[] newNeighborPorts = {PortNumber.portNumber(41),
316 PortNumber.portNumber(42)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700317 List<PortNumber> newOutPorts = new ArrayList<>();
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700318 newOutPorts.addAll(Collections.singletonList(newNeighborPorts[0]));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800319
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700320 List<GroupBucket> toAddBuckets = new ArrayList<>();
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800321 for (PortNumber portNumber: newOutPorts) {
322 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
323 tBuilder.setOutput(portNumber)
324 .setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
325 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
326 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100327 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800328 toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
329 tBuilder.build()));
330 }
331 GroupBuckets toAddGroupBuckets = new GroupBuckets(toAddBuckets);
332 buckets.addAll(toAddBuckets);
333 GroupBuckets updatedGroupBuckets = new GroupBuckets(buckets);
334 InternalGroupStoreDelegate updateGroupDescDelegate =
335 new InternalGroupStoreDelegate(addKey,
336 updatedGroupBuckets,
337 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
338 simpleGroupStore.setDelegate(updateGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800339 simpleGroupStore.updateGroupDescription(D1,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800340 currKey,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800341 UpdateType.ADD,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800342 toAddGroupBuckets,
343 addKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800344 simpleGroupStore.unsetDelegate(updateGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800345 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800346
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800347 // Testing updateGroupDescription for REMOVE operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700348 private void testRemoveBuckets(GroupKey currKey, GroupKey removeKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800349 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700350 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800351 buckets.addAll(existingGroup.buckets().buckets());
352
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700353 List<GroupBucket> toRemoveBuckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800354
355 // There should be 4 buckets in the current group
356 toRemoveBuckets.add(buckets.remove(0));
357 toRemoveBuckets.add(buckets.remove(1));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800358 GroupBuckets toRemoveGroupBuckets = new GroupBuckets(toRemoveBuckets);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800359
360 GroupBuckets remainingGroupBuckets = new GroupBuckets(buckets);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800361 InternalGroupStoreDelegate removeGroupDescDelegate =
362 new InternalGroupStoreDelegate(removeKey,
363 remainingGroupBuckets,
364 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
365 simpleGroupStore.setDelegate(removeGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800366 simpleGroupStore.updateGroupDescription(D1,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800367 currKey,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800368 UpdateType.REMOVE,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800369 toRemoveGroupBuckets,
370 removeKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800371 simpleGroupStore.unsetDelegate(removeGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800372 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800373
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800374 // Testing deleteGroupDescription operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700375 private void testDeleteGroup(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800376 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800377 InternalGroupStoreDelegate deleteGroupDescDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800378 new InternalGroupStoreDelegate(currKey,
379 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800380 GroupEvent.Type.GROUP_REMOVE_REQUESTED);
381 simpleGroupStore.setDelegate(deleteGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800382 simpleGroupStore.deleteGroupDescription(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800383 simpleGroupStore.unsetDelegate(deleteGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800384 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800385
Charles Chan0c7c43b2016-01-14 17:39:20 -0800386 // Testing deleteGroupDescription operation from northbound
387 private void testDeleteGroupOnDevice(GroupKey currKey) {
388 assertThat(simpleGroupStore.getGroupCount(D1), is(1));
389 simpleGroupStore.purgeGroupEntry(D1);
390 assertThat(simpleGroupStore.getGroupCount(D1), is(0));
391 }
392
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800393 // Testing removeGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700394 private void testRemoveGroupFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800395 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800396 InternalGroupStoreDelegate removeGroupEntryDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800397 new InternalGroupStoreDelegate(currKey,
398 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800399 GroupEvent.Type.GROUP_REMOVED);
400 simpleGroupStore.setDelegate(removeGroupEntryDelegate);
401 simpleGroupStore.removeGroupEntry(existingGroup);
402
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800403 // Testing getGroup operation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800404 existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800405 assertEquals(null, existingGroup);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800406 assertEquals(0, Iterables.size(simpleGroupStore.getGroups(D1)));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800407 assertEquals(0, simpleGroupStore.getGroupCount(D1));
408
409 simpleGroupStore.unsetDelegate(removeGroupEntryDelegate);
sangho7ff01812015-02-09 16:21:53 -0800410 }
411
412 @Test
413 public void testGroupOperationFailure() {
414
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800415 simpleGroupStore.deviceInitialAuditCompleted(D1, true);
sangho7ff01812015-02-09 16:21:53 -0800416
417 ApplicationId appId =
418 new DefaultApplicationId(2, "org.groupstore.test");
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700419 GroupKey key = new DefaultGroupKey("group1".getBytes());
sangho7ff01812015-02-09 16:21:53 -0800420 PortNumber[] ports = {PortNumber.portNumber(31),
421 PortNumber.portNumber(32)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700422 List<PortNumber> outPorts = new ArrayList<>();
sangho7ff01812015-02-09 16:21:53 -0800423 outPorts.add(ports[0]);
424 outPorts.add(ports[1]);
425
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700426 List<GroupBucket> buckets = new ArrayList<>();
sangho7ff01812015-02-09 16:21:53 -0800427 for (PortNumber portNumber: outPorts) {
428 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
429 tBuilder.setOutput(portNumber)
430 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
431 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
432 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100433 .setMpls(MplsLabel.mplsLabel(106));
sangho7ff01812015-02-09 16:21:53 -0800434 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
435 tBuilder.build()));
436 }
437 GroupBuckets groupBuckets = new GroupBuckets(buckets);
438 GroupDescription groupDesc = new DefaultGroupDescription(
439 D1,
440 Group.Type.SELECT,
441 groupBuckets,
442 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700443 null,
sangho7ff01812015-02-09 16:21:53 -0800444 appId);
445 InternalGroupStoreDelegate checkStoreGroupDelegate =
446 new InternalGroupStoreDelegate(key,
447 groupBuckets,
448 GroupEvent.Type.GROUP_ADD_REQUESTED);
449 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
450 // Testing storeGroup operation
451 simpleGroupStore.storeGroupDescription(groupDesc);
452 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
453
454 // Testing Group add operation failure
455 Group createdGroup = simpleGroupStore.getGroup(D1, key);
456 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
457
458 GroupOperation groupAddOp = GroupOperation.
459 createAddGroupOperation(createdGroup.id(),
460 createdGroup.type(),
461 createdGroup.buckets());
462 InternalGroupStoreDelegate checkGroupAddFailureDelegate =
463 new InternalGroupStoreDelegate(key,
464 groupBuckets,
465 GroupEvent.Type.GROUP_ADD_FAILED);
466 simpleGroupStore.setDelegate(checkGroupAddFailureDelegate);
467 simpleGroupStore.groupOperationFailed(D1, groupAddOp);
468
469
470 // Testing Group modify operation failure
471 simpleGroupStore.unsetDelegate(checkGroupAddFailureDelegate);
472 GroupOperation groupModOp = GroupOperation.
473 createModifyGroupOperation(createdGroup.id(),
474 createdGroup.type(),
475 createdGroup.buckets());
476 InternalGroupStoreDelegate checkGroupModFailureDelegate =
477 new InternalGroupStoreDelegate(key,
478 groupBuckets,
479 GroupEvent.Type.GROUP_UPDATE_FAILED);
480 simpleGroupStore.setDelegate(checkGroupModFailureDelegate);
481 simpleGroupStore.groupOperationFailed(D1, groupModOp);
482
483 // Testing Group modify operation failure
484 simpleGroupStore.unsetDelegate(checkGroupModFailureDelegate);
485 GroupOperation groupDelOp = GroupOperation.
486 createDeleteGroupOperation(createdGroup.id(),
487 createdGroup.type());
488 InternalGroupStoreDelegate checkGroupDelFailureDelegate =
489 new InternalGroupStoreDelegate(key,
490 groupBuckets,
491 GroupEvent.Type.GROUP_REMOVE_FAILED);
492 simpleGroupStore.setDelegate(checkGroupDelFailureDelegate);
493 simpleGroupStore.groupOperationFailed(D1, groupDelOp);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800494 }
495}
496