diff options
Diffstat (limited to 'v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write')
3 files changed, 12 insertions, 145 deletions
diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/AbstractCompositeWriter.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/AbstractCompositeWriter.java deleted file mode 100644 index 5f1391c74..000000000 --- a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/AbstractCompositeWriter.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.v3po.translate.impl.write; - -import static com.google.common.base.Preconditions.checkArgument; - -import io.fd.honeycomb.v3po.translate.write.WriteContext; -import io.fd.honeycomb.v3po.translate.write.WriteFailedException; -import io.fd.honeycomb.v3po.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; - -abstract class AbstractCompositeWriter<D extends DataObject> implements Writer<D> { - - private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeWriter.class); - - private final InstanceIdentifier<D> instanceIdentifier; - - AbstractCompositeWriter(final InstanceIdentifier<D> type) { - this.instanceIdentifier = type; - } - - protected void writeCurrent(final InstanceIdentifier<D> id, final D data, final WriteContext ctx) - throws WriteFailedException { - LOG.debug("{}: Writing current: {} data: {}", this, id, data); - writeCurrentAttributes(id, data, ctx); - LOG.debug("{}: Current node written successfully", this); - } - - protected void updateCurrent(final InstanceIdentifier<D> id, final D dataBefore, final D dataAfter, - final WriteContext ctx) throws WriteFailedException { - LOG.debug("{}: Updating current: {} dataBefore: {}, datAfter: {}", this, id, dataBefore, dataAfter); - - if (dataBefore.equals(dataAfter)) { - LOG.debug("{}: Skipping current(no update): {}", this, id); - // No change, ignore - return; - } - updateCurrentAttributes(id, dataBefore, dataAfter, ctx); - LOG.debug("{}: Current node updated successfully", this); - } - - protected void deleteCurrent(final InstanceIdentifier<D> id, final D dataBefore, final WriteContext ctx) - throws WriteFailedException { - LOG.debug("{}: Deleting current: {} dataBefore: {}", this, id, dataBefore); - deleteCurrentAttributes(id, dataBefore, ctx); - } - - @SuppressWarnings("unchecked") - @Override - public void update(@Nonnull final InstanceIdentifier<? extends DataObject> id, - @Nullable final DataObject dataBefore, - @Nullable final DataObject dataAfter, - @Nonnull final WriteContext ctx) throws WriteFailedException { - LOG.debug("{}: Updating : {}", this, id); - LOG.trace("{}: Updating : {}, from: {} to: {}", 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)) { - writeCurrent((InstanceIdentifier<D>) id, castToManaged(dataAfter), ctx); - } else if (isDelete(dataBefore, dataAfter)) { - deleteCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), ctx); - } else { - checkArgument(dataBefore != null && dataAfter != null, "No data to process"); - updateCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), castToManaged(dataAfter), ctx); - } - } - - private void checkDataType(@Nonnull final DataObject dataAfter) { - checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(dataAfter.getClass())); - } - - private D castToManaged(final DataObject data) { - checkDataType(data); - return getManagedDataObjectType().getTargetType().cast(data); - } - - private static boolean isWrite(final DataObject dataBefore, - final DataObject dataAfter) { - return dataBefore == null && dataAfter != null; - } - - private static boolean isDelete(final DataObject dataBefore, - final DataObject dataAfter) { - return dataAfter == null && dataBefore != null; - } - - private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier<? extends DataObject> id) { - return id.getTargetType().equals(getManagedDataObjectType().getTargetType()); - } - - protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, - @Nonnull final D data, - @Nonnull final WriteContext ctx) throws WriteFailedException; - - protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, - @Nonnull final D dataBefore, - @Nonnull final WriteContext ctx) throws WriteFailedException; - - protected abstract void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, - @Nonnull final D dataBefore, - @Nonnull final D dataAfter, - @Nonnull final WriteContext ctx) throws WriteFailedException; - - @Nonnull - @Override - public InstanceIdentifier<D> getManagedDataObjectType() { - return instanceIdentifier; - } - - - @Override - public String toString() { - return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName()); - } -} diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/GenericListWriter.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/GenericListWriter.java index b61fb51f7..32daf5975 100644 --- a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/GenericListWriter.java +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/GenericListWriter.java @@ -17,10 +17,12 @@ package io.fd.honeycomb.v3po.translate.impl.write; import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer; +import io.fd.honeycomb.v3po.translate.spi.write.WriterCustomizer; import io.fd.honeycomb.v3po.translate.util.RWUtils; +import io.fd.honeycomb.v3po.translate.util.write.AbstractGenericWriter; +import io.fd.honeycomb.v3po.translate.write.ListWriter; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; -import io.fd.honeycomb.v3po.translate.write.Writer; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.Identifiable; @@ -28,12 +30,12 @@ import org.opendaylight.yangtools.yang.binding.Identifier; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** - * Special writer handling updates for nodes of type list. + * Generic list node writer with customizable behavior thanks to injected customizer. */ public final class GenericListWriter<D extends DataObject & Identifiable<K>, K extends Identifier<D>> extends - AbstractCompositeWriter<D> implements Writer<D> { + AbstractGenericWriter<D> implements ListWriter<D, K> { - private final ListWriterCustomizer<D, K> customizer; + private final WriterCustomizer<D> customizer; public GenericListWriter(@Nonnull final InstanceIdentifier<D> type, @Nonnull final ListWriterCustomizer<D, K> customizer) { diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/GenericWriter.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/GenericWriter.java index 6ca80ca37..65c192ffa 100644 --- a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/GenericWriter.java +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/GenericWriter.java @@ -16,7 +16,8 @@ package io.fd.honeycomb.v3po.translate.impl.write; -import io.fd.honeycomb.v3po.translate.spi.write.RootWriterCustomizer; +import io.fd.honeycomb.v3po.translate.spi.write.WriterCustomizer; +import io.fd.honeycomb.v3po.translate.util.write.AbstractGenericWriter; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import javax.annotation.Nonnull; @@ -24,14 +25,14 @@ import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** - * Special writer handling updates for any complex nodes. + * Generic writer with customizable behavior thanks to injected customizer. */ -public final class GenericWriter<D extends DataObject> extends AbstractCompositeWriter<D> { +public final class GenericWriter<D extends DataObject> extends AbstractGenericWriter<D> { - private final RootWriterCustomizer<D> customizer; + private final WriterCustomizer<D> customizer; public GenericWriter(@Nonnull final InstanceIdentifier<D> type, - @Nonnull final RootWriterCustomizer<D> customizer) { + @Nonnull final WriterCustomizer<D> customizer) { super(type); this.customizer = customizer; } |