X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fstore%2Fimpl%2FInMemoryDOMDataStore.java;h=427e7a00dbb443e1bc0f5de8553aa66ab5fc63c8;hp=0a2b66c2d0112a35e4173d11ac8c7e06de1f73c2;hb=da578e5d9ad4240d718ba0a54fba9d017d573704;hpb=fee7a0ff61c980453e0302e750b464988f58a5ce diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/InMemoryDOMDataStore.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/InMemoryDOMDataStore.java index 0a2b66c2d0..427e7a00db 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/InMemoryDOMDataStore.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/InMemoryDOMDataStore.java @@ -14,7 +14,6 @@ import static org.opendaylight.controller.md.sal.dom.store.impl.StoreUtils.incre import java.util.Collections; import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicLong; -import java.util.concurrent.atomic.AtomicReference; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener; @@ -34,7 +33,6 @@ import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeUtils; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaContextListener; import org.slf4j.Logger; @@ -54,23 +52,16 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch 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); - private final ListenerTree listenerTree; - private final AtomicReference snapshot; - - private ModificationApplyOperation operationTree; - - private SchemaContext schemaContext; + private final ListenerTree listenerTree = ListenerTree.create(); + private final DataTree dataTree = DataTree.create(null); + private ModificationApplyOperation operationTree = new AlwaysFailOperation(); public InMemoryDOMDataStore(final String name, final ListeningExecutorService executor) { this.name = Preconditions.checkNotNull(name); this.executor = Preconditions.checkNotNull(executor); - this.listenerTree = ListenerTree.create(); - this.snapshot = new AtomicReference(DataAndMetadataSnapshot.createEmpty()); - this.operationTree = new AlwaysFailOperation(); } @Override @@ -80,23 +71,30 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch @Override public DOMStoreReadTransaction newReadOnlyTransaction() { - return new SnapshotBackedReadTransaction(nextIdentifier(), snapshot.get()); + return new SnapshotBackedReadTransaction(nextIdentifier(), dataTree.takeSnapshot()); } @Override public DOMStoreReadWriteTransaction newReadWriteTransaction() { - return new SnapshotBackedReadWriteTransaction(nextIdentifier(), snapshot.get(), this, operationTree); + return new SnapshotBackedReadWriteTransaction(nextIdentifier(), dataTree.takeSnapshot(), this, operationTree); } @Override public DOMStoreWriteTransaction newWriteOnlyTransaction() { - return new SnapshotBackedWriteTransaction(nextIdentifier(), snapshot.get(), this, operationTree); + return new SnapshotBackedWriteTransaction(nextIdentifier(), dataTree.takeSnapshot(), this, operationTree); } @Override public synchronized void onGlobalContextUpdated(final SchemaContext ctx) { - operationTree = SchemaAwareApplyOperationRoot.from(ctx); - schemaContext = ctx; + /* + * Order of operations is important: dataTree may reject the context + * and creation of ModificationApplyOperation may fail. So pre-construct + * the operation, then update the data tree and then move the operation + * into view. + */ + final ModificationApplyOperation newOperationTree = SchemaAwareApplyOperationRoot.from(ctx); + dataTree.setSchemaContext(ctx); + operationTree = newOperationTree; } @Override @@ -104,21 +102,21 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch 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 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); - Optional currentState = snapshot.get().read(path); + Optional> currentState = dataTree.takeSnapshot().readNode(path); if (currentState.isPresent()) { - final NormalizedNode data = currentState.get().getData(); + final NormalizedNode data = currentState.get(); final DOMImmutableDataChangeEvent event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE) // .setAfter(data) // @@ -138,9 +136,8 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch }; } - 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); } @@ -148,28 +145,23 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch 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 DataTree.Snapshot currentSnapshot, final StoreMetadataNode newDataTree, + final ResolveDataChangeEventsTask listenerResolver) { + LOG.debug("Updating Store snaphot version: {} with version:{}", currentSnapshot, newDataTree.getSubtreeVersion()); - if(LOG.isTraceEnabled()) { - LOG.trace("Data Tree is {}",StoreUtils.toStringTree(newDataTree)); + if (LOG.isTraceEnabled()) { + LOG.trace("Data Tree is {}", StoreUtils.toStringTree(newDataTree.getData())); } - final DataAndMetadataSnapshot newSnapshot = DataAndMetadataSnapshot.builder() // - .setMetadataTree(newDataTree) // - .setSchemaContext(schemaContext) // - .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."); + dataTree.commitSnapshot(currentSnapshot, newDataTree); for (ChangeListenerNotifyTask task : listenerResolver.call()) { - LOG.trace("Scheduling invocation of listeners: {}",task); + LOG.trace("Scheduling invocation of listeners: {}", task); executor.submit(task); } } @@ -195,7 +187,8 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch /** * 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) { @@ -203,13 +196,14 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch } } - private static class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction implements DOMStoreReadTransaction { - private DataAndMetadataSnapshot stableSnapshot; + private static final class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction implements + DOMStoreReadTransaction { + private DataTree.Snapshot stableSnapshot; - public SnapshotBackedReadTransaction(final Object identifier, final DataAndMetadataSnapshot snapshot) { + public SnapshotBackedReadTransaction(final Object identifier, final DataTree.Snapshot 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); } @Override @@ -222,21 +216,22 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch public ListenableFuture>> read(final InstanceIdentifier path) { checkNotNull(path, "Path must not be null."); checkState(stableSnapshot != null, "Transaction is closed"); - return Futures.immediateFuture(NormalizedNodeUtils.findNode(stableSnapshot.getDataTree(), path)); + return Futures.immediateFuture(stableSnapshot.readNode(path)); } } - private static class SnapshotBackedWriteTransaction extends AbstractDOMStoreTransaction implements DOMStoreWriteTransaction { - private MutableDataTree mutableTree; + private static class SnapshotBackedWriteTransaction extends AbstractDOMStoreTransaction implements + DOMStoreWriteTransaction { + private DataTreeModification mutableTree; private InMemoryDOMDataStore store; private boolean ready = false; - public SnapshotBackedWriteTransaction(final Object identifier, final DataAndMetadataSnapshot snapshot, + public SnapshotBackedWriteTransaction(final Object identifier, final DataTree.Snapshot snapshot, final InMemoryDOMDataStore store, final ModificationApplyOperation applyOper) { super(identifier); - mutableTree = MutableDataTree.from(snapshot, applyOper); + mutableTree = DataTreeModification.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); } @Override @@ -250,11 +245,11 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch 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); } } @@ -262,11 +257,11 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch 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); } } @@ -274,11 +269,11 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch 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); } } @@ -300,7 +295,7 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch return store.submit(this); } - protected MutableDataTree getMutatedView() { + protected DataTreeModification getMutatedView() { return mutableTree; } @@ -313,18 +308,18 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch private static class SnapshotBackedReadWriteTransaction extends SnapshotBackedWriteTransaction implements DOMStoreReadWriteTransaction { - protected SnapshotBackedReadWriteTransaction(final Object identifier, final DataAndMetadataSnapshot snapshot, + protected SnapshotBackedReadWriteTransaction(final Object identifier, final DataTree.Snapshot snapshot, final InMemoryDOMDataStore store, final ModificationApplyOperation applyOper) { super(identifier, snapshot, store, applyOper); } @Override public ListenableFuture>> 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; } } @@ -335,7 +330,7 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch private final SnapshotBackedWriteTransaction transaction; private final NodeModification modification; - private DataAndMetadataSnapshot storeSnapshot; + private DataTree.Snapshot storeSnapshot; private Optional proposedSubtree; private ResolveDataChangeEventsTask listenerResolver; @@ -346,16 +341,23 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch @Override public ListenableFuture canCommit() { - final DataAndMetadataSnapshot snapshotCapture = snapshot.get(); + final DataTree.Snapshot snapshotCapture = dataTree.takeSnapshot(); final ModificationApplyOperation snapshotOperation = operationTree; return executor.submit(new Callable() { @Override public Boolean call() throws Exception { - boolean applicable = snapshotOperation.isApplicable(modification, - Optional.of(snapshotCapture.getMetadataTree())); - LOG.debug("Store Transcation: {} : canCommit : {}", transaction.getIdentifier(), applicable); + Boolean applicable = false; + try { + snapshotOperation.checkApplicable(PUBLIC_ROOT_PATH, modification, + Optional.of(snapshotCapture.getRootNode())); + 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; } }); @@ -363,17 +365,15 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch @Override public ListenableFuture preCommit() { - storeSnapshot = snapshot.get(); - if(modification.getModificationType() == ModificationType.UNMODIFIED) { + storeSnapshot = dataTree.takeSnapshot(); + if (modification.getModificationType() == ModificationType.UNMODIFIED) { return Futures.immediateFuture(null); } return executor.submit(new Callable() { - - @Override public Void call() throws Exception { - StoreMetadataNode metadataTree = storeSnapshot.getMetadataTree(); + StoreMetadataNode metadataTree = storeSnapshot.getRootNode(); proposedSubtree = operationTree.apply(modification, Optional.of(metadataTree), increase(metadataTree.getSubtreeVersion())); @@ -399,14 +399,14 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch @Override public ListenableFuture 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. immediateFuture(null); } @@ -421,7 +421,7 @@ public class InMemoryDOMDataStore implements DOMStore, Identifiable, Sch } @Override - public boolean isApplicable(final NodeModification modification, final Optional storeMetadata) { + public void checkApplicable(final InstanceIdentifier path,final NodeModification modification, final Optional storeMetadata) { throw new IllegalStateException("Schema Context is not available."); }