diff options
author | Maros Marsalek <mmarsale@cisco.com> | 2016-11-08 10:13:36 +0100 |
---|---|---|
committer | Maros Marsalek <mmarsale@cisco.com> | 2016-11-08 13:17:57 +0100 |
commit | 757222979bc02d0aaba1870eea36413383d15bde (patch) | |
tree | 3fe2a502026fbfa9a4ae5627e1a453a057f6c1fa /infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write | |
parent | 03a638b95da83e150d4f69451c8733b5f09c37aa (diff) |
HONEYCOMB-270 Add isPresent() to Readers/Customizers
So that they can influence whether empty data is to be considered as present
+ Move registries implementations from util to impl
+ Introduce DelegatingReader trait
+ Extend GenericReader where possible to reduce duplication
Change-Id: I5a416acd0c4eab1fbc30fcbe585719991dbe9215
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
Diffstat (limited to 'infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write')
3 files changed, 0 insertions, 470 deletions
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistry.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistry.java deleted file mode 100644 index e5b829d6a..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistry.java +++ /dev/null @@ -1,314 +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.translate.util.write.registry; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; -import static io.fd.honeycomb.translate.util.RWUtils.makeIidWildcarded; - -import com.google.common.base.Optional; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; -import io.fd.honeycomb.translate.TranslationException; -import io.fd.honeycomb.translate.util.RWUtils; -import io.fd.honeycomb.translate.write.DataObjectUpdate; -import io.fd.honeycomb.translate.write.WriteContext; -import io.fd.honeycomb.translate.write.WriteFailedException; -import io.fd.honeycomb.translate.write.Writer; -import io.fd.honeycomb.translate.write.registry.WriterRegistry; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import javax.annotation.concurrent.ThreadSafe; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Flat writer registry, delegating updates to writers in the order writers were submitted. - */ -@ThreadSafe -final class FlatWriterRegistry implements WriterRegistry { - - private static final Logger LOG = LoggerFactory.getLogger(FlatWriterRegistry.class); - - // All types handled by writers directly or as children - private final ImmutableSet<InstanceIdentifier<?>> handledTypes; - - private final Set<InstanceIdentifier<?>> writersOrderReversed; - private final Set<InstanceIdentifier<?>> writersOrder; - private final Map<InstanceIdentifier<?>, Writer<?>> writers; - - /** - * Create flat registry instance. - * - * @param writers immutable, ordered map of writers to use to process updates. Order of the writers has to be - * one in which create and update operations should be handled. Deletes will be handled in reversed - * order. All deletes are handled before handling all the updates. - */ - FlatWriterRegistry(@Nonnull final ImmutableMap<InstanceIdentifier<?>, Writer<?>> writers) { - this.writers = writers; - this.writersOrderReversed = Sets.newLinkedHashSet(Lists.reverse(Lists.newArrayList(writers.keySet()))); - this.writersOrder = writers.keySet(); - this.handledTypes = getAllHandledTypes(writers); - } - - private static ImmutableSet<InstanceIdentifier<?>> getAllHandledTypes( - @Nonnull final ImmutableMap<InstanceIdentifier<?>, Writer<?>> writers) { - final ImmutableSet.Builder<InstanceIdentifier<?>> handledTypesBuilder = ImmutableSet.builder(); - for (Map.Entry<InstanceIdentifier<?>, Writer<?>> writerEntry : writers.entrySet()) { - final InstanceIdentifier<?> writerType = writerEntry.getKey(); - final Writer<?> writer = writerEntry.getValue(); - handledTypesBuilder.add(writerType); - if (writer instanceof SubtreeWriter) { - handledTypesBuilder.addAll(((SubtreeWriter<?>) writer).getHandledChildTypes()); - } - } - return handledTypesBuilder.build(); - } - - @Override - public void update(@Nonnull final DataObjectUpdates updates, - @Nonnull final WriteContext ctx) throws TranslationException { - if (updates.isEmpty()) { - return; - } - - // Optimization - if (updates.containsOnlySingleType()) { - // First process delete - singleUpdate(updates.getDeletes(), ctx); - // Next is update - singleUpdate(updates.getUpdates(), ctx); - } else { - // First process deletes - bulkUpdate(updates.getDeletes(), ctx, true, writersOrderReversed); - // Next are updates - bulkUpdate(updates.getUpdates(), ctx, true, writersOrder); - } - - LOG.debug("Update successful for types: {}", updates.getTypeIntersection()); - LOG.trace("Update successful for: {}", updates); - } - - private void singleUpdate(@Nonnull final Multimap<InstanceIdentifier<?>, ? extends DataObjectUpdate> updates, - @Nonnull final WriteContext ctx) throws WriteFailedException { - if (updates.isEmpty()) { - return; - } - - final InstanceIdentifier<?> singleType = updates.keySet().iterator().next(); - LOG.debug("Performing single type update for: {}", singleType); - Collection<? extends DataObjectUpdate> singleTypeUpdates = updates.get(singleType); - Writer<?> writer = getWriter(singleType); - - if (writer == null) { - // This node must be handled by a subtree writer, find it and call it or else fail - checkArgument(handledTypes.contains(singleType), "Unable to process update. Missing writers for: %s", - singleType); - writer = getSubtreeWriterResponsible(singleType); - singleTypeUpdates = getParentDataObjectUpdate(ctx, updates, writer); - } - - LOG.trace("Performing single type update with writer: {}", writer); - for (DataObjectUpdate singleUpdate : singleTypeUpdates) { - writer.update(singleUpdate.getId(), singleUpdate.getDataBefore(), singleUpdate.getDataAfter(), ctx); - } - } - - private Writer<?> getSubtreeWriterResponsible(final InstanceIdentifier<?> singleType) { - return writers.values().stream() - .filter(w -> w instanceof SubtreeWriter) - .filter(w -> ((SubtreeWriter<?>) w).getHandledChildTypes().contains(singleType)) - .findFirst() - .get(); - } - - private Collection<DataObjectUpdate> getParentDataObjectUpdate(final WriteContext ctx, - final Multimap<InstanceIdentifier<?>, ? extends DataObjectUpdate> updates, - final Writer<?> writer) { - // Now read data for subtree reader root, but first keyed ID is needed and that ID can be cut from updates - InstanceIdentifier<?> firstAffectedChildId = ((SubtreeWriter<?>) writer).getHandledChildTypes().stream() - .filter(updates::containsKey) - .map(unkeyedId -> updates.get(unkeyedId)) - .flatMap(doUpdates -> doUpdates.stream()) - .map(DataObjectUpdate::getId) - .findFirst() - .get(); - - final InstanceIdentifier<?> parentKeyedId = - RWUtils.cutId(firstAffectedChildId, writer.getManagedDataObjectType()); - - final Optional<? extends DataObject> parentBefore = ctx.readBefore(parentKeyedId); - final Optional<? extends DataObject> parentAfter = ctx.readAfter(parentKeyedId); - return Collections.singleton( - DataObjectUpdate.create(parentKeyedId, parentBefore.orNull(), parentAfter.orNull())); - } - - private void bulkUpdate(@Nonnull final Multimap<InstanceIdentifier<?>, ? extends DataObjectUpdate> updates, - @Nonnull final WriteContext ctx, - final boolean attemptRevert, - @Nonnull final Set<InstanceIdentifier<?>> writersOrder) throws BulkUpdateException { - if (updates.isEmpty()) { - return; - } - - LOG.debug("Performing bulk update with revert attempt: {} for: {}", attemptRevert, updates.keySet()); - - // Check that all updates can be handled - checkAllTypesCanBeHandled(updates); - - // Capture all changes successfully processed in case revert is needed - final Set<InstanceIdentifier<?>> processedNodes = new HashSet<>(); - - // Iterate over all writers and call update if there are any related updates - for (InstanceIdentifier<?> writerType : writersOrder) { - Collection<? extends DataObjectUpdate> writersData = updates.get(writerType); - final Writer<?> writer = getWriter(writerType); - - if (writersData.isEmpty()) { - // If there are no data for current writer, but it is a SubtreeWriter and there are updates to - // its children, still invoke it with its root data - if (writer instanceof SubtreeWriter<?> && isAffected(((SubtreeWriter<?>) writer), updates)) { - // Provide parent data for SubtreeWriter for further processing - writersData = getParentDataObjectUpdate(ctx, updates, writer); - } else { - // Skipping unaffected writer - // Alternative to this would be modification sort according to the order of writers - continue; - } - } - - LOG.debug("Performing update for: {}", writerType); - LOG.trace("Performing update with writer: {}", writer); - - for (DataObjectUpdate singleUpdate : writersData) { - try { - writer.update(singleUpdate.getId(), singleUpdate.getDataBefore(), singleUpdate.getDataAfter(), ctx); - processedNodes.add(singleUpdate.getId()); - LOG.trace("Update successful for type: {}", writerType); - LOG.debug("Update successful for: {}", singleUpdate); - } catch (Exception e) { - // do not log this exception here,its logged in ModifiableDataTreeDelegator - - final Reverter reverter = attemptRevert - ? new ReverterImpl(processedNodes, updates, writersOrder) - : (final WriteContext writeContext) -> {};//NOOP reverter - - // Find out which changes left unprocessed - final Set<InstanceIdentifier<?>> unprocessedChanges = updates.values().stream() - .map(DataObjectUpdate::getId) - .filter(id -> !processedNodes.contains(id)) - .collect(Collectors.toSet()); - throw new BulkUpdateException(unprocessedChanges, reverter, e); - } - } - } - } - - private void checkAllTypesCanBeHandled( - @Nonnull final Multimap<InstanceIdentifier<?>, ? extends DataObjectUpdate> updates) { - if (!handledTypes.containsAll(updates.keySet())) { - final Sets.SetView<InstanceIdentifier<?>> missingWriters = Sets.difference(updates.keySet(), handledTypes); - LOG.warn("Unable to process update. Missing writers for: {}", missingWriters); - throw new IllegalArgumentException("Unable to process update. Missing writers for: " + missingWriters); - } - } - - /** - * Check whether {@link SubtreeWriter} is affected by the updates. - * - * @return true if there are any updates to SubtreeWriter's child nodes (those marked by SubtreeWriter - * as being taken care of) - * */ - private static boolean isAffected(final SubtreeWriter<?> writer, - final Multimap<InstanceIdentifier<?>, ? extends DataObjectUpdate> updates) { - return !Sets.intersection(writer.getHandledChildTypes(), updates.keySet()).isEmpty(); - } - - @Nullable - private Writer<?> getWriter(@Nonnull final InstanceIdentifier<?> singleType) { - return writers.get(singleType); - } - - private final class ReverterImpl implements Reverter { - - private final Collection<InstanceIdentifier<?>> processedNodes; - private final Multimap<InstanceIdentifier<?>, ? extends DataObjectUpdate> updates; - private final Set<InstanceIdentifier<?>> revertDeleteOrder; - - ReverterImpl(final Collection<InstanceIdentifier<?>> processedNodes, - final Multimap<InstanceIdentifier<?>, ? extends DataObjectUpdate> updates, - final Set<InstanceIdentifier<?>> writersOrderOriginal) { - this.processedNodes = processedNodes; - this.updates = updates; - // Use opposite ordering when executing revert - this.revertDeleteOrder = writersOrderOriginal == FlatWriterRegistry.this.writersOrder - ? FlatWriterRegistry.this.writersOrderReversed - : FlatWriterRegistry.this.writersOrder; - } - - @Override - public void revert(@Nonnull final WriteContext writeContext) throws RevertFailedException { - checkNotNull(writeContext, "Cannot revert changes for null context"); - - Multimap<InstanceIdentifier<?>, DataObjectUpdate> updatesToRevert = - filterAndRevertProcessed(updates, processedNodes); - - LOG.info("Attempting revert for changes: {}", updatesToRevert); - try { - // Perform reversed bulk update without revert attempt - bulkUpdate(updatesToRevert, writeContext, true, revertDeleteOrder); - LOG.info("Revert successful"); - } catch (BulkUpdateException e) { - LOG.error("Revert failed", e); - throw new RevertFailedException(e.getFailedIds(), e); - } - } - - /** - * Create new updates map, but only keep already processed changes. Switching before and after data for each - * update. - */ - private Multimap<InstanceIdentifier<?>, DataObjectUpdate> filterAndRevertProcessed( - final Multimap<InstanceIdentifier<?>, ? extends DataObjectUpdate> updates, - final Collection<InstanceIdentifier<?>> processedNodes) { - final Multimap<InstanceIdentifier<?>, DataObjectUpdate> filtered = HashMultimap.create(); - for (InstanceIdentifier<?> processedNode : processedNodes) { - final InstanceIdentifier<?> wildcardedIid = makeIidWildcarded(processedNode); - if (updates.containsKey(wildcardedIid)) { - updates.get(wildcardedIid).stream() - .filter(dataObjectUpdate -> processedNode.contains(dataObjectUpdate.getId())) - // putting under unkeyed identifier, to prevent failing of checkAllTypesCanBeHandled - .forEach(dataObjectUpdate -> filtered.put(wildcardedIid, dataObjectUpdate.reverse())); - } - } - return filtered; - } - } - -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilder.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilder.java deleted file mode 100644 index 0f75de7e7..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/FlatWriterRegistryBuilder.java +++ /dev/null @@ -1,71 +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.translate.util.write.registry; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.ImmutableMap; -import io.fd.honeycomb.translate.write.registry.WriterRegistryBuilder; -import io.fd.honeycomb.translate.util.AbstractSubtreeManagerRegistryBuilderBuilder; -import io.fd.honeycomb.translate.write.Writer; -import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder; -import io.fd.honeycomb.translate.write.registry.WriterRegistry; -import java.util.Set; -import java.util.stream.Collectors; -import javax.annotation.Nonnull; -import javax.annotation.concurrent.NotThreadSafe; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Builder for {@link FlatWriterRegistry} allowing users to specify inter-writer relationships. - */ -@NotThreadSafe -public final class FlatWriterRegistryBuilder - extends AbstractSubtreeManagerRegistryBuilderBuilder<Writer<? extends DataObject>, WriterRegistry> - implements ModifiableWriterRegistryBuilder, WriterRegistryBuilder { - - private static final Logger LOG = LoggerFactory.getLogger(FlatWriterRegistryBuilder.class); - - @Override - protected Writer<? extends DataObject> getSubtreeHandler(final @Nonnull Set<InstanceIdentifier<?>> handledChildren, - final @Nonnull Writer<? extends DataObject> writer) { - return SubtreeWriter.createForWriter(handledChildren, writer); - } - - /** - * Create FlatWriterRegistry with writers ordered according to submitted relationships. - */ - @Override - public WriterRegistry build() { - final ImmutableMap<InstanceIdentifier<?>, Writer<?>> mappedWriters = getMappedHandlers(); - LOG.debug("Building writer registry with writers: {}", - mappedWriters.keySet().stream() - .map(InstanceIdentifier::getTargetType) - .map(Class::getSimpleName) - .collect(Collectors.joining(", "))); - LOG.trace("Building writer registry with writers: {}", mappedWriters); - return new FlatWriterRegistry(mappedWriters); - } - - @VisibleForTesting - @Override - protected ImmutableMap<InstanceIdentifier<?>, Writer<? extends DataObject>> getMappedHandlers() { - return super.getMappedHandlers(); - } -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriter.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriter.java deleted file mode 100644 index bab1da16f..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/write/registry/SubtreeWriter.java +++ /dev/null @@ -1,85 +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.translate.util.write.registry; - -import static com.google.common.base.Preconditions.checkArgument; - -import com.google.common.collect.Iterables; -import io.fd.honeycomb.translate.write.WriteContext; -import io.fd.honeycomb.translate.write.WriteFailedException; -import io.fd.honeycomb.translate.write.Writer; -import java.util.HashSet; -import java.util.Set; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -/** - * Simple writer delegate for subtree writers (writers handling also children nodes) providing a list of all the - * children nodes being handled. - */ -final class SubtreeWriter<D extends DataObject> implements Writer<D> { - - private final Writer<D> delegate; - private final Set<InstanceIdentifier<?>> handledChildTypes = new HashSet<>(); - - private SubtreeWriter(final Writer<D> delegate, Set<InstanceIdentifier<?>> handledTypes) { - this.delegate = delegate; - for (InstanceIdentifier<?> handledType : handledTypes) { - // Iid has to start with writer's handled root type - checkArgument(delegate.getManagedDataObjectType().getTargetType().equals( - handledType.getPathArguments().iterator().next().getType()), - "Handled node from subtree has to be identified by an instance identifier starting from: %s." - + "Instance identifier was: %s", getManagedDataObjectType().getTargetType(), handledType); - checkArgument(Iterables.size(handledType.getPathArguments()) > 1, - "Handled node from subtree identifier too short: %s", handledType); - handledChildTypes.add(InstanceIdentifier.create(Iterables.concat( - getManagedDataObjectType().getPathArguments(), Iterables.skip(handledType.getPathArguments(), 1)))); - } - } - - /** - * Return set of types also handled by this writer. All of the types are children of the type managed by this - * writer excluding the type of this writer. - */ - Set<InstanceIdentifier<?>> getHandledChildTypes() { - return handledChildTypes; - } - - @Override - public void update( - @Nonnull final InstanceIdentifier<? extends DataObject> id, - @Nullable final DataObject dataBefore, - @Nullable final DataObject dataAfter, @Nonnull final WriteContext ctx) throws WriteFailedException { - delegate.update(id, dataBefore, dataAfter, ctx); - } - - @Override - @Nonnull - public InstanceIdentifier<D> getManagedDataObjectType() { - return delegate.getManagedDataObjectType(); - } - - /** - * Wrap a writer as a subtree writer. - */ - static Writer<?> createForWriter(@Nonnull final Set<InstanceIdentifier<?>> handledChildren, - @Nonnull final Writer<? extends DataObject> writer) { - return new SubtreeWriter<>(writer, handledChildren); - } -} |