BUG-5280: expand ShardDataTree to cover transaction mechanics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / DistributedEntityOwnershipServiceTest.java
index b7f75c98f3b49276be2ed4dc96bb768792627df3..52adc5af42c3fa3d0911055cf2d69b24f3df8d2a 100644 (file)
@@ -7,19 +7,62 @@
  */
 package org.opendaylight.controller.cluster.datastore.entityownership;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_ID_QNAME;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.candidatePath;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithEntityTypeEntry;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
+import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityTypeEntryWithEntityEntry;
 import akka.actor.ActorRef;
-import java.util.Collections;
-import java.util.List;
+import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
+import java.util.Collection;
 import java.util.concurrent.TimeUnit;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+import org.opendaylight.controller.cluster.access.concepts.MemberName;
 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.Shard;
+import org.opendaylight.controller.cluster.datastore.ShardDataTree;
+import org.opendaylight.controller.cluster.datastore.config.Configuration;
+import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
+import org.opendaylight.controller.cluster.datastore.config.EmptyModuleShardConfigProvider;
+import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
+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.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
-import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
-import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
+import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
+import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
+import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
+import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
+import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
+import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
+import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import scala.concurrent.Await;
 import scala.concurrent.Future;
 import scala.concurrent.duration.Duration;
@@ -29,40 +72,236 @@ import scala.concurrent.duration.Duration;
  *
  * @author Thomas Pantelis
  */
-public class DistributedEntityOwnershipServiceTest extends AbstractActorTest {
-    private static int ID_COUNTER = 1;
+public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnershipTest {
+    static final String ENTITY_TYPE = "test";
+    static final String ENTITY_TYPE2 = "test2";
+    static final QName QNAME = QName.create("test", "2015-08-11", "foo");
+    static int ID_COUNTER = 1;
 
-    private final String dataStoreType = "config" + ID_COUNTER++;
-    private DistributedEntityOwnershipService service;
+    private final String dataStoreName = "config" + ID_COUNTER++;
     private DistributedDataStore dataStore;
 
     @Before
-    public void setUp() throws Exception {
-        DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreType(dataStoreType).
+    public void setUp() {
+        DatastoreContext datastoreContext = DatastoreContext.newBuilder().dataStoreName(dataStoreName).
                 shardInitializationTimeout(10, TimeUnit.SECONDS).build();
-        dataStore = new DistributedDataStore(getSystem(), new MockClusterWrapper(),
-                new MockConfiguration(Collections.<String, List<String>>emptyMap()), datastoreContext );
 
-        dataStore.onGlobalContextUpdated(TestModel.createTestContext());
+        Configuration configuration = new ConfigurationImpl(new EmptyModuleShardConfigProvider()) {
+            @Override
+            public Collection<MemberName> getUniqueMemberNamesForAllShards() {
+                return Sets.newHashSet(MemberName.forName("member-1"));
+            }
+        };
 
-        service = new DistributedEntityOwnershipService(dataStore);
+        DatastoreContextFactory mockContextFactory = 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, null);
+
+        dataStore.onGlobalContextUpdated(SchemaContextHelper.entityOwners());
+    }
+
+    @After
+    public void tearDown() {
+        dataStore.close();
+    }
+
+    private static <T> T verifyMessage(final DistributedEntityOwnershipService mock, final Class<T> type) {
+        final ArgumentCaptor<T> message = ArgumentCaptor.forClass(type);
+        verify(mock).executeLocalEntityOwnershipShardOperation(message.capture());
+        return message.getValue();
     }
 
     @Test
     public void testEntityOwnershipShardCreated() throws Exception {
-        service.start();
+        DistributedEntityOwnershipService service = DistributedEntityOwnershipService.start(dataStore.getActorContext(),
+                EntityOwnerSelectionStrategyConfig.newBuilder().build());
 
         Future<ActorRef> future = dataStore.getActorContext().findLocalShardAsync(
                 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
         ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
         assertNotNull(DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME + " not found", shardActor);
+
+        service.close();
     }
 
     @Test
-    public void testRegisterCandidate() {
+    public void testRegisterCandidate() throws Exception {
+        DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(
+            dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
+
+        YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
+        DOMEntity entity = new DOMEntity(ENTITY_TYPE, entityId);
+
+        DOMEntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
+        verifyRegisterCandidateLocal(service, entity);
+        verifyEntityOwnershipCandidateRegistration(entity, reg);
+        verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE, entityId,
+                dataStore.getActorContext().getCurrentMemberName().getName());
+
+        // Register the same entity - should throw exception
+
+        try {
+            service.registerCandidate(entity);
+            fail("Expected CandidateAlreadyRegisteredException");
+        } catch(CandidateAlreadyRegisteredException e) {
+            // expected
+            assertEquals("getEntity", entity, e.getEntity());
+        }
+
+        // Register a different entity - should succeed
+        reset(service);
+
+        DOMEntity entity2 = new DOMEntity(ENTITY_TYPE2, entityId);
+        DOMEntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2);
+
+        verifyEntityOwnershipCandidateRegistration(entity2, reg2);
+        verifyEntityCandidate(service.getLocalEntityOwnershipShard(), ENTITY_TYPE2, entityId,
+                dataStore.getActorContext().getCurrentMemberName().getName());
+
+        service.close();
     }
 
     @Test
-    public void testRegisterListener() {
+    public void testCloseCandidateRegistration() throws Exception {
+        DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(
+            dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
+
+        DOMEntity entity = new DOMEntity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
+
+        DOMEntityOwnershipCandidateRegistration reg = service.registerCandidate(entity);
+
+        verifyEntityOwnershipCandidateRegistration(entity, reg);
+        verifyRegisterCandidateLocal(service, entity);
+
+        reset(service);
+        reg.close();
+        UnregisterCandidateLocal unregCandidate = verifyMessage(service, UnregisterCandidateLocal.class);
+        assertEquals("getEntity", entity, unregCandidate.getEntity());
+
+        // Re-register - should succeed.
+        reset(service);
+        service.registerCandidate(entity);
+        verifyRegisterCandidateLocal(service, entity);
+
+        service.close();
+    }
+
+    @Test
+    public void testListenerRegistration() {
+        DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(
+            dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
+
+        YangInstanceIdentifier entityId = YangInstanceIdentifier.of(QNAME);
+        DOMEntity entity = new DOMEntity(ENTITY_TYPE, entityId);
+        DOMEntityOwnershipListener listener = mock(DOMEntityOwnershipListener.class);
+
+        DOMEntityOwnershipListenerRegistration reg = service.registerListener(entity.getType(), listener);
+
+        assertNotNull("EntityOwnershipListenerRegistration null", reg);
+        assertEquals("getEntityType", entity.getType(), reg.getEntityType());
+        assertEquals("getInstance", listener, reg.getInstance());
+
+        RegisterListenerLocal regListener = verifyMessage(service, RegisterListenerLocal.class);
+        assertSame("getListener", listener, regListener.getListener());
+        assertEquals("getEntityType", entity.getType(), regListener.getEntityType());
+
+        reset(service);
+        reg.close();
+        UnregisterListenerLocal unregListener = verifyMessage(service, UnregisterListenerLocal.class);
+        assertEquals("getEntityType", entity.getType(), unregListener.getEntityType());
+        assertSame("getListener", listener, unregListener.getListener());
+
+        service.close();
+    }
+
+    @Test
+    public void testGetOwnershipState() throws Exception {
+        DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(
+            dataStore.getActorContext(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
+
+        final Shard mockShard = Mockito.mock(Shard.class);
+        ShardDataTree shardDataTree = new ShardDataTree(mockShard, SchemaContextHelper.entityOwners(),
+            TreeType.OPERATIONAL);
+
+        when(service.getLocalEntityOwnershipShardDataTree()).thenReturn(shardDataTree.getDataTree());
+
+        DOMEntity entity1 = new DOMEntity(ENTITY_TYPE, "one");
+        writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getIdentifier(), "member-1"), shardDataTree);
+        writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(),
+                entityEntryWithOwner(entity1.getIdentifier(), "member-1"))), shardDataTree);
+        verifyGetOwnershipState(service, entity1, EntityOwnershipState.IS_OWNER);
+
+        writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE,
+                entity1.getIdentifier(), "member-2"), shardDataTree);
+        writeNode(entityPath(entity1.getType(), entity1.getIdentifier()),
+                entityEntryWithOwner(entity1.getIdentifier(), "member-2"), shardDataTree);
+        verifyGetOwnershipState(service, entity1, EntityOwnershipState.OWNED_BY_OTHER);
+
+        writeNode(entityPath(entity1.getType(), entity1.getIdentifier()), entityEntryWithOwner(entity1.getIdentifier(), ""),
+                shardDataTree);
+        verifyGetOwnershipState(service, entity1, EntityOwnershipState.NO_OWNER);
+
+        DOMEntity entity2 = new DOMEntity(ENTITY_TYPE, "two");
+        Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
+        assertEquals("getOwnershipState present", false, state.isPresent());
+
+        writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity2.getIdentifier(), "member-1"),
+                shardDataTree);
+        writeNode(entityPath(entity2.getType(), entity2.getIdentifier()), ImmutableNodes.mapEntry(ENTITY_QNAME,
+                ENTITY_ID_QNAME, entity2.getIdentifier()), shardDataTree);
+        verifyGetOwnershipState(service, entity2, EntityOwnershipState.NO_OWNER);
+
+        deleteNode(candidatePath(entityPath(entity2.getType(), entity2.getIdentifier()), "member-1"), shardDataTree);
+        Optional<EntityOwnershipState> state2 = service.getOwnershipState(entity2);
+        assertEquals("getOwnershipState present", false, state2.isPresent());
+        service.close();
+    }
+
+    @Test
+    public void testIsCandidateRegistered() throws CandidateAlreadyRegisteredException {
+        DistributedEntityOwnershipService service = DistributedEntityOwnershipService.start(dataStore.getActorContext(),
+                EntityOwnerSelectionStrategyConfig.newBuilder().build());
+
+        final DOMEntity test = new DOMEntity("test-type", "test");
+
+        assertFalse(service.isCandidateRegistered(test));
+
+        service.registerCandidate(test);
+
+        assertTrue(service.isCandidateRegistered(test));
+
+        service.close();
+    }
+
+    private static void verifyGetOwnershipState(final DistributedEntityOwnershipService service, final DOMEntity entity,
+            final EntityOwnershipState expState) {
+        Optional<EntityOwnershipState> state = service.getOwnershipState(entity);
+        assertEquals("getOwnershipState present", true, state.isPresent());
+        assertEquals("EntityOwnershipState", expState, state.get());
+    }
+
+    private void verifyEntityCandidate(final ActorRef entityOwnershipShard, final String entityType,
+            final YangInstanceIdentifier entityId, final String candidateName) {
+        verifyEntityCandidate(entityType, entityId, candidateName,
+                path -> {
+                    try {
+                        return dataStore.newReadOnlyTransaction().read(path).get(5, TimeUnit.SECONDS).get();
+                    } catch (Exception e) {
+                        return null;
+                    }
+                });
+    }
+
+    private static void verifyRegisterCandidateLocal(final DistributedEntityOwnershipService service, final DOMEntity entity) {
+        RegisterCandidateLocal regCandidate = verifyMessage(service, RegisterCandidateLocal.class);
+        assertEquals("getEntity", entity, regCandidate.getEntity());
+    }
+
+    private static void verifyEntityOwnershipCandidateRegistration(final DOMEntity entity,
+            final DOMEntityOwnershipCandidateRegistration reg) {
+        assertNotNull("EntityOwnershipCandidateRegistration null", reg);
+        assertEquals("getInstance", entity, reg.getInstance());
     }
 }