a621ac65abd448693b2d5108617c01f1bd5774a8
[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 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
27
28 public class EntityOwnershipStatisticsTest extends AbstractActorTest {
29     private static final String LOCAL_MEMBER_NAME = "member-1";
30     private static final String REMOTE_MEMBER_NAME1 = "member-2";
31     private static final String REMOTE_MEMBER_NAME2 = "member-3";
32     private static final String ENTITY_TYPE = "test";
33     private static final YangInstanceIdentifier ENTITY_ID1 =
34             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
35     private static final YangInstanceIdentifier ENTITY_ID2 =
36             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
37
38     private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners(),
39         TreeType.OPERATIONAL);
40     private EntityOwnershipStatistics ownershipStatistics;
41
42     @Before
43     public void setup() {
44         ownershipStatistics = new EntityOwnershipStatistics();
45         ownershipStatistics.init(shardDataTree);
46     }
47
48     @Test
49     public void testOnDataTreeChanged() throws Exception {
50         Map<String, Map<String, Long>> statistics = null;
51         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME));
52         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, LOCAL_MEMBER_NAME));
53
54         // Write local member as owner for entity 1
55
56         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
57         assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
58
59         // Add remote member 1 as candidate for entity 1 - ownershipStatistics support should not get notified
60
61         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
62         assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
63
64         // Change owner to remote member 1 for entity 1
65
66         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1));
67         statistics = ownershipStatistics.all();
68         assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
69         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 1L);
70
71         // Change owner to remote member 2 for entity 1
72
73         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2));
74         statistics = ownershipStatistics.all();
75         assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
76         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
77         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 1L);
78
79         // Clear the owner for entity 1
80
81         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, ""));
82         statistics = ownershipStatistics.all();
83         assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
84         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
85         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
86
87         // Change owner to the local member for entity 1
88
89         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
90         statistics = ownershipStatistics.all();
91         assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
92         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
93         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
94
95         // Change owner to remote member 1 for entity 2
96
97         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1));
98         statistics = ownershipStatistics.all();
99         assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
100         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 1L);
101         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
102
103         // Change owner to the local member for entity 2
104
105         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
106         statistics = ownershipStatistics.all();
107         assertStatistics(statistics, LOCAL_MEMBER_NAME, 2L);
108         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
109         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
110
111         // Write local member owner for entity 2 again - expect no change
112         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
113         statistics = ownershipStatistics.all();
114         assertStatistics(statistics, LOCAL_MEMBER_NAME, 2L);
115         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
116         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
117
118         // Clear the owner for entity 2
119         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null));
120         statistics = ownershipStatistics.all();
121         assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
122         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
123         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
124
125         // Clear the owner for entity 2 again - expect no change
126
127         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null));
128         statistics = ownershipStatistics.all();
129         assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
130         assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
131         assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
132
133     }
134
135     private static void assertStatistics(Map<String, Map<String, Long>> statistics, String memberName, long val) {
136         assertEquals(val, statistics.get(ENTITY_TYPE).get(memberName).longValue());
137     }
138
139     private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
140         AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
141     }
142 }