blob: 834c8ee26042f5bc610e53d66cd942ae4de90602 [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());
Victor Silvadf1eeae2016-08-12 15:28:57 -0300133 for (GroupBucket bucket:event.subject().buckets().buckets()) {
134 Optional<GroupBucket> matched = createdBuckets.buckets()
135 .stream()
136 .filter((expected) -> expected.equals(bucket))
137 .findFirst();
138 assertEquals(matched.get().weight(),
139 bucket.weight());
140 assertEquals(matched.get().watchGroup(),
141 bucket.watchGroup());
142 assertEquals(matched.get().watchPort(),
143 bucket.watchPort());
144 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800145 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVE_REQUESTED) {
146 assertEquals(Group.GroupState.PENDING_DELETE,
147 event.subject().state());
148 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVED) {
149 createdGroupId = event.subject().id();
150 assertEquals(Group.GroupState.PENDING_DELETE,
151 event.subject().state());
sangho7ff01812015-02-09 16:21:53 -0800152 } else if (expectedEvent == GroupEvent.Type.GROUP_ADD_FAILED) {
153 createdGroupId = event.subject().id();
154 assertEquals(Group.GroupState.PENDING_ADD,
155 event.subject().state());
156 } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATE_FAILED) {
157 createdGroupId = event.subject().id();
158 assertEquals(Group.GroupState.PENDING_UPDATE,
159 event.subject().state());
160 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVE_FAILED) {
161 createdGroupId = event.subject().id();
162 assertEquals(Group.GroupState.PENDING_DELETE,
163 event.subject().state());
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800164 }
165 }
166
167 public void verifyGroupId(GroupId id) {
168 assertEquals(createdGroupId, id);
169 }
170 }
171
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800172 /**
173 * Tests group store operations. The following operations are tested:
174 * a)Tests device group audit completion status change
175 * b)Tests storeGroup operation
176 * c)Tests getGroupCount operation
177 * d)Tests getGroup operation
178 * e)Tests getGroups operation
179 * f)Tests addOrUpdateGroupEntry operation from southbound
180 * g)Tests updateGroupDescription for ADD operation from northbound
181 * h)Tests updateGroupDescription for REMOVE operation from northbound
182 * i)Tests deleteGroupDescription operation from northbound
183 * j)Tests removeGroupEntry operation from southbound
184 */
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800185 @Test
186 public void testGroupStoreOperations() {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800187 // Set the Device AUDIT completed in the store
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800188 simpleGroupStore.deviceInitialAuditCompleted(D1, true);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800189
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800190 // Testing storeGroup operation
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700191 GroupKey newKey = new DefaultGroupKey("group1".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800192 testStoreAndGetGroup(newKey);
193
194 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700195 GroupKey currKey = newKey;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800196 testAddGroupEntryFromSB(currKey);
197
198 // Testing updateGroupDescription for ADD operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700199 newKey = new DefaultGroupKey("group1AddBuckets".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800200 testAddBuckets(currKey, newKey);
201
202 // Testing updateGroupDescription for REMOVE operation from northbound
203 currKey = newKey;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700204 newKey = new DefaultGroupKey("group1RemoveBuckets".getBytes());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800205 testRemoveBuckets(currKey, newKey);
206
207 // Testing addOrUpdateGroupEntry operation from southbound
208 currKey = newKey;
209 testUpdateGroupEntryFromSB(currKey);
210
211 // Testing deleteGroupDescription operation from northbound
212 testDeleteGroup(currKey);
213
214 // Testing removeGroupEntry operation from southbound
215 testRemoveGroupFromSB(currKey);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800216
217 // Testing removing all groups on the given device
218 newKey = new DefaultGroupKey("group1".getBytes());
219 testStoreAndGetGroup(newKey);
220 testDeleteGroupOnDevice(newKey);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800221 }
222
223 // Testing storeGroup operation
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700224 private void testStoreAndGetGroup(GroupKey key) {
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800225 PortNumber[] ports = {PortNumber.portNumber(31),
226 PortNumber.portNumber(32)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700227 List<PortNumber> outPorts = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800228 outPorts.addAll(Arrays.asList(ports));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800229
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700230 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800231 for (PortNumber portNumber: outPorts) {
232 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
233 tBuilder.setOutput(portNumber)
234 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
235 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
236 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100237 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800238 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
239 tBuilder.build()));
240 }
241 GroupBuckets groupBuckets = new GroupBuckets(buckets);
242 GroupDescription groupDesc = new DefaultGroupDescription(
243 D1,
244 Group.Type.SELECT,
245 groupBuckets,
246 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700247 null,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800248 appId);
249 InternalGroupStoreDelegate checkStoreGroupDelegate =
250 new InternalGroupStoreDelegate(key,
251 groupBuckets,
252 GroupEvent.Type.GROUP_ADD_REQUESTED);
253 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800254 // Testing storeGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800255 simpleGroupStore.storeGroupDescription(groupDesc);
256
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800257 // Testing getGroupCount operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800258 assertEquals(1, simpleGroupStore.getGroupCount(D1));
259
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800260 // Testing getGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800261 Group createdGroup = simpleGroupStore.getGroup(D1, key);
262 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
263
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800264 // Testing getGroups operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800265 Iterable<Group> createdGroups = simpleGroupStore.getGroups(D1);
266 int groupCount = 0;
267 for (Group group:createdGroups) {
268 checkStoreGroupDelegate.verifyGroupId(group.id());
269 groupCount++;
270 }
271 assertEquals(1, groupCount);
272 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800273 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800274
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800275 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700276 private void testAddGroupEntryFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800277 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
278
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800279 InternalGroupStoreDelegate addGroupEntryDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800280 new InternalGroupStoreDelegate(currKey,
281 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800282 GroupEvent.Type.GROUP_ADDED);
283 simpleGroupStore.setDelegate(addGroupEntryDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800284 simpleGroupStore.addOrUpdateGroupEntry(existingGroup);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800285 simpleGroupStore.unsetDelegate(addGroupEntryDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800286 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800287
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800288 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700289 private void testUpdateGroupEntryFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800290 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700291 int totalPkts = 0;
292 int totalBytes = 0;
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700293 List<GroupBucket> newBucketList = new ArrayList<>();
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700294 for (GroupBucket bucket:existingGroup.buckets().buckets()) {
295 StoredGroupBucketEntry newBucket =
296 (StoredGroupBucketEntry)
297 DefaultGroupBucket.createSelectGroupBucket(bucket.treatment());
298 newBucket.setPackets(10);
299 newBucket.setBytes(10 * 256 * 8);
300 totalPkts += 10;
301 totalBytes += 10 * 256 * 8;
302 newBucketList.add(newBucket);
303 }
304 GroupBuckets updatedBuckets = new GroupBuckets(newBucketList);
305 Group updatedGroup = new DefaultGroup(existingGroup.id(),
306 existingGroup.deviceId(),
307 existingGroup.type(),
308 updatedBuckets);
309 ((StoredGroupEntry) updatedGroup).setPackets(totalPkts);
310 ((StoredGroupEntry) updatedGroup).setBytes(totalBytes);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800311
312 InternalGroupStoreDelegate updateGroupEntryDelegate =
313 new InternalGroupStoreDelegate(currKey,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700314 updatedBuckets,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800315 GroupEvent.Type.GROUP_UPDATED);
316 simpleGroupStore.setDelegate(updateGroupEntryDelegate);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700317 simpleGroupStore.addOrUpdateGroupEntry(updatedGroup);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800318 simpleGroupStore.unsetDelegate(updateGroupEntryDelegate);
319 }
320
321 // Testing updateGroupDescription for ADD operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700322 private void testAddBuckets(GroupKey currKey, GroupKey addKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800323 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700324 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800325 buckets.addAll(existingGroup.buckets().buckets());
326
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800327 PortNumber[] newNeighborPorts = {PortNumber.portNumber(41),
328 PortNumber.portNumber(42)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700329 List<PortNumber> newOutPorts = new ArrayList<>();
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700330 newOutPorts.addAll(Collections.singletonList(newNeighborPorts[0]));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800331
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700332 List<GroupBucket> toAddBuckets = new ArrayList<>();
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800333 for (PortNumber portNumber: newOutPorts) {
334 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
335 tBuilder.setOutput(portNumber)
336 .setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
337 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
338 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100339 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800340 toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
341 tBuilder.build()));
342 }
343 GroupBuckets toAddGroupBuckets = new GroupBuckets(toAddBuckets);
344 buckets.addAll(toAddBuckets);
345 GroupBuckets updatedGroupBuckets = new GroupBuckets(buckets);
346 InternalGroupStoreDelegate updateGroupDescDelegate =
347 new InternalGroupStoreDelegate(addKey,
348 updatedGroupBuckets,
349 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
350 simpleGroupStore.setDelegate(updateGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800351 simpleGroupStore.updateGroupDescription(D1,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800352 currKey,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800353 UpdateType.ADD,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800354 toAddGroupBuckets,
355 addKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800356 simpleGroupStore.unsetDelegate(updateGroupDescDelegate);
Victor Silvadf1eeae2016-08-12 15:28:57 -0300357
358 short weight = 5;
359 toAddBuckets = new ArrayList<>();
360 for (PortNumber portNumber: newOutPorts) {
361 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
362 tBuilder.setOutput(portNumber)
363 .setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
364 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
365 .pushMpls()
366 .setMpls(MplsLabel.mplsLabel(106));
367 toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
368 tBuilder.build(), weight));
369 }
370
371 toAddGroupBuckets = new GroupBuckets(toAddBuckets);
372 buckets = new ArrayList<>();
373 buckets.addAll(existingGroup.buckets().buckets());
374 buckets.addAll(toAddBuckets);
375 updatedGroupBuckets = new GroupBuckets(buckets);
376 updateGroupDescDelegate =
377 new InternalGroupStoreDelegate(addKey,
378 updatedGroupBuckets,
379 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
380 simpleGroupStore.setDelegate(updateGroupDescDelegate);
381 simpleGroupStore.updateGroupDescription(D1,
382 addKey,
383 UpdateType.ADD,
384 toAddGroupBuckets,
385 addKey);
386 simpleGroupStore.unsetDelegate(updateGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800387 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800388
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800389 // Testing updateGroupDescription for REMOVE operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700390 private void testRemoveBuckets(GroupKey currKey, GroupKey removeKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800391 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700392 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800393 buckets.addAll(existingGroup.buckets().buckets());
394
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700395 List<GroupBucket> toRemoveBuckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800396
397 // There should be 4 buckets in the current group
398 toRemoveBuckets.add(buckets.remove(0));
399 toRemoveBuckets.add(buckets.remove(1));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800400 GroupBuckets toRemoveGroupBuckets = new GroupBuckets(toRemoveBuckets);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800401
402 GroupBuckets remainingGroupBuckets = new GroupBuckets(buckets);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800403 InternalGroupStoreDelegate removeGroupDescDelegate =
404 new InternalGroupStoreDelegate(removeKey,
405 remainingGroupBuckets,
406 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
407 simpleGroupStore.setDelegate(removeGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800408 simpleGroupStore.updateGroupDescription(D1,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800409 currKey,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800410 UpdateType.REMOVE,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800411 toRemoveGroupBuckets,
412 removeKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800413 simpleGroupStore.unsetDelegate(removeGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800414 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800415
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800416 // Testing deleteGroupDescription operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700417 private void testDeleteGroup(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800418 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800419 InternalGroupStoreDelegate deleteGroupDescDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800420 new InternalGroupStoreDelegate(currKey,
421 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800422 GroupEvent.Type.GROUP_REMOVE_REQUESTED);
423 simpleGroupStore.setDelegate(deleteGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800424 simpleGroupStore.deleteGroupDescription(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800425 simpleGroupStore.unsetDelegate(deleteGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800426 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800427
Charles Chan0c7c43b2016-01-14 17:39:20 -0800428 // Testing deleteGroupDescription operation from northbound
429 private void testDeleteGroupOnDevice(GroupKey currKey) {
430 assertThat(simpleGroupStore.getGroupCount(D1), is(1));
431 simpleGroupStore.purgeGroupEntry(D1);
432 assertThat(simpleGroupStore.getGroupCount(D1), is(0));
433 }
434
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800435 // Testing removeGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700436 private void testRemoveGroupFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800437 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800438 InternalGroupStoreDelegate removeGroupEntryDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800439 new InternalGroupStoreDelegate(currKey,
440 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800441 GroupEvent.Type.GROUP_REMOVED);
442 simpleGroupStore.setDelegate(removeGroupEntryDelegate);
443 simpleGroupStore.removeGroupEntry(existingGroup);
444
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800445 // Testing getGroup operation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800446 existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800447 assertEquals(null, existingGroup);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800448 assertEquals(0, Iterables.size(simpleGroupStore.getGroups(D1)));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800449 assertEquals(0, simpleGroupStore.getGroupCount(D1));
450
451 simpleGroupStore.unsetDelegate(removeGroupEntryDelegate);
sangho7ff01812015-02-09 16:21:53 -0800452 }
453
454 @Test
455 public void testGroupOperationFailure() {
456
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800457 simpleGroupStore.deviceInitialAuditCompleted(D1, true);
sangho7ff01812015-02-09 16:21:53 -0800458
459 ApplicationId appId =
460 new DefaultApplicationId(2, "org.groupstore.test");
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700461 GroupKey key = new DefaultGroupKey("group1".getBytes());
sangho7ff01812015-02-09 16:21:53 -0800462 PortNumber[] ports = {PortNumber.portNumber(31),
463 PortNumber.portNumber(32)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700464 List<PortNumber> outPorts = new ArrayList<>();
sangho7ff01812015-02-09 16:21:53 -0800465 outPorts.add(ports[0]);
466 outPorts.add(ports[1]);
467
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700468 List<GroupBucket> buckets = new ArrayList<>();
sangho7ff01812015-02-09 16:21:53 -0800469 for (PortNumber portNumber: outPorts) {
470 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
471 tBuilder.setOutput(portNumber)
472 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
473 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
474 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100475 .setMpls(MplsLabel.mplsLabel(106));
sangho7ff01812015-02-09 16:21:53 -0800476 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
477 tBuilder.build()));
478 }
479 GroupBuckets groupBuckets = new GroupBuckets(buckets);
480 GroupDescription groupDesc = new DefaultGroupDescription(
481 D1,
482 Group.Type.SELECT,
483 groupBuckets,
484 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700485 null,
sangho7ff01812015-02-09 16:21:53 -0800486 appId);
487 InternalGroupStoreDelegate checkStoreGroupDelegate =
488 new InternalGroupStoreDelegate(key,
489 groupBuckets,
490 GroupEvent.Type.GROUP_ADD_REQUESTED);
491 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
492 // Testing storeGroup operation
493 simpleGroupStore.storeGroupDescription(groupDesc);
494 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
495
496 // Testing Group add operation failure
497 Group createdGroup = simpleGroupStore.getGroup(D1, key);
498 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
499
500 GroupOperation groupAddOp = GroupOperation.
501 createAddGroupOperation(createdGroup.id(),
502 createdGroup.type(),
503 createdGroup.buckets());
504 InternalGroupStoreDelegate checkGroupAddFailureDelegate =
505 new InternalGroupStoreDelegate(key,
506 groupBuckets,
507 GroupEvent.Type.GROUP_ADD_FAILED);
508 simpleGroupStore.setDelegate(checkGroupAddFailureDelegate);
509 simpleGroupStore.groupOperationFailed(D1, groupAddOp);
510
511
512 // Testing Group modify operation failure
513 simpleGroupStore.unsetDelegate(checkGroupAddFailureDelegate);
514 GroupOperation groupModOp = GroupOperation.
515 createModifyGroupOperation(createdGroup.id(),
516 createdGroup.type(),
517 createdGroup.buckets());
518 InternalGroupStoreDelegate checkGroupModFailureDelegate =
519 new InternalGroupStoreDelegate(key,
520 groupBuckets,
521 GroupEvent.Type.GROUP_UPDATE_FAILED);
522 simpleGroupStore.setDelegate(checkGroupModFailureDelegate);
523 simpleGroupStore.groupOperationFailed(D1, groupModOp);
524
525 // Testing Group modify operation failure
526 simpleGroupStore.unsetDelegate(checkGroupModFailureDelegate);
527 GroupOperation groupDelOp = GroupOperation.
528 createDeleteGroupOperation(createdGroup.id(),
529 createdGroup.type());
530 InternalGroupStoreDelegate checkGroupDelFailureDelegate =
531 new InternalGroupStoreDelegate(key,
532 groupBuckets,
533 GroupEvent.Type.GROUP_REMOVE_FAILED);
534 simpleGroupStore.setDelegate(checkGroupDelFailureDelegate);
535 simpleGroupStore.groupOperationFailed(D1, groupDelOp);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800536 }
537}
538