Maintain EntityOwnershipStatistics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnershipStatisticsTest.java
1 package org.opendaylight.controller.cluster.datastore.entityownership;
2
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;
8 import java.util.Map;
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;
18
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"));
28
29     private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
30     private EntityOwnershipStatistics ownershipStatistics;
31
32     @Before
33     public void setup() {
34         ownershipStatistics = new EntityOwnershipStatistics();
35         ownershipStatistics.init(shardDataTree);
36     }
37
38     @Test
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));
43
44         // Write local member as owner for entity 1
45
46         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
47         assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
48
49         // Add remote member 1 as candidate for entity 1 - ownershipStatistics support should not get notified
50
51         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
52         assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
53
54         // Change owner to remote member 1 for entity 1
55
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);
60
61         // Change owner to remote member 2 for entity 1
62
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);
68
69         // Clear the owner for entity 1
70
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);
76
77         // Change owner to the local member for entity 1
78
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);
84
85         // Change owner to remote member 1 for entity 2
86
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);
92
93         // Change owner to the local member for entity 2
94
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);
100
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);
107
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);
114
115         // Clear the owner for entity 2 again - expect no change
116
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);
122
123     }
124
125     private void assertStatistics(Map<String, Map<String, Long>> statistics, String memberName, long val) {
126         assertEquals(val, statistics.get(ENTITY_TYPE).get(memberName).longValue());
127     }
128
129     private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
130         AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
131     }
132 }