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;
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;
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"));
37 private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
38 private EntityOwnershipStatistics ownershipStatistics;
42 ownershipStatistics = new EntityOwnershipStatistics();
43 ownershipStatistics.init(shardDataTree);
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));
52 // Write local member as owner for entity 1
54 writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
55 assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
57 // Add remote member 1 as candidate for entity 1 - ownershipStatistics support should not get notified
59 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
60 assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
62 // Change owner to remote member 1 for entity 1
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);
69 // Change owner to remote member 2 for entity 1
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);
77 // Clear the owner for entity 1
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);
85 // Change owner to the local member for entity 1
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);
93 // Change owner to remote member 1 for entity 2
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);
101 // Change owner to the local member for entity 2
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);
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);
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);
123 // Clear the owner for entity 2 again - expect no change
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);
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());
137 private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
138 AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);