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_OWNERS_PATH;
17 import akka.actor.ActorRef;
18 import akka.actor.PoisonPill;
19 import akka.actor.Props;
20 import com.google.common.base.Optional;
21 import com.google.common.base.Stopwatch;
22 import com.google.common.util.concurrent.Uninterruptibles;
23 import java.util.Collections;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicReference;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
32 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
33 import org.opendaylight.controller.cluster.datastore.config.Configuration;
34 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
35 import org.opendaylight.controller.cluster.datastore.config.ModuleConfig;
36 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfigProvider;
37 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
38 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
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.sal.core.spi.data.DOMStoreReadTransaction;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import scala.concurrent.Await;
52 import scala.concurrent.Future;
53 import scala.concurrent.duration.Duration;
56 * Unit tests for DistributedEntityOwnershipService.
58 * @author Thomas Pantelis
60 public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnershipTest {
61 static String ENTITY_TYPE = "test";
62 static String ENTITY_TYPE2 = "test2";
63 static int ID_COUNTER = 1;
64 static final QName QNAME = QName.create("test", "2015-08-11", "foo");
66 private final String dataStoreType = "config" + ID_COUNTER++;
67 private DistributedDataStore dataStore;
71 DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreType(dataStoreType).
72 shardInitializationTimeout(10, TimeUnit.SECONDS).build();
74 Configuration configuration = new ConfigurationImpl(new ModuleShardConfigProvider() {
76 public Map<String, ModuleConfig> retrieveModuleConfigs(Configuration configuration) {
77 return Collections.emptyMap();
81 dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, datastoreContext );
83 dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners());
87 public void tearDown() {
88 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), ActorRef.noSender());
92 public void testEntityOwnershipShardCreated() throws Exception {
93 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore);
96 Future<ActorRef> future = dataStore.getActorContext().findLocalShardAsync(
97 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
98 ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
99 assertNotNull(DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME + " not found", shardActor);
105 public void testRegisterCandidate() throws Exception {
106 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
107 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
109 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
110 return shardPropsCreator;
116 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
118 YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
119 Entity entity = new Entity(ENTITY_TYPE, entityId);
120 EntityOwnershipCandidate candidate = mock(EntityOwnershipCandidate.class);
122 EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity, candidate);
124 verifyEntityOwnershipCandidateRegistration(entity, reg);
125 verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate);
126 verifyEntityCandidate(readEntityOwners(service.getLocalEntityOwnershipShard()), ENTITY_TYPE, entityId,
127 dataStore.getActorContext().getCurrentMemberName());
129 // Register the same entity - should throw exception
131 EntityOwnershipCandidate candidate2 = mock(EntityOwnershipCandidate.class);
133 service.registerCandidate(entity, candidate2);
134 fail("Expected CandidateAlreadyRegisteredException");
135 } catch(CandidateAlreadyRegisteredException e) {
137 assertSame("getCandidate", candidate, e.getRegisteredCandidate());
138 assertEquals("getEntity", entity, e.getEntity());
141 // Register a different entity - should succeed
143 Entity entity2 = new Entity(ENTITY_TYPE2, entityId);
144 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
146 EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2, candidate);
148 verifyEntityOwnershipCandidateRegistration(entity2, reg2);
149 verifyRegisterCandidateLocal(shardPropsCreator, entity2, candidate);
150 verifyEntityCandidate(readEntityOwners(service.getLocalEntityOwnershipShard()), ENTITY_TYPE2, entityId,
151 dataStore.getActorContext().getCurrentMemberName());
157 public void testCloseCandidateRegistration() throws Exception {
158 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
159 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
161 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
162 return shardPropsCreator;
168 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
170 Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
171 EntityOwnershipCandidate candidate = mock(EntityOwnershipCandidate.class);
173 EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity, candidate);
175 verifyEntityOwnershipCandidateRegistration(entity, reg);
176 verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate);
178 shardPropsCreator.expectShardMessage(UnregisterCandidateLocal.class);
182 UnregisterCandidateLocal unregCandidate = shardPropsCreator.waitForShardMessage();
183 assertEquals("getEntity", entity, unregCandidate.getEntity());
184 assertSame("getCandidate", candidate, unregCandidate.getCandidate());
186 // Re-register - should succeed.
188 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
190 service.registerCandidate(entity, candidate);
192 verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate);
198 public void testRegisterListener() {
201 private NormalizedNode<?, ?> readEntityOwners(ActorRef shard) throws Exception {
202 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
203 Stopwatch sw = Stopwatch.createStarted();
204 while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) {
205 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
206 Optional<NormalizedNode<?, ?>> optional = readTx.read(ENTITY_OWNERS_PATH).
207 checkedGet(5, TimeUnit.SECONDS);
208 if(optional.isPresent()) {
209 return optional.get();
212 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
218 private void verifyRegisterCandidateLocal(final TestShardPropsCreator shardPropsCreator, Entity entity,
219 EntityOwnershipCandidate candidate) {
220 RegisterCandidateLocal regCandidate = shardPropsCreator.waitForShardMessage();
221 assertSame("getCandidate", candidate, regCandidate.getCandidate());
222 assertEquals("getEntity", entity, regCandidate.getEntity());
225 private void verifyEntityOwnershipCandidateRegistration(Entity entity, EntityOwnershipCandidateRegistration reg) {
226 assertNotNull("EntityOwnershipCandidateRegistration null", reg);
227 assertEquals("getEntity", entity, reg.getEntity());
230 static class TestShardPropsCreator extends EntityOwnershipShardPropsCreator {
231 TestShardPropsCreator() {
235 private final AtomicReference<CountDownLatch> messageReceived = new AtomicReference<>();
236 private final AtomicReference<Object> receivedMessage = new AtomicReference<>();
237 private final AtomicReference<Class<?>> messageClass = new AtomicReference<>();
240 public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
241 DatastoreContext datastoreContext, SchemaContext schemaContext) {
242 return Props.create(TestEntityOwnershipShard.class, shardId, peerAddresses, datastoreContext,
243 schemaContext, "member-1", messageClass, messageReceived, receivedMessage);
246 @SuppressWarnings("unchecked")
247 <T> T waitForShardMessage() {
248 assertTrue("Message " + messageClass.get().getSimpleName() + " was not received",
249 Uninterruptibles.awaitUninterruptibly(messageReceived.get(), 5, TimeUnit.SECONDS));
250 assertEquals("Message type", messageClass.get(), receivedMessage.get().getClass());
251 return (T) receivedMessage.get();
254 void expectShardMessage(Class<?> ofType) {
255 messageReceived.set(new CountDownLatch(1));
256 receivedMessage.set(null);
257 messageClass.set(ofType);
261 static class TestEntityOwnershipShard extends EntityOwnershipShard {
262 private final AtomicReference<CountDownLatch> messageReceived;
263 private final AtomicReference<Object> receivedMessage;
264 private final AtomicReference<Class<?>> messageClass;
266 protected TestEntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
267 DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName,
268 AtomicReference<Class<?>> messageClass, AtomicReference<CountDownLatch> messageReceived,
269 AtomicReference<Object> receivedMessage) {
270 super(name, peerAddresses, datastoreContext, schemaContext, localMemberName);
271 this.messageClass = messageClass;
272 this.messageReceived = messageReceived;
273 this.receivedMessage = receivedMessage;
277 public void onReceiveCommand(final Object message) throws Exception {
279 super.onReceiveCommand(message);
281 Class<?> expMsgClass = messageClass.get();
282 if(expMsgClass != null && expMsgClass.equals(message.getClass())) {
283 receivedMessage.set(message);
284 messageReceived.get().countDown();