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.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
18 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
19 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
20 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner;
21 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithEntityTypeEntry;
22 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
23 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityTypeEntryWithEntityEntry;
24 import akka.actor.ActorRef;
25 import akka.actor.PoisonPill;
26 import akka.actor.Props;
27 import com.google.common.base.Function;
28 import com.google.common.base.Optional;
29 import com.google.common.util.concurrent.Uninterruptibles;
30 import java.util.Collections;
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.TimeUnit;
34 import java.util.concurrent.atomic.AtomicReference;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
39 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
40 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
41 import org.opendaylight.controller.cluster.datastore.config.Configuration;
42 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
43 import org.opendaylight.controller.cluster.datastore.config.ModuleConfig;
44 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfigProvider;
45 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
46 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
47 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
48 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
49 import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
50 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
51 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
52 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
53 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
54 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
55 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
56 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
57 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
58 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
59 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
60 import org.opendaylight.yangtools.yang.common.QName;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
64 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66 import scala.concurrent.Await;
67 import scala.concurrent.Future;
68 import scala.concurrent.duration.Duration;
71 * Unit tests for DistributedEntityOwnershipService.
73 * @author Thomas Pantelis
75 public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnershipTest {
76 static String ENTITY_TYPE = "test";
77 static String ENTITY_TYPE2 = "test2";
78 static int ID_COUNTER = 1;
79 static final QName QNAME = QName.create("test", "2015-08-11", "foo");
81 private final String dataStoreType = "config" + ID_COUNTER++;
82 private DistributedDataStore dataStore;
86 DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreType(dataStoreType).
87 shardInitializationTimeout(10, TimeUnit.SECONDS).build();
89 Configuration configuration = new ConfigurationImpl(new ModuleShardConfigProvider() {
91 public Map<String, ModuleConfig> retrieveModuleConfigs(Configuration configuration) {
92 return Collections.emptyMap();
96 dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, datastoreContext );
98 dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners());
102 public void tearDown() {
103 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), ActorRef.noSender());
107 public void testEntityOwnershipShardCreated() throws Exception {
108 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build());
111 Future<ActorRef> future = dataStore.getActorContext().findLocalShardAsync(
112 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
113 ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
114 assertNotNull(DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME + " not found", shardActor);
120 public void testRegisterCandidate() throws Exception {
121 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
122 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
124 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
125 return shardPropsCreator;
131 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
133 YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
134 Entity entity = new Entity(ENTITY_TYPE, entityId);
136 EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
138 verifyEntityOwnershipCandidateRegistration(entity, reg);
139 verifyRegisterCandidateLocal(shardPropsCreator, entity);
140 verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId,
141 dataStore.getActorContext().getCurrentMemberName());
143 // Register the same entity - should throw exception
146 service.registerCandidate(entity);
147 fail("Expected CandidateAlreadyRegisteredException");
148 } catch(CandidateAlreadyRegisteredException e) {
150 assertEquals("getEntity", entity, e.getEntity());
153 // Register a different entity - should succeed
155 Entity entity2 = new Entity(ENTITY_TYPE2, entityId);
156 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
158 EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2);
160 verifyEntityOwnershipCandidateRegistration(entity2, reg2);
161 verifyRegisterCandidateLocal(shardPropsCreator, entity2);
162 verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId,
163 dataStore.getActorContext().getCurrentMemberName());
169 public void testCloseCandidateRegistration() throws Exception {
170 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
171 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
173 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
174 return shardPropsCreator;
180 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
182 Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
184 EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
186 verifyEntityOwnershipCandidateRegistration(entity, reg);
187 verifyRegisterCandidateLocal(shardPropsCreator, entity);
189 shardPropsCreator.expectShardMessage(UnregisterCandidateLocal.class);
193 UnregisterCandidateLocal unregCandidate = shardPropsCreator.waitForShardMessage();
194 assertEquals("getEntity", entity, unregCandidate.getEntity());
196 // Re-register - should succeed.
198 shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
200 service.registerCandidate(entity);
202 verifyRegisterCandidateLocal(shardPropsCreator, entity);
208 public void testListenerRegistration() {
209 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
210 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
212 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
213 return shardPropsCreator;
219 shardPropsCreator.expectShardMessage(RegisterListenerLocal.class);
221 YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
222 Entity entity = new Entity(ENTITY_TYPE, entityId);
223 EntityOwnershipListener listener = mock(EntityOwnershipListener.class);
225 EntityOwnershipListenerRegistration reg = service.registerListener(entity.getType(), listener);
227 assertNotNull("EntityOwnershipListenerRegistration null", reg);
228 assertEquals("getEntityType", entity.getType(), reg.getEntityType());
229 assertEquals("getInstance", listener, reg.getInstance());
231 RegisterListenerLocal regListener = shardPropsCreator.waitForShardMessage();
232 assertSame("getListener", listener, regListener.getListener());
233 assertEquals("getEntityType", entity.getType(), regListener.getEntityType());
235 shardPropsCreator.expectShardMessage(UnregisterListenerLocal.class);
239 UnregisterListenerLocal unregListener = shardPropsCreator.waitForShardMessage();
240 assertEquals("getEntityType", entity.getType(), unregListener.getEntityType());
241 assertSame("getListener", listener, unregListener.getListener());
247 public void testGetOwnershipState() throws Exception {
248 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
249 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
251 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
252 return shardPropsCreator;
258 ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
259 shardPropsCreator.setDataTree(shardDataTree.getDataTree());
261 Entity entity1 = new Entity(ENTITY_TYPE, "one");
262 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(),
263 entityEntryWithOwner(entity1.getId(), "member-1"))), shardDataTree);
264 verifyGetOwnershipState(service, entity1, true, true);
266 writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), "member-2"),
268 verifyGetOwnershipState(service, entity1, false, true);
270 writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), ""),
272 verifyGetOwnershipState(service, entity1, false, false);
274 Entity entity2 = new Entity(ENTITY_TYPE, "two");
275 Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
276 assertEquals("getOwnershipState present", false, state.isPresent());
278 writeNode(entityPath(entity2.getType(), entity2.getId()), ImmutableNodes.mapEntry(ENTITY_QNAME,
279 ENTITY_ID_QNAME, entity2.getId()), shardDataTree);
280 verifyGetOwnershipState(service, entity2, false, false);
286 public void testIsCandidateRegistered() throws CandidateAlreadyRegisteredException {
287 final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
288 DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, EntityOwnerSelectionStrategyConfig.newBuilder().build()) {
290 protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
291 return shardPropsCreator;
297 final Entity test = new Entity("test-type", "test");
299 assertFalse(service.isCandidateRegistered(test));
301 service.registerCandidate(test);
303 assertTrue(service.isCandidateRegistered(test));
306 private void verifyGetOwnershipState(DistributedEntityOwnershipService service, Entity entity,
307 boolean isOwner, boolean hasOwner) {
308 Optional<EntityOwnershipState> state = service.getOwnershipState(entity);
309 assertEquals("getOwnershipState present", true, state.isPresent());
310 assertEquals("isOwner", isOwner, state.get().isOwner());
311 assertEquals("hasOwner", hasOwner, state.get().hasOwner());
314 private void verifyEntityCandidate(ActorRef entityOwnershipShard, String entityType,
315 YangInstanceIdentifier entityId, String candidateName) {
316 verifyEntityCandidate(entityType, entityId, candidateName,
317 new Function<YangInstanceIdentifier, NormalizedNode<?,?>>() {
319 public NormalizedNode<?, ?> apply(YangInstanceIdentifier path) {
321 return dataStore.newReadOnlyTransaction().read(path).get(5, TimeUnit.SECONDS).get();
322 } catch (Exception e) {
329 private void verifyRegisterCandidateLocal(final TestShardPropsCreator shardPropsCreator, Entity entity) {
330 RegisterCandidateLocal regCandidate = shardPropsCreator.waitForShardMessage();
331 assertEquals("getEntity", entity, regCandidate.getEntity());
334 private void verifyEntityOwnershipCandidateRegistration(Entity entity, EntityOwnershipCandidateRegistration reg) {
335 assertNotNull("EntityOwnershipCandidateRegistration null", reg);
336 assertEquals("getInstance", entity, reg.getInstance());
339 static class TestShardPropsCreator extends EntityOwnershipShardPropsCreator {
340 TestShardPropsCreator() {
341 super("member-1", EntityOwnerSelectionStrategyConfig.newBuilder().build());
344 private final AtomicReference<CountDownLatch> messageReceived = new AtomicReference<>();
345 private final AtomicReference<Object> receivedMessage = new AtomicReference<>();
346 private final AtomicReference<Class<?>> messageClass = new AtomicReference<>();
347 private final AtomicReference<DataTree> dataTree = new AtomicReference<>();
350 public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
351 DatastoreContext datastoreContext, SchemaContext schemaContext) {
352 return Props.create(TestEntityOwnershipShard.class, shardId, peerAddresses, datastoreContext,
353 schemaContext, "member-1", messageClass, messageReceived, receivedMessage, dataTree);
356 @SuppressWarnings("unchecked")
357 <T> T waitForShardMessage() {
358 assertTrue("Message " + messageClass.get().getSimpleName() + " was not received",
359 Uninterruptibles.awaitUninterruptibly(messageReceived.get(), 5, TimeUnit.SECONDS));
360 assertEquals("Message type", messageClass.get(), receivedMessage.get().getClass());
361 return (T) receivedMessage.get();
364 void expectShardMessage(Class<?> ofType) {
365 messageReceived.set(new CountDownLatch(1));
366 receivedMessage.set(null);
367 messageClass.set(ofType);
370 void setDataTree(DataTree tree) {
371 this.dataTree.set(tree);
375 static class TestEntityOwnershipShard extends EntityOwnershipShard {
376 private final AtomicReference<CountDownLatch> messageReceived;
377 private final AtomicReference<Object> receivedMessage;
378 private final AtomicReference<Class<?>> messageClass;
379 private final AtomicReference<DataTree> dataTree;
381 protected TestEntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
382 DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName,
383 AtomicReference<Class<?>> messageClass, AtomicReference<CountDownLatch> messageReceived,
384 AtomicReference<Object> receivedMessage, AtomicReference<DataTree> dataTree) {
385 super(name, peerAddresses, datastoreContext, schemaContext, localMemberName, EntityOwnerSelectionStrategyConfig.newBuilder().build());
386 this.messageClass = messageClass;
387 this.messageReceived = messageReceived;
388 this.receivedMessage = receivedMessage;
389 this.dataTree = dataTree;
393 public void onReceiveCommand(final Object message) throws Exception {
395 if(dataTree.get() != null && message instanceof GetShardDataTree) {
396 sender().tell(dataTree.get(), self());
398 super.onReceiveCommand(message);
401 Class<?> expMsgClass = messageClass.get();
402 if(expMsgClass != null && expMsgClass.equals(message.getClass())) {
403 receivedMessage.set(message);
404 messageReceived.get().countDown();