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 akka.actor.ActorRef;
17 import akka.actor.PoisonPill;
18 import akka.actor.Props;
19 import com.google.common.base.Function;
20 import com.google.common.util.concurrent.Uninterruptibles;
21 import java.util.Collections;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.AtomicReference;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
30 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
31 import org.opendaylight.controller.cluster.datastore.config.Configuration;
32 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
33 import org.opendaylight.controller.cluster.datastore.config.ModuleConfig;
34 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfigProvider;
35 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
36 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
37 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
38 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
39 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
40 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
41 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
42 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
43 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
44 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
45 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
46 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
47 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
51 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
52 import scala.concurrent.Await;
53 import scala.concurrent.Future;
54 import scala.concurrent.duration.Duration;
57 * Unit tests for DistributedEntityOwnershipService.
59 * @author Thomas Pantelis
61 public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnershipTest {
62 static String ENTITY_TYPE = "test";
63 static String ENTITY_TYPE2 = "test2";
64 static int ID_COUNTER = 1;
65 static final QName QNAME = QName.create("test", "2015-08-11", "foo");
67 private final String dataStoreType = "config" + ID_COUNTER++;
68 private DistributedDataStore dataStore;
72 DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreType(dataStoreType).
73 shardInitializationTimeout(10, TimeUnit.SECONDS).build();
75 Configuration configuration = new ConfigurationImpl(new ModuleShardConfigProvider() {
77 public Map<String, ModuleConfig> retrieveModuleConfigs(Configuration configuration) {
78 return Collections.emptyMap();
82 dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, datastoreContext );
84 dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners());
88 public void tearDown() {
89 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), ActorRef.noSender());
93 public void testEntityOwnershipShardCreated() throws Exception {
94 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore);
97 Future<ActorRef> future = dataStore.getActorContext().findLocalShardAsync(
98 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
99 ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
100 assertNotNull(DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME + " not found", shardActor);
106 public void testRegisterCandidate() throws Exception {
107 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
108 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
110 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
111 return shardPropsCreator;
117 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
119 YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
120 Entity entity = new Entity(ENTITY_TYPE, entityId);
121 EntityOwnershipCandidate candidate = mock(EntityOwnershipCandidate.class);
123 EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity, candidate);
125 verifyEntityOwnershipCandidateRegistration(entity, reg);
126 verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate);
127 verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId,
128 dataStore.getActorContext().getCurrentMemberName());
130 // Register the same entity - should throw exception
132 EntityOwnershipCandidate candidate2 = mock(EntityOwnershipCandidate.class);
134 service.registerCandidate(entity, candidate2);
135 fail("Expected CandidateAlreadyRegisteredException");
136 } catch(CandidateAlreadyRegisteredException e) {
138 assertSame("getCandidate", candidate, e.getRegisteredCandidate());
139 assertEquals("getEntity", entity, e.getEntity());
142 // Register a different entity - should succeed
144 Entity entity2 = new Entity(ENTITY_TYPE2, entityId);
145 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
147 EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2, candidate);
149 verifyEntityOwnershipCandidateRegistration(entity2, reg2);
150 verifyRegisterCandidateLocal(shardPropsCreator, entity2, candidate);
151 verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId,
152 dataStore.getActorContext().getCurrentMemberName());
158 public void testCloseCandidateRegistration() throws Exception {
159 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
160 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
162 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
163 return shardPropsCreator;
169 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
171 Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
172 EntityOwnershipCandidate candidate = mock(EntityOwnershipCandidate.class);
174 EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity, candidate);
176 verifyEntityOwnershipCandidateRegistration(entity, reg);
177 verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate);
179 shardPropsCreator.expectShardMessage(UnregisterCandidateLocal.class);
183 UnregisterCandidateLocal unregCandidate = shardPropsCreator.waitForShardMessage();
184 assertEquals("getEntity", entity, unregCandidate.getEntity());
185 assertSame("getCandidate", candidate, unregCandidate.getCandidate());
187 // Re-register - should succeed.
189 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
191 service.registerCandidate(entity, candidate);
193 verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate);
199 public void testListenerRegistration() {
200 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
201 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
203 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
204 return shardPropsCreator;
210 shardPropsCreator.expectShardMessage(RegisterListenerLocal.class);
212 YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
213 Entity entity = new Entity(ENTITY_TYPE, entityId);
214 EntityOwnershipListener listener = mock(EntityOwnershipListener.class);
216 EntityOwnershipListenerRegistration reg = service.registerListener(entity.getType(), listener);
218 assertNotNull("EntityOwnershipListenerRegistration null", reg);
219 assertEquals("getEntityType", entity.getType(), reg.getEntityType());
220 assertEquals("getInstance", listener, reg.getInstance());
222 RegisterListenerLocal regListener = shardPropsCreator.waitForShardMessage();
223 assertSame("getListener", listener, regListener.getListener());
224 assertEquals("getEntityType", entity.getType(), regListener.getEntityType());
226 shardPropsCreator.expectShardMessage(UnregisterListenerLocal.class);
230 UnregisterListenerLocal unregListener = shardPropsCreator.waitForShardMessage();
231 assertEquals("getEntityType", entity.getType(), unregListener.getEntityType());
232 assertSame("getListener", listener, unregListener.getListener());
237 private void verifyEntityCandidate(ActorRef entityOwnershipShard, String entityType,
238 YangInstanceIdentifier entityId, String candidateName) {
239 verifyEntityCandidate(entityType, entityId, candidateName,
240 new Function<YangInstanceIdentifier, NormalizedNode<?,?>>() {
242 public NormalizedNode<?, ?> apply(YangInstanceIdentifier path) {
244 return dataStore.newReadOnlyTransaction().read(path).get(5, TimeUnit.SECONDS).get();
245 } catch (Exception e) {
252 private void verifyRegisterCandidateLocal(final TestShardPropsCreator shardPropsCreator, Entity entity,
253 EntityOwnershipCandidate candidate) {
254 RegisterCandidateLocal regCandidate = shardPropsCreator.waitForShardMessage();
255 assertSame("getCandidate", candidate, regCandidate.getCandidate());
256 assertEquals("getEntity", entity, regCandidate.getEntity());
259 private void verifyEntityOwnershipCandidateRegistration(Entity entity, EntityOwnershipCandidateRegistration reg) {
260 assertNotNull("EntityOwnershipCandidateRegistration null", reg);
261 assertEquals("getEntity", entity, reg.getEntity());
264 static class TestShardPropsCreator extends EntityOwnershipShardPropsCreator {
265 TestShardPropsCreator() {
269 private final AtomicReference<CountDownLatch> messageReceived = new AtomicReference<>();
270 private final AtomicReference<Object> receivedMessage = new AtomicReference<>();
271 private final AtomicReference<Class<?>> messageClass = new AtomicReference<>();
274 public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
275 DatastoreContext datastoreContext, SchemaContext schemaContext) {
276 return Props.create(TestEntityOwnershipShard.class, shardId, peerAddresses, datastoreContext,
277 schemaContext, "member-1", messageClass, messageReceived, receivedMessage);
280 @SuppressWarnings("unchecked")
281 <T> T waitForShardMessage() {
282 assertTrue("Message " + messageClass.get().getSimpleName() + " was not received",
283 Uninterruptibles.awaitUninterruptibly(messageReceived.get(), 5, TimeUnit.SECONDS));
284 assertEquals("Message type", messageClass.get(), receivedMessage.get().getClass());
285 return (T) receivedMessage.get();
288 void expectShardMessage(Class<?> ofType) {
289 messageReceived.set(new CountDownLatch(1));
290 receivedMessage.set(null);
291 messageClass.set(ofType);
295 static class TestEntityOwnershipShard extends EntityOwnershipShard {
296 private final AtomicReference<CountDownLatch> messageReceived;
297 private final AtomicReference<Object> receivedMessage;
298 private final AtomicReference<Class<?>> messageClass;
300 protected TestEntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
301 DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName,
302 AtomicReference<Class<?>> messageClass, AtomicReference<CountDownLatch> messageReceived,
303 AtomicReference<Object> receivedMessage) {
304 super(name, peerAddresses, datastoreContext, schemaContext, localMemberName);
305 this.messageClass = messageClass;
306 this.messageReceived = messageReceived;
307 this.receivedMessage = receivedMessage;
311 public void onReceiveCommand(final Object message) throws Exception {
313 super.onReceiveCommand(message);
315 Class<?> expMsgClass = messageClass.get();
316 if(expMsgClass != null && expMsgClass.equals(message.getClass())) {
317 receivedMessage.set(message);
318 messageReceived.get().countDown();