X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fentityownership%2FDistributedEntityOwnershipServiceTest.java;h=084c5e79db447b04bf464dddb67406ac90fa1dfa;hp=5591772afcdeafba3cb6f1989bd06610f537e9cc;hb=a623206f49e3c376e1a8494ba584ea0018468f12;hpb=689370fdbfdd18d6bd25359b94b1a8f6d76aba1c diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java index 5591772afc..084c5e79db 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java @@ -34,7 +34,9 @@ import java.util.concurrent.atomic.AtomicReference; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import org.opendaylight.controller.cluster.datastore.DatastoreContext; +import org.opendaylight.controller.cluster.datastore.DatastoreContextFactory; import org.opendaylight.controller.cluster.datastore.DistributedDataStore; import org.opendaylight.controller.cluster.datastore.ShardDataTree; import org.opendaylight.controller.cluster.datastore.config.Configuration; @@ -45,7 +47,7 @@ import org.opendaylight.controller.cluster.datastore.entityownership.messages.Re import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal; import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal; import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal; -import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier; +import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig; import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree; import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper; import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper; @@ -60,7 +62,6 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.Duration; @@ -91,7 +92,11 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh } }); - dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, datastoreContext ); + DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class); + Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext(); + Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString()); + + dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(), configuration, mockContextFactory); dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners()); } @@ -103,7 +108,8 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh @Test public void testEntityOwnershipShardCreated() throws Exception { - DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore); + DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, + EntityOwnerSelectionStrategyConfig.newBuilder().build()); service.start(); Future future = dataStore.getActorContext().findLocalShardAsync( @@ -116,17 +122,18 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh @Test public void testRegisterCandidate() throws Exception { - final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator(); - DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) { + final TestShardBuilder shardBuilder = new TestShardBuilder(); + DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, + EntityOwnerSelectionStrategyConfig.newBuilder().build()) { @Override - protected EntityOwnershipShardPropsCreator newShardPropsCreator() { - return shardPropsCreator; + protected EntityOwnershipShard.Builder newShardBuilder() { + return shardBuilder; } }; service.start(); - shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class); + shardBuilder.expectShardMessage(RegisterCandidateLocal.class); YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME); Entity entity = new Entity(ENTITY_TYPE, entityId); @@ -134,7 +141,7 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity); verifyEntityOwnershipCandidateRegistration(entity, reg); - verifyRegisterCandidateLocal(shardPropsCreator, entity); + verifyRegisterCandidateLocal(shardBuilder, entity); verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId, dataStore.getActorContext().getCurrentMemberName()); @@ -151,12 +158,12 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh // Register a different entity - should succeed Entity entity2 = new Entity(ENTITY_TYPE2, entityId); - shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class); + shardBuilder.expectShardMessage(RegisterCandidateLocal.class); EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2); verifyEntityOwnershipCandidateRegistration(entity2, reg2); - verifyRegisterCandidateLocal(shardPropsCreator, entity2); + verifyRegisterCandidateLocal(shardBuilder, entity2); verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId, dataStore.getActorContext().getCurrentMemberName()); @@ -165,56 +172,58 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh @Test public void testCloseCandidateRegistration() throws Exception { - final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator(); - DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) { + final TestShardBuilder shardBuilder = new TestShardBuilder(); + DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, + EntityOwnerSelectionStrategyConfig.newBuilder().build()) { @Override - protected EntityOwnershipShardPropsCreator newShardPropsCreator() { - return shardPropsCreator; + protected EntityOwnershipShard.Builder newShardBuilder() { + return shardBuilder; } }; service.start(); - shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class); + shardBuilder.expectShardMessage(RegisterCandidateLocal.class); Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME)); EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity); verifyEntityOwnershipCandidateRegistration(entity, reg); - verifyRegisterCandidateLocal(shardPropsCreator, entity); + verifyRegisterCandidateLocal(shardBuilder, entity); - shardPropsCreator.expectShardMessage(UnregisterCandidateLocal.class); + shardBuilder.expectShardMessage(UnregisterCandidateLocal.class); reg.close(); - UnregisterCandidateLocal unregCandidate = shardPropsCreator.waitForShardMessage(); + UnregisterCandidateLocal unregCandidate = shardBuilder.waitForShardMessage(); assertEquals("getEntity", entity, unregCandidate.getEntity()); // Re-register - should succeed. - shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class); + shardBuilder.expectShardMessage(RegisterCandidateLocal.class); service.registerCandidate(entity); - verifyRegisterCandidateLocal(shardPropsCreator, entity); + verifyRegisterCandidateLocal(shardBuilder, entity); service.close(); } @Test public void testListenerRegistration() { - final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator(); - DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) { + final TestShardBuilder shardBuilder = new TestShardBuilder(); + DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, + EntityOwnerSelectionStrategyConfig.newBuilder().build()) { @Override - protected EntityOwnershipShardPropsCreator newShardPropsCreator() { - return shardPropsCreator; + protected EntityOwnershipShard.Builder newShardBuilder() { + return shardBuilder; } }; service.start(); - shardPropsCreator.expectShardMessage(RegisterListenerLocal.class); + shardBuilder.expectShardMessage(RegisterListenerLocal.class); YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME); Entity entity = new Entity(ENTITY_TYPE, entityId); @@ -226,15 +235,15 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh assertEquals("getEntityType", entity.getType(), reg.getEntityType()); assertEquals("getInstance", listener, reg.getInstance()); - RegisterListenerLocal regListener = shardPropsCreator.waitForShardMessage(); + RegisterListenerLocal regListener = shardBuilder.waitForShardMessage(); assertSame("getListener", listener, regListener.getListener()); assertEquals("getEntityType", entity.getType(), regListener.getEntityType()); - shardPropsCreator.expectShardMessage(UnregisterListenerLocal.class); + shardBuilder.expectShardMessage(UnregisterListenerLocal.class); reg.close(); - UnregisterListenerLocal unregListener = shardPropsCreator.waitForShardMessage(); + UnregisterListenerLocal unregListener = shardBuilder.waitForShardMessage(); assertEquals("getEntityType", entity.getType(), unregListener.getEntityType()); assertSame("getListener", listener, unregListener.getListener()); @@ -243,18 +252,19 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh @Test public void testGetOwnershipState() throws Exception { - final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator(); - DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) { + final TestShardBuilder shardBuilder = new TestShardBuilder(); + DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore, + EntityOwnerSelectionStrategyConfig.newBuilder().build()) { @Override - protected EntityOwnershipShardPropsCreator newShardPropsCreator() { - return shardPropsCreator; + protected EntityOwnershipShard.Builder newShardBuilder() { + return shardBuilder; } }; service.start(); ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners()); - shardPropsCreator.setDataTree(shardDataTree.getDataTree()); + shardBuilder.setDataTree(shardDataTree.getDataTree()); Entity entity1 = new Entity(ENTITY_TYPE, "one"); writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(), @@ -280,7 +290,7 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh service.close(); } - private void verifyGetOwnershipState(DistributedEntityOwnershipService service, Entity entity, + private static void verifyGetOwnershipState(DistributedEntityOwnershipService service, Entity entity, boolean isOwner, boolean hasOwner) { Optional state = service.getOwnershipState(entity); assertEquals("getOwnershipState present", true, state.isPresent()); @@ -303,19 +313,20 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh }); } - private void verifyRegisterCandidateLocal(final TestShardPropsCreator shardPropsCreator, Entity entity) { - RegisterCandidateLocal regCandidate = shardPropsCreator.waitForShardMessage(); + private static void verifyRegisterCandidateLocal(final TestShardBuilder shardBuilder, Entity entity) { + RegisterCandidateLocal regCandidate = shardBuilder.waitForShardMessage(); assertEquals("getEntity", entity, regCandidate.getEntity()); } - private void verifyEntityOwnershipCandidateRegistration(Entity entity, EntityOwnershipCandidateRegistration reg) { + private static void verifyEntityOwnershipCandidateRegistration(Entity entity, EntityOwnershipCandidateRegistration reg) { assertNotNull("EntityOwnershipCandidateRegistration null", reg); assertEquals("getInstance", entity, reg.getInstance()); } - static class TestShardPropsCreator extends EntityOwnershipShardPropsCreator { - TestShardPropsCreator() { - super("member-1"); + static class TestShardBuilder extends EntityOwnershipShard.Builder { + TestShardBuilder() { + localMemberName("member-1").ownerSelectionStrategyConfig( + EntityOwnerSelectionStrategyConfig.newBuilder().build()); } private final AtomicReference messageReceived = new AtomicReference<>(); @@ -324,10 +335,10 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh private final AtomicReference dataTree = new AtomicReference<>(); @Override - public Props newProps(ShardIdentifier shardId, Map peerAddresses, - DatastoreContext datastoreContext, SchemaContext schemaContext) { - return Props.create(TestEntityOwnershipShard.class, shardId, peerAddresses, datastoreContext, - schemaContext, "member-1", messageClass, messageReceived, receivedMessage, dataTree); + public Props props() { + verify(); + return Props.create(TestEntityOwnershipShard.class,this, messageClass, messageReceived, + receivedMessage, dataTree); } @SuppressWarnings("unchecked") @@ -355,11 +366,10 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh private final AtomicReference> messageClass; private final AtomicReference dataTree; - protected TestEntityOwnershipShard(ShardIdentifier name, Map peerAddresses, - DatastoreContext datastoreContext, SchemaContext schemaContext, String localMemberName, + protected TestEntityOwnershipShard(EntityOwnershipShard.Builder builder, AtomicReference> messageClass, AtomicReference messageReceived, AtomicReference receivedMessage, AtomicReference dataTree) { - super(name, peerAddresses, datastoreContext, schemaContext, localMemberName); + super(builder); this.messageClass = messageClass; this.messageReceived = messageReceived; this.receivedMessage = receivedMessage;