de299c99f7f7a1ebeb1f25ef7c4139408ad9c189
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnershipStatisticsTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore.entityownership;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
13 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner;
14 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
15 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
16 import java.util.Map;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
20 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
21 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
26
27 public class EntityOwnershipStatisticsTest extends AbstractActorTest {
28     private static final String LOCAL_MEMBER_NAME = "member-1";
29     private static final String REMOTE_MEMBER_NAME1 = "member-2";
30     private static final String REMOTE_MEMBER_NAME2 = "member-3";
31     private static final String ENTITY_TYPE = "test";
32     private static final YangInstanceIdentifier ENTITY_ID1 =
33             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
34     private static final YangInstanceIdentifier ENTITY_ID2 =
35             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
36
37     private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
38     private EntityOwnershipStatistics ownershipStatistics;
39
40     @Before
41     public void setup() {
42         ownershipStatistics = new EntityOwnershipStatistics();
43         ownershipStatistics.init(shardDataTree);
44     }
45
46     @Test
47     public void testOnDataTreeChanged() throws Exception {
48         Map<String, Map<String, Long>> statistics = null;
49         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME));
50         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, LOCAL_MEMBER_NAME));
51
52         // Write local member as owner for entity 1
53
54         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
55         assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
56
57         // Add remote member 1 as candidate for entity 1 - ownershipStatistics support should not get notified
58
59         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
60         assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
61
62         // Change owner to remote member 1 for entity 1
63
64         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1));
65         statistics = ownershipStatistics.all();
66         assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
67         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 1L);
68
69         // Change owner to remote member 2 for entity 1
70
71         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2));
72         statistics = ownershipStatistics.all();
73         assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
74         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
75         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 1L);
76
77         // Clear the owner for entity 1
78
79         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, ""));
80         statistics = ownershipStatistics.all();
81         assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
82         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
83         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
84
85         // Change owner to the local member for entity 1
86
87         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
88         statistics = ownershipStatistics.all();
89         assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
90         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
91         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
92
93         // Change owner to remote member 1 for entity 2
94
95         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1));
96         statistics = ownershipStatistics.all();
97         assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
98         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 1L);
99         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
100
101         // Change owner to the local member for entity 2
102
103         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
104         statistics = ownershipStatistics.all();
105         assertStatistics(statistics, LOCAL_MEMBER_NAME, 2L);
106         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
107         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
108
109         // Write local member owner for entity 2 again - expect no change
110         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
111         statistics = ownershipStatistics.all();
112         assertStatistics(statistics, LOCAL_MEMBER_NAME, 2L);
113         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
114         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
115
116         // Clear the owner for entity 2
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         // Clear the owner for entity 2 again - expect no change
124
125         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null));
126         statistics = ownershipStatistics.all();
127         assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
128         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
129         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
130
131     }
132
133     private static void assertStatistics(Map<String, Map<String, Long>> statistics, String memberName, long val) {
134         assertEquals(val, statistics.get(ENTITY_TYPE).get(memberName).longValue());
135     }
136
137     private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
138         AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
139     }
140 }