2 * Copyright (c) 2015 Brocade Communications 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.datastore.entityownership;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.reset;
18 import static org.mockito.Mockito.spy;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.when;
21 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
22 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
23 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
24 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.candidatePath;
25 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner;
26 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
27 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithEntityTypeEntry;
28 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
29 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityTypeEntryWithEntityEntry;
30 import akka.actor.ActorRef;
31 import akka.actor.PoisonPill;
32 import com.google.common.base.Optional;
33 import com.google.common.collect.Sets;
34 import java.util.Collection;
35 import java.util.concurrent.CountDownLatch;
36 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.atomic.AtomicReference;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Mockito;
43 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
44 import org.opendaylight.controller.cluster.datastore.DatastoreContextFactory;
45 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
46 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
47 import org.opendaylight.controller.cluster.datastore.config.Configuration;
48 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
49 import org.opendaylight.controller.cluster.datastore.config.EmptyModuleShardConfigProvider;
50 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
51 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
52 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
53 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
54 import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
55 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
56 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
57 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
58 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
59 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
60 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
61 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
62 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
63 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
64 import org.opendaylight.yangtools.yang.common.QName;
65 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
66 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
67 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
68 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
69 import scala.concurrent.Await;
70 import scala.concurrent.Future;
71 import scala.concurrent.duration.Duration;
74 * Unit tests for DistributedEntityOwnershipService.
76 * @author Thomas Pantelis
78 public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnershipTest {
79 static final String ENTITY_TYPE = "test";
80 static final String ENTITY_TYPE2 = "test2";
81 static final QName QNAME = QName.create("test", "2015-08-11", "foo");
82 static int ID_COUNTER = 1;
84 private final String dataStoreName = "config" + ID_COUNTER++;
85 private DistributedDataStore dataStore;
89 DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreName(dataStoreName).
90 shardInitializationTimeout(10, TimeUnit.SECONDS).build();
92 Configuration configuration = new ConfigurationImpl(new EmptyModuleShardConfigProvider()) {
94 public Collection<String> getUniqueMemberNamesForAllShards() {
95 return Sets.newHashSet("member-1");
99 DatastoreContextFactory mockContextFactory = mock(DatastoreContextFactory.class);
100 Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext();
101 Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
103 dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, mockContextFactory, null);
105 dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners());
109 public void tearDown() {
110 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), ActorRef.noSender());
113 private static <T> T verifyMessage(final DistributedEntityOwnershipService mock, final Class<T> type) {
114 final ArgumentCaptor<T> message = ArgumentCaptor.forClass(type);
115 verify(mock).executeLocalEntityOwnershipShardOperation(message.capture());
116 return message.getValue();
120 public void testEntityOwnershipShardCreated() throws Exception {
121 DistributedEntityOwnershipService service = DistributedEntityOwnershipService.start(dataStore.getActorContext(),
122 EntityOwnerSelectionStrategyConfig.newBuilder().build());
124 Future<ActorRef> future = dataStore.getActorContext().findLocalShardAsync(
125 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
126 ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
127 assertNotNull(DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME + " not found", shardActor);
133 public void testRegisterCandidate() throws Exception {
134 DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(
135 dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
137 YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
138 Entity entity = new Entity(ENTITY_TYPE, entityId);
140 EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
141 verifyRegisterCandidateLocal(service, entity);
142 verifyEntityOwnershipCandidateRegistration(entity, reg);
143 verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId,
144 dataStore.getActorContext().getCurrentMemberName());
146 // Register the same entity - should throw exception
149 service.registerCandidate(entity);
150 fail("Expected CandidateAlreadyRegisteredException");
151 } catch(CandidateAlreadyRegisteredException e) {
153 assertEquals("getEntity", entity, e.getEntity());
156 // Register a different entity - should succeed
159 Entity entity2 = new Entity(ENTITY_TYPE2, entityId);
160 EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2);
161 verifyRegisterCandidateLocal(service, entity2);
162 verifyEntityOwnershipCandidateRegistration(entity2, reg2);
163 verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId,
164 dataStore.getActorContext().getCurrentMemberName());
170 public void testCloseCandidateRegistration() throws Exception {
171 DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(
172 dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
174 Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
175 EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
177 verifyEntityOwnershipCandidateRegistration(entity, reg);
178 verifyRegisterCandidateLocal(service, entity);
182 UnregisterCandidateLocal unregCandidate = verifyMessage(service, UnregisterCandidateLocal.class);
183 assertEquals("getEntity", entity, unregCandidate.getEntity());
185 // Re-register - should succeed.
187 service.registerCandidate(entity);
188 verifyRegisterCandidateLocal(service, entity);
194 public void testListenerRegistration() {
195 DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(
196 dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
198 YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
199 Entity entity = new Entity(ENTITY_TYPE, entityId);
200 EntityOwnershipListener listener = mock(EntityOwnershipListener.class);
202 EntityOwnershipListenerRegistration reg = service.registerListener(entity.getType(), listener);
204 assertNotNull("EntityOwnershipListenerRegistration null", reg);
205 assertEquals("getEntityType", entity.getType(), reg.getEntityType());
206 assertEquals("getInstance", listener, reg.getInstance());
208 RegisterListenerLocal regListener = verifyMessage(service, RegisterListenerLocal.class);
209 assertSame("getListener", listener, regListener.getListener());
210 assertEquals("getEntityType", entity.getType(), regListener.getEntityType());
214 UnregisterListenerLocal unregListener = verifyMessage(service, UnregisterListenerLocal.class);
215 assertEquals("getEntityType", entity.getType(), unregListener.getEntityType());
216 assertSame("getListener", listener, unregListener.getListener());
222 public void testGetOwnershipState() throws Exception {
223 DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(
224 dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
226 ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners(), TreeType.OPERATIONAL);
228 when(service.getLocalEntityOwnershipShardDataTree()).thenReturn(shardDataTree.getDataTree());
230 Entity entity1 = new Entity(ENTITY_TYPE, "one");
231 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getId(), "member-1"), shardDataTree);
232 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(),
233 entityEntryWithOwner(entity1.getId(), "member-1"))), shardDataTree);
234 verifyGetOwnershipState(service, entity1, true, true);
236 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getId(), "member-2"), shardDataTree);
237 writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), "member-2"),
239 verifyGetOwnershipState(service, entity1, false, true);
241 writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), ""),
243 verifyGetOwnershipState(service, entity1, false, false);
245 Entity entity2 = new Entity(ENTITY_TYPE, "two");
246 Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
247 assertEquals("getOwnershipState present", false, state.isPresent());
249 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity2.getId(), "member-1"), shardDataTree);
250 writeNode(entityPath(entity2.getType(), entity2.getId()), ImmutableNodes.mapEntry(ENTITY_QNAME,
251 ENTITY_ID_QNAME, entity2.getId()), shardDataTree);
252 verifyGetOwnershipState(service, entity2, false, false);
254 deleteNode(candidatePath(entityPath(entity2.getType(), entity2.getId()), "member-1"), shardDataTree);
255 Optional<EntityOwnershipState> state2 = service.getOwnershipState(entity2);
256 assertEquals("getOwnershipState present", false, state2.isPresent());
261 public void testIsCandidateRegistered() throws CandidateAlreadyRegisteredException {
262 DistributedEntityOwnershipService service = DistributedEntityOwnershipService.start(dataStore.getActorContext(),
263 EntityOwnerSelectionStrategyConfig.newBuilder().build());
265 final Entity test = new Entity("test-type", "test");
267 assertFalse(service.isCandidateRegistered(test));
269 service.registerCandidate(test);
271 assertTrue(service.isCandidateRegistered(test));
276 private static void verifyGetOwnershipState(final DistributedEntityOwnershipService service, final Entity entity,
277 final boolean isOwner, final boolean hasOwner) {
278 Optional<EntityOwnershipState> state = service.getOwnershipState(entity);
279 assertEquals("getOwnershipState present", true, state.isPresent());
280 assertEquals("isOwner", isOwner, state.get().isOwner());
281 assertEquals("hasOwner", hasOwner, state.get().hasOwner());
284 private void verifyEntityCandidate(final ActorRef entityOwnershipShard, final String entityType,
285 final YangInstanceIdentifier entityId, final String candidateName) {
286 verifyEntityCandidate(entityType, entityId, candidateName,
289 return dataStore.newReadOnlyTransaction().read(path).get(5, TimeUnit.SECONDS).get();
290 } catch (Exception e) {
296 private static void verifyRegisterCandidateLocal(final DistributedEntityOwnershipService service, final Entity entity) {
297 RegisterCandidateLocal regCandidate = verifyMessage(service, RegisterCandidateLocal.class);
298 assertEquals("getEntity", entity, regCandidate.getEntity());
301 private static void verifyEntityOwnershipCandidateRegistration(final Entity entity, final EntityOwnershipCandidateRegistration reg) {
302 assertNotNull("EntityOwnershipCandidateRegistration null", reg);
303 assertEquals("getInstance", entity, reg.getInstance());
306 static class TestEntityOwnershipShard extends EntityOwnershipShard {
307 private final AtomicReference<CountDownLatch> messageReceived;
308 private final AtomicReference<Object> receivedMessage;
309 private final AtomicReference<Class<?>> messageClass;
310 private final AtomicReference<DataTree> dataTree;
312 protected TestEntityOwnershipShard(final EntityOwnershipShard.Builder builder,
313 final AtomicReference<Class<?>> messageClass, final AtomicReference<CountDownLatch> messageReceived,
314 final AtomicReference<Object> receivedMessage, final AtomicReference<DataTree> dataTree) {
316 this.messageClass = messageClass;
317 this.messageReceived = messageReceived;
318 this.receivedMessage = receivedMessage;
319 this.dataTree = dataTree;
323 public void handleCommand(final Object message) {
325 if(dataTree.get() != null && message instanceof GetShardDataTree) {
326 sender().tell(dataTree.get(), self());
328 super.handleCommand(message);
331 Class<?> expMsgClass = messageClass.get();
332 if(expMsgClass != null && expMsgClass.equals(message.getClass())) {
333 receivedMessage.set(message);
334 messageReceived.get().countDown();