blob: 2f87a5cf487c66bc672d2c3969881ef2bf17dded [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
Victor Silva0282ab82016-11-15 16:30:27 -0300207 // Testing updateGroupDescription for SET operation from northbound
208 currKey = newKey;
209 newKey = new DefaultGroupKey("group1SetBuckets".getBytes());
210 testSetBuckets(currKey, newKey);
211
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800212 // Testing addOrUpdateGroupEntry operation from southbound
213 currKey = newKey;
214 testUpdateGroupEntryFromSB(currKey);
215
216 // Testing deleteGroupDescription operation from northbound
217 testDeleteGroup(currKey);
218
219 // Testing removeGroupEntry operation from southbound
220 testRemoveGroupFromSB(currKey);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800221
Victor Silva4e8b7832016-08-17 17:11:19 -0300222 // Testing removing all groups on the given device by deviceid
Charles Chan0c7c43b2016-01-14 17:39:20 -0800223 newKey = new DefaultGroupKey("group1".getBytes());
224 testStoreAndGetGroup(newKey);
225 testDeleteGroupOnDevice(newKey);
Victor Silva4e8b7832016-08-17 17:11:19 -0300226
227 // Testing removing all groups on the given device
228 newKey = new DefaultGroupKey("group1".getBytes());
229 testStoreAndGetGroup(newKey);
230 testPurgeGroupEntries();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800231 }
232
233 // Testing storeGroup operation
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700234 private void testStoreAndGetGroup(GroupKey key) {
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800235 PortNumber[] ports = {PortNumber.portNumber(31),
236 PortNumber.portNumber(32)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700237 List<PortNumber> outPorts = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800238 outPorts.addAll(Arrays.asList(ports));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800239
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700240 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800241 for (PortNumber portNumber: outPorts) {
242 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
243 tBuilder.setOutput(portNumber)
244 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
245 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
246 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100247 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800248 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
249 tBuilder.build()));
250 }
251 GroupBuckets groupBuckets = new GroupBuckets(buckets);
252 GroupDescription groupDesc = new DefaultGroupDescription(
253 D1,
254 Group.Type.SELECT,
255 groupBuckets,
256 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700257 null,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800258 appId);
259 InternalGroupStoreDelegate checkStoreGroupDelegate =
260 new InternalGroupStoreDelegate(key,
261 groupBuckets,
262 GroupEvent.Type.GROUP_ADD_REQUESTED);
263 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800264 // Testing storeGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800265 simpleGroupStore.storeGroupDescription(groupDesc);
266
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800267 // Testing getGroupCount operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800268 assertEquals(1, simpleGroupStore.getGroupCount(D1));
269
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800270 // Testing getGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800271 Group createdGroup = simpleGroupStore.getGroup(D1, key);
272 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
273
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800274 // Testing getGroups operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800275 Iterable<Group> createdGroups = simpleGroupStore.getGroups(D1);
276 int groupCount = 0;
277 for (Group group:createdGroups) {
278 checkStoreGroupDelegate.verifyGroupId(group.id());
279 groupCount++;
280 }
281 assertEquals(1, groupCount);
282 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800283 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800284
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800285 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700286 private void testAddGroupEntryFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800287 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
288
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800289 InternalGroupStoreDelegate addGroupEntryDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800290 new InternalGroupStoreDelegate(currKey,
291 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800292 GroupEvent.Type.GROUP_ADDED);
293 simpleGroupStore.setDelegate(addGroupEntryDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800294 simpleGroupStore.addOrUpdateGroupEntry(existingGroup);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800295 simpleGroupStore.unsetDelegate(addGroupEntryDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800296 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800297
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800298 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700299 private void testUpdateGroupEntryFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800300 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700301 int totalPkts = 0;
302 int totalBytes = 0;
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700303 List<GroupBucket> newBucketList = new ArrayList<>();
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700304 for (GroupBucket bucket:existingGroup.buckets().buckets()) {
305 StoredGroupBucketEntry newBucket =
306 (StoredGroupBucketEntry)
307 DefaultGroupBucket.createSelectGroupBucket(bucket.treatment());
308 newBucket.setPackets(10);
309 newBucket.setBytes(10 * 256 * 8);
310 totalPkts += 10;
311 totalBytes += 10 * 256 * 8;
312 newBucketList.add(newBucket);
313 }
314 GroupBuckets updatedBuckets = new GroupBuckets(newBucketList);
315 Group updatedGroup = new DefaultGroup(existingGroup.id(),
316 existingGroup.deviceId(),
317 existingGroup.type(),
318 updatedBuckets);
319 ((StoredGroupEntry) updatedGroup).setPackets(totalPkts);
320 ((StoredGroupEntry) updatedGroup).setBytes(totalBytes);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800321
322 InternalGroupStoreDelegate updateGroupEntryDelegate =
323 new InternalGroupStoreDelegate(currKey,
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700324 updatedBuckets,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800325 GroupEvent.Type.GROUP_UPDATED);
326 simpleGroupStore.setDelegate(updateGroupEntryDelegate);
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700327 simpleGroupStore.addOrUpdateGroupEntry(updatedGroup);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800328 simpleGroupStore.unsetDelegate(updateGroupEntryDelegate);
329 }
330
331 // Testing updateGroupDescription for ADD operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700332 private void testAddBuckets(GroupKey currKey, GroupKey addKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800333 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700334 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800335 buckets.addAll(existingGroup.buckets().buckets());
336
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800337 PortNumber[] newNeighborPorts = {PortNumber.portNumber(41),
338 PortNumber.portNumber(42)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700339 List<PortNumber> newOutPorts = new ArrayList<>();
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700340 newOutPorts.addAll(Collections.singletonList(newNeighborPorts[0]));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800341
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700342 List<GroupBucket> toAddBuckets = new ArrayList<>();
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800343 for (PortNumber portNumber: newOutPorts) {
344 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
345 tBuilder.setOutput(portNumber)
346 .setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
347 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
348 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100349 .setMpls(MplsLabel.mplsLabel(106));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800350 toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
351 tBuilder.build()));
352 }
353 GroupBuckets toAddGroupBuckets = new GroupBuckets(toAddBuckets);
354 buckets.addAll(toAddBuckets);
355 GroupBuckets updatedGroupBuckets = new GroupBuckets(buckets);
356 InternalGroupStoreDelegate updateGroupDescDelegate =
357 new InternalGroupStoreDelegate(addKey,
358 updatedGroupBuckets,
359 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
360 simpleGroupStore.setDelegate(updateGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800361 simpleGroupStore.updateGroupDescription(D1,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800362 currKey,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800363 UpdateType.ADD,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800364 toAddGroupBuckets,
365 addKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800366 simpleGroupStore.unsetDelegate(updateGroupDescDelegate);
Victor Silvadf1eeae2016-08-12 15:28:57 -0300367
368 short weight = 5;
369 toAddBuckets = new ArrayList<>();
370 for (PortNumber portNumber: newOutPorts) {
371 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
372 tBuilder.setOutput(portNumber)
373 .setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
374 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
375 .pushMpls()
376 .setMpls(MplsLabel.mplsLabel(106));
377 toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
378 tBuilder.build(), weight));
379 }
380
381 toAddGroupBuckets = new GroupBuckets(toAddBuckets);
382 buckets = new ArrayList<>();
383 buckets.addAll(existingGroup.buckets().buckets());
384 buckets.addAll(toAddBuckets);
385 updatedGroupBuckets = new GroupBuckets(buckets);
386 updateGroupDescDelegate =
387 new InternalGroupStoreDelegate(addKey,
388 updatedGroupBuckets,
389 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
390 simpleGroupStore.setDelegate(updateGroupDescDelegate);
391 simpleGroupStore.updateGroupDescription(D1,
392 addKey,
393 UpdateType.ADD,
394 toAddGroupBuckets,
395 addKey);
396 simpleGroupStore.unsetDelegate(updateGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800397 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800398
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800399 // Testing updateGroupDescription for REMOVE operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700400 private void testRemoveBuckets(GroupKey currKey, GroupKey removeKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800401 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700402 List<GroupBucket> buckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800403 buckets.addAll(existingGroup.buckets().buckets());
404
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700405 List<GroupBucket> toRemoveBuckets = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800406
407 // There should be 4 buckets in the current group
408 toRemoveBuckets.add(buckets.remove(0));
409 toRemoveBuckets.add(buckets.remove(1));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800410 GroupBuckets toRemoveGroupBuckets = new GroupBuckets(toRemoveBuckets);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800411
412 GroupBuckets remainingGroupBuckets = new GroupBuckets(buckets);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800413 InternalGroupStoreDelegate removeGroupDescDelegate =
414 new InternalGroupStoreDelegate(removeKey,
415 remainingGroupBuckets,
416 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
417 simpleGroupStore.setDelegate(removeGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800418 simpleGroupStore.updateGroupDescription(D1,
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800419 currKey,
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800420 UpdateType.REMOVE,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800421 toRemoveGroupBuckets,
422 removeKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800423 simpleGroupStore.unsetDelegate(removeGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800424 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800425
Victor Silva0282ab82016-11-15 16:30:27 -0300426 // Testing updateGroupDescription for SET operation from northbound
427 private void testSetBuckets(GroupKey currKey, GroupKey setKey) {
428 List<GroupBucket> toSetBuckets = new ArrayList<>();
429
430 short weight = 5;
431 PortNumber portNumber = PortNumber.portNumber(42);
432 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
433 tBuilder.setOutput(portNumber)
434 .setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
435 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
436 .pushMpls()
437 .setMpls(MplsLabel.mplsLabel(106));
438 toSetBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
439 tBuilder.build(), weight));
440
441 GroupBuckets toSetGroupBuckets = new GroupBuckets(toSetBuckets);
442 InternalGroupStoreDelegate updateGroupDescDelegate =
443 new InternalGroupStoreDelegate(setKey,
444 toSetGroupBuckets,
445 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
446 simpleGroupStore.setDelegate(updateGroupDescDelegate);
447 simpleGroupStore.updateGroupDescription(D1,
448 currKey,
449 UpdateType.SET,
450 toSetGroupBuckets,
451 setKey);
452 simpleGroupStore.unsetDelegate(updateGroupDescDelegate);
453 }
454
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800455 // Testing deleteGroupDescription operation from northbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700456 private void testDeleteGroup(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800457 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800458 InternalGroupStoreDelegate deleteGroupDescDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800459 new InternalGroupStoreDelegate(currKey,
460 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800461 GroupEvent.Type.GROUP_REMOVE_REQUESTED);
462 simpleGroupStore.setDelegate(deleteGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800463 simpleGroupStore.deleteGroupDescription(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800464 simpleGroupStore.unsetDelegate(deleteGroupDescDelegate);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800465 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800466
Charles Chan0c7c43b2016-01-14 17:39:20 -0800467 // Testing deleteGroupDescription operation from northbound
468 private void testDeleteGroupOnDevice(GroupKey currKey) {
469 assertThat(simpleGroupStore.getGroupCount(D1), is(1));
470 simpleGroupStore.purgeGroupEntry(D1);
471 assertThat(simpleGroupStore.getGroupCount(D1), is(0));
472 }
473
Victor Silva4e8b7832016-08-17 17:11:19 -0300474 // Testing purgeGroupEntries
475 private void testPurgeGroupEntries() {
476 assertThat(simpleGroupStore.getGroupCount(D1), is(1));
477 simpleGroupStore.purgeGroupEntries();
478 assertThat(simpleGroupStore.getGroupCount(D1), is(0));
479 }
480
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800481 // Testing removeGroupEntry operation from southbound
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700482 private void testRemoveGroupFromSB(GroupKey currKey) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800483 Group existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800484 InternalGroupStoreDelegate removeGroupEntryDelegate =
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800485 new InternalGroupStoreDelegate(currKey,
486 existingGroup.buckets(),
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800487 GroupEvent.Type.GROUP_REMOVED);
488 simpleGroupStore.setDelegate(removeGroupEntryDelegate);
489 simpleGroupStore.removeGroupEntry(existingGroup);
490
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800491 // Testing getGroup operation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800492 existingGroup = simpleGroupStore.getGroup(D1, currKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800493 assertEquals(null, existingGroup);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800494 assertEquals(0, Iterables.size(simpleGroupStore.getGroups(D1)));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800495 assertEquals(0, simpleGroupStore.getGroupCount(D1));
496
497 simpleGroupStore.unsetDelegate(removeGroupEntryDelegate);
sangho7ff01812015-02-09 16:21:53 -0800498 }
499
500 @Test
501 public void testGroupOperationFailure() {
502
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800503 simpleGroupStore.deviceInitialAuditCompleted(D1, true);
sangho7ff01812015-02-09 16:21:53 -0800504
505 ApplicationId appId =
506 new DefaultApplicationId(2, "org.groupstore.test");
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700507 GroupKey key = new DefaultGroupKey("group1".getBytes());
sangho7ff01812015-02-09 16:21:53 -0800508 PortNumber[] ports = {PortNumber.portNumber(31),
509 PortNumber.portNumber(32)};
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700510 List<PortNumber> outPorts = new ArrayList<>();
sangho7ff01812015-02-09 16:21:53 -0800511 outPorts.add(ports[0]);
512 outPorts.add(ports[1]);
513
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700514 List<GroupBucket> buckets = new ArrayList<>();
sangho7ff01812015-02-09 16:21:53 -0800515 for (PortNumber portNumber: outPorts) {
516 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
517 tBuilder.setOutput(portNumber)
518 .setEthDst(MacAddress.valueOf("00:00:00:00:00:02"))
519 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
520 .pushMpls()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100521 .setMpls(MplsLabel.mplsLabel(106));
sangho7ff01812015-02-09 16:21:53 -0800522 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
523 tBuilder.build()));
524 }
525 GroupBuckets groupBuckets = new GroupBuckets(buckets);
526 GroupDescription groupDesc = new DefaultGroupDescription(
527 D1,
528 Group.Type.SELECT,
529 groupBuckets,
530 key,
Saurav Das100e3b82015-04-30 11:12:10 -0700531 null,
sangho7ff01812015-02-09 16:21:53 -0800532 appId);
533 InternalGroupStoreDelegate checkStoreGroupDelegate =
534 new InternalGroupStoreDelegate(key,
535 groupBuckets,
536 GroupEvent.Type.GROUP_ADD_REQUESTED);
537 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
538 // Testing storeGroup operation
539 simpleGroupStore.storeGroupDescription(groupDesc);
540 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
541
542 // Testing Group add operation failure
543 Group createdGroup = simpleGroupStore.getGroup(D1, key);
544 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
545
546 GroupOperation groupAddOp = GroupOperation.
547 createAddGroupOperation(createdGroup.id(),
548 createdGroup.type(),
549 createdGroup.buckets());
550 InternalGroupStoreDelegate checkGroupAddFailureDelegate =
551 new InternalGroupStoreDelegate(key,
552 groupBuckets,
553 GroupEvent.Type.GROUP_ADD_FAILED);
554 simpleGroupStore.setDelegate(checkGroupAddFailureDelegate);
555 simpleGroupStore.groupOperationFailed(D1, groupAddOp);
556
557
558 // Testing Group modify operation failure
559 simpleGroupStore.unsetDelegate(checkGroupAddFailureDelegate);
560 GroupOperation groupModOp = GroupOperation.
561 createModifyGroupOperation(createdGroup.id(),
562 createdGroup.type(),
563 createdGroup.buckets());
564 InternalGroupStoreDelegate checkGroupModFailureDelegate =
565 new InternalGroupStoreDelegate(key,
566 groupBuckets,
567 GroupEvent.Type.GROUP_UPDATE_FAILED);
568 simpleGroupStore.setDelegate(checkGroupModFailureDelegate);
569 simpleGroupStore.groupOperationFailed(D1, groupModOp);
570
571 // Testing Group modify operation failure
572 simpleGroupStore.unsetDelegate(checkGroupModFailureDelegate);
573 GroupOperation groupDelOp = GroupOperation.
574 createDeleteGroupOperation(createdGroup.id(),
575 createdGroup.type());
576 InternalGroupStoreDelegate checkGroupDelFailureDelegate =
577 new InternalGroupStoreDelegate(key,
578 groupBuckets,
579 GroupEvent.Type.GROUP_REMOVE_FAILED);
580 simpleGroupStore.setDelegate(checkGroupDelFailureDelegate);
581 simpleGroupStore.groupOperationFailed(D1, groupDelOp);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800582 }
583}
584