diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2018-04-13 13:38:16 +0200 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2018-08-17 10:17:58 +0000 |
commit | d1b102f6fafced3c7badb09ecc78fec590704c8a (patch) | |
tree | fb9c7041b25cdd36b46cf25e789c850db38f2349 /infra/translate-utils | |
parent | fa641a3e06a905cb3222ebd15a2b4ab90b599efc (diff) |
HONEYCOMB-431: add validation support to Writers
This patch introduces FlatWriterRegistry.validateModifications.
Implementation iterates over writersOrder following bulkUpdate logic
to properly support subtree writers case.
Writers are now cabable of validating modifications.
Commonly used implementations (GenericWriter and GenericListWriter)
delegate validation capbility to Validators.
Change-Id: If7a0bb0838c0b8f2c0393c989f3b03853a2ea679
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'infra/translate-utils')
-rw-r--r-- | infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/AbstractGenericWriter.java | 54 |
1 files changed, 49 insertions, 5 deletions
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/AbstractGenericWriter.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/AbstractGenericWriter.java index 4f1b696cf..ba8f1d992 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/AbstractGenericWriter.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/AbstractGenericWriter.java @@ -16,32 +16,40 @@ package io.fd.honeycomb.translate.util.write; +import static com.google.common.base.Preconditions.checkArgument; + import io.fd.honeycomb.translate.util.RWUtils; +import io.fd.honeycomb.translate.write.DataValidationFailedException; +import io.fd.honeycomb.translate.write.Validator; import io.fd.honeycomb.translate.write.WriteContext; import io.fd.honeycomb.translate.write.WriteFailedException; import io.fd.honeycomb.translate.write.Writer; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import static com.google.common.base.Preconditions.checkArgument; - public abstract class AbstractGenericWriter<D extends DataObject> implements Writer<D> { private static final Logger LOG = LoggerFactory.getLogger(AbstractGenericWriter.class); private final InstanceIdentifier<D> instanceIdentifier; private final boolean supportsUpdate; + private Validator<D> validator; protected AbstractGenericWriter(final InstanceIdentifier<D> type, final boolean supportsUpdate) { this.instanceIdentifier = RWUtils.makeIidWildcarded(type); this.supportsUpdate = supportsUpdate; } + protected AbstractGenericWriter(final InstanceIdentifier<D> type, final boolean supportsUpdate, + final Validator<D> validator) { + this(type, supportsUpdate); + this.validator = validator; + } + protected void writeCurrent(final InstanceIdentifier<D> id, final D data, final WriteContext ctx) throws WriteFailedException { LOG.debug("{}: Writing current: {} data: {}", this, id, data); @@ -90,6 +98,42 @@ public abstract class AbstractGenericWriter<D extends DataObject> implements Wri } } + @Override + public void validate(@Nonnull final InstanceIdentifier<? extends DataObject> id, + @Nullable final DataObject dataBefore, @Nullable final DataObject dataAfter, + @Nonnull final WriteContext ctx) throws DataValidationFailedException { + if (validator == null) { + LOG.trace("{}: validator is not defined. Skipping validation for {}", this, id); + return; + } + LOG.trace("{}: Validating : {}, before: {} after: {}", this, id, dataBefore, dataAfter); + + checkArgument(idPointsToCurrent(id), "Cannot handle data: %s. Only: %s can be handled by writer: %s", + id, getManagedDataObjectType(), this); + + if (isWrite(dataBefore, dataAfter)) { + final D after = castToManaged(dataAfter); + validator.validateWrite(getManagedId(id, after), after, ctx); + } else if (isDelete(dataBefore, dataAfter)) { + final D before = castToManaged(dataBefore); + validator.validateDelete(getManagedId(id, before), before, ctx); + } else { + checkArgument(dataBefore != null && dataAfter != null, "No data to process"); + if (dataBefore.equals(dataAfter)) { + LOG.debug("{}: Skipping validation (no update) for: {}", this, id); + // No change, ignore + return; + } + final D before = castToManaged(dataBefore); + validator.validateUpdate(getManagedId(id, before), before, castToManaged(dataAfter), ctx); + } + } + + protected InstanceIdentifier<D> getManagedId(@Nonnull final InstanceIdentifier<? extends DataObject> currentId, + @Nonnull final D current) { + return (InstanceIdentifier<D>) currentId; + } + private void checkDataType(@Nonnull final DataObject dataAfter) { checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(dataAfter.getClass())); } |