diff options
Diffstat (limited to 'infra/data-impl/src/main')
3 files changed, 107 insertions, 35 deletions
diff --git a/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/ModifiableDataTreeDelegator.java b/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/ModifiableDataTreeDelegator.java index 92c4b393c..b9246f3c4 100644 --- a/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/ModifiableDataTreeDelegator.java +++ b/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/ModifiableDataTreeDelegator.java @@ -18,6 +18,8 @@ package io.fd.honeycomb.data.impl; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.util.concurrent.Futures.immediateCheckedFuture; +import static io.fd.honeycomb.data.impl.ModifiableDataTreeDelegator.DataTreeWriteContextFactory.DataTreeWriteContext; +import static io.fd.honeycomb.data.impl.ModifiableDataTreeManager.DataTreeContextFactory.DataTreeContext; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; @@ -28,6 +30,7 @@ import io.fd.honeycomb.data.DataModification; import io.fd.honeycomb.data.ReadableDataManager; import io.fd.honeycomb.translate.MappingContext; import io.fd.honeycomb.translate.TranslationException; +import io.fd.honeycomb.translate.ValidationFailedException; import io.fd.honeycomb.translate.util.RWUtils; import io.fd.honeycomb.translate.util.TransactionMappingContext; import io.fd.honeycomb.translate.util.write.TransactionWriteContext; @@ -35,6 +38,7 @@ import io.fd.honeycomb.translate.write.DataObjectUpdate; import io.fd.honeycomb.translate.write.WriteContext; import io.fd.honeycomb.translate.write.registry.UpdateFailedException; import io.fd.honeycomb.translate.write.registry.WriterRegistry; +import io.fd.honeycomb.translate.write.registry.WriterRegistry.DataObjectUpdates; import java.util.List; import java.util.Map; import java.util.Set; @@ -83,7 +87,7 @@ public final class ModifiableDataTreeDelegator extends ModifiableDataTreeManager @Nonnull final SchemaContext schema, @Nonnull final WriterRegistry writerRegistry, @Nonnull final DataBroker contextBroker) { - super(dataTree); + super(dataTree, new DataTreeWriteContextFactory()); this.contextBroker = checkNotNull(contextBroker, "contextBroker should not be null"); this.serializer = checkNotNull(serializer, "serializer should not be null"); this.writerRegistry = checkNotNull(writerRegistry, "writerRegistry should not be null"); @@ -109,28 +113,50 @@ public final class ModifiableDataTreeDelegator extends ModifiableDataTreeManager this.untouchedModification = untouchedModification; } - /** - * Pass the changes to underlying writer layer. - * Transform from BI to BA. - * Revert(Write data before to subtrees that have been successfully modified before failure) in case of failure. - */ @Override - protected void processCandidate(final DataTreeCandidate candidate) - throws TranslationException { + protected void validateCandidate(final DataTreeContext dataTreeContext) throws ValidationFailedException { + final DataObjectUpdates baUpdates = + getUpdates((DataTreeWriteContextFactory.DataTreeWriteContext) dataTreeContext); + LOG.debug("ModifiableDataTreeDelegator.validateCandidate() extracted updates={}", baUpdates); - final DataTreeCandidateNode rootNode = candidate.getRootNode(); - final YangInstanceIdentifier rootPath = candidate.getRootPath(); - LOG.trace("ConfigDataTree.modify() rootPath={}, rootNode={}, dataBefore={}, dataAfter={}", - rootPath, rootNode, rootNode.getDataBefore(), rootNode.getDataAfter()); + try (final WriteContext ctx = getTransactionWriteContext()) { + writerRegistry.validateModifications(baUpdates, ctx); + } + } - final ModificationDiff modificationDiff = new ModificationDiff.ModificationDiffBuilder() + private DataObjectUpdates getUpdates(final DataTreeWriteContext dataTreeContext) { + DataObjectUpdates updates = dataTreeContext.getUpdates(); + if (updates != null) { + return updates; + } else { + final DataTreeCandidate candidate = dataTreeContext.getCandidate(); + final DataTreeCandidateNode rootNode = candidate.getRootNode(); + final YangInstanceIdentifier rootPath = candidate.getRootPath(); + LOG.trace("ConfigDataTree.getUpdates() rootPath={}, rootNode={}, dataBefore={}, dataAfter={}", + rootPath, rootNode, rootNode.getDataBefore(), rootNode.getDataAfter()); + + final ModificationDiff modificationDiff = new ModificationDiff.ModificationDiffBuilder() .setCtx(schema).build(rootNode); - LOG.debug("ConfigDataTree.modify() diff: {}", modificationDiff); + LOG.debug("ConfigDataTree.getUpdates() diff: {}", modificationDiff); - // Distinguish between updates (create + update) and deletes - final WriterRegistry.DataObjectUpdates baUpdates = - toBindingAware(writerRegistry, modificationDiff.getUpdates()); - LOG.debug("ConfigDataTree.modify() extracted updates={}", baUpdates); + // Distinguish between updates (create + update) and deletes + updates = toBindingAware(writerRegistry, modificationDiff.getUpdates()); + dataTreeContext.setUpdates(updates); + return updates; + } + } + + /** + * Pass the changes to underlying writer layer. Transform from BI to BA. Revert(Write data before to subtrees + * that have been successfully modified before failure) in case of failure. + */ + @Override + protected void processCandidate(final DataTreeContext dataTreeContext) throws TranslationException { + final DataTreeWriteContextFactory.DataTreeWriteContext dataTreeWriteContext = + (DataTreeWriteContextFactory.DataTreeWriteContext) dataTreeContext; + + final DataObjectUpdates baUpdates = dataTreeWriteContext.getUpdates(); + LOG.debug("ModifiableDataTreeDelegator.processCandidate() extracted updates={}", baUpdates); final WriteContext ctx = getTransactionWriteContext(); final MappingContext mappingContext = ctx.getMappingContext(); @@ -297,6 +323,40 @@ public final class ModifiableDataTreeDelegator extends ModifiableDataTreeManager return dataObject; } + static final class DataTreeWriteContextFactory implements ModifiableDataTreeManager.DataTreeContextFactory { + @Nonnull + @Override + public DataTreeWriteContext create(@Nonnull final DataTreeCandidate candidate) { + return new DataTreeWriteContext(candidate); + } + + /** + * Implementation of {@link DataTreeContext} that, in addition to {@link DataTreeCandidate}, stores also + * {@DataObjectUpdates}. The {@link DataObjectUpdates} are shared by the validation and translation process. + */ + static final class DataTreeWriteContext implements DataTreeContext { + private final DataTreeCandidate candidate; + private DataObjectUpdates updates; + + DataTreeWriteContext(@Nonnull final DataTreeCandidate candidate) { + this.candidate = candidate; + } + + @Nonnull + @Override + public DataTreeCandidate getCandidate() { + return candidate; + } + + public void setUpdates(@Nonnull final DataObjectUpdates updates) { + this.updates = updates; + } + + DataObjectUpdates getUpdates() { + return updates; + } + } + } } diff --git a/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/ModifiableDataTreeManager.java b/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/ModifiableDataTreeManager.java index aa5b3e5cf..acaa6e5ee 100644 --- a/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/ModifiableDataTreeManager.java +++ b/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/ModifiableDataTreeManager.java @@ -19,6 +19,7 @@ package io.fd.honeycomb.data.impl; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static com.google.common.util.concurrent.Futures.immediateCheckedFuture; +import static io.fd.honeycomb.data.impl.ModifiableDataTreeManager.DataTreeContextFactory.DataTreeContext; import com.google.common.base.Optional; import com.google.common.util.concurrent.CheckedFuture; @@ -50,9 +51,16 @@ public class ModifiableDataTreeManager implements ModifiableDataManager { private static final Logger LOG = LoggerFactory.getLogger(ModifiableDataTreeManager.class); private final DataTree dataTree; + private final DataTreeContextFactory contextFactory; public ModifiableDataTreeManager(@Nonnull final DataTree dataTree) { + this(dataTree, candidate -> (() -> candidate)); + } + + public ModifiableDataTreeManager(@Nonnull final DataTree dataTree, + @Nonnull final DataTreeContextFactory contextFactory) { this.dataTree = checkNotNull(dataTree, "dataTree should not be null"); + this.contextFactory = contextFactory; } @Override @@ -102,13 +110,13 @@ public class ModifiableDataTreeManager implements ModifiableDataManager { @Override public final void commit() throws TranslationException { - final DataTreeCandidate candidate = prepareCandidate(modification); - validateCandidate(candidate); - processCandidate(candidate); - dataTree.commit(candidate); + final DataTreeContext candidateContext = prepareCandidateContext(modification); + validateCandidate(candidateContext); + processCandidate(candidateContext); + dataTree.commit(candidateContext.getCandidate()); } - private DataTreeCandidate prepareCandidate(final DataTreeModification dataTreeModification) + private DataTreeContext prepareCandidateContext(final DataTreeModification dataTreeModification) throws ValidationFailedException { // Seal the modification (required to perform validate) dataTreeModification.ready(); @@ -120,14 +128,14 @@ public class ModifiableDataTreeManager implements ModifiableDataManager { throw new ValidationFailedException(e); } - return dataTree.prepare(dataTreeModification); + return contextFactory.create(dataTree.prepare(dataTreeModification)); } - protected void validateCandidate(final DataTreeCandidate candidate) throws ValidationFailedException { + protected void validateCandidate(final DataTreeContext dataTreeContext) throws ValidationFailedException { // NOOP } - protected void processCandidate(final DataTreeCandidate candidate) throws TranslationException { + protected void processCandidate(final DataTreeContext dataTreeContext) throws TranslationException { // NOOP } @@ -141,7 +149,7 @@ public class ModifiableDataTreeManager implements ModifiableDataManager { checkState(cursor != null, "DataTreeModificationCursor for root path should not be null"); modification.applyToCursor(cursor); // Then validate it. - validateCandidate(prepareCandidate(modificationCopy)); + validateCandidate(prepareCandidateContext(modificationCopy)); } @Override @@ -155,4 +163,16 @@ public class ModifiableDataTreeManager implements ModifiableDataManager { ) + '}'; } } + + interface DataTreeContextFactory { + DataTreeContext create(final DataTreeCandidate candidate); + + /** + * Stores DataTreeCandidate. Implementation may also store additional data to optimize validation + * and translation process. + */ + interface DataTreeContext { + DataTreeCandidate getCandidate(); + } + } } diff --git a/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/WriteTransaction.java b/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/WriteTransaction.java index 8e7dca07b..cd47c65db 100644 --- a/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/WriteTransaction.java +++ b/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/WriteTransaction.java @@ -125,14 +125,6 @@ final class WriteTransaction implements DOMDataWriteTransaction { try { status = SUBMITED; - // Validate first to catch any issues before attempting commit - if (configModification != null) { - configModification.validate(); - } - if (operationalModification != null) { - operationalModification.validate(); - } - if(configModification != null) { configModification.commit(); } |