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