+ 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<YangInstanceIdentifier,NormalizedNode<?,?>> 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<YangInstanceIdentifier,NormalizedNode<?,?>> 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<EntityOwnershipChange>() {
+ @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<EntityOwnershipChange>() {
+ @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));
+ }
+ });