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.assertNotNull;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.mock;
16 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
17 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
18 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
19 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner;
20 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithEntityTypeEntry;
21 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
22 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityTypeEntryWithEntityEntry;
23 import akka.actor.ActorRef;
24 import akka.actor.PoisonPill;
25 import akka.actor.Props;
26 import com.google.common.base.Function;
27 import com.google.common.base.Optional;
28 import com.google.common.util.concurrent.Uninterruptibles;
29 import java.util.Collections;
31 import java.util.concurrent.CountDownLatch;
32 import java.util.concurrent.TimeUnit;
33 import java.util.concurrent.atomic.AtomicReference;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
38 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
39 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
40 import org.opendaylight.controller.cluster.datastore.config.Configuration;
41 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
42 import org.opendaylight.controller.cluster.datastore.config.ModuleConfig;
43 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfigProvider;
44 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
45 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
46 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
47 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
48 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
49 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
50 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
51 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
52 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
53 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
54 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
55 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
56 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
57 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
58 import org.opendaylight.yangtools.yang.common.QName;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
62 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
63 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
64 import scala.concurrent.Await;
65 import scala.concurrent.Future;
66 import scala.concurrent.duration.Duration;
69 * Unit tests for DistributedEntityOwnershipService.
71 * @author Thomas Pantelis
73 public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnershipTest {
74 static String ENTITY_TYPE = "test";
75 static String ENTITY_TYPE2 = "test2";
76 static int ID_COUNTER = 1;
77 static final QName QNAME = QName.create("test", "2015-08-11", "foo");
79 private final String dataStoreType = "config" + ID_COUNTER++;
80 private DistributedDataStore dataStore;
84 DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreType(dataStoreType).
85 shardInitializationTimeout(10, TimeUnit.SECONDS).build();
87 Configuration configuration = new ConfigurationImpl(new ModuleShardConfigProvider() {
89 public Map<String, ModuleConfig> retrieveModuleConfigs(Configuration configuration) {
90 return Collections.emptyMap();
94 dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, datastoreContext );
96 dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners());
100 public void tearDown() {
101 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), ActorRef.noSender());
105 public void testEntityOwnershipShardCreated() throws Exception {
106 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore);
109 Future<ActorRef> future = dataStore.getActorContext().findLocalShardAsync(
110 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
111 ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
112 assertNotNull(DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME + " not found", shardActor);
118 public void testRegisterCandidate() throws Exception {
119 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
120 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
122 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
123 return shardPropsCreator;
129 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
131 YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
132 Entity entity = new Entity(ENTITY_TYPE, entityId);
134 EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
136 verifyEntityOwnershipCandidateRegistration(entity, reg);
137 verifyRegisterCandidateLocal(shardPropsCreator, entity);
138 verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId,
139 dataStore.getActorContext().getCurrentMemberName());
141 // Register the same entity - should throw exception
144 service.registerCandidate(entity);
145 fail("Expected CandidateAlreadyRegisteredException");
146 } catch(CandidateAlreadyRegisteredException e) {
148 assertEquals("getEntity", entity, e.getEntity());
151 // Register a different entity - should succeed
153 Entity entity2 = new Entity(ENTITY_TYPE2, entityId);
154 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
156 EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2);
158 verifyEntityOwnershipCandidateRegistration(entity2, reg2);
159 verifyRegisterCandidateLocal(shardPropsCreator, entity2);
160 verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId,
161 dataStore.getActorContext().getCurrentMemberName());
167 public void testCloseCandidateRegistration() throws Exception {
168 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
169 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
171 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
172 return shardPropsCreator;
178 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
180 Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
182 EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
184 verifyEntityOwnershipCandidateRegistration(entity, reg);
185 verifyRegisterCandidateLocal(shardPropsCreator, entity);
187 shardPropsCreator.expectShardMessage(UnregisterCandidateLocal.class);
191 UnregisterCandidateLocal unregCandidate = shardPropsCreator.waitForShardMessage();
192 assertEquals("getEntity", entity, unregCandidate.getEntity());
194 // Re-register - should succeed.
196 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
198 service.registerCandidate(entity);
200 verifyRegisterCandidateLocal(shardPropsCreator, entity);
206 public void testListenerRegistration() {
207 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
208 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
210 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
211 return shardPropsCreator;
217 shardPropsCreator.expectShardMessage(RegisterListenerLocal.class);
219 YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
220 Entity entity = new Entity(ENTITY_TYPE, entityId);
221 EntityOwnershipListener listener = mock(EntityOwnershipListener.class);
223 EntityOwnershipListenerRegistration reg = service.registerListener(entity.getType(), listener);
225 assertNotNull("EntityOwnershipListenerRegistration null", reg);
226 assertEquals("getEntityType", entity.getType(), reg.getEntityType());
227 assertEquals("getInstance", listener, reg.getInstance());
229 RegisterListenerLocal regListener = shardPropsCreator.waitForShardMessage();
230 assertSame("getListener", listener, regListener.getListener());
231 assertEquals("getEntityType", entity.getType(), regListener.getEntityType());
233 shardPropsCreator.expectShardMessage(UnregisterListenerLocal.class);
237 UnregisterListenerLocal unregListener = shardPropsCreator.waitForShardMessage();
238 assertEquals("getEntityType", entity.getType(), unregListener.getEntityType());
239 assertSame("getListener", listener, unregListener.getListener());
245 public void testGetOwnershipState() throws Exception {
246 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
247 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
249 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
250 return shardPropsCreator;
256 ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
257 shardPropsCreator.setDataTree(shardDataTree.getDataTree());
259 Entity entity1 = new Entity(ENTITY_TYPE, "one");
260 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(),
261 entityEntryWithOwner(entity1.getId(), "member-1"))), shardDataTree);
262 verifyGetOwnershipState(service, entity1, true, true);
264 writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), "member-2"),
266 verifyGetOwnershipState(service, entity1, false, true);
268 writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), ""),
270 verifyGetOwnershipState(service, entity1, false, false);
272 Entity entity2 = new Entity(ENTITY_TYPE, "two");
273 Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
274 assertEquals("getOwnershipState present", false, state.isPresent());
276 writeNode(entityPath(entity2.getType(), entity2.getId()), ImmutableNodes.mapEntry(ENTITY_QNAME,
277 ENTITY_ID_QNAME, entity2.getId()), shardDataTree);
278 verifyGetOwnershipState(service, entity2, false, false);
283 private static void verifyGetOwnershipState(DistributedEntityOwnershipService service, Entity entity,
284 boolean isOwner, boolean hasOwner) {
285 Optional<EntityOwnershipState> state = service.getOwnershipState(entity);
286 assertEquals("getOwnershipState present", true, state.isPresent());
287 assertEquals("isOwner", isOwner, state.get().isOwner());
288 assertEquals("hasOwner", hasOwner, state.get().hasOwner());
291 private void verifyEntityCandidate(ActorRef entityOwnershipShard, String entityType,
292 YangInstanceIdentifier entityId, String candidateName) {
293 verifyEntityCandidate(entityType, entityId, candidateName,
294 new Function<YangInstanceIdentifier, NormalizedNode<?,?>>() {
296 public NormalizedNode<?, ?> apply(YangInstanceIdentifier path) {
298 return dataStore.newReadOnlyTransaction().read(path).get(5, TimeUnit.SECONDS).get();
299 } catch (Exception e) {
306 private static void verifyRegisterCandidateLocal(final TestShardPropsCreator shardPropsCreator, Entity entity) {
307 RegisterCandidateLocal regCandidate = shardPropsCreator.waitForShardMessage();
308 assertEquals("getEntity", entity, regCandidate.getEntity());
311 private static void verifyEntityOwnershipCandidateRegistration(Entity entity, EntityOwnershipCandidateRegistration reg) {
312 assertNotNull("EntityOwnershipCandidateRegistration null", reg);
313 assertEquals("getInstance", entity, reg.getInstance());
316 static class TestShardPropsCreator extends EntityOwnershipShardPropsCreator {
317 TestShardPropsCreator() {
321 private final AtomicReference<CountDownLatch> messageReceived = new AtomicReference<>();
322 private final AtomicReference<Object> receivedMessage = new AtomicReference<>();
323 private final AtomicReference<Class<?>> messageClass = new AtomicReference<>();
324 private final AtomicReference<DataTree> dataTree = new AtomicReference<>();
327 public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
328 DatastoreContext datastoreContext, SchemaContext schemaContext) {
329 return Props.create(TestEntityOwnershipShard.class, shardId, peerAddresses, datastoreContext,
330 schemaContext, "member-1", messageClass, messageReceived, receivedMessage, dataTree);
333 @SuppressWarnings("unchecked")
334 <T> T waitForShardMessage() {
335 assertTrue("Message " + messageClass.get().getSimpleName() + " was not received",
336 Uninterruptibles.awaitUninterruptibly(messageReceived.get(), 5, TimeUnit.SECONDS));
337 assertEquals("Message type", messageClass.get(), receivedMessage.get().getClass());
338 return (T) receivedMessage.get();
341 void expectShardMessage(Class<?> ofType) {
342 messageReceived.set(new CountDownLatch(1));
343 receivedMessage.set(null);
344 messageClass.set(ofType);
347 void setDataTree(DataTree tree) {
348 this.dataTree.set(tree);
352 static class TestEntityOwnershipShard extends EntityOwnershipShard {
353 private final AtomicReference<CountDownLatch> messageReceived;
354 private final AtomicReference<Object> receivedMessage;
355 private final AtomicReference<Class<?>> messageClass;
356 private final AtomicReference<DataTree> dataTree;
358 protected TestEntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
359 DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName,
360 AtomicReference<Class<?>> messageClass, AtomicReference<CountDownLatch> messageReceived,
361 AtomicReference<Object> receivedMessage, AtomicReference<DataTree> dataTree) {
362 super(name, peerAddresses, datastoreContext, schemaContext, localMemberName);
363 this.messageClass = messageClass;
364 this.messageReceived = messageReceived;
365 this.receivedMessage = receivedMessage;
366 this.dataTree = dataTree;
370 public void onReceiveCommand(final Object message) throws Exception {
372 if(dataTree.get() != null && message instanceof GetShardDataTree) {
373 sender().tell(dataTree.get(), self());
375 super.onReceiveCommand(message);
378 Class<?> expMsgClass = messageClass.get();
379 if(expMsgClass != null && expMsgClass.equals(message.getClass())) {
380 receivedMessage.set(message);
381 messageReceived.get().countDown();