1 package org.opendaylight.controller.cluster.datastore.entityownership;
3 import static org.junit.Assert.assertEquals;
4 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
5 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner;
6 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
7 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
12 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
13 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
19 public class EntityOwnershipStatisticsTest extends AbstractActorTest {
20 private static final String LOCAL_MEMBER_NAME = "member-1";
21 private static final String REMOTE_MEMBER_NAME1 = "member-2";
22 private static final String REMOTE_MEMBER_NAME2 = "member-3";
23 private static final String ENTITY_TYPE = "test";
24 private static final YangInstanceIdentifier ENTITY_ID1 =
25 YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
26 private static final YangInstanceIdentifier ENTITY_ID2 =
27 YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
29 private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
30 private EntityOwnershipStatistics ownershipStatistics;
34 ownershipStatistics = new EntityOwnershipStatistics();
35 ownershipStatistics.init(shardDataTree);
39 public void testOnDataTreeChanged() throws Exception {
40 Map<String, Map<String, Long>> statistics = null;
41 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME));
42 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, LOCAL_MEMBER_NAME));
44 // Write local member as owner for entity 1
46 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
47 assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
49 // Add remote member 1 as candidate for entity 1 - ownershipStatistics support should not get notified
51 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
52 assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
54 // Change owner to remote member 1 for entity 1
56 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1));
57 statistics = ownershipStatistics.all();
58 assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
59 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 1L);
61 // Change owner to remote member 2 for entity 1
63 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2));
64 statistics = ownershipStatistics.all();
65 assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
66 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
67 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 1L);
69 // Clear the owner for entity 1
71 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, ""));
72 statistics = ownershipStatistics.all();
73 assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
74 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
75 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
77 // Change owner to the local member for entity 1
79 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
80 statistics = ownershipStatistics.all();
81 assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
82 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
83 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
85 // Change owner to remote member 1 for entity 2
87 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1));
88 statistics = ownershipStatistics.all();
89 assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
90 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 1L);
91 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
93 // Change owner to the local member for entity 2
95 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
96 statistics = ownershipStatistics.all();
97 assertStatistics(statistics, LOCAL_MEMBER_NAME, 2L);
98 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
99 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
101 // Write local member owner for entity 2 again - expect no change
102 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
103 statistics = ownershipStatistics.all();
104 assertStatistics(statistics, LOCAL_MEMBER_NAME, 2L);
105 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
106 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
108 // Clear the owner for entity 2
109 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null));
110 statistics = ownershipStatistics.all();
111 assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
112 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
113 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
115 // Clear the owner for entity 2 again - expect no change
117 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null));
118 statistics = ownershipStatistics.all();
119 assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
120 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
121 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
125 private void assertStatistics(Map<String, Map<String, Long>> statistics, String memberName, long val) {
126 assertEquals(val, statistics.get(ENTITY_TYPE).get(memberName).longValue());
129 private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
130 AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);