blob: 0a443591841a8d35735076e5ba395bb677d5672e [file] [log] [blame]
Ray Milkeyb3c5ce22015-08-10 09:07:36 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyb3c5ce22015-08-10 09:07:36 -07003 *
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 */
16package org.onosproject.store.group.impl;
17
Sivachidambaram Subramanian9f816de2017-06-13 07:16:54 +053018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
20import com.google.common.testing.EqualsTester;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070021import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.junit.TestUtils;
alshabibb0285992016-03-28 23:30:37 -070025import org.onosproject.cfg.ComponentConfigAdapter;
Charles Chanf4838a72015-12-07 18:13:45 -080026import org.onosproject.cluster.NodeId;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070027import org.onosproject.core.GroupId;
28import org.onosproject.mastership.MastershipServiceAdapter;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.MastershipRole;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.group.DefaultGroup;
35import org.onosproject.net.group.DefaultGroupBucket;
36import org.onosproject.net.group.DefaultGroupDescription;
37import org.onosproject.net.group.DefaultGroupKey;
38import org.onosproject.net.group.Group;
Saurav Dasf1027d42018-06-11 17:02:31 -070039import org.onosproject.net.group.Group.GroupState;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070040import org.onosproject.net.group.GroupBucket;
41import org.onosproject.net.group.GroupBuckets;
42import org.onosproject.net.group.GroupDescription;
43import org.onosproject.net.group.GroupEvent;
44import org.onosproject.net.group.GroupKey;
45import org.onosproject.net.group.GroupOperation;
46import org.onosproject.net.group.GroupStore;
47import org.onosproject.net.group.GroupStoreDelegate;
Saurav Dasf1027d42018-06-11 17:02:31 -070048import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070049import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
Madan Jampani0b847532016-03-03 13:44:15 -080050import org.onosproject.store.service.ConsistentMap;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070051import org.onosproject.store.service.TestStorageService;
52
Sivachidambaram Subramanian9f816de2017-06-13 07:16:54 +053053import java.util.ArrayList;
54import java.util.LinkedList;
55import java.util.List;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070056
57import static org.hamcrest.MatcherAssert.assertThat;
58import static org.hamcrest.Matchers.hasSize;
59import static org.hamcrest.Matchers.instanceOf;
60import static org.hamcrest.Matchers.is;
61import static org.hamcrest.Matchers.notNullValue;
62import static org.hamcrest.Matchers.nullValue;
Victor Silvadf1eeae2016-08-12 15:28:57 -030063import static org.junit.Assert.assertEquals;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070064import static org.onosproject.net.NetTestTools.APP_ID;
65import static org.onosproject.net.NetTestTools.did;
Sivachidambaram Subramanian9f816de2017-06-13 07:16:54 +053066import static org.onosproject.net.group.GroupDescription.Type.ALL;
67import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
68import static org.onosproject.net.group.GroupDescription.Type.SELECT;
69import static org.onosproject.net.group.GroupStore.UpdateType.ADD;
70import static org.onosproject.net.group.GroupStore.UpdateType.SET;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070071/**
72 * Distributed group store test.
73 */
74public class DistributedGroupStoreTest {
75
76 DeviceId deviceId1 = did("dev1");
77 DeviceId deviceId2 = did("dev2");
Yi Tsengfa394de2017-02-01 11:26:40 -080078 GroupId groupId1 = new GroupId(1);
79 GroupId groupId2 = new GroupId(2);
80 GroupId groupId3 = new GroupId(3);
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070081 GroupKey groupKey1 = new DefaultGroupKey("abc".getBytes());
82 GroupKey groupKey2 = new DefaultGroupKey("def".getBytes());
Charles Chan0c7c43b2016-01-14 17:39:20 -080083 GroupKey groupKey3 = new DefaultGroupKey("ghi".getBytes());
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070084
85 TrafficTreatment treatment =
86 DefaultTrafficTreatment.emptyTreatment();
87 GroupBucket selectGroupBucket =
88 DefaultGroupBucket.createSelectGroupBucket(treatment);
89 GroupBucket failoverGroupBucket =
90 DefaultGroupBucket.createFailoverGroupBucket(treatment,
91 PortNumber.IN_PORT, groupId1);
92
93 GroupBuckets buckets = new GroupBuckets(ImmutableList.of(selectGroupBucket));
94 GroupDescription groupDescription1 = new DefaultGroupDescription(
95 deviceId1,
Jayasree Ghosh2d459852016-07-02 19:06:52 +053096 ALL,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070097 buckets,
98 groupKey1,
99 groupId1.id(),
100 APP_ID);
101 GroupDescription groupDescription2 = new DefaultGroupDescription(
102 deviceId2,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530103 INDIRECT,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700104 buckets,
105 groupKey2,
106 groupId2.id(),
107 APP_ID);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800108 GroupDescription groupDescription3 = new DefaultGroupDescription(
109 deviceId2,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530110 INDIRECT,
Charles Chan0c7c43b2016-01-14 17:39:20 -0800111 buckets,
112 groupKey3,
113 groupId3.id(),
114 APP_ID);
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700115
116 DistributedGroupStore groupStoreImpl;
117 GroupStore groupStore;
Madan Jampani0b847532016-03-03 13:44:15 -0800118 ConsistentMap auditPendingReqQueue;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700119
120 static class MasterOfAll extends MastershipServiceAdapter {
121 @Override
122 public MastershipRole getLocalRole(DeviceId deviceId) {
123 return MastershipRole.MASTER;
124 }
Charles Chanf4838a72015-12-07 18:13:45 -0800125
126 @Override
127 public NodeId getMasterFor(DeviceId deviceId) {
128 return new NodeId("foo");
129 }
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700130 }
131
Sivachidambaram Subramanian9f816de2017-06-13 07:16:54 +0530132 static class MasterNull extends MastershipServiceAdapter {
133 @Override
134 public MastershipRole getLocalRole(DeviceId deviceId) {
135 return null;
136 }
137
138 @Override
139 public NodeId getMasterFor(DeviceId deviceId) {
140 return null;
141 }
142 }
143
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700144 @Before
145 public void setUp() throws Exception {
146 groupStoreImpl = new DistributedGroupStore();
147 groupStoreImpl.storageService = new TestStorageService();
148 groupStoreImpl.clusterCommunicator = new ClusterCommunicationServiceAdapter();
149 groupStoreImpl.mastershipService = new MasterOfAll();
alshabibb0285992016-03-28 23:30:37 -0700150 groupStoreImpl.cfgService = new ComponentConfigAdapter();
sisubram4beea652017-08-09 10:38:14 +0000151 groupStoreImpl.activate(null);
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700152 groupStore = groupStoreImpl;
153 auditPendingReqQueue =
154 TestUtils.getField(groupStoreImpl, "auditPendingReqQueue");
155 }
156
157 @After
158 public void tearDown() throws Exception {
159 groupStoreImpl.deactivate();
160 }
161
162 /**
163 * Tests the initial state of the store.
164 */
165 @Test
166 public void testEmptyStore() {
167 assertThat(groupStore.getGroupCount(deviceId1), is(0));
168 assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue());
169 assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue());
170 }
171
172 /**
173 * Tests adding a pending group.
174 */
175 @Test
176 public void testAddPendingGroup() throws Exception {
177 // Make sure the pending list starts out empty
178 assertThat(auditPendingReqQueue.size(), is(0));
179
180 // Add a new pending group. Make sure that the store remains empty
181 groupStore.storeGroupDescription(groupDescription1);
182 assertThat(groupStore.getGroupCount(deviceId1), is(0));
183 assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue());
184 assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue());
185
186 // Make sure the group is pending
187 assertThat(auditPendingReqQueue.size(), is(1));
188
189 groupStore.deviceInitialAuditCompleted(deviceId1, true);
190
191 // Make sure the group isn't pending anymore
192 assertThat(auditPendingReqQueue.size(), is(0));
193 }
194
195
196 /**
197 * Tests adding and removing a group.
198 */
199 @Test
200 public void testAddRemoveGroup() throws Exception {
201 groupStore.deviceInitialAuditCompleted(deviceId1, true);
202 assertThat(groupStore.deviceInitialAuditStatus(deviceId1), is(true));
203
204 // Make sure the pending list starts out empty
205 assertThat(auditPendingReqQueue.size(), is(0));
206
207 groupStore.storeGroupDescription(groupDescription1);
208 assertThat(groupStore.getGroupCount(deviceId1), is(1));
209 assertThat(groupStore.getGroup(deviceId1, groupId1), notNullValue());
210 assertThat(groupStore.getGroup(deviceId1, groupKey1), notNullValue());
211
212 // Make sure that nothing is pending
213 assertThat(auditPendingReqQueue.size(), is(0));
214
215 Group groupById = groupStore.getGroup(deviceId1, groupId1);
216 Group groupByKey = groupStore.getGroup(deviceId1, groupKey1);
217 assertThat(groupById, notNullValue());
218 assertThat(groupByKey, notNullValue());
219 assertThat(groupById, is(groupByKey));
220 assertThat(groupById.deviceId(), is(did("dev1")));
221
222 groupStore.removeGroupEntry(groupById);
223
224 assertThat(groupStore.getGroupCount(deviceId1), is(0));
225 assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue());
226 assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue());
227
228 // Make sure that nothing is pending
229 assertThat(auditPendingReqQueue.size(), is(0));
230 }
231
232 /**
Charles Chan0c7c43b2016-01-14 17:39:20 -0800233 * Tests removing all groups on the given device.
234 */
235 @Test
236 public void testRemoveGroupOnDevice() throws Exception {
237 groupStore.deviceInitialAuditCompleted(deviceId1, true);
238 assertThat(groupStore.deviceInitialAuditStatus(deviceId1), is(true));
239 groupStore.deviceInitialAuditCompleted(deviceId2, true);
240 assertThat(groupStore.deviceInitialAuditStatus(deviceId2), is(true));
241
242 // Make sure the pending list starts out empty
243 assertThat(auditPendingReqQueue.size(), is(0));
244
245 groupStore.storeGroupDescription(groupDescription1);
246 groupStore.storeGroupDescription(groupDescription2);
247 groupStore.storeGroupDescription(groupDescription3);
248 assertThat(groupStore.getGroupCount(deviceId1), is(1));
249 assertThat(groupStore.getGroupCount(deviceId2), is(2));
250
251 groupStore.purgeGroupEntry(deviceId2);
252 assertThat(groupStore.getGroupCount(deviceId1), is(1));
253 assertThat(groupStore.getGroupCount(deviceId2), is(0));
Victor Silva4e8b7832016-08-17 17:11:19 -0300254
255 groupStore.purgeGroupEntries();
256 assertThat(groupStore.getGroupCount(deviceId1), is(0));
257 assertThat(groupStore.getGroupCount(deviceId2), is(0));
Charles Chan0c7c43b2016-01-14 17:39:20 -0800258 }
259
260 /**
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700261 * Tests adding and removing a group.
262 */
263 @Test
264 public void testRemoveGroupDescription() throws Exception {
265 groupStore.deviceInitialAuditCompleted(deviceId1, true);
266
267 groupStore.storeGroupDescription(groupDescription1);
268
269 groupStore.deleteGroupDescription(deviceId1, groupKey1);
270
271 // Group should still be there, marked for removal
272 assertThat(groupStore.getGroupCount(deviceId1), is(1));
273 Group queriedGroup = groupStore.getGroup(deviceId1, groupId1);
274 assertThat(queriedGroup.state(), is(Group.GroupState.PENDING_DELETE));
275
276 }
277
278 /**
279 * Tests pushing group metrics.
280 */
281 @Test
282 public void testPushGroupMetrics() {
283 groupStore.deviceInitialAuditCompleted(deviceId1, true);
284 groupStore.deviceInitialAuditCompleted(deviceId2, true);
285
286 GroupDescription groupDescription3 = new DefaultGroupDescription(
287 deviceId1,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530288 SELECT,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700289 buckets,
290 new DefaultGroupKey("aaa".getBytes()),
291 null,
292 APP_ID);
293
294 groupStore.storeGroupDescription(groupDescription1);
295 groupStore.storeGroupDescription(groupDescription2);
296 groupStore.storeGroupDescription(groupDescription3);
297 Group group1 = groupStore.getGroup(deviceId1, groupId1);
298
299 assertThat(group1, instanceOf(DefaultGroup.class));
300 DefaultGroup defaultGroup1 = (DefaultGroup) group1;
301 defaultGroup1.setPackets(55L);
302 defaultGroup1.setBytes(66L);
303 groupStore.pushGroupMetrics(deviceId1, ImmutableList.of(group1));
304
305 // Make sure the group was updated.
306
307 Group requeryGroup1 = groupStore.getGroup(deviceId1, groupId1);
308 assertThat(requeryGroup1.packets(), is(55L));
309 assertThat(requeryGroup1.bytes(), is(66L));
310
311 }
312
313 class TestDelegate implements GroupStoreDelegate {
314 private List<GroupEvent> eventsSeen = new LinkedList<>();
315 @Override
316 public void notify(GroupEvent event) {
317 eventsSeen.add(event);
318 }
319
320 public List<GroupEvent> eventsSeen() {
321 return eventsSeen;
322 }
323
324 public void resetEvents() {
325 eventsSeen.clear();
326 }
327 }
328
329 /**
330 * Tests group operation failed interface.
331 */
332 @Test
333 public void testGroupOperationFailed() {
334 TestDelegate delegate = new TestDelegate();
335 groupStore.setDelegate(delegate);
336 groupStore.deviceInitialAuditCompleted(deviceId1, true);
337 groupStore.deviceInitialAuditCompleted(deviceId2, true);
338
339 groupStore.storeGroupDescription(groupDescription1);
340 groupStore.storeGroupDescription(groupDescription2);
341
342 List<GroupEvent> eventsAfterAdds = delegate.eventsSeen();
343 assertThat(eventsAfterAdds, hasSize(2));
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700344 eventsAfterAdds.forEach(event -> assertThat(event.type(), is(GroupEvent.Type.GROUP_ADD_REQUESTED)));
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700345 delegate.resetEvents();
346
347 GroupOperation opAdd =
348 GroupOperation.createAddGroupOperation(groupId1,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530349 INDIRECT,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700350 buckets);
351 groupStore.groupOperationFailed(deviceId1, opAdd);
352
353 List<GroupEvent> eventsAfterAddFailed = delegate.eventsSeen();
354 assertThat(eventsAfterAddFailed, hasSize(2));
355 assertThat(eventsAfterAddFailed.get(0).type(),
356 is(GroupEvent.Type.GROUP_ADD_FAILED));
357 assertThat(eventsAfterAddFailed.get(1).type(),
358 is(GroupEvent.Type.GROUP_REMOVED));
359 delegate.resetEvents();
360
361 GroupOperation opModify =
362 GroupOperation.createModifyGroupOperation(groupId2,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530363 INDIRECT,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700364 buckets);
365 groupStore.groupOperationFailed(deviceId2, opModify);
366 List<GroupEvent> eventsAfterModifyFailed = delegate.eventsSeen();
367 assertThat(eventsAfterModifyFailed, hasSize(1));
368 assertThat(eventsAfterModifyFailed.get(0).type(),
369 is(GroupEvent.Type.GROUP_UPDATE_FAILED));
370 delegate.resetEvents();
371
372 GroupOperation opDelete =
373 GroupOperation.createDeleteGroupOperation(groupId2,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530374 INDIRECT);
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700375 groupStore.groupOperationFailed(deviceId2, opDelete);
376 List<GroupEvent> eventsAfterDeleteFailed = delegate.eventsSeen();
377 assertThat(eventsAfterDeleteFailed, hasSize(1));
378 assertThat(eventsAfterDeleteFailed.get(0).type(),
379 is(GroupEvent.Type.GROUP_REMOVE_FAILED));
380 delegate.resetEvents();
381 }
382
383 /**
Saurav Dasf1027d42018-06-11 17:02:31 -0700384 * Tests group operation failed interface, with error codes for failures.
385 */
386 @Test
387 public void testGroupOperationFailedWithErrorCode() {
388 TestDelegate delegate = new TestDelegate();
389 groupStore.setDelegate(delegate);
390 groupStore.deviceInitialAuditCompleted(deviceId1, true);
391 groupStore.storeGroupDescription(groupDescription1);
392 groupStore.deviceInitialAuditCompleted(deviceId2, true);
393 groupStore.storeGroupDescription(groupDescription2);
394
395 List<GroupEvent> eventsAfterAdds = delegate.eventsSeen();
396 assertThat(eventsAfterAdds, hasSize(2));
397 eventsAfterAdds.forEach(event -> assertThat(event
398 .type(), is(GroupEvent.Type.GROUP_ADD_REQUESTED)));
399 delegate.resetEvents();
400
401 // test group exists
402 GroupOperation opAdd = GroupOperation
403 .createAddGroupOperation(groupId1, INDIRECT, buckets);
404 GroupOperation addFailedExists = GroupOperation
405 .createFailedGroupOperation(opAdd, GroupMsgErrorCode.GROUP_EXISTS);
406 groupStore.groupOperationFailed(deviceId1, addFailedExists);
407
408 List<GroupEvent> eventsAfterAddFailed = delegate.eventsSeen();
409 assertThat(eventsAfterAddFailed, hasSize(2));
410 assertThat(eventsAfterAddFailed.get(0).type(),
411 is(GroupEvent.Type.GROUP_ADDED));
412 assertThat(eventsAfterAddFailed.get(1).type(),
413 is(GroupEvent.Type.GROUP_ADDED));
414 Group g1 = groupStore.getGroup(deviceId1, groupId1);
415 assertEquals(0, g1.failedRetryCount());
416 delegate.resetEvents();
417
418 // test invalid group
419 Group g2 = groupStore.getGroup(deviceId2, groupId2);
420 assertEquals(0, g2.failedRetryCount());
421 assertEquals(GroupState.PENDING_ADD, g2.state());
422 GroupOperation opAdd1 = GroupOperation
423 .createAddGroupOperation(groupId2, INDIRECT, buckets);
424 GroupOperation addFailedInvalid = GroupOperation
425 .createFailedGroupOperation(opAdd1, GroupMsgErrorCode.INVALID_GROUP);
426
427 groupStore.groupOperationFailed(deviceId2, addFailedInvalid);
428 groupStore.pushGroupMetrics(deviceId2, ImmutableList.of());
429 List<GroupEvent> eventsAfterAddFailed1 = delegate.eventsSeen();
430 assertThat(eventsAfterAddFailed1, hasSize(1));
431 assertThat(eventsAfterAddFailed.get(0).type(),
432 is(GroupEvent.Type.GROUP_ADD_REQUESTED));
433 g2 = groupStore.getGroup(deviceId2, groupId2);
434 assertEquals(1, g2.failedRetryCount());
435 assertEquals(GroupState.PENDING_ADD_RETRY, g2.state());
436 delegate.resetEvents();
437
438 groupStore.groupOperationFailed(deviceId2, addFailedInvalid);
439 groupStore.pushGroupMetrics(deviceId2, ImmutableList.of());
440 List<GroupEvent> eventsAfterAddFailed2 = delegate.eventsSeen();
441 assertThat(eventsAfterAddFailed2, hasSize(1));
442 assertThat(eventsAfterAddFailed.get(0).type(),
443 is(GroupEvent.Type.GROUP_ADD_REQUESTED));
444 g2 = groupStore.getGroup(deviceId2, groupId2);
445 assertEquals(2, g2.failedRetryCount());
446 assertEquals(GroupState.PENDING_ADD_RETRY, g2.state());
447 delegate.resetEvents();
448
449 groupStore.groupOperationFailed(deviceId2, addFailedInvalid);
450 groupStore.pushGroupMetrics(deviceId2, ImmutableList.of());
451 List<GroupEvent> eventsAfterAddFailed3 = delegate.eventsSeen();
452 assertThat(eventsAfterAddFailed3, hasSize(2));
453 assertThat(eventsAfterAddFailed.get(0).type(),
454 is(GroupEvent.Type.GROUP_ADD_FAILED));
455 assertThat(eventsAfterAddFailed.get(1).type(),
456 is(GroupEvent.Type.GROUP_REMOVED));
457 g2 = groupStore.getGroup(deviceId2, groupId2);
458 assertEquals(null, g2);
459 delegate.resetEvents();
460 }
461
462 /**
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700463 * Tests extraneous group operations.
464 */
465 @Test
466 public void testExtraneousOperations() {
467 ArrayList<Group> extraneous;
468 groupStore.deviceInitialAuditCompleted(deviceId1, true);
469
470 groupStore.storeGroupDescription(groupDescription1);
471 Group group1 = groupStore.getGroup(deviceId1, groupId1);
472
473 extraneous = Lists.newArrayList(groupStore.getExtraneousGroups(deviceId1));
474 assertThat(extraneous, hasSize(0));
475
476 groupStore.addOrUpdateExtraneousGroupEntry(group1);
477 extraneous = Lists.newArrayList(groupStore.getExtraneousGroups(deviceId1));
478 assertThat(extraneous, hasSize(1));
479
480 groupStore.removeExtraneousGroupEntry(group1);
481 extraneous = Lists.newArrayList(groupStore.getExtraneousGroups(deviceId1));
482 assertThat(extraneous, hasSize(0));
483 }
484
485 /**
486 * Tests updating of group descriptions.
487 */
488 @Test
489 public void testUpdateGroupDescription() {
490
491 GroupBuckets buckets =
Victor Silvadf1eeae2016-08-12 15:28:57 -0300492 new GroupBuckets(ImmutableList.of(failoverGroupBucket, selectGroupBucket));
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700493
494 groupStore.deviceInitialAuditCompleted(deviceId1, true);
495 groupStore.storeGroupDescription(groupDescription1);
496
497 GroupKey newKey = new DefaultGroupKey("123".getBytes());
498 groupStore.updateGroupDescription(deviceId1,
499 groupKey1,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530500 ADD,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700501 buckets,
502 newKey);
503 Group group1 = groupStore.getGroup(deviceId1, groupId1);
504 assertThat(group1.appCookie(), is(newKey));
505 assertThat(group1.buckets().buckets(), hasSize(2));
Victor Silvadf1eeae2016-08-12 15:28:57 -0300506
507 short weight = 5;
508 GroupBucket selectGroupBucketWithWeight =
509 DefaultGroupBucket.createSelectGroupBucket(treatment, weight);
510 buckets = new GroupBuckets(ImmutableList.of(failoverGroupBucket,
511 selectGroupBucketWithWeight));
512
513 groupStore.updateGroupDescription(deviceId1,
514 newKey,
515 ADD,
516 buckets,
517 newKey);
518
519 group1 = groupStore.getGroup(deviceId1, groupId1);
520 assertThat(group1.appCookie(), is(newKey));
521 assertThat(group1.buckets().buckets(), hasSize(2));
522 for (GroupBucket bucket : group1.buckets().buckets()) {
523 if (bucket.type() == SELECT) {
524 assertEquals(weight, bucket.weight());
525 }
526 }
Victor Silva0282ab82016-11-15 16:30:27 -0300527
528 buckets = new GroupBuckets(ImmutableList.of(selectGroupBucketWithWeight));
529
530 groupStore.updateGroupDescription(deviceId1,
531 newKey,
532 SET,
533 buckets,
534 newKey);
535
536 group1 = groupStore.getGroup(deviceId1, groupId1);
537 assertThat(group1.appCookie(), is(newKey));
538 assertThat(group1.buckets().buckets(), hasSize(1));
539 GroupBucket onlyBucket = group1.buckets().buckets().iterator().next();
540 assertEquals(weight, onlyBucket.weight());
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700541 }
542
543 @Test
544 public void testEqualsGroupStoreIdMapKey() {
545 DistributedGroupStore.GroupStoreIdMapKey key1 =
546 new DistributedGroupStore.GroupStoreIdMapKey(deviceId1, groupId1);
547 DistributedGroupStore.GroupStoreIdMapKey sameAsKey1 =
548 new DistributedGroupStore.GroupStoreIdMapKey(deviceId1, groupId1);
549 DistributedGroupStore.GroupStoreIdMapKey key2 =
550 new DistributedGroupStore.GroupStoreIdMapKey(deviceId2, groupId1);
551 DistributedGroupStore.GroupStoreIdMapKey key3 =
552 new DistributedGroupStore.GroupStoreIdMapKey(deviceId1, groupId2);
553
554 new EqualsTester()
555 .addEqualityGroup(key1, sameAsKey1)
556 .addEqualityGroup(key2)
557 .addEqualityGroup(key3)
558 .testEquals();
559 }
560
561 @Test
562 public void testEqualsGroupStoreKeyMapKey() {
563 DistributedGroupStore.GroupStoreKeyMapKey key1 =
564 new DistributedGroupStore.GroupStoreKeyMapKey(deviceId1, groupKey1);
565 DistributedGroupStore.GroupStoreKeyMapKey sameAsKey1 =
566 new DistributedGroupStore.GroupStoreKeyMapKey(deviceId1, groupKey1);
567 DistributedGroupStore.GroupStoreKeyMapKey key2 =
568 new DistributedGroupStore.GroupStoreKeyMapKey(deviceId2, groupKey1);
569 DistributedGroupStore.GroupStoreKeyMapKey key3 =
570 new DistributedGroupStore.GroupStoreKeyMapKey(deviceId1, groupKey2);
571
572 new EqualsTester()
573 .addEqualityGroup(key1, sameAsKey1)
574 .addEqualityGroup(key2)
575 .addEqualityGroup(key3)
576 .testEquals();
577 }
578
579 @Test
580 public void testEqualsGroupStoreMapKey() {
581 DistributedGroupStore.GroupStoreMapKey key1 =
582 new DistributedGroupStore.GroupStoreMapKey(deviceId1);
583 DistributedGroupStore.GroupStoreMapKey sameAsKey1 =
584 new DistributedGroupStore.GroupStoreMapKey(deviceId1);
585 DistributedGroupStore.GroupStoreMapKey key2 =
586 new DistributedGroupStore.GroupStoreMapKey(deviceId2);
587 DistributedGroupStore.GroupStoreMapKey key3 =
588 new DistributedGroupStore.GroupStoreMapKey(did("dev3"));
589
590 new EqualsTester()
591 .addEqualityGroup(key1, sameAsKey1)
592 .addEqualityGroup(key2)
593 .addEqualityGroup(key3)
594 .testEquals();
595 }
Sivachidambaram Subramanian9f816de2017-06-13 07:16:54 +0530596
597 @Test
598 public void testMasterNull() throws Exception {
599 groupStore.deviceInitialAuditCompleted(deviceId1, true);
600 assertThat(groupStore.deviceInitialAuditStatus(deviceId1), is(true));
601 // Make sure the pending list starts out empty
602 assertThat(auditPendingReqQueue.size(), is(0));
603 //Simulate master null
604 groupStoreImpl.mastershipService = new MasterNull();
605 //Add a group
606 groupStore.storeGroupDescription(groupDescription1);
607 assertThat(groupStore.getGroupCount(deviceId1), is(0));
608 assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue());
609 assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue());
610 //reset master
611 groupStoreImpl.mastershipService = new MasterOfAll();
612 // Master was null when the group add attempt is made.
613 // So size of the pending list should be 1 now.
614 assertThat(auditPendingReqQueue.size(), is(1));
615 groupStore.deviceInitialAuditCompleted(deviceId1, true);
616 //After the audit , the group should be removed from pending audit queue
617 assertThat(auditPendingReqQueue.size(), is(0));
618 //test whether the group is added to the store
619 assertThat(groupStore.getGroupCount(deviceId1), is(1));
620 assertThat(groupStore.getGroup(deviceId1, groupId1), notNullValue());
621 assertThat(groupStore.getGroup(deviceId1, groupKey1), notNullValue());
622 }
623
624
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700625}