private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
private static final InstanceIdentifier PUBLIC_ROOT_PATH = InstanceIdentifier.builder().build();
-
private final ListeningExecutorService executor;
private final String name;
private final AtomicLong txCounter = new AtomicLong(0);
final InstanceIdentifier path, final L listener, final DataChangeScope scope) {
/*
- * Make sure commit is not occurring right now. Listener has to be registered and its
- * state capture enqueued at a consistent point.
+ * Make sure commit is not occurring right now. Listener has to be
+ * registered and its state capture enqueued at a consistent point.
*
- * FIXME: improve this to read-write lock, such that multiple listener registrations
- * can occur simultaneously
+ * FIXME: improve this to read-write lock, such that multiple listener
+ * registrations can occur simultaneously
*/
final DataChangeListenerRegistration<L> reg;
synchronized (this) {
- LOG.debug("{}: Registering data change listener {} for {}",name,listener,path);
+ LOG.debug("{}: Registering data change listener {} for {}", name, listener, path);
reg = listenerTree.registerDataChangeListener(path, listener, scope);
};
}
- private synchronized DOMStoreThreePhaseCommitCohort submit(
- final SnapshotBackedWriteTransaction writeTx) {
- LOG.debug("Tx: {} is submitted. Modifications: {}",writeTx.getIdentifier(),writeTx.getMutatedView());
+ private synchronized DOMStoreThreePhaseCommitCohort submit(final SnapshotBackedWriteTransaction writeTx) {
+ LOG.debug("Tx: {} is submitted. Modifications: {}", writeTx.getIdentifier(), writeTx.getMutatedView());
return new ThreePhaseCommitImpl(writeTx);
}
return name + "-" + txCounter.getAndIncrement();
}
- private void commit(final DataAndMetadataSnapshot currentSnapshot,
- final StoreMetadataNode newDataTree, final ResolveDataChangeEventsTask listenerResolver) {
- LOG.debug("Updating Store snaphot version: {} with version:{}",currentSnapshot.getMetadataTree().getSubtreeVersion(),newDataTree.getSubtreeVersion());
+ private void commit(final DataAndMetadataSnapshot currentSnapshot, final StoreMetadataNode newDataTree,
+ final ResolveDataChangeEventsTask listenerResolver) {
+ LOG.debug("Updating Store snaphot version: {} with version:{}", currentSnapshot.getMetadataTree()
+ .getSubtreeVersion(), newDataTree.getSubtreeVersion());
- if(LOG.isTraceEnabled()) {
- LOG.trace("Data Tree is {}",StoreUtils.toStringTree(newDataTree));
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Data Tree is {}", StoreUtils.toStringTree(newDataTree));
}
final DataAndMetadataSnapshot newSnapshot = DataAndMetadataSnapshot.builder() //
.build();
/*
- * The commit has to occur atomically with regard to listener registrations.
+ * The commit has to occur atomically with regard to listener
+ * registrations.
*/
synchronized (this) {
final boolean success = snapshot.compareAndSet(currentSnapshot, newSnapshot);
checkState(success, "Store snapshot and transaction snapshot differ. This should never happen.");
for (ChangeListenerNotifyTask task : listenerResolver.call()) {
- LOG.trace("Scheduling invocation of listeners: {}",task);
+ LOG.trace("Scheduling invocation of listeners: {}", task);
executor.submit(task);
}
}
/**
* Add class-specific toString attributes.
*
- * @param toStringHelper ToStringHelper instance
+ * @param toStringHelper
+ * ToStringHelper instance
* @return ToStringHelper instance which was passed in
*/
protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
}
}
- private static class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction implements DOMStoreReadTransaction {
+ private static class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction implements
+ DOMStoreReadTransaction {
private DataAndMetadataSnapshot stableSnapshot;
public SnapshotBackedReadTransaction(final Object identifier, final DataAndMetadataSnapshot snapshot) {
super(identifier);
this.stableSnapshot = Preconditions.checkNotNull(snapshot);
- LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot.getMetadataTree().getSubtreeVersion());
+ LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot.getMetadataTree()
+ .getSubtreeVersion());
}
@Override
}
}
- private static class SnapshotBackedWriteTransaction extends AbstractDOMStoreTransaction implements DOMStoreWriteTransaction {
+ private static class SnapshotBackedWriteTransaction extends AbstractDOMStoreTransaction implements
+ DOMStoreWriteTransaction {
private MutableDataTree mutableTree;
private InMemoryDOMDataStore store;
private boolean ready = false;
super(identifier);
mutableTree = MutableDataTree.from(snapshot, applyOper);
this.store = store;
- LOG.debug("Write Tx: {} allocated with snapshot {}",identifier,snapshot.getMetadataTree().getSubtreeVersion());
+ LOG.debug("Write Tx: {} allocated with snapshot {}", identifier, snapshot.getMetadataTree()
+ .getSubtreeVersion());
}
@Override
public void write(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
checkNotReady();
try {
- LOG.trace("Tx: {} Write: {}:{}",getIdentifier(),path,data);
+ LOG.trace("Tx: {} Write: {}:{}", getIdentifier(), path, data);
mutableTree.write(path, data);
- // FIXME: Add checked exception
+ // FIXME: Add checked exception
} catch (Exception e) {
- LOG.error("Tx: {}, failed to write {}:{} in {}",getIdentifier(),path,data,mutableTree,e);
+ LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, mutableTree, e);
}
}
public void merge(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
checkNotReady();
try {
- LOG.trace("Tx: {} Merge: {}:{}",getIdentifier(),path,data);
+ LOG.trace("Tx: {} Merge: {}:{}", getIdentifier(), path, data);
mutableTree.merge(path, data);
- // FIXME: Add checked exception
+ // FIXME: Add checked exception
} catch (Exception e) {
- LOG.error("Tx: {}, failed to write {}:{} in {}",getIdentifier(),path,data,mutableTree,e);
+ LOG.error("Tx: {}, failed to write {}:{} in {}", getIdentifier(), path, data, mutableTree, e);
}
}
public void delete(final InstanceIdentifier path) {
checkNotReady();
try {
- LOG.trace("Tx: {} Delete: {}",getIdentifier(),path);
+ LOG.trace("Tx: {} Delete: {}", getIdentifier(), path);
mutableTree.delete(path);
- // FIXME: Add checked exception
+ // FIXME: Add checked exception
} catch (Exception e) {
- LOG.error("Tx: {}, failed to delete {} in {}",getIdentifier(),path,mutableTree,e);
+ LOG.error("Tx: {}, failed to delete {} in {}", getIdentifier(), path, mutableTree, e);
}
}
@Override
public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final InstanceIdentifier path) {
- LOG.trace("Tx: {} Read: {}",getIdentifier(),path);
+ LOG.trace("Tx: {} Read: {}", getIdentifier(), path);
try {
return Futures.immediateFuture(getMutatedView().read(path));
} catch (Exception e) {
- LOG.error("Tx: {} Failed Read of {}",getIdentifier(),path,e);
+ LOG.error("Tx: {} Failed Read of {}", getIdentifier(), path, e);
throw e;
}
}
@Override
public Boolean call() throws Exception {
- boolean applicable = snapshotOperation.isApplicable(modification,
+ Boolean applicable = false;
+ try {
+ snapshotOperation.checkApplicable(PUBLIC_ROOT_PATH, modification,
Optional.of(snapshotCapture.getMetadataTree()));
- LOG.debug("Store Transcation: {} : canCommit : {}", transaction.getIdentifier(), applicable);
+ applicable = true;
+ } catch (DataPreconditionFailedException e) {
+ LOG.warn("Store Tx: {} Data Precondition failed for {}.",transaction.getIdentifier(),e.getPath(),e);
+ applicable = false;
+ }
+ LOG.debug("Store Transaction: {} : canCommit : {}", transaction.getIdentifier(), applicable);
return applicable;
}
});
@Override
public ListenableFuture<Void> preCommit() {
storeSnapshot = snapshot.get();
- if(modification.getModificationType() == ModificationType.UNMODIFIED) {
+ if (modification.getModificationType() == ModificationType.UNMODIFIED) {
return Futures.immediateFuture(null);
}
return executor.submit(new Callable<Void>() {
-
-
@Override
public Void call() throws Exception {
StoreMetadataNode metadataTree = storeSnapshot.getMetadataTree();
@Override
public ListenableFuture<Void> commit() {
- if(modification.getModificationType() == ModificationType.UNMODIFIED) {
+ if (modification.getModificationType() == ModificationType.UNMODIFIED) {
return Futures.immediateFuture(null);
}
- checkState(proposedSubtree != null,"Proposed subtree must be computed");
- checkState(storeSnapshot != null,"Proposed subtree must be computed");
+ checkState(proposedSubtree != null, "Proposed subtree must be computed");
+ checkState(storeSnapshot != null, "Proposed subtree must be computed");
// return ImmediateFuture<>;
- InMemoryDOMDataStore.this.commit(storeSnapshot, proposedSubtree.get(),listenerResolver);
+ InMemoryDOMDataStore.this.commit(storeSnapshot, proposedSubtree.get(), listenerResolver);
return Futures.<Void> immediateFuture(null);
}
}
@Override
- public boolean isApplicable(final NodeModification modification, final Optional<StoreMetadataNode> storeMetadata) {
+ public void checkApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> storeMetadata) {
throw new IllegalStateException("Schema Context is not available.");
}
import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreNodeCompositeBuilder;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
protected abstract void verifyWritenStructure(NormalizedNode<?, ?> writenValue);
@Override
- public boolean isApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
+ public void checkApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
switch (modification.getModificationType()) {
case DELETE:
- return isDeleteApplicable(modification, current);
+ checkDeleteApplicable(modification, current);
case SUBTREE_MODIFIED:
- return isSubtreeModificationApplicable(modification, current);
+ checkSubtreeModificationApplicable(path,modification, current);
+ return;
case WRITE:
- return isWriteApplicable(modification, current);
+ checkWriteApplicable(path,modification, current);
+ return;
case MERGE:
- return isMergeApplicable(modification,current);
+ checkMergeApplicable(path,modification,current);
+ return;
case UNMODIFIED:
- return true;
+ return;
default:
- return false;
+ throw new UnsupportedOperationException("Suplied modification type "+modification.getModificationType()+ "is not supported.");
}
+
}
- private boolean isMergeApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
+ protected void checkMergeApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
Optional<StoreMetadataNode> original = modification.getOriginal();
if (original.isPresent() && current.isPresent()) {
- return isNotConflicting(original.get(), current.get());
- } else if (current.isPresent()) {
- return true;
+ checkNotConflicting(path,original.get(), current.get());
}
- return true;
}
- protected boolean isWriteApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
+ protected void checkWriteApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
Optional<StoreMetadataNode> original = modification.getOriginal();
if (original.isPresent() && current.isPresent()) {
- return isNotConflicting(original.get(), current.get());
- } else if (current.isPresent()) {
- return false;
+ checkNotConflicting(path,original.get(), current.get());
+ } else if(original.isPresent()) {
+ throw new DataPreconditionFailedException(path,"Node was deleted by other transaction.");
}
- return true;
-
}
- protected final boolean isNotConflicting(final StoreMetadataNode original, final StoreMetadataNode current) {
- return original.getNodeVersion().equals(current.getNodeVersion())
- && original.getSubtreeVersion().equals(current.getSubtreeVersion());
+ protected static final void checkNotConflicting(final InstanceIdentifier path,final StoreMetadataNode original, final StoreMetadataNode current) throws DataPreconditionFailedException {
+ checkDataPrecondition(path, original.getNodeVersion().equals(current.getNodeVersion()),"Node was replaced by other transaction.");
+ checkDataPrecondition(path,original.getSubtreeVersion().equals(current.getSubtreeVersion()), "Node children was modified by other transaction");
}
- protected abstract boolean isSubtreeModificationApplicable(final NodeModification modification,
- final Optional<StoreMetadataNode> current);
+ protected abstract void checkSubtreeModificationApplicable(InstanceIdentifier path,final NodeModification modification,
+ final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException;
- private boolean isDeleteApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
- // FiXME: Add delete conflict detection.
- return true;
+ private void checkDeleteApplicable(final NodeModification modification, final Optional<StoreMetadataNode> current) {
}
@Override
}
@Override
- protected boolean isSubtreeModificationApplicable(final NodeModification modification,
- final Optional<StoreMetadataNode> current) {
- return false;
+ protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
+ final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
+ throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
}
}
}
}
+ @Override
+ protected void checkWriteApplicable(final InstanceIdentifier path, final NodeModification modification,
+ final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
+ // FIXME: Implement proper write check for replacement of node container
+ // prerequisite is to have transaction chain available for clients
+ // otherwise this will break chained writes to same node.
+ }
+
@SuppressWarnings("rawtypes")
@Override
protected void verifyWritenStructure(final NormalizedNode<?, ?> writenValue) {
}
@Override
- protected boolean isSubtreeModificationApplicable(final NodeModification modification,
- final Optional<StoreMetadataNode> current) {
- if (false == current.isPresent()) {
- return false;
- }
- boolean result = true;
+ protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
+ final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
+ checkDataPrecondition(path, current.isPresent(), "Node was deleted by other transaction.");
+ checkChildPreconditions(path,modification,current);
+
+ }
+
+ private void checkChildPreconditions(final InstanceIdentifier path, final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
StoreMetadataNode currentMeta = current.get();
for (NodeModification childMod : modification.getModifications()) {
PathArgument childId = childMod.getIdentifier();
Optional<StoreMetadataNode> childMeta = currentMeta.getChild(childId);
- result &= resolveChildOperation(childId).isApplicable(childMod, childMeta);
+ InstanceIdentifier childPath = StoreUtils.append(path, childId);
+ resolveChildOperation(childId).checkApplicable(childPath,childMod, childMeta);
+ }
+ }
+
+ @Override
+ protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification,
+ final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
+ if(current.isPresent()) {
+ checkChildPreconditions(path,modification,current);
}
- return result;
}
@SuppressWarnings("rawtypes")
protected AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved) {
super(createAugmentProxy(schema,resolved), AugmentationNode.class);
- // FIXME: Use resolved children instead of unresolved.
-
}
@Override
}
@Override
- protected boolean isSubtreeModificationApplicable(final NodeModification modification,
- final Optional<StoreMetadataNode> current) {
- return false;
+ protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
+ final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
+ throw new DataPreconditionFailedException(path, "Subtree modification is not allowed.");
}
}
return new AugmentationSchemaProxy(schema, realChildSchemas);
}
+ public static boolean checkDataPrecondition(final InstanceIdentifier path, final boolean condition, final String message) throws DataPreconditionFailedException {
+ if(!condition) {
+ throw new DataPreconditionFailedException(path, message);
+ }
+ return condition;
+ }
+
}