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%2FAbstractEntityOwnershipTest.java;h=ea7648efb9df918bd034f858f6d26eebd5a48afd;hp=3d31a06349897394e4443cbaf869fc6f73096943;hb=1f3f61ea49191bb0ada2d4de831a10f0a38a104d;hpb=a8f617e6dd21f9a453e9ece1e57f4549899584e7 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/AbstractEntityOwnershipTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/AbstractEntityOwnershipTest.java index 3d31a06349..ea7648efb9 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/AbstractEntityOwnershipTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/AbstractEntityOwnershipTest.java @@ -10,12 +10,27 @@ package org.opendaylight.controller.cluster.datastore.entityownership; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.CANDIDATE_NAME_QNAME; 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_OWNER_QNAME; import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME; import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_TYPE_QNAME; +import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath; +import com.google.common.base.Function; import com.google.common.base.Optional; +import com.google.common.base.Stopwatch; +import com.google.common.util.concurrent.Uninterruptibles; +import java.util.concurrent.TimeUnit; +import org.hamcrest.Description; +import org.junit.Assert; +import org.mockito.ArgumentMatcher; +import org.mockito.Matchers; import org.opendaylight.controller.cluster.datastore.AbstractActorTest; +import org.opendaylight.controller.cluster.datastore.ShardDataTree; +import org.opendaylight.controller.md.sal.common.api.clustering.Entity; +import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.entity.Candidate; @@ -30,6 +45,9 @@ import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; /** * Abstract base class providing utility methods. @@ -38,7 +56,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; */ public class AbstractEntityOwnershipTest extends AbstractActorTest { protected void verifyEntityCandidate(NormalizedNode node, String entityType, - YangInstanceIdentifier entityId, String candidateName) { + YangInstanceIdentifier entityId, String candidateName, boolean expectPresent) { try { assertNotNull("Missing " + EntityOwners.QNAME.toString(), node); assertTrue(node instanceof ContainerNode); @@ -46,18 +64,42 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { ContainerNode entityOwnersNode = (ContainerNode) node; MapEntryNode entityTypeEntry = getMapEntryNodeChild(entityOwnersNode, EntityType.QNAME, - ENTITY_TYPE_QNAME, entityType); + ENTITY_TYPE_QNAME, entityType, true); - MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME, entityId); + MapEntryNode entityEntry = getMapEntryNodeChild(entityTypeEntry, ENTITY_QNAME, ENTITY_ID_QNAME, + entityId, true); - getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName); + getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName, expectPresent); } catch(AssertionError e) { - throw new AssertionError("Verification of enitity candidate failed - returned data was: " + node, e); + throw new AssertionError("Verification of entity candidate failed - returned data was: " + node, e); } } + protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName, + Function> reader, boolean expectPresent) { + AssertionError lastError = null; + Stopwatch sw = Stopwatch.createStarted(); + while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) { + NormalizedNode node = reader.apply(ENTITY_OWNERS_PATH); + try { + verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent); + return; + } catch (AssertionError e) { + lastError = e; + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); + } + } + + throw lastError; + } + + protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName, + Function> reader) { + verifyEntityCandidate(entityType, entityId, candidateName, reader, true); + } + protected MapEntryNode getMapEntryNodeChild(DataContainerNode parent, QName childMap, - QName child, Object key) { + QName child, Object key, boolean expectPresent) { Optional> childNode = parent.getChild(new NodeIdentifier(childMap)); assertEquals("Missing " + childMap.toString(), true, childNode.isPresent()); @@ -65,7 +107,106 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { MapNode entityTypeMapNode = (MapNode) childNode.get(); Optional entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates( childMap, child, key)); - assertEquals("Missing " + childMap.toString() + " entry for " + key, true, entityTypeEntry.isPresent()); - return entityTypeEntry.get(); + if(expectPresent && !entityTypeEntry.isPresent()) { + fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue()); + } else if(!expectPresent && entityTypeEntry.isPresent()) { + fail("Found unexpected " + childMap.toString() + " entry for " + key); + } + + return entityTypeEntry.isPresent() ? entityTypeEntry.get() : null; + } + + static void verifyOwner(String expected, String entityType, YangInstanceIdentifier entityId, + Function> reader) { + AssertionError lastError = null; + YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME); + Stopwatch sw = Stopwatch.createStarted(); + while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) { + try { + NormalizedNode node = reader.apply(entityPath); + Assert.assertNotNull("Owner was not set for entityId: " + entityId, node); + Assert.assertEquals("Entity owner", expected, node.getValue().toString()); + return; + } catch(AssertionError e) { + lastError = e; + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); + } + } + + throw lastError; + } + + protected void verifyNodeRemoved(YangInstanceIdentifier path, + Function> reader) { + AssertionError lastError = null; + Stopwatch sw = Stopwatch.createStarted(); + while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) { + try { + NormalizedNode node = reader.apply(path); + Assert.assertNull("Node was not removed at path: " + path, node); + return; + } catch(AssertionError e) { + lastError = e; + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); + } + } + + throw lastError; + } + + static void writeNode(YangInstanceIdentifier path, NormalizedNode node, ShardDataTree shardDataTree) + throws DataValidationFailedException { + DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification(); + modification.merge(path, node); + commit(shardDataTree, modification); + } + + static void deleteNode(YangInstanceIdentifier path, ShardDataTree shardDataTree) + throws DataValidationFailedException { + DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification(); + modification.delete(path); + commit(shardDataTree, modification); + } + + static void commit(ShardDataTree shardDataTree, DataTreeModification modification) + throws DataValidationFailedException { + modification.ready(); + + shardDataTree.getDataTree().validate(modification); + DataTreeCandidateTip candidate = shardDataTree.getDataTree().prepare(modification); + shardDataTree.getDataTree().commit(candidate); + shardDataTree.notifyListeners(candidate); + } + + static EntityOwnershipChange ownershipChange(final Entity expEntity, final boolean expWasOwner, + final boolean expIsOwner, final boolean expHasOwner) { + return Matchers.argThat(new ArgumentMatcher() { + @Override + public boolean matches(Object argument) { + EntityOwnershipChange change = (EntityOwnershipChange) argument; + return expEntity.equals(change.getEntity()) && expWasOwner == change.wasOwner() && + expIsOwner == change.isOwner() && expHasOwner == change.hasOwner(); + } + + @Override + public void describeTo(Description description) { + description.appendValue(new EntityOwnershipChange(expEntity, expWasOwner, expIsOwner, expHasOwner)); + } + }); + } + + static EntityOwnershipChange ownershipChange(final Entity expEntity) { + return Matchers.argThat(new ArgumentMatcher() { + @Override + public boolean matches(Object argument) { + EntityOwnershipChange change = (EntityOwnershipChange) argument; + return expEntity.equals(change.getEntity()); + } + + @Override + public void describeTo(Description description) { + description.appendValue(new EntityOwnershipChange(expEntity, false, false, false)); + } + }); } }