blob: 83b8abcd5f95bf6936c7e694cd2347848dd89796 [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;
pier7aceddf2019-12-19 16:04:23 +010025import org.onlab.packet.Ip4Address;
alshabibb0285992016-03-28 23:30:37 -070026import org.onosproject.cfg.ComponentConfigAdapter;
pier7aceddf2019-12-19 16:04:23 +010027import org.onosproject.cluster.ClusterService;
28import org.onosproject.cluster.ControllerNode;
Charles Chanf4838a72015-12-07 18:13:45 -080029import org.onosproject.cluster.NodeId;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070030import org.onosproject.core.GroupId;
31import org.onosproject.mastership.MastershipServiceAdapter;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.MastershipRole;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.flow.DefaultTrafficTreatment;
36import org.onosproject.net.flow.TrafficTreatment;
37import org.onosproject.net.group.DefaultGroup;
38import org.onosproject.net.group.DefaultGroupBucket;
39import org.onosproject.net.group.DefaultGroupDescription;
40import org.onosproject.net.group.DefaultGroupKey;
41import org.onosproject.net.group.Group;
Saurav Dasf1027d42018-06-11 17:02:31 -070042import org.onosproject.net.group.Group.GroupState;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070043import org.onosproject.net.group.GroupBucket;
44import org.onosproject.net.group.GroupBuckets;
45import org.onosproject.net.group.GroupDescription;
46import org.onosproject.net.group.GroupEvent;
47import org.onosproject.net.group.GroupKey;
48import org.onosproject.net.group.GroupOperation;
49import org.onosproject.net.group.GroupStore;
50import org.onosproject.net.group.GroupStoreDelegate;
Saurav Dasf1027d42018-06-11 17:02:31 -070051import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070052import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
Madan Jampani0b847532016-03-03 13:44:15 -080053import org.onosproject.store.service.ConsistentMap;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070054import org.onosproject.store.service.TestStorageService;
55
Sivachidambaram Subramanian9f816de2017-06-13 07:16:54 +053056import java.util.ArrayList;
57import java.util.LinkedList;
58import java.util.List;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070059
pier7aceddf2019-12-19 16:04:23 +010060import static org.easymock.EasyMock.*;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070061import static org.hamcrest.MatcherAssert.assertThat;
62import static org.hamcrest.Matchers.hasSize;
63import static org.hamcrest.Matchers.instanceOf;
64import static org.hamcrest.Matchers.is;
65import static org.hamcrest.Matchers.notNullValue;
66import static org.hamcrest.Matchers.nullValue;
Victor Silvadf1eeae2016-08-12 15:28:57 -030067import static org.junit.Assert.assertEquals;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070068import static org.onosproject.net.NetTestTools.APP_ID;
69import static org.onosproject.net.NetTestTools.did;
Sivachidambaram Subramanian9f816de2017-06-13 07:16:54 +053070import static org.onosproject.net.group.GroupDescription.Type.ALL;
71import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
72import static org.onosproject.net.group.GroupDescription.Type.SELECT;
73import static org.onosproject.net.group.GroupStore.UpdateType.ADD;
74import static org.onosproject.net.group.GroupStore.UpdateType.SET;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070075/**
76 * Distributed group store test.
77 */
78public class DistributedGroupStoreTest {
79
80 DeviceId deviceId1 = did("dev1");
81 DeviceId deviceId2 = did("dev2");
Yi Tsengfa394de2017-02-01 11:26:40 -080082 GroupId groupId1 = new GroupId(1);
83 GroupId groupId2 = new GroupId(2);
84 GroupId groupId3 = new GroupId(3);
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070085 GroupKey groupKey1 = new DefaultGroupKey("abc".getBytes());
86 GroupKey groupKey2 = new DefaultGroupKey("def".getBytes());
Charles Chan0c7c43b2016-01-14 17:39:20 -080087 GroupKey groupKey3 = new DefaultGroupKey("ghi".getBytes());
Ray Milkeyb3c5ce22015-08-10 09:07:36 -070088
89 TrafficTreatment treatment =
90 DefaultTrafficTreatment.emptyTreatment();
91 GroupBucket selectGroupBucket =
92 DefaultGroupBucket.createSelectGroupBucket(treatment);
93 GroupBucket failoverGroupBucket =
94 DefaultGroupBucket.createFailoverGroupBucket(treatment,
95 PortNumber.IN_PORT, groupId1);
96
97 GroupBuckets buckets = new GroupBuckets(ImmutableList.of(selectGroupBucket));
98 GroupDescription groupDescription1 = new DefaultGroupDescription(
99 deviceId1,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530100 ALL,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700101 buckets,
102 groupKey1,
103 groupId1.id(),
104 APP_ID);
105 GroupDescription groupDescription2 = new DefaultGroupDescription(
106 deviceId2,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530107 INDIRECT,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700108 buckets,
109 groupKey2,
110 groupId2.id(),
111 APP_ID);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800112 GroupDescription groupDescription3 = new DefaultGroupDescription(
113 deviceId2,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530114 INDIRECT,
Charles Chan0c7c43b2016-01-14 17:39:20 -0800115 buckets,
116 groupKey3,
117 groupId3.id(),
118 APP_ID);
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700119
120 DistributedGroupStore groupStoreImpl;
121 GroupStore groupStore;
Madan Jampani0b847532016-03-03 13:44:15 -0800122 ConsistentMap auditPendingReqQueue;
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700123
124 static class MasterOfAll extends MastershipServiceAdapter {
125 @Override
126 public MastershipRole getLocalRole(DeviceId deviceId) {
127 return MastershipRole.MASTER;
128 }
Charles Chanf4838a72015-12-07 18:13:45 -0800129
130 @Override
131 public NodeId getMasterFor(DeviceId deviceId) {
pier7aceddf2019-12-19 16:04:23 +0100132 return new NodeId(NODE_ID);
Charles Chanf4838a72015-12-07 18:13:45 -0800133 }
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700134 }
135
Sivachidambaram Subramanian9f816de2017-06-13 07:16:54 +0530136 static class MasterNull extends MastershipServiceAdapter {
137 @Override
138 public MastershipRole getLocalRole(DeviceId deviceId) {
139 return null;
140 }
141
142 @Override
143 public NodeId getMasterFor(DeviceId deviceId) {
144 return null;
145 }
146 }
147
pier7aceddf2019-12-19 16:04:23 +0100148 private static class MockControllerNode implements ControllerNode {
149 final NodeId id;
150
151 public MockControllerNode(NodeId id) {
152 this.id = id;
153 }
154
155 @Override
156 public NodeId id() {
157 return this.id;
158 }
159
160 @Override
161 public Ip4Address ip() {
162 return Ip4Address.valueOf("127.0.0.1");
163 }
164
165 @Override
166 public int tcpPort() {
167 return 0;
168 }
169 }
170
171 private static final String NODE_ID = "foo";
172
173
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700174 @Before
175 public void setUp() throws Exception {
176 groupStoreImpl = new DistributedGroupStore();
177 groupStoreImpl.storageService = new TestStorageService();
178 groupStoreImpl.clusterCommunicator = new ClusterCommunicationServiceAdapter();
179 groupStoreImpl.mastershipService = new MasterOfAll();
alshabibb0285992016-03-28 23:30:37 -0700180 groupStoreImpl.cfgService = new ComponentConfigAdapter();
pier7aceddf2019-12-19 16:04:23 +0100181
182 ClusterService mockClusterService = createMock(ClusterService.class);
183 NodeId nodeId = new NodeId(NODE_ID);
184 MockControllerNode mockControllerNode = new MockControllerNode(nodeId);
185 expect(mockClusterService.getLocalNode())
186 .andReturn(mockControllerNode).anyTimes();
187 replay(mockClusterService);
188
189 groupStoreImpl.clusterService = mockClusterService;
sisubram4beea652017-08-09 10:38:14 +0000190 groupStoreImpl.activate(null);
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700191 groupStore = groupStoreImpl;
192 auditPendingReqQueue =
193 TestUtils.getField(groupStoreImpl, "auditPendingReqQueue");
194 }
195
196 @After
197 public void tearDown() throws Exception {
198 groupStoreImpl.deactivate();
199 }
200
201 /**
202 * Tests the initial state of the store.
203 */
204 @Test
205 public void testEmptyStore() {
206 assertThat(groupStore.getGroupCount(deviceId1), is(0));
207 assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue());
208 assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue());
209 }
210
211 /**
212 * Tests adding a pending group.
213 */
214 @Test
215 public void testAddPendingGroup() throws Exception {
216 // Make sure the pending list starts out empty
217 assertThat(auditPendingReqQueue.size(), is(0));
218
219 // Add a new pending group. Make sure that the store remains empty
220 groupStore.storeGroupDescription(groupDescription1);
221 assertThat(groupStore.getGroupCount(deviceId1), is(0));
222 assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue());
223 assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue());
224
225 // Make sure the group is pending
226 assertThat(auditPendingReqQueue.size(), is(1));
227
228 groupStore.deviceInitialAuditCompleted(deviceId1, true);
229
230 // Make sure the group isn't pending anymore
231 assertThat(auditPendingReqQueue.size(), is(0));
232 }
233
234
235 /**
236 * Tests adding and removing a group.
237 */
238 @Test
239 public void testAddRemoveGroup() throws Exception {
240 groupStore.deviceInitialAuditCompleted(deviceId1, true);
241 assertThat(groupStore.deviceInitialAuditStatus(deviceId1), is(true));
242
243 // Make sure the pending list starts out empty
244 assertThat(auditPendingReqQueue.size(), is(0));
245
246 groupStore.storeGroupDescription(groupDescription1);
247 assertThat(groupStore.getGroupCount(deviceId1), is(1));
248 assertThat(groupStore.getGroup(deviceId1, groupId1), notNullValue());
249 assertThat(groupStore.getGroup(deviceId1, groupKey1), notNullValue());
250
251 // Make sure that nothing is pending
252 assertThat(auditPendingReqQueue.size(), is(0));
253
254 Group groupById = groupStore.getGroup(deviceId1, groupId1);
255 Group groupByKey = groupStore.getGroup(deviceId1, groupKey1);
256 assertThat(groupById, notNullValue());
257 assertThat(groupByKey, notNullValue());
258 assertThat(groupById, is(groupByKey));
259 assertThat(groupById.deviceId(), is(did("dev1")));
260
261 groupStore.removeGroupEntry(groupById);
262
263 assertThat(groupStore.getGroupCount(deviceId1), is(0));
264 assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue());
265 assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue());
266
267 // Make sure that nothing is pending
268 assertThat(auditPendingReqQueue.size(), is(0));
269 }
270
271 /**
Charles Chan0c7c43b2016-01-14 17:39:20 -0800272 * Tests removing all groups on the given device.
273 */
274 @Test
275 public void testRemoveGroupOnDevice() throws Exception {
276 groupStore.deviceInitialAuditCompleted(deviceId1, true);
277 assertThat(groupStore.deviceInitialAuditStatus(deviceId1), is(true));
278 groupStore.deviceInitialAuditCompleted(deviceId2, true);
279 assertThat(groupStore.deviceInitialAuditStatus(deviceId2), is(true));
280
281 // Make sure the pending list starts out empty
282 assertThat(auditPendingReqQueue.size(), is(0));
283
284 groupStore.storeGroupDescription(groupDescription1);
285 groupStore.storeGroupDescription(groupDescription2);
286 groupStore.storeGroupDescription(groupDescription3);
287 assertThat(groupStore.getGroupCount(deviceId1), is(1));
288 assertThat(groupStore.getGroupCount(deviceId2), is(2));
289
290 groupStore.purgeGroupEntry(deviceId2);
291 assertThat(groupStore.getGroupCount(deviceId1), is(1));
292 assertThat(groupStore.getGroupCount(deviceId2), is(0));
Victor Silva4e8b7832016-08-17 17:11:19 -0300293
294 groupStore.purgeGroupEntries();
295 assertThat(groupStore.getGroupCount(deviceId1), is(0));
296 assertThat(groupStore.getGroupCount(deviceId2), is(0));
Charles Chan0c7c43b2016-01-14 17:39:20 -0800297 }
298
299 /**
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700300 * Tests adding and removing a group.
301 */
302 @Test
303 public void testRemoveGroupDescription() throws Exception {
304 groupStore.deviceInitialAuditCompleted(deviceId1, true);
305
306 groupStore.storeGroupDescription(groupDescription1);
307
308 groupStore.deleteGroupDescription(deviceId1, groupKey1);
309
310 // Group should still be there, marked for removal
311 assertThat(groupStore.getGroupCount(deviceId1), is(1));
312 Group queriedGroup = groupStore.getGroup(deviceId1, groupId1);
313 assertThat(queriedGroup.state(), is(Group.GroupState.PENDING_DELETE));
314
315 }
316
317 /**
318 * Tests pushing group metrics.
319 */
320 @Test
321 public void testPushGroupMetrics() {
322 groupStore.deviceInitialAuditCompleted(deviceId1, true);
323 groupStore.deviceInitialAuditCompleted(deviceId2, true);
324
325 GroupDescription groupDescription3 = new DefaultGroupDescription(
326 deviceId1,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530327 SELECT,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700328 buckets,
329 new DefaultGroupKey("aaa".getBytes()),
330 null,
331 APP_ID);
332
333 groupStore.storeGroupDescription(groupDescription1);
334 groupStore.storeGroupDescription(groupDescription2);
335 groupStore.storeGroupDescription(groupDescription3);
336 Group group1 = groupStore.getGroup(deviceId1, groupId1);
337
338 assertThat(group1, instanceOf(DefaultGroup.class));
339 DefaultGroup defaultGroup1 = (DefaultGroup) group1;
340 defaultGroup1.setPackets(55L);
341 defaultGroup1.setBytes(66L);
342 groupStore.pushGroupMetrics(deviceId1, ImmutableList.of(group1));
343
344 // Make sure the group was updated.
345
346 Group requeryGroup1 = groupStore.getGroup(deviceId1, groupId1);
347 assertThat(requeryGroup1.packets(), is(55L));
348 assertThat(requeryGroup1.bytes(), is(66L));
349
350 }
351
352 class TestDelegate implements GroupStoreDelegate {
353 private List<GroupEvent> eventsSeen = new LinkedList<>();
354 @Override
355 public void notify(GroupEvent event) {
356 eventsSeen.add(event);
357 }
358
359 public List<GroupEvent> eventsSeen() {
360 return eventsSeen;
361 }
362
363 public void resetEvents() {
364 eventsSeen.clear();
365 }
366 }
367
368 /**
369 * Tests group operation failed interface.
370 */
371 @Test
372 public void testGroupOperationFailed() {
373 TestDelegate delegate = new TestDelegate();
374 groupStore.setDelegate(delegate);
375 groupStore.deviceInitialAuditCompleted(deviceId1, true);
376 groupStore.deviceInitialAuditCompleted(deviceId2, true);
377
378 groupStore.storeGroupDescription(groupDescription1);
379 groupStore.storeGroupDescription(groupDescription2);
380
381 List<GroupEvent> eventsAfterAdds = delegate.eventsSeen();
382 assertThat(eventsAfterAdds, hasSize(2));
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700383 eventsAfterAdds.forEach(event -> assertThat(event.type(), is(GroupEvent.Type.GROUP_ADD_REQUESTED)));
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700384 delegate.resetEvents();
385
386 GroupOperation opAdd =
387 GroupOperation.createAddGroupOperation(groupId1,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530388 INDIRECT,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700389 buckets);
390 groupStore.groupOperationFailed(deviceId1, opAdd);
391
392 List<GroupEvent> eventsAfterAddFailed = delegate.eventsSeen();
393 assertThat(eventsAfterAddFailed, hasSize(2));
394 assertThat(eventsAfterAddFailed.get(0).type(),
395 is(GroupEvent.Type.GROUP_ADD_FAILED));
396 assertThat(eventsAfterAddFailed.get(1).type(),
397 is(GroupEvent.Type.GROUP_REMOVED));
398 delegate.resetEvents();
399
400 GroupOperation opModify =
401 GroupOperation.createModifyGroupOperation(groupId2,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530402 INDIRECT,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700403 buckets);
404 groupStore.groupOperationFailed(deviceId2, opModify);
405 List<GroupEvent> eventsAfterModifyFailed = delegate.eventsSeen();
406 assertThat(eventsAfterModifyFailed, hasSize(1));
407 assertThat(eventsAfterModifyFailed.get(0).type(),
408 is(GroupEvent.Type.GROUP_UPDATE_FAILED));
409 delegate.resetEvents();
410
411 GroupOperation opDelete =
412 GroupOperation.createDeleteGroupOperation(groupId2,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530413 INDIRECT);
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700414 groupStore.groupOperationFailed(deviceId2, opDelete);
415 List<GroupEvent> eventsAfterDeleteFailed = delegate.eventsSeen();
416 assertThat(eventsAfterDeleteFailed, hasSize(1));
417 assertThat(eventsAfterDeleteFailed.get(0).type(),
418 is(GroupEvent.Type.GROUP_REMOVE_FAILED));
419 delegate.resetEvents();
420 }
421
422 /**
Saurav Dasf1027d42018-06-11 17:02:31 -0700423 * Tests group operation failed interface, with error codes for failures.
424 */
425 @Test
426 public void testGroupOperationFailedWithErrorCode() {
427 TestDelegate delegate = new TestDelegate();
428 groupStore.setDelegate(delegate);
429 groupStore.deviceInitialAuditCompleted(deviceId1, true);
430 groupStore.storeGroupDescription(groupDescription1);
431 groupStore.deviceInitialAuditCompleted(deviceId2, true);
432 groupStore.storeGroupDescription(groupDescription2);
433
434 List<GroupEvent> eventsAfterAdds = delegate.eventsSeen();
435 assertThat(eventsAfterAdds, hasSize(2));
436 eventsAfterAdds.forEach(event -> assertThat(event
437 .type(), is(GroupEvent.Type.GROUP_ADD_REQUESTED)));
438 delegate.resetEvents();
439
440 // test group exists
441 GroupOperation opAdd = GroupOperation
442 .createAddGroupOperation(groupId1, INDIRECT, buckets);
443 GroupOperation addFailedExists = GroupOperation
444 .createFailedGroupOperation(opAdd, GroupMsgErrorCode.GROUP_EXISTS);
445 groupStore.groupOperationFailed(deviceId1, addFailedExists);
446
447 List<GroupEvent> eventsAfterAddFailed = delegate.eventsSeen();
448 assertThat(eventsAfterAddFailed, hasSize(2));
449 assertThat(eventsAfterAddFailed.get(0).type(),
450 is(GroupEvent.Type.GROUP_ADDED));
451 assertThat(eventsAfterAddFailed.get(1).type(),
452 is(GroupEvent.Type.GROUP_ADDED));
453 Group g1 = groupStore.getGroup(deviceId1, groupId1);
454 assertEquals(0, g1.failedRetryCount());
455 delegate.resetEvents();
456
457 // test invalid group
458 Group g2 = groupStore.getGroup(deviceId2, groupId2);
459 assertEquals(0, g2.failedRetryCount());
460 assertEquals(GroupState.PENDING_ADD, g2.state());
461 GroupOperation opAdd1 = GroupOperation
462 .createAddGroupOperation(groupId2, INDIRECT, buckets);
463 GroupOperation addFailedInvalid = GroupOperation
464 .createFailedGroupOperation(opAdd1, GroupMsgErrorCode.INVALID_GROUP);
465
466 groupStore.groupOperationFailed(deviceId2, addFailedInvalid);
467 groupStore.pushGroupMetrics(deviceId2, ImmutableList.of());
468 List<GroupEvent> eventsAfterAddFailed1 = delegate.eventsSeen();
469 assertThat(eventsAfterAddFailed1, hasSize(1));
470 assertThat(eventsAfterAddFailed.get(0).type(),
471 is(GroupEvent.Type.GROUP_ADD_REQUESTED));
472 g2 = groupStore.getGroup(deviceId2, groupId2);
473 assertEquals(1, g2.failedRetryCount());
474 assertEquals(GroupState.PENDING_ADD_RETRY, g2.state());
475 delegate.resetEvents();
476
477 groupStore.groupOperationFailed(deviceId2, addFailedInvalid);
478 groupStore.pushGroupMetrics(deviceId2, ImmutableList.of());
479 List<GroupEvent> eventsAfterAddFailed2 = delegate.eventsSeen();
480 assertThat(eventsAfterAddFailed2, hasSize(1));
481 assertThat(eventsAfterAddFailed.get(0).type(),
482 is(GroupEvent.Type.GROUP_ADD_REQUESTED));
483 g2 = groupStore.getGroup(deviceId2, groupId2);
484 assertEquals(2, g2.failedRetryCount());
485 assertEquals(GroupState.PENDING_ADD_RETRY, g2.state());
486 delegate.resetEvents();
487
488 groupStore.groupOperationFailed(deviceId2, addFailedInvalid);
489 groupStore.pushGroupMetrics(deviceId2, ImmutableList.of());
490 List<GroupEvent> eventsAfterAddFailed3 = delegate.eventsSeen();
491 assertThat(eventsAfterAddFailed3, hasSize(2));
492 assertThat(eventsAfterAddFailed.get(0).type(),
493 is(GroupEvent.Type.GROUP_ADD_FAILED));
494 assertThat(eventsAfterAddFailed.get(1).type(),
495 is(GroupEvent.Type.GROUP_REMOVED));
496 g2 = groupStore.getGroup(deviceId2, groupId2);
497 assertEquals(null, g2);
498 delegate.resetEvents();
499 }
500
501 /**
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700502 * Tests extraneous group operations.
503 */
504 @Test
505 public void testExtraneousOperations() {
506 ArrayList<Group> extraneous;
507 groupStore.deviceInitialAuditCompleted(deviceId1, true);
508
509 groupStore.storeGroupDescription(groupDescription1);
510 Group group1 = groupStore.getGroup(deviceId1, groupId1);
511
512 extraneous = Lists.newArrayList(groupStore.getExtraneousGroups(deviceId1));
513 assertThat(extraneous, hasSize(0));
514
515 groupStore.addOrUpdateExtraneousGroupEntry(group1);
516 extraneous = Lists.newArrayList(groupStore.getExtraneousGroups(deviceId1));
517 assertThat(extraneous, hasSize(1));
518
519 groupStore.removeExtraneousGroupEntry(group1);
520 extraneous = Lists.newArrayList(groupStore.getExtraneousGroups(deviceId1));
521 assertThat(extraneous, hasSize(0));
522 }
523
524 /**
525 * Tests updating of group descriptions.
526 */
527 @Test
528 public void testUpdateGroupDescription() {
529
530 GroupBuckets buckets =
Victor Silvadf1eeae2016-08-12 15:28:57 -0300531 new GroupBuckets(ImmutableList.of(failoverGroupBucket, selectGroupBucket));
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700532
533 groupStore.deviceInitialAuditCompleted(deviceId1, true);
534 groupStore.storeGroupDescription(groupDescription1);
535
536 GroupKey newKey = new DefaultGroupKey("123".getBytes());
537 groupStore.updateGroupDescription(deviceId1,
538 groupKey1,
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530539 ADD,
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700540 buckets,
541 newKey);
542 Group group1 = groupStore.getGroup(deviceId1, groupId1);
543 assertThat(group1.appCookie(), is(newKey));
544 assertThat(group1.buckets().buckets(), hasSize(2));
Victor Silvadf1eeae2016-08-12 15:28:57 -0300545
546 short weight = 5;
547 GroupBucket selectGroupBucketWithWeight =
548 DefaultGroupBucket.createSelectGroupBucket(treatment, weight);
549 buckets = new GroupBuckets(ImmutableList.of(failoverGroupBucket,
550 selectGroupBucketWithWeight));
551
552 groupStore.updateGroupDescription(deviceId1,
553 newKey,
554 ADD,
555 buckets,
556 newKey);
557
558 group1 = groupStore.getGroup(deviceId1, groupId1);
559 assertThat(group1.appCookie(), is(newKey));
560 assertThat(group1.buckets().buckets(), hasSize(2));
561 for (GroupBucket bucket : group1.buckets().buckets()) {
562 if (bucket.type() == SELECT) {
563 assertEquals(weight, bucket.weight());
564 }
565 }
Victor Silva0282ab82016-11-15 16:30:27 -0300566
567 buckets = new GroupBuckets(ImmutableList.of(selectGroupBucketWithWeight));
568
569 groupStore.updateGroupDescription(deviceId1,
570 newKey,
571 SET,
572 buckets,
573 newKey);
574
575 group1 = groupStore.getGroup(deviceId1, groupId1);
576 assertThat(group1.appCookie(), is(newKey));
577 assertThat(group1.buckets().buckets(), hasSize(1));
578 GroupBucket onlyBucket = group1.buckets().buckets().iterator().next();
579 assertEquals(weight, onlyBucket.weight());
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700580 }
581
582 @Test
583 public void testEqualsGroupStoreIdMapKey() {
584 DistributedGroupStore.GroupStoreIdMapKey key1 =
585 new DistributedGroupStore.GroupStoreIdMapKey(deviceId1, groupId1);
586 DistributedGroupStore.GroupStoreIdMapKey sameAsKey1 =
587 new DistributedGroupStore.GroupStoreIdMapKey(deviceId1, groupId1);
588 DistributedGroupStore.GroupStoreIdMapKey key2 =
589 new DistributedGroupStore.GroupStoreIdMapKey(deviceId2, groupId1);
590 DistributedGroupStore.GroupStoreIdMapKey key3 =
591 new DistributedGroupStore.GroupStoreIdMapKey(deviceId1, groupId2);
592
593 new EqualsTester()
594 .addEqualityGroup(key1, sameAsKey1)
595 .addEqualityGroup(key2)
596 .addEqualityGroup(key3)
597 .testEquals();
598 }
599
600 @Test
601 public void testEqualsGroupStoreKeyMapKey() {
602 DistributedGroupStore.GroupStoreKeyMapKey key1 =
603 new DistributedGroupStore.GroupStoreKeyMapKey(deviceId1, groupKey1);
604 DistributedGroupStore.GroupStoreKeyMapKey sameAsKey1 =
605 new DistributedGroupStore.GroupStoreKeyMapKey(deviceId1, groupKey1);
606 DistributedGroupStore.GroupStoreKeyMapKey key2 =
607 new DistributedGroupStore.GroupStoreKeyMapKey(deviceId2, groupKey1);
608 DistributedGroupStore.GroupStoreKeyMapKey key3 =
609 new DistributedGroupStore.GroupStoreKeyMapKey(deviceId1, groupKey2);
610
611 new EqualsTester()
612 .addEqualityGroup(key1, sameAsKey1)
613 .addEqualityGroup(key2)
614 .addEqualityGroup(key3)
615 .testEquals();
616 }
617
618 @Test
619 public void testEqualsGroupStoreMapKey() {
620 DistributedGroupStore.GroupStoreMapKey key1 =
621 new DistributedGroupStore.GroupStoreMapKey(deviceId1);
622 DistributedGroupStore.GroupStoreMapKey sameAsKey1 =
623 new DistributedGroupStore.GroupStoreMapKey(deviceId1);
624 DistributedGroupStore.GroupStoreMapKey key2 =
625 new DistributedGroupStore.GroupStoreMapKey(deviceId2);
626 DistributedGroupStore.GroupStoreMapKey key3 =
627 new DistributedGroupStore.GroupStoreMapKey(did("dev3"));
628
629 new EqualsTester()
630 .addEqualityGroup(key1, sameAsKey1)
631 .addEqualityGroup(key2)
632 .addEqualityGroup(key3)
633 .testEquals();
634 }
Sivachidambaram Subramanian9f816de2017-06-13 07:16:54 +0530635
636 @Test
637 public void testMasterNull() throws Exception {
638 groupStore.deviceInitialAuditCompleted(deviceId1, true);
639 assertThat(groupStore.deviceInitialAuditStatus(deviceId1), is(true));
640 // Make sure the pending list starts out empty
641 assertThat(auditPendingReqQueue.size(), is(0));
642 //Simulate master null
643 groupStoreImpl.mastershipService = new MasterNull();
644 //Add a group
645 groupStore.storeGroupDescription(groupDescription1);
646 assertThat(groupStore.getGroupCount(deviceId1), is(0));
647 assertThat(groupStore.getGroup(deviceId1, groupId1), nullValue());
648 assertThat(groupStore.getGroup(deviceId1, groupKey1), nullValue());
649 //reset master
650 groupStoreImpl.mastershipService = new MasterOfAll();
651 // Master was null when the group add attempt is made.
652 // So size of the pending list should be 1 now.
653 assertThat(auditPendingReqQueue.size(), is(1));
654 groupStore.deviceInitialAuditCompleted(deviceId1, true);
655 //After the audit , the group should be removed from pending audit queue
656 assertThat(auditPendingReqQueue.size(), is(0));
657 //test whether the group is added to the store
658 assertThat(groupStore.getGroupCount(deviceId1), is(1));
659 assertThat(groupStore.getGroup(deviceId1, groupId1), notNullValue());
660 assertThat(groupStore.getGroup(deviceId1, groupKey1), notNullValue());
661 }
662
663
Ray Milkeyb3c5ce22015-08-10 09:07:36 -0700664}