diff options
5 files changed, 38 insertions, 21 deletions
diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeListWriter.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeListWriter.java index 1ac39b877..de1ac39d3 100644 --- a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeListWriter.java +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeListWriter.java @@ -17,15 +17,16 @@ package io.fd.honeycomb.v3po.translate.impl.write; import com.google.common.base.Function; -import com.google.common.collect.ImmutableMap; +import com.google.common.base.Optional; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import io.fd.honeycomb.v3po.translate.impl.TraversalType; import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer; +import io.fd.honeycomb.v3po.translate.util.RWUtils; import io.fd.honeycomb.v3po.translate.write.ChildWriter; import io.fd.honeycomb.v3po.translate.write.WriteContext; -import io.fd.honeycomb.v3po.translate.util.RWUtils; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; +import java.util.Collections; import java.util.List; import java.util.Map; import javax.annotation.Nonnull; @@ -40,7 +41,7 @@ public class CompositeListWriter<D extends DataObject & Identifiable<K>, K exten AbstractCompositeWriter<D> implements ChildWriter<D> { - public static final Function<DataObject, Object> INDEX_FUNCTION = input -> input instanceof Identifiable<?> + private static final Function<DataObject, Object> INDEX_FUNCTION = input -> input instanceof Identifiable<?> ? ((Identifiable<?>) input).getKey() : input; @@ -99,9 +100,11 @@ public class CompositeListWriter<D extends DataObject & Identifiable<K>, K exten @Nonnull final DataObject parentData, @Nonnull final WriteContext ctx) throws WriteFailedException { final InstanceIdentifier<D> currentId = RWUtils.appendTypeToId(parentId, getManagedDataObjectType()); - final List<D> currentData = customizer.extract(currentId, parentData); - for (D entry : currentData) { - writeCurrent(currentId, entry, ctx); + final Optional<List<D>> currentData = customizer.extract(currentId, parentData); + if (currentData.isPresent()) { + for (D entry : currentData.get()) { + writeCurrent(currentId, entry, ctx); + } } } @@ -110,21 +113,30 @@ public class CompositeListWriter<D extends DataObject & Identifiable<K>, K exten @Nonnull final DataObject parentDataBefore, @Nonnull final WriteContext ctx) throws WriteFailedException { final InstanceIdentifier<D> currentId = RWUtils.appendTypeToId(parentId, getManagedDataObjectType()); - final List<D> dataBefore = customizer.extract(currentId, parentDataBefore); - for (D entry : dataBefore) { - deleteCurrent(currentId, entry, ctx); + final Optional<List<D>> dataBefore = customizer.extract(currentId, parentDataBefore); + if (dataBefore.isPresent()) { + for (D entry : dataBefore.get()) { + deleteCurrent(currentId, entry, ctx); + } } } + private Map<Object, D> listOfIdentifiableToMap(Optional<List<D>> list) { + if (list.isPresent()) { + return Maps.uniqueIndex(list.get(), INDEX_FUNCTION); + } else { + return Collections.emptyMap(); + } + + } + @Override public void updateChild(@Nonnull final InstanceIdentifier<? extends DataObject> parentId, @Nonnull final DataObject parentDataBefore, @Nonnull final DataObject parentDataAfter, @Nonnull final WriteContext ctx) throws WriteFailedException { final InstanceIdentifier<D> currentId = RWUtils.appendTypeToId(parentId, getManagedDataObjectType()); - final ImmutableMap<Object, D> - dataBefore = Maps.uniqueIndex(customizer.extract(currentId, parentDataBefore), INDEX_FUNCTION); - final ImmutableMap<Object, D> - dataAfter = Maps.uniqueIndex(customizer.extract(currentId, parentDataAfter), INDEX_FUNCTION); + final Map<Object, D> dataBefore = listOfIdentifiableToMap(customizer.extract(currentId, parentDataBefore)); + final Map<Object, D> dataAfter = listOfIdentifiableToMap(customizer.extract(currentId, parentDataAfter)); for (Map.Entry<Object, D> after : dataAfter.entrySet()) { final D before = dataBefore.get(after.getKey()); diff --git a/v3po/translate-spi/src/main/java/io/fd/honeycomb/v3po/translate/spi/write/ListWriterCustomizer.java b/v3po/translate-spi/src/main/java/io/fd/honeycomb/v3po/translate/spi/write/ListWriterCustomizer.java index 1eb18a238..ecc591191 100644 --- a/v3po/translate-spi/src/main/java/io/fd/honeycomb/v3po/translate/spi/write/ListWriterCustomizer.java +++ b/v3po/translate-spi/src/main/java/io/fd/honeycomb/v3po/translate/spi/write/ListWriterCustomizer.java @@ -17,6 +17,7 @@ package io.fd.honeycomb.v3po.translate.spi.write; import com.google.common.annotations.Beta; +import com.google.common.base.Optional; import java.util.List; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.binding.DataObject; @@ -41,6 +42,7 @@ public interface ListWriterCustomizer<C extends DataObject & Identifiable<K>, K * @param parentData Parent data object from which managed data object must be extracted */ @Nonnull - List<C> extract(@Nonnull final InstanceIdentifier<C> currentId, @Nonnull final DataObject parentData); + Optional<List<C>> extract(@Nonnull final InstanceIdentifier<C> currentId, @Nonnull final DataObject parentData); + // TODO consider removing Optional and make extract return @Nullable (applies also to ChildWriterCustomizer) }
\ No newline at end of file diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java index 4e81ca0f2..3a4638d90 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java @@ -16,6 +16,7 @@ package io.fd.honeycomb.v3po.translate.v3po.interfaces; +import com.google.common.base.Optional; import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; @@ -89,9 +90,9 @@ public class InterfaceCustomizer extends FutureJVppCustomizer implements ListWri @Nonnull @Override - public List<Interface> extract(@Nonnull final InstanceIdentifier<Interface> currentId, + public Optional<List<Interface>> extract(@Nonnull final InstanceIdentifier<Interface> currentId, @Nonnull final DataObject parentData) { - return ((Interfaces) parentData).getInterface(); + return Optional.fromNullable(((Interfaces) parentData).getInterface()); } diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizer.java index b6c8d9a59..cb0a2c3df 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizer.java @@ -20,6 +20,7 @@ import static com.google.common.base.Preconditions.checkState; import static io.fd.honeycomb.v3po.translate.v3po.util.SubInterfaceUtils.getSubInterfaceName; import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.booleanToByte; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; @@ -74,9 +75,9 @@ public class SubInterfaceCustomizer extends FutureJVppCustomizer @Nonnull @Override - public List<SubInterface> extract(@Nonnull final InstanceIdentifier<SubInterface> currentId, + public Optional<List<SubInterface>> extract(@Nonnull final InstanceIdentifier<SubInterface> currentId, @Nonnull final DataObject parentData) { - return ((SubInterfaces) parentData).getSubInterface(); + return Optional.fromNullable(((SubInterfaces) parentData).getSubInterface()); } diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vpp/BridgeDomainCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vpp/BridgeDomainCustomizer.java index 0b3545ddd..750ff575e 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vpp/BridgeDomainCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/vpp/BridgeDomainCustomizer.java @@ -19,12 +19,13 @@ package io.fd.honeycomb.v3po.translate.v3po.vpp; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; -import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils; +import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; import io.fd.honeycomb.v3po.translate.write.WriteContext; import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.util.List; @@ -57,9 +58,9 @@ public class BridgeDomainCustomizer @Nonnull @Override - public List<BridgeDomain> extract(@Nonnull final InstanceIdentifier<BridgeDomain> currentId, + public Optional<List<BridgeDomain>> extract(@Nonnull final InstanceIdentifier<BridgeDomain> currentId, @Nonnull final DataObject parentData) { - return ((BridgeDomains) parentData).getBridgeDomain(); + return Optional.fromNullable(((BridgeDomains) parentData).getBridgeDomain()); } private BridgeDomainAddDelReply addOrUpdateBridgeDomain(final int bdId, @Nonnull final BridgeDomain bd) |