X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fentityownership%2FAbstractEntityOwnershipTest.java;h=6b148af4fa1195150ce03d6ae42def94e4c46b5e;hb=f41c5e6e6f6e10b36b1e4b1992877e38e718c8fb;hp=eb797eebd1afebe6143a094ba2e0b7f0130d7ba2;hpb=0fab6c716548e89938c1a8493dc25991c006aa10;p=controller.git 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 eb797eebd1..6b148af4fa 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 @@ -24,9 +24,9 @@ import akka.pattern.Patterns; import akka.testkit.TestActorRef; import akka.util.Timeout; 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.Optional; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; @@ -58,6 +58,7 @@ 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.DataTreeCandidate; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; import org.slf4j.Logger; @@ -77,8 +78,8 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { private static final AtomicInteger NEXT_SHARD_NUM = new AtomicInteger(); - protected void verifyEntityCandidate(NormalizedNode node, String entityType, - YangInstanceIdentifier entityId, String candidateName, boolean expectPresent) { + protected void verifyEntityCandidate(final NormalizedNode node, final String entityType, + final YangInstanceIdentifier entityId, final String candidateName, final boolean expectPresent) { try { assertNotNull("Missing " + EntityOwners.QNAME.toString(), node); assertTrue(node instanceof ContainerNode); @@ -92,16 +93,17 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { entityId, true); getMapEntryNodeChild(entityEntry, Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName, expectPresent); - } catch(AssertionError e) { + } catch (AssertionError 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) { + protected void verifyEntityCandidate(final String entityType, final YangInstanceIdentifier entityId, + final String candidateName, final Function> reader, + final boolean expectPresent) { AssertionError lastError = null; Stopwatch sw = Stopwatch.createStarted(); - while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) { + while (sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) { NormalizedNode node = reader.apply(ENTITY_OWNERS_PATH); try { verifyEntityCandidate(node, entityType, entityId, candidateName, expectPresent); @@ -115,13 +117,13 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { throw lastError; } - protected void verifyEntityCandidate(String entityType, YangInstanceIdentifier entityId, String candidateName, - Function> reader) { + protected void verifyEntityCandidate(final String entityType, final YangInstanceIdentifier entityId, + final String candidateName, final Function> reader) { verifyEntityCandidate(entityType, entityId, candidateName, reader, true); } - protected MapEntryNode getMapEntryNodeChild(DataContainerNode parent, QName childMap, - QName child, Object key, boolean expectPresent) { + protected MapEntryNode getMapEntryNodeChild(final DataContainerNode parent, + final QName childMap, final QName child, final Object key, final boolean expectPresent) { Optional> childNode = parent.getChild(new NodeIdentifier(childMap)); assertEquals("Missing " + childMap.toString(), true, childNode.isPresent()); @@ -129,27 +131,27 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { MapNode entityTypeMapNode = (MapNode) childNode.get(); Optional entityTypeEntry = entityTypeMapNode.getChild(new NodeIdentifierWithPredicates( childMap, child, key)); - if(expectPresent && !entityTypeEntry.isPresent()) { + if (expectPresent && !entityTypeEntry.isPresent()) { fail("Missing " + childMap.toString() + " entry for " + key + ". Actual: " + entityTypeMapNode.getValue()); - } else if(!expectPresent && entityTypeEntry.isPresent()) { + } 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) { + static void verifyOwner(final String expected, final String entityType, final YangInstanceIdentifier entityId, + final Function> reader) { AssertionError lastError = null; YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME); Stopwatch sw = Stopwatch.createStarted(); - while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) { + 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) { + } catch (AssertionError e) { lastError = e; Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); } @@ -158,16 +160,28 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { throw lastError; } - protected void verifyNodeRemoved(YangInstanceIdentifier path, - Function> reader) { + @SuppressWarnings("checkstyle:IllegalCatch") + static void verifyOwner(final TestActorRef shard, final String entityType, + final YangInstanceIdentifier entityId, final String localMemberName) { + verifyOwner(localMemberName, entityType, entityId, path -> { + try { + return AbstractShardTest.readStore(shard, path); + } catch (Exception e) { + return null; + } + }); + } + + protected void verifyNodeRemoved(final YangInstanceIdentifier path, + final Function> reader) { AssertionError lastError = null; Stopwatch sw = Stopwatch.createStarted(); - while(sw.elapsed(TimeUnit.MILLISECONDS) <= 5000) { + 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) { + } catch (AssertionError e) { lastError = e; Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); } @@ -176,23 +190,27 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { throw lastError; } - static void writeNode(YangInstanceIdentifier path, NormalizedNode node, ShardDataTree shardDataTree) - throws DataValidationFailedException { + static void writeNode(final YangInstanceIdentifier path, final NormalizedNode node, + final ShardDataTree shardDataTree) throws DataValidationFailedException { DataTreeModification modification = shardDataTree.newModification(); modification.merge(path, node); commit(shardDataTree, modification); } - static void deleteNode(YangInstanceIdentifier path, ShardDataTree shardDataTree) + static void deleteNode(final YangInstanceIdentifier path, final ShardDataTree shardDataTree) throws DataValidationFailedException { DataTreeModification modification = shardDataTree.newModification(); modification.delete(path); commit(shardDataTree, modification); } - static void commit(ShardDataTree shardDataTree, DataTreeModification modification) + static void commit(final ShardDataTree shardDataTree, final DataTreeModification modification) throws DataValidationFailedException { - shardDataTree.notifyListeners(shardDataTree.commit(modification)); + modification.ready(); + shardDataTree.getDataTree().validate(modification); + final DataTreeCandidate candidate = shardDataTree.getDataTree().prepare(modification); + shardDataTree.getDataTree().commit(candidate); + shardDataTree.notifyListeners(candidate); } static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity, final boolean expWasOwner, @@ -204,15 +222,15 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { final boolean expIsOwner, final boolean expHasOwner, final boolean expInJeopardy) { return Matchers.argThat(new ArgumentMatcher() { @Override - public boolean matches(Object argument) { + public boolean matches(final Object argument) { DOMEntityOwnershipChange change = (DOMEntityOwnershipChange) argument; - return expEntity.equals(change.getEntity()) && expWasOwner == change.getState().wasOwner() && - expIsOwner == change.getState().isOwner() && expHasOwner == change.getState().hasOwner() && - expInJeopardy == change.inJeopardy(); + return expEntity.equals(change.getEntity()) && expWasOwner == change.getState().wasOwner() + && expIsOwner == change.getState().isOwner() && expHasOwner == change.getState().hasOwner() + && expInJeopardy == change.inJeopardy(); } @Override - public void describeTo(Description description) { + public void describeTo(final Description description) { description.appendValue(new DOMEntityOwnershipChange(expEntity, EntityOwnershipChangeState.from( expWasOwner, expIsOwner, expHasOwner), expInJeopardy)); } @@ -222,36 +240,26 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { static DOMEntityOwnershipChange ownershipChange(final DOMEntity expEntity) { return Matchers.argThat(new ArgumentMatcher() { @Override - public boolean matches(Object argument) { + public boolean matches(final Object argument) { DOMEntityOwnershipChange change = (DOMEntityOwnershipChange) argument; return expEntity.equals(change.getEntity()); } @Override - public void describeTo(Description description) { + public void describeTo(final Description description) { description.appendValue(new DOMEntityOwnershipChange(expEntity, EntityOwnershipChangeState.from( false, false, false))); } }); } - static void verifyOwner(final TestActorRef shard, String entityType, - YangInstanceIdentifier entityId, String localMemberName) { - verifyOwner(localMemberName, entityType, entityId, path -> { - try { - return AbstractShardTest.readStore(shard, path); - } catch(Exception e) { - return null; - } - }); - } - - static void verifyNoOwnerSet(TestActorRef shard, String entityType, - YangInstanceIdentifier entityId) { + @SuppressWarnings("checkstyle:IllegalCatch") + static void verifyNoOwnerSet(final TestActorRef shard, final String entityType, + final YangInstanceIdentifier entityId) { YangInstanceIdentifier entityPath = entityPath(entityType, entityId).node(ENTITY_OWNER_QNAME); try { NormalizedNode node = AbstractShardTest.readStore(shard, entityPath); - if(node != null) { + if (node != null) { Assert.fail("Owner " + node.getValue() + " was set for " + entityPath); } @@ -260,11 +268,12 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { } } - static void verifyRaftState(final TestActorRef shard, Consumer verifier) + static void verifyRaftState(final TestActorRef shard, + final Consumer verifier) throws Exception { AssertionError lastError = null; Stopwatch sw = Stopwatch.createStarted(); - while(sw.elapsed(TimeUnit.SECONDS) <= 5) { + while (sw.elapsed(TimeUnit.SECONDS) <= 5) { FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS); Future future = Patterns.ask(shard, GetOnDemandRaftState.INSTANCE, new Timeout(operationDuration)); OnDemandRaftState raftState = (OnDemandRaftState)Await.result(future, operationDuration); @@ -280,40 +289,42 @@ public class AbstractEntityOwnershipTest extends AbstractActorTest { throw lastError; } - static ShardIdentifier newShardId(String memberName) { + static ShardIdentifier newShardId(final String memberName) { return ShardIdentifier.create("entity-ownership", MemberName.forName(memberName), "operational" + NEXT_SHARD_NUM.getAndIncrement()); } - void verifyEntityCandidateRemoved(final TestActorRef shard, String entityType, - YangInstanceIdentifier entityId, String candidateName) { - verifyNodeRemoved(candidatePath(entityType, entityId, candidateName), - path -> { - try { - return AbstractShardTest.readStore(shard, path); - } catch(Exception e) { - throw new AssertionError("Failed to read " + path, e); - } - }); + @SuppressWarnings("checkstyle:IllegalCatch") + void verifyEntityCandidateRemoved(final TestActorRef shard, final String entityType, + final YangInstanceIdentifier entityId, final String candidateName) { + verifyNodeRemoved(candidatePath(entityType, entityId, candidateName), path -> { + try { + return AbstractShardTest.readStore(shard, path); + } catch (Exception e) { + throw new AssertionError("Failed to read " + path, e); + } + }); } - void verifyCommittedEntityCandidate(final TestActorRef shard, String entityType, - YangInstanceIdentifier entityId, String candidateName) { + @SuppressWarnings("checkstyle:IllegalCatch") + void verifyCommittedEntityCandidate(final TestActorRef shard, + final String entityType, final YangInstanceIdentifier entityId, final String candidateName) { verifyEntityCandidate(entityType, entityId, candidateName, path -> { try { return AbstractShardTest.readStore(shard, path); - } catch(Exception e) { + } catch (Exception e) { throw new AssertionError("Failed to read " + path, e); } }); } - void verifyNoEntityCandidate(final TestActorRef shard, String entityType, - YangInstanceIdentifier entityId, String candidateName) { + @SuppressWarnings("checkstyle:IllegalCatch") + void verifyNoEntityCandidate(final TestActorRef shard, final String entityType, + final YangInstanceIdentifier entityId, final String candidateName) { verifyEntityCandidate(entityType, entityId, candidateName, path -> { try { return AbstractShardTest.readStore(shard, path); - } catch(Exception e) { + } catch (Exception e) { throw new AssertionError("Failed to read " + path, e); } }, false);