blob: 1e6f86584d712cfb474f99a8bd4aa52b4f2a01b0 [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
Victor Silva4e8b7832016-08-17 17:11:19 -0300217 // Testing removing all groups on the given device by deviceid
Charles Chan0c7c43b2016-01-14 17:39:20 -0800218 newKey = new DefaultGroupKey("group1".getBytes());
219 testStoreAndGetGroup(newKey);
220 testDeleteGroupOnDevice(newKey);
Victor Silva4e8b7832016-08-17 17:11:19 -0300221
222 // Testing removing all groups on the given device
223 newKey = new DefaultGroupKey("group1".getBytes());
224 testStoreAndGetGroup(newKey);
225 testPurgeGroupEntries();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800226 }
227
228 // Testing storeGroup operation
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700229 private void testStoreAndGetGroup(GroupKey key) {
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800230 PortNumber[] ports = {PortNumber.portNumber(31),
231 PortNumber.portNumber(32)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700232 List<PortNumber> outPorts = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800233 outPorts.addAll(Arrays.asList(ports));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800234
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700235 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800236 for (PortNumber portNumber: outPorts) {
237 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
238 tBuilder.setOutput(portNumber)
239 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
240 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
241 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100242 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800243 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
244 tBuilder.build()));
245 }
246 GroupBuckets groupBuckets = new GroupBuckets(buckets);
247 GroupDescription groupDesc = new DefaultGroupDescription(
248 D1,
249 Group.Type.SELECT,
250 groupBuckets,
251 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700252 null,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800253 appId);
254 InternalGroupStoreDelegate checkStoreGroupDelegate =
255 new InternalGroupStoreDelegate(key,
256 groupBuckets,
257 GroupEvent.Type.GROUP_ADD_REQUESTED);
258 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800259 // Testing storeGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800260 simpleGroupStore.storeGroupDescription(groupDesc);
261
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800262 // Testing getGroupCount operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800263 assertEquals(1, simpleGroupStore.getGroupCount(D1));
264
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800265 // Testing getGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800266 Group createdGroup = simpleGroupStore.getGroup(D1, key);
267 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
268
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800269 // Testing getGroups operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800270 Iterable<Group> createdGroups = simpleGroupStore.getGroups(D1);
271 int groupCount = 0;
272 for (Group group:createdGroups) {
273 checkStoreGroupDelegate.verifyGroupId(group.id());
274 groupCount++;
275 }
276 assertEquals(1, groupCount);
277 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800278 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800279
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800280 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700281 private void testAddGroupEntryFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800282 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
283
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800284 InternalGroupStoreDelegate addGroupEntryDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800285 new InternalGroupStoreDelegate(currKey,
286 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800287 GroupEvent.Type.GROUP_ADDED);
288 simpleGroupStore.setDelegate(addGroupEntryDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800289 simpleGroupStore.addOrUpdateGroupEntry(existingGroup);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800290 simpleGroupStore.unsetDelegate(addGroupEntryDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800291 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800292
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800293 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700294 private void testUpdateGroupEntryFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800295 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700296 int totalPkts = 0;
297 int totalBytes = 0;
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700298 List<GroupBucket> newBucketList = new ArrayList<>();
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700299 for (GroupBucket bucket:existingGroup.buckets().buckets()) {
300 StoredGroupBucketEntry newBucket =
301 (StoredGroupBucketEntry)
302 DefaultGroupBucket.createSelectGroupBucket(bucket.treatment());
303 newBucket.setPackets(10);
304 newBucket.setBytes(10 * 256 * 8);
305 totalPkts += 10;
306 totalBytes += 10 * 256 * 8;
307 newBucketList.add(newBucket);
308 }
309 GroupBuckets updatedBuckets = new GroupBuckets(newBucketList);
310 Group updatedGroup = new DefaultGroup(existingGroup.id(),
311 existingGroup.deviceId(),
312 existingGroup.type(),
313 updatedBuckets);
314 ((StoredGroupEntry) updatedGroup).setPackets(totalPkts);
315 ((StoredGroupEntry) updatedGroup).setBytes(totalBytes);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800316
317 InternalGroupStoreDelegate updateGroupEntryDelegate =
318 new InternalGroupStoreDelegate(currKey,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700319 updatedBuckets,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800320 GroupEvent.Type.GROUP_UPDATED);
321 simpleGroupStore.setDelegate(updateGroupEntryDelegate);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700322 simpleGroupStore.addOrUpdateGroupEntry(updatedGroup);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800323 simpleGroupStore.unsetDelegate(updateGroupEntryDelegate);
324 }
325
326 // Testing updateGroupDescription for ADD operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700327 private void testAddBuckets(GroupKey currKey, GroupKey addKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800328 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700329 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800330 buckets.addAll(existingGroup.buckets().buckets());
331
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800332 PortNumber[] newNeighborPorts = {PortNumber.portNumber(41),
333 PortNumber.portNumber(42)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700334 List<PortNumber> newOutPorts = new ArrayList<>();
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700335 newOutPorts.addAll(Collections.singletonList(newNeighborPorts[0]));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800336
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700337 List<GroupBucket> toAddBuckets = new ArrayList<>();
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800338 for (PortNumber portNumber: newOutPorts) {
339 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
340 tBuilder.setOutput(portNumber)
341 .setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
342 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
343 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100344 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800345 toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
346 tBuilder.build()));
347 }
348 GroupBuckets toAddGroupBuckets = new GroupBuckets(toAddBuckets);
349 buckets.addAll(toAddBuckets);
350 GroupBuckets updatedGroupBuckets = new GroupBuckets(buckets);
351 InternalGroupStoreDelegate updateGroupDescDelegate =
352 new InternalGroupStoreDelegate(addKey,
353 updatedGroupBuckets,
354 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
355 simpleGroupStore.setDelegate(updateGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800356 simpleGroupStore.updateGroupDescription(D1,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800357 currKey,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800358 UpdateType.ADD,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800359 toAddGroupBuckets,
360 addKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800361 simpleGroupStore.unsetDelegate(updateGroupDescDelegate);
Victor Silvadf1eeae2016-08-12 15:28:57 -0300362
363 short weight = 5;
364 toAddBuckets = new ArrayList<>();
365 for (PortNumber portNumber: newOutPorts) {
366 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
367 tBuilder.setOutput(portNumber)
368 .setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
369 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
370 .pushMpls()
371 .setMpls(MplsLabel.mplsLabel(106));
372 toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
373 tBuilder.build(), weight));
374 }
375
376 toAddGroupBuckets = new GroupBuckets(toAddBuckets);
377 buckets = new ArrayList<>();
378 buckets.addAll(existingGroup.buckets().buckets());
379 buckets.addAll(toAddBuckets);
380 updatedGroupBuckets = new GroupBuckets(buckets);
381 updateGroupDescDelegate =
382 new InternalGroupStoreDelegate(addKey,
383 updatedGroupBuckets,
384 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
385 simpleGroupStore.setDelegate(updateGroupDescDelegate);
386 simpleGroupStore.updateGroupDescription(D1,
387 addKey,
388 UpdateType.ADD,
389 toAddGroupBuckets,
390 addKey);
391 simpleGroupStore.unsetDelegate(updateGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800392 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800393
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800394 // Testing updateGroupDescription for REMOVE operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700395 private void testRemoveBuckets(GroupKey currKey, GroupKey removeKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800396 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700397 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800398 buckets.addAll(existingGroup.buckets().buckets());
399
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700400 List<GroupBucket> toRemoveBuckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800401
402 // There should be 4 buckets in the current group
403 toRemoveBuckets.add(buckets.remove(0));
404 toRemoveBuckets.add(buckets.remove(1));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800405 GroupBuckets toRemoveGroupBuckets = new GroupBuckets(toRemoveBuckets);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800406
407 GroupBuckets remainingGroupBuckets = new GroupBuckets(buckets);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800408 InternalGroupStoreDelegate removeGroupDescDelegate =
409 new InternalGroupStoreDelegate(removeKey,
410 remainingGroupBuckets,
411 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
412 simpleGroupStore.setDelegate(removeGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800413 simpleGroupStore.updateGroupDescription(D1,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800414 currKey,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800415 UpdateType.REMOVE,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800416 toRemoveGroupBuckets,
417 removeKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800418 simpleGroupStore.unsetDelegate(removeGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800419 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800420
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800421 // Testing deleteGroupDescription operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700422 private void testDeleteGroup(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800423 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800424 InternalGroupStoreDelegate deleteGroupDescDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800425 new InternalGroupStoreDelegate(currKey,
426 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800427 GroupEvent.Type.GROUP_REMOVE_REQUESTED);
428 simpleGroupStore.setDelegate(deleteGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800429 simpleGroupStore.deleteGroupDescription(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800430 simpleGroupStore.unsetDelegate(deleteGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800431 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800432
Charles Chan0c7c43b2016-01-14 17:39:20 -0800433 // Testing deleteGroupDescription operation from northbound
434 private void testDeleteGroupOnDevice(GroupKey currKey) {
435 assertThat(simpleGroupStore.getGroupCount(D1), is(1));
436 simpleGroupStore.purgeGroupEntry(D1);
437 assertThat(simpleGroupStore.getGroupCount(D1), is(0));
438 }
439
Victor Silva4e8b7832016-08-17 17:11:19 -0300440 // Testing purgeGroupEntries
441 private void testPurgeGroupEntries() {
442 assertThat(simpleGroupStore.getGroupCount(D1), is(1));
443 simpleGroupStore.purgeGroupEntries();
444 assertThat(simpleGroupStore.getGroupCount(D1), is(0));
445 }
446
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800447 // Testing removeGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700448 private void testRemoveGroupFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800449 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800450 InternalGroupStoreDelegate removeGroupEntryDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800451 new InternalGroupStoreDelegate(currKey,
452 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800453 GroupEvent.Type.GROUP_REMOVED);
454 simpleGroupStore.setDelegate(removeGroupEntryDelegate);
455 simpleGroupStore.removeGroupEntry(existingGroup);
456
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800457 // Testing getGroup operation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800458 existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800459 assertEquals(null, existingGroup);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800460 assertEquals(0, Iterables.size(simpleGroupStore.getGroups(D1)));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800461 assertEquals(0, simpleGroupStore.getGroupCount(D1));
462
463 simpleGroupStore.unsetDelegate(removeGroupEntryDelegate);
sangho7ff01812015-02-09 16:21:53 -0800464 }
465
466 @Test
467 public void testGroupOperationFailure() {
468
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800469 simpleGroupStore.deviceInitialAuditCompleted(D1, true);
sangho7ff01812015-02-09 16:21:53 -0800470
471 ApplicationId appId =
472 new DefaultApplicationId(2, "org.groupstore.test");
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700473 GroupKey key = new DefaultGroupKey("group1".getBytes());
sangho7ff01812015-02-09 16:21:53 -0800474 PortNumber[] ports = {PortNumber.portNumber(31),
475 PortNumber.portNumber(32)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700476 List<PortNumber> outPorts = new ArrayList<>();
sangho7ff01812015-02-09 16:21:53 -0800477 outPorts.add(ports[0]);
478 outPorts.add(ports[1]);
479
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700480 List<GroupBucket> buckets = new ArrayList<>();
sangho7ff01812015-02-09 16:21:53 -0800481 for (PortNumber portNumber: outPorts) {
482 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
483 tBuilder.setOutput(portNumber)
484 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
485 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
486 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100487 .setMpls(MplsLabel.mplsLabel(106));
sangho7ff01812015-02-09 16:21:53 -0800488 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
489 tBuilder.build()));
490 }
491 GroupBuckets groupBuckets = new GroupBuckets(buckets);
492 GroupDescription groupDesc = new DefaultGroupDescription(
493 D1,
494 Group.Type.SELECT,
495 groupBuckets,
496 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700497 null,
sangho7ff01812015-02-09 16:21:53 -0800498 appId);
499 InternalGroupStoreDelegate checkStoreGroupDelegate =
500 new InternalGroupStoreDelegate(key,
501 groupBuckets,
502 GroupEvent.Type.GROUP_ADD_REQUESTED);
503 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
504 // Testing storeGroup operation
505 simpleGroupStore.storeGroupDescription(groupDesc);
506 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
507
508 // Testing Group add operation failure
509 Group createdGroup = simpleGroupStore.getGroup(D1, key);
510 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
511
512 GroupOperation groupAddOp = GroupOperation.
513 createAddGroupOperation(createdGroup.id(),
514 createdGroup.type(),
515 createdGroup.buckets());
516 InternalGroupStoreDelegate checkGroupAddFailureDelegate =
517 new InternalGroupStoreDelegate(key,
518 groupBuckets,
519 GroupEvent.Type.GROUP_ADD_FAILED);
520 simpleGroupStore.setDelegate(checkGroupAddFailureDelegate);
521 simpleGroupStore.groupOperationFailed(D1, groupAddOp);
522
523
524 // Testing Group modify operation failure
525 simpleGroupStore.unsetDelegate(checkGroupAddFailureDelegate);
526 GroupOperation groupModOp = GroupOperation.
527 createModifyGroupOperation(createdGroup.id(),
528 createdGroup.type(),
529 createdGroup.buckets());
530 InternalGroupStoreDelegate checkGroupModFailureDelegate =
531 new InternalGroupStoreDelegate(key,
532 groupBuckets,
533 GroupEvent.Type.GROUP_UPDATE_FAILED);
534 simpleGroupStore.setDelegate(checkGroupModFailureDelegate);
535 simpleGroupStore.groupOperationFailed(D1, groupModOp);
536
537 // Testing Group modify operation failure
538 simpleGroupStore.unsetDelegate(checkGroupModFailureDelegate);
539 GroupOperation groupDelOp = GroupOperation.
540 createDeleteGroupOperation(createdGroup.id(),
541 createdGroup.type());
542 InternalGroupStoreDelegate checkGroupDelFailureDelegate =
543 new InternalGroupStoreDelegate(key,
544 groupBuckets,
545 GroupEvent.Type.GROUP_REMOVE_FAILED);
546 simpleGroupStore.setDelegate(checkGroupDelFailureDelegate);
547 simpleGroupStore.groupOperationFailed(D1, groupDelOp);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800548 }
549}
550