2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.entityownership;
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
12 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.entityEntryWithOwner;
13 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
14 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.entityPath;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mockito;
20 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
21 import org.opendaylight.controller.cluster.datastore.Shard;
22 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
29 public class EntityOwnershipStatisticsTest extends AbstractActorTest {
30 private static final String LOCAL_MEMBER_NAME = "member-1";
31 private static final String REMOTE_MEMBER_NAME1 = "member-2";
32 private static final String REMOTE_MEMBER_NAME2 = "member-3";
33 private static final String ENTITY_TYPE = "test";
34 private static final YangInstanceIdentifier ENTITY_ID1 =
35 YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
36 private static final YangInstanceIdentifier ENTITY_ID2 =
37 YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
39 private final Shard mockShard = Mockito.mock(Shard.class);
41 private final ShardDataTree shardDataTree = new ShardDataTree(mockShard, EOSTestUtils.SCHEMA_CONTEXT,
42 TreeType.OPERATIONAL);
43 private EntityOwnershipStatistics ownershipStatistics;
47 ownershipStatistics = new EntityOwnershipStatistics();
48 ownershipStatistics.init(shardDataTree);
52 public void testOnDataTreeChanged() throws Exception {
53 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME));
54 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, LOCAL_MEMBER_NAME));
56 // Write local member as owner for entity 1
58 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
59 assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
61 // Add remote member 1 as candidate for entity 1 - ownershipStatistics support should not get notified
63 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
64 assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
66 // Change owner to remote member 1 for entity 1
68 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1));
69 Map<String, Map<String, Long>> statistics = ownershipStatistics.all();
70 assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
71 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 1L);
73 // Change owner to remote member 2 for entity 1
75 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2));
76 statistics = ownershipStatistics.all();
77 assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
78 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
79 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 1L);
81 // Clear the owner for entity 1
83 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, ""));
84 statistics = ownershipStatistics.all();
85 assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
86 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
87 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
89 // Change owner to the local member for entity 1
91 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
92 statistics = ownershipStatistics.all();
93 assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
94 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
95 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
97 // Change owner to remote member 1 for entity 2
99 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1));
100 statistics = ownershipStatistics.all();
101 assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
102 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 1L);
103 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
105 // Change owner to the local member for entity 2
107 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
108 statistics = ownershipStatistics.all();
109 assertStatistics(statistics, LOCAL_MEMBER_NAME, 2L);
110 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
111 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
113 // Write local member owner for entity 2 again - expect no change
114 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
115 statistics = ownershipStatistics.all();
116 assertStatistics(statistics, LOCAL_MEMBER_NAME, 2L);
117 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
118 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
120 // Clear the owner for entity 2
121 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, ""));
122 statistics = ownershipStatistics.all();
123 assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
124 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
125 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
127 // Clear the owner for entity 2 again - expect no change
129 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, ""));
130 statistics = ownershipStatistics.all();
131 assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
132 assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
133 assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
137 private static void assertStatistics(final Map<String, Map<String, Long>> statistics, final String memberName,
139 assertEquals(val, statistics.get(ENTITY_TYPE).get(memberName).longValue());
142 private void writeNode(final YangInstanceIdentifier path, final NormalizedNode node)
143 throws DataValidationFailedException {
144 AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);