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
9 package org.opendaylight.controller.cluster.datastore.entityownership;
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;
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;
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"));
41 private final Shard mockShard = Mockito.mock(Shard.class);
43 private final ShardDataTree shardDataTree = new ShardDataTree(mockShard, SchemaContextHelper.entityOwners(),
44 TreeType.OPERATIONAL);
45 private EntityOwnershipStatistics ownershipStatistics;
49 ownershipStatistics = new EntityOwnershipStatistics();
50 ownershipStatistics.init(shardDataTree);
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));
58 // Write local member as owner for entity 1
60 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
61 assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
63 // Add remote member 1 as candidate for entity 1 - ownershipStatistics support should not get notified
65 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
66 assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
68 // Change owner to remote member 1 for entity 1
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);
75 // Change owner to remote member 2 for entity 1
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);
83 // Clear the owner for entity 1
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);
91 // Change owner to the local member for entity 1
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);
99 // Change owner to remote member 1 for entity 2
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);
107 // Change owner to the local member for entity 2
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);
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);
122 // Clear the owner for entity 2
123 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null));
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);
129 // Clear the owner for entity 2 again - expect no change
131 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null));
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);
139 private static void assertStatistics(final Map<String, Map<String, Long>> statistics, final String memberName,
141 assertEquals(val, statistics.get(ENTITY_TYPE).get(memberName).longValue());
144 private void writeNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> node)
145 throws DataValidationFailedException {
146 AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);