diff options
Diffstat (limited to 'v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/AbstractCompositeVppWriter.java')
-rw-r--r-- | v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/AbstractCompositeVppWriter.java | 270 |
1 files changed, 270 insertions, 0 deletions
diff --git a/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/AbstractCompositeVppWriter.java b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/AbstractCompositeVppWriter.java new file mode 100644 index 000000000..add62e2ba --- /dev/null +++ b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/AbstractCompositeVppWriter.java @@ -0,0 +1,270 @@ +/* + * 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.vpp.facade.impl.write; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.base.Optional; +import com.google.common.collect.Lists; +import io.fd.honeycomb.v3po.vpp.facade.VppException; +import io.fd.honeycomb.v3po.vpp.facade.impl.util.VppRWUtils; +import io.fd.honeycomb.v3po.vpp.facade.write.ChildVppWriter; +import io.fd.honeycomb.v3po.vpp.facade.write.VppWriter; +import io.fd.honeycomb.v3po.vpp.facade.write.WriteContext; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.yangtools.yang.binding.Augmentation; +import org.opendaylight.yangtools.yang.binding.ChildOf; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class AbstractCompositeVppWriter<D extends DataObject> implements VppWriter<D> { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeVppWriter.class); + + private final Map<Class<? extends DataObject>, ChildVppWriter<? extends ChildOf<D>>> childWriters; + private final Map<Class<? extends DataObject>, ChildVppWriter<? extends Augmentation<D>>> augWriters; + private final InstanceIdentifier<D> instanceIdentifier; + + public AbstractCompositeVppWriter(final Class<D> type, + final List<ChildVppWriter<? extends ChildOf<D>>> childWriters, + final List<ChildVppWriter<? extends Augmentation<D>>> augWriters) { + this.instanceIdentifier = InstanceIdentifier.create(type); + this.childWriters = VppRWUtils.uniqueLinkedIndex(childWriters, VppRWUtils.MANAGER_CLASS_FUNCTION); + this.augWriters = VppRWUtils.uniqueLinkedIndex(augWriters, VppRWUtils.MANAGER_CLASS_AUG_FUNCTION); + } + + protected void writeCurrent(final InstanceIdentifier<D> id, final D data, final WriteContext ctx) { + LOG.debug("{}: Writing current: {} data: {}", this, id, data); + + LOG.trace("{}: Writing current attributes", this); + writeCurrentAttributes(id, data, ctx); + + for (ChildVppWriter<? extends ChildOf<D>> child : childWriters.values()) { + LOG.debug("{}: Writing child in: {}", this, child); + child.writeChild(id, data, ctx); + } + + for (ChildVppWriter<? extends Augmentation<D>> child : augWriters.values()) { + LOG.debug("{}: Writing augment in: {}", this, child); + child.writeChild(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) { + 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; + } + + LOG.trace("{}: Updating current attributes", this); + updateCurrentAttributes(id, dataBefore, dataAfter, ctx); + + for (ChildVppWriter<? extends ChildOf<D>> child : childWriters.values()) { + LOG.debug("{}: Updating child in: {}", this, child); + child.updateChild(id, dataBefore, dataAfter, ctx); + } + + for (ChildVppWriter<? extends Augmentation<D>> child : augWriters.values()) { + LOG.debug("{}: Updating augment in: {}", this, child); + child.updateChild(id, dataBefore, dataAfter, ctx); + } + + LOG.debug("{}: Current node updated successfully", this); + } + + protected void deleteCurrent(final InstanceIdentifier<D> id, final D dataBefore, final WriteContext ctx) { + LOG.debug("{}: Deleting current: {} dataBefore: {}", this, id, dataBefore); + + // delete in reversed order + for (ChildVppWriter<? extends Augmentation<D>> child : reverseCollection(augWriters.values())) { + LOG.debug("{}: Deleting augment in: {}", this, child); + child.deleteChild(id, dataBefore, ctx); + } + + for (ChildVppWriter<? extends ChildOf<D>> child : reverseCollection(childWriters.values())) { + LOG.debug("{}: Deleting child in: {}", this, child); + child.deleteChild(id, dataBefore, ctx); + } + + LOG.trace("{}: Deleting current attributes", this); + 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 VppException { + LOG.debug("{}: Updating : {}", this, id); + LOG.trace("{}: Updating : {}, from: {} to: {}", this, id, dataBefore, dataAfter); + + if (idPointsToCurrent(id)) { + 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); + } + } else { + if (isWrite(dataBefore, dataAfter)) { + writeSubtree(id, dataAfter, ctx); + } else if (isDelete(dataBefore, dataAfter)) { + deleteSubtree(id, dataBefore, ctx); + } else { + checkArgument(dataBefore != null && dataAfter != null, "No data to process"); + updateSubtree(id, dataBefore, dataAfter, ctx); + } + } + } + + private void checkDataType(final @Nullable 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 void writeSubtree(final InstanceIdentifier<? extends DataObject> id, + final DataObject dataAfter, final WriteContext ctx) throws VppException { + LOG.debug("{}: Writing subtree: {}", this, id); + final VppWriter<? extends ChildOf<D>> vppWriter = getNextWriter(id); + + if (vppWriter != null) { + LOG.debug("{}: Writing subtree: {} in: {}", this, id, vppWriter); + vppWriter.update(id, null, dataAfter, ctx); + } else { + // If there's no dedicated writer, use write current + // But we need current data after to do so + final InstanceIdentifier<D> currentId = VppRWUtils.cutId(id, getManagedDataObjectType()); + Optional<DataObject> currentDataAfter = ctx.readAfter(currentId); + LOG.debug("{}: Dedicated subtree writer missing for: {}. Writing current.", this, + VppRWUtils.getNextId(id, getManagedDataObjectType()).getType(), currentDataAfter); + writeCurrent(currentId, castToManaged(currentDataAfter.get()), ctx); + } + } + + private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier<? extends DataObject> id) { + return id.getTargetType().equals(getManagedDataObjectType().getTargetType()); + } + + @SuppressWarnings("unchecked") + private void deleteSubtree(final InstanceIdentifier<? extends DataObject> id, + final DataObject dataBefore, final WriteContext ctx) throws VppException { + LOG.debug("{}: Deleting subtree: {}", this, id); + final VppWriter<? extends ChildOf<D>> vppWriter = getNextWriter(id); + + if (vppWriter != null) { + LOG.debug("{}: Deleting subtree: {} in: {}", this, id, vppWriter); + vppWriter.update(id, dataBefore, null, ctx); + } else { + updateSubtreeFromCurrent(id, ctx); + } + } + + @SuppressWarnings("unchecked") + private void updateSubtreeFromCurrent(final InstanceIdentifier<? extends DataObject> id, final WriteContext ctx) { + final InstanceIdentifier<D> currentId = VppRWUtils.cutId(id, getManagedDataObjectType()); + Optional<DataObject> currentDataBefore = ctx.readBefore(currentId); + Optional<DataObject> currentDataAfter = ctx.readAfter(currentId); + LOG.debug("{}: Dedicated subtree writer missing for: {}. Updating current without subtree", this, + VppRWUtils.getNextId(id, getManagedDataObjectType()).getType(), currentDataAfter); + updateCurrent((InstanceIdentifier<D>) id, castToManaged(currentDataBefore.orNull()), + castToManaged(currentDataAfter.orNull()), ctx); + } + + @SuppressWarnings("unchecked") + private void updateSubtree(final InstanceIdentifier<? extends DataObject> id, + final DataObject dataBefore, + final DataObject dataAfter, + final WriteContext ctx) throws VppException { + LOG.debug("{}: Updating subtree: {}", this, id); + final VppWriter<? extends ChildOf<D>> vppWriter = getNextWriter(id); + + if (vppWriter != null) { + LOG.debug("{}: Updating subtree: {} in: {}", this, id, vppWriter); + vppWriter.update(id, dataBefore, dataAfter, ctx); + } else { + updateSubtreeFromCurrent(id, ctx); + } + } + + private VppWriter<? extends ChildOf<D>> getNextWriter(final InstanceIdentifier<? extends DataObject> id) { + final Class<? extends DataObject> next = VppRWUtils.getNextId(id, getManagedDataObjectType()).getType(); + return childWriters.get(next); + } + + private static <T> List<T> reverseCollection(final Collection<T> original) { + // TODO find a better reverse mechanism (probably a different collection for child writers is necessary) + final ArrayList<T> list = Lists.newArrayList(original); + Collections.reverse(list); + return list; + } + + protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, + @Nonnull final D data, + @Nonnull final WriteContext ctx); + + protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, + @Nonnull final D dataBefore, + @Nonnull final WriteContext ctx); + + protected abstract void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, + @Nonnull final D dataBefore, + @Nonnull final D dataAfter, + @Nonnull final WriteContext ctx); + + @Nonnull + @Override + public InstanceIdentifier<D> getManagedDataObjectType() { + return instanceIdentifier; + } + + + @Override + public String toString() { + return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName()); + } +} |