diff options
Diffstat (limited to 'v3po/vpp-facade-impl/src/main/java')
8 files changed, 1189 insertions, 0 deletions
diff --git a/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/AbstractCompositeVppReader.java b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/AbstractCompositeVppReader.java new file mode 100644 index 000000000..2856937ba --- /dev/null +++ b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/AbstractCompositeVppReader.java @@ -0,0 +1,232 @@ +/* + * 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.read; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.annotations.Beta; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import io.fd.honeycomb.v3po.vpp.facade.impl.util.ReflectionUtils; +import io.fd.honeycomb.v3po.vpp.facade.impl.util.VppRWUtils; +import io.fd.honeycomb.v3po.vpp.facade.read.ChildVppReader; +import io.fd.honeycomb.v3po.vpp.facade.read.ReadContext; +import io.fd.honeycomb.v3po.vpp.facade.read.ReadFailedException; +import io.fd.honeycomb.v3po.vpp.facade.read.VppReader; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.yangtools.concepts.Builder; +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.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Beta +abstract class AbstractCompositeVppReader<D extends DataObject, B extends Builder<D>> implements VppReader<D> { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeVppReader.class); + + private final Map<Class<? extends DataObject>, ChildVppReader<? extends ChildOf<D>>> childReaders; + private final Map<Class<? extends DataObject>, ChildVppReader<? extends Augmentation<D>>> augReaders; + private final InstanceIdentifier<D> instanceIdentifier; + + AbstractCompositeVppReader(final Class<D> managedDataObjectType, + final List<ChildVppReader<? extends ChildOf<D>>> childReaders, + final List<ChildVppReader<? extends Augmentation<D>>> augReaders) { + this.childReaders = VppRWUtils.uniqueLinkedIndex(childReaders, VppRWUtils.MANAGER_CLASS_FUNCTION); + this.augReaders = VppRWUtils.uniqueLinkedIndex(augReaders, VppRWUtils.MANAGER_CLASS_AUG_FUNCTION); + this.instanceIdentifier = InstanceIdentifier.create(managedDataObjectType); + } + + @Nonnull + @Override + public final InstanceIdentifier<D> getManagedDataObjectType() { + return instanceIdentifier; + } + + /** + * @param id {@link InstanceIdentifier} pointing to current node. In case of keyed list, key must be present. + * + */ + protected Optional<D> readCurrent(final InstanceIdentifier<D> id, + @Nonnull final ReadContext ctx) throws ReadFailedException { + LOG.debug("{}: Reading current: {}", this, id); + final B builder = getBuilder(id); + // Cache empty value to determine if anything has changed later TODO cache in a field + final D emptyValue = builder.build(); + + LOG.trace("{}: Reading current attributes", this); + readCurrentAttributes(id, builder, ctx); + + // TODO expect exceptions from reader + for (ChildVppReader<? extends ChildOf<D>> child : childReaders.values()) { + LOG.debug("{}: Reading child from: {}", this, child); + child.read(id, builder, ctx); + } + + for (ChildVppReader<? extends Augmentation<D>> child : augReaders.values()) { + LOG.debug("{}: Reading augment from: {}", this, child); + child.read(id, builder, ctx); + } + + // Need to check whether anything was filled in to determine if data is present or not. + final D built = builder.build(); + final Optional<D> read = built.equals(emptyValue) + ? Optional.<D>absent() + : Optional.of(built); + + LOG.debug("{}: Current node read successfully. Result: {}", this, read); + return read; + } + + @Nonnull + @Override + @SuppressWarnings("unchecked") + public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> id, + @Nonnull final ReadContext ctx) + throws ReadFailedException { + LOG.trace("{}: Reading : {}", this, id); + if (id.getTargetType().equals(getManagedDataObjectType().getTargetType())) { + return readCurrent((InstanceIdentifier<D>) id, ctx); + } else { + return readSubtree(id, ctx); + } + } + + private Optional<? extends DataObject> readSubtree(final InstanceIdentifier<? extends DataObject> id, + @Nonnull final ReadContext ctx) + throws ReadFailedException { + LOG.debug("{}: Reading subtree: {}", this, id); + final Class<? extends DataObject> next = VppRWUtils.getNextId(id, getManagedDataObjectType()).getType(); + final ChildVppReader<? extends ChildOf<D>> vppReader = childReaders.get(next); + + if (vppReader != null) { + LOG.debug("{}: Reading subtree: {} from: {}", this, id, vppReader); + return vppReader.read(id, ctx); + } else { + LOG.debug("{}: Dedicated subtree reader missing for: {}. Reading current and filtering", this, next); + // If there's no dedicated reader, use read current + final InstanceIdentifier<D> currentId = VppRWUtils.cutId(id, getManagedDataObjectType()); + final Optional<D> current = readCurrent(currentId, ctx); + // then perform post-reading filtering (return only requested sub-node) + final Optional<? extends DataObject> readSubtree = current.isPresent() + ? filterSubtree(current.get(), id, getManagedDataObjectType().getTargetType()) + : current; + + LOG.debug("{}: Subtree: {} read successfully. Result: {}", this, id, readSubtree); + return readSubtree; + } + } + + /** + * Fill in current node's attributes + * + * @param id {@link InstanceIdentifier} pointing to current node. In case of keyed list, key must be present. + * @param builder Builder object for current node where the read attributes must be placed + * @param ctx Current read context + */ + protected abstract void readCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final B builder, + @Nonnull final ReadContext ctx) throws ReadFailedException; + + /** + * Return new instance of a builder object for current node + * + * @param id {@link InstanceIdentifier} pointing to current node. In case of keyed list, key must be present. + * @return Builder object for current node type + */ + protected abstract B getBuilder(InstanceIdentifier<D> id); + + // TODO move filtering out of here into a dedicated Filter ifc + @Nonnull + private static Optional<? extends DataObject> filterSubtree(@Nonnull final DataObject parent, + @Nonnull final InstanceIdentifier<? extends DataObject> absolutPath, + @Nonnull final Class<?> managedType) { + // TODO is there a better way than reflection ? e.g. convert into NN and filter out with a utility + // FIXME this needs to be recursive. right now it expects only 1 additional element in ID + test + + final InstanceIdentifier.PathArgument nextId = + VppRWUtils.getNextId(absolutPath, InstanceIdentifier.create(parent.getClass())); + + Optional<Method> method = ReflectionUtils.findMethodReflex(managedType, "get", + Collections.<Class<?>>emptyList(), nextId.getType()); + + if (method.isPresent()) { + return Optional.fromNullable(filterSingle(parent, nextId, method.get())); + } else { + // List child nodes + method = ReflectionUtils.findMethodReflex(managedType, + "get" + nextId.getType().getSimpleName(), Collections.<Class<?>>emptyList(), List.class); + + if (method.isPresent()) { + return filterList(parent, nextId, method.get()); + } else { + throw new IllegalStateException( + "Unable to filter " + nextId + " from " + parent + " getters not found using reflexion"); + } + } + } + + @SuppressWarnings("unchecked") + private static Optional<? extends DataObject> filterList(final DataObject parent, + final InstanceIdentifier.PathArgument nextId, + final Method method) { + final List<? extends DataObject> invoke = (List<? extends DataObject>) invoke(method, nextId, parent); + + checkArgument(nextId instanceof InstanceIdentifier.IdentifiableItem<?, ?>, + "Unable to perform wildcarded read for %s", nextId); + final Identifier key = ((InstanceIdentifier.IdentifiableItem) nextId).getKey(); + return Iterables.tryFind(invoke, new Predicate<DataObject>() { + @Override + public boolean apply(@Nullable final DataObject input) { + final Optional<Method> keyGetter = + ReflectionUtils.findMethodReflex(nextId.getType(), "get", + Collections.<Class<?>>emptyList(), key.getClass()); + final Object actualKey; + actualKey = invoke(keyGetter.get(), nextId, input); + return key.equals(actualKey); + } + }); + } + + private static DataObject filterSingle(final DataObject parent, + final InstanceIdentifier.PathArgument nextId, final Method method) { + return nextId.getType().cast(invoke(method, nextId, parent)); + } + + private static Object invoke(final Method method, + final InstanceIdentifier.PathArgument nextId, final DataObject parent) { + try { + return method.invoke(parent); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new IllegalArgumentException("Unable to get " + nextId + " from " + parent, e); + } + } + + @Override + public String toString() { + return String.format("Reader[%s]", getManagedDataObjectType().getTargetType().getSimpleName()); + } +} diff --git a/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/CompositeChildVppReader.java b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/CompositeChildVppReader.java new file mode 100644 index 000000000..76e793dbe --- /dev/null +++ b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/CompositeChildVppReader.java @@ -0,0 +1,104 @@ +/* + * 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.read; + +import com.google.common.annotations.Beta; +import com.google.common.base.Optional; +import io.fd.honeycomb.v3po.vpp.facade.impl.util.VppRWUtils; +import io.fd.honeycomb.v3po.vpp.facade.read.ChildVppReader; +import io.fd.honeycomb.v3po.vpp.facade.read.ReadContext; +import io.fd.honeycomb.v3po.vpp.facade.read.ReadFailedException; +import io.fd.honeycomb.v3po.vpp.facade.spi.read.ChildVppReaderCustomizer; +import java.util.List; +import javax.annotation.Nonnull; +import javax.annotation.concurrent.ThreadSafe; +import org.opendaylight.yangtools.concepts.Builder; +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; + +/** + * Composite implementation of {@link ChildVppReader} able to place the read result into + * parent builder object. + */ +@Beta +@ThreadSafe +public final class CompositeChildVppReader<C extends DataObject, B extends Builder<C>> extends AbstractCompositeVppReader<C, B> + implements ChildVppReader<C> { + + private final ChildVppReaderCustomizer<C, B> customizer; + + /** + * Create new {@link CompositeChildVppReader} + * + * @param managedDataObjectType Class object for managed data type + * @param childReaders Child nodes(container, list) readers + * @param augReaders Child augmentations readers + * @param customizer Customizer instance to customize this generic reader + * + */ + public CompositeChildVppReader(@Nonnull final Class<C> managedDataObjectType, + @Nonnull final List<ChildVppReader<? extends ChildOf<C>>> childReaders, + @Nonnull final List<ChildVppReader<? extends Augmentation<C>>> augReaders, + @Nonnull final ChildVppReaderCustomizer<C, B> customizer) { + super(managedDataObjectType, childReaders, augReaders); + this.customizer = customizer; + } + + /** + * @see {@link CompositeChildVppReader#CompositeChildVppReader(Class, List, List, ChildVppReaderCustomizer)} + */ + public CompositeChildVppReader(@Nonnull final Class<C> managedDataObjectType, + @Nonnull final List<ChildVppReader<? extends ChildOf<C>>> childReaders, + @Nonnull final ChildVppReaderCustomizer<C, B> customizer) { + this(managedDataObjectType, childReaders, VppRWUtils.<C>emptyAugReaderList(), customizer); + } + + /** + * @see {@link CompositeChildVppReader#CompositeChildVppReader(Class, List, List, ChildVppReaderCustomizer)} + */ + public CompositeChildVppReader(@Nonnull final Class<C> managedDataObjectType, + @Nonnull final ChildVppReaderCustomizer<C, B> customizer) { + this(managedDataObjectType, VppRWUtils.<C>emptyChildReaderList(), VppRWUtils.<C>emptyAugReaderList(), + customizer); + } + + @Override + public final void read(@Nonnull final InstanceIdentifier<? extends DataObject> parentId, + @Nonnull final Builder<? extends DataObject> parentBuilder, + @Nonnull final ReadContext ctx) throws ReadFailedException { + final Optional<C> read = readCurrent(VppRWUtils.appendTypeToId(parentId, getManagedDataObjectType()), ctx); + + if(read.isPresent()) { + customizer.merge(parentBuilder, read.get()); + } + } + + @Override + protected void readCurrentAttributes(@Nonnull final InstanceIdentifier<C> id, @Nonnull final B builder, + @Nonnull final ReadContext ctx) + throws ReadFailedException { + customizer.readCurrentAttributes(id, builder, ctx.getContext()); + } + + @Override + protected B getBuilder(@Nonnull final InstanceIdentifier<C> id) { + return customizer.getBuilder(id); + } + +} diff --git a/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/CompositeListVppReader.java b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/CompositeListVppReader.java new file mode 100644 index 000000000..fcfa8b428 --- /dev/null +++ b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/CompositeListVppReader.java @@ -0,0 +1,137 @@ +/* + * 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.read; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.annotations.Beta; +import com.google.common.base.Optional; +import io.fd.honeycomb.v3po.vpp.facade.impl.util.VppRWUtils; +import io.fd.honeycomb.v3po.vpp.facade.read.ChildVppReader; +import io.fd.honeycomb.v3po.vpp.facade.read.ListVppReader; +import io.fd.honeycomb.v3po.vpp.facade.read.ReadContext; +import io.fd.honeycomb.v3po.vpp.facade.read.ReadFailedException; +import io.fd.honeycomb.v3po.vpp.facade.spi.read.ListVppReaderCustomizer; +import java.util.ArrayList; +import java.util.List; +import javax.annotation.Nonnull; +import javax.annotation.concurrent.ThreadSafe; +import org.opendaylight.yangtools.concepts.Builder; +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.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Composite implementation of {@link io.fd.honeycomb.v3po.vpp.facade.read.ChildVppReader} able to place the read result into parent builder object intended + * for list node type. + * + * This reader checks if the IDs are wildcarded in which case it performs read of all list entries. In case the ID has a + * key, it reads only the specified value. + */ +@Beta +@ThreadSafe +public final class CompositeListVppReader<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>> + extends AbstractCompositeVppReader<C, B> implements ChildVppReader<C>, ListVppReader<C, K> { + + private static final Logger LOG = LoggerFactory.getLogger(CompositeListVppReader.class); + + private final ListVppReaderCustomizer<C, K, B> customizer; + + /** + * Create new {@link CompositeListVppReader} + * + * @param managedDataObjectType Class object for managed data type. Must come from a list node type. + * @param childReaders Child nodes(container, list) readers + * @param augReaders Child augmentations readers + * @param customizer Customizer instance to customize this generic reader + */ + public CompositeListVppReader(@Nonnull final Class<C> managedDataObjectType, + @Nonnull final List<ChildVppReader<? extends ChildOf<C>>> childReaders, + @Nonnull final List<ChildVppReader<? extends Augmentation<C>>> augReaders, + @Nonnull final ListVppReaderCustomizer<C, K, B> customizer) { + super(managedDataObjectType, childReaders, augReaders); + this.customizer = customizer; + } + + /** + * @see {@link CompositeListVppReader#CompositeListVppReader(Class, List, List, ListVppReaderCustomizer)} + */ + public CompositeListVppReader(@Nonnull final Class<C> managedDataObjectType, + @Nonnull final List<ChildVppReader<? extends ChildOf<C>>> childReaders, + @Nonnull final ListVppReaderCustomizer<C, K, B> customizer) { + this(managedDataObjectType, childReaders, VppRWUtils.<C>emptyAugReaderList(), customizer); + } + + /** + * @see {@link CompositeListVppReader#CompositeListVppReader(Class, List, List, ListVppReaderCustomizer)} + */ + public CompositeListVppReader(@Nonnull final Class<C> managedDataObjectType, + @Nonnull final ListVppReaderCustomizer<C, K, B> customizer) { + this(managedDataObjectType, VppRWUtils.<C>emptyChildReaderList(), VppRWUtils.<C>emptyAugReaderList(), + customizer); + } + + @Override + public void read(@Nonnull final InstanceIdentifier<? extends DataObject> id, + @Nonnull final Builder<? extends DataObject> parentBuilder, + @Nonnull final ReadContext ctx) throws ReadFailedException { + // Create ID pointing to current node + final InstanceIdentifier<C> currentId = VppRWUtils.appendTypeToId(id, getManagedDataObjectType()); + // Read all, since current ID is definitely wildcarded + final List<C> ifcs = readList(currentId, ctx); + customizer.merge(parentBuilder, ifcs); + } + + @Override + @Nonnull + public List<C> readList(@Nonnull final InstanceIdentifier<C> id, + @Nonnull final ReadContext ctx) throws ReadFailedException { + LOG.trace("{}: Reading all list entries", this); + final List<K> allIds = customizer.getAllIds(id, ctx.getContext()); + LOG.debug("{}: Reading list entries for: {}", this, allIds); + + final ArrayList<C> allEntries = new ArrayList<>(allIds.size()); + for (K key : allIds) { + final InstanceIdentifier.IdentifiableItem<C, K> currentBdItem = + VppRWUtils.getCurrentIdItem(id, key); + final InstanceIdentifier<C> keyedId = VppRWUtils.replaceLastInId(id, currentBdItem); + final Optional<C> read = readCurrent(keyedId, ctx); + final DataObject singleItem = read.get(); + checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(singleItem.getClass())); + allEntries.add(getManagedDataObjectType().getTargetType().cast(singleItem)); + } + return allEntries; + } + + @Override + protected void readCurrentAttributes(@Nonnull final InstanceIdentifier<C> id, @Nonnull final B builder, + @Nonnull final ReadContext ctx) + throws ReadFailedException { + customizer.readCurrentAttributes(id, builder, ctx.getContext()); + } + + @Override + protected B getBuilder(@Nonnull final InstanceIdentifier<C> id) { + return customizer.getBuilder(id); + } + +} diff --git a/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/CompositeRootVppReader.java b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/CompositeRootVppReader.java new file mode 100644 index 000000000..95f2a8eec --- /dev/null +++ b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/read/CompositeRootVppReader.java @@ -0,0 +1,91 @@ +/* + * 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.read; + +import com.google.common.annotations.Beta; +import io.fd.honeycomb.v3po.vpp.facade.impl.util.VppRWUtils; +import io.fd.honeycomb.v3po.vpp.facade.read.ChildVppReader; +import io.fd.honeycomb.v3po.vpp.facade.read.ReadContext; +import io.fd.honeycomb.v3po.vpp.facade.read.ReadFailedException; +import io.fd.honeycomb.v3po.vpp.facade.read.VppReader; +import io.fd.honeycomb.v3po.vpp.facade.spi.read.RootVppReaderCustomizer; +import java.util.List; +import javax.annotation.Nonnull; +import javax.annotation.concurrent.ThreadSafe; +import org.opendaylight.yangtools.concepts.Builder; +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; + +/** + * Composite implementation of {@link io.fd.honeycomb.v3po.vpp.facade.read.VppReader} + */ +@Beta +@ThreadSafe +public final class CompositeRootVppReader<C extends DataObject, B extends Builder<C>> extends AbstractCompositeVppReader<C, B> + implements VppReader<C> { + + private final RootVppReaderCustomizer<C, B> customizer; + + /** + * Create new {@link CompositeRootVppReader} + * + * @param managedDataObjectType Class object for managed data type + * @param childReaders Child nodes(container, list) readers + * @param augReaders Child augmentations readers + * @param customizer Customizer instance to customize this generic reader + * + */ + public CompositeRootVppReader(@Nonnull final Class<C> managedDataObjectType, + @Nonnull final List<ChildVppReader<? extends ChildOf<C>>> childReaders, + @Nonnull final List<ChildVppReader<? extends Augmentation<C>>> augReaders, + @Nonnull final RootVppReaderCustomizer<C, B> customizer) { + super(managedDataObjectType, childReaders, augReaders); + this.customizer = customizer; + } + + /** + * @see {@link CompositeRootVppReader#CompositeRootVppReader(Class, List, List, RootVppReaderCustomizer)} + */ + public CompositeRootVppReader(@Nonnull final Class<C> managedDataObjectType, + @Nonnull final List<ChildVppReader<? extends ChildOf<C>>> childReaders, + @Nonnull final RootVppReaderCustomizer<C, B> customizer) { + this(managedDataObjectType, childReaders, VppRWUtils.<C>emptyAugReaderList(), customizer); + } + + /** + * @see {@link CompositeRootVppReader#CompositeRootVppReader(Class, List, List, RootVppReaderCustomizer)} + */ + public CompositeRootVppReader(@Nonnull final Class<C> managedDataObjectType, + @Nonnull final RootVppReaderCustomizer<C, B> customizer) { + this(managedDataObjectType, VppRWUtils.<C>emptyChildReaderList(), VppRWUtils.<C>emptyAugReaderList(), + customizer); + } + + @Override + protected void readCurrentAttributes(@Nonnull final InstanceIdentifier<C> id, @Nonnull final B builder, + @Nonnull final ReadContext ctx) throws ReadFailedException { + customizer.readCurrentAttributes(id, builder, ctx.getContext()); + } + + @Override + protected B getBuilder(@Nonnull final InstanceIdentifier<C> id) { + return customizer.getBuilder(id); + } + +} 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()); + } +} diff --git a/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/CompositeChildVppWriter.java b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/CompositeChildVppWriter.java new file mode 100644 index 000000000..a11004e86 --- /dev/null +++ b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/CompositeChildVppWriter.java @@ -0,0 +1,105 @@ +/* + * 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 com.google.common.base.Optional; +import io.fd.honeycomb.v3po.vpp.facade.impl.util.VppRWUtils; +import io.fd.honeycomb.v3po.vpp.facade.spi.write.ChildVppWriterCustomizer; +import io.fd.honeycomb.v3po.vpp.facade.write.ChildVppWriter; +import io.fd.honeycomb.v3po.vpp.facade.write.WriteContext; +import java.util.List; +import javax.annotation.Nonnull; +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; + +public class CompositeChildVppWriter<D extends DataObject> extends AbstractCompositeVppWriter<D> + implements ChildVppWriter<D> { + + private final ChildVppWriterCustomizer<D> customizer; + + public CompositeChildVppWriter(@Nonnull final Class<D> type, + @Nonnull final List<ChildVppWriter<? extends ChildOf<D>>> childWriters, + @Nonnull final List<ChildVppWriter<? extends Augmentation<D>>> augWriters, + @Nonnull final ChildVppWriterCustomizer<D> customizer) { + super(type, childWriters, augWriters); + this.customizer = customizer; + } + + public CompositeChildVppWriter(@Nonnull final Class<D> type, + @Nonnull final List<ChildVppWriter<? extends ChildOf<D>>> childWriters, + @Nonnull final ChildVppWriterCustomizer<D> customizer) { + this(type, childWriters, VppRWUtils.<D>emptyAugWriterList(), customizer); + } + + public CompositeChildVppWriter(@Nonnull final Class<D> type, + @Nonnull final ChildVppWriterCustomizer<D> customizer) { + this(type, VppRWUtils.<D>emptyChildWriterList(), VppRWUtils.<D>emptyAugWriterList(), customizer); + } + + @Override + protected void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D data, + @Nonnull final WriteContext ctx) { + customizer.writeCurrentAttributes(id, data, ctx.getContext()); + } + + @Override + protected void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataBefore, + @Nonnull WriteContext ctx) { + customizer.deleteCurrentAttributes(id, dataBefore, ctx.getContext()); + } + + @Override + protected void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataBefore, + @Nonnull final D dataAfter, @Nonnull WriteContext ctx) { + customizer.updateCurrentAttributes(id, dataBefore, dataAfter, ctx.getContext()); + } + + @Override + public void writeChild(@Nonnull final InstanceIdentifier<? extends DataObject> parentId, + @Nonnull final DataObject parentData, @Nonnull WriteContext ctx) { + final InstanceIdentifier<D> currentId = VppRWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final Optional<D> currentData = customizer.extract(currentId, parentData); + if(currentData.isPresent()) { + writeCurrent(currentId, currentData.get(), ctx); + } + } + + @Override + public void deleteChild(@Nonnull final InstanceIdentifier<? extends DataObject> parentId, + @Nonnull final DataObject parentData, + @Nonnull final WriteContext ctx) { + final InstanceIdentifier<D> currentId = VppRWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final Optional<D> currentData = customizer.extract(currentId, parentData); + if(currentData.isPresent()) { + deleteCurrent(currentId, currentData.get(), ctx); + } + } + + @Override + public void updateChild(@Nonnull final InstanceIdentifier<? extends DataObject> parentId, + @Nonnull final DataObject parentDataBefore, @Nonnull final DataObject parentDataAfter, + @Nonnull final WriteContext ctx) { + final InstanceIdentifier<D> currentId = VppRWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final Optional<D> before = customizer.extract(currentId, parentDataBefore); + final Optional<D> after = customizer.extract(currentId, parentDataAfter); + if(before.isPresent() && after.isPresent()) { + updateCurrent(currentId, before.get(), after.get(), ctx); + } + } +} diff --git a/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/CompositeListVppWriter.java b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/CompositeListVppWriter.java new file mode 100644 index 000000000..805d951f9 --- /dev/null +++ b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/CompositeListVppWriter.java @@ -0,0 +1,178 @@ +/* + * 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 com.google.common.base.Function; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import io.fd.honeycomb.v3po.vpp.facade.impl.util.VppRWUtils; +import io.fd.honeycomb.v3po.vpp.facade.spi.write.ListVppWriterCustomizer; +import io.fd.honeycomb.v3po.vpp.facade.write.ChildVppWriter; +import io.fd.honeycomb.v3po.vpp.facade.write.WriteContext; +import java.util.List; +import java.util.Map; +import javax.annotation.Nonnull; +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.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class CompositeListVppWriter<D extends DataObject & Identifiable<K>, K extends Identifier<D>> extends AbstractCompositeVppWriter<D> + implements ChildVppWriter<D> { + + public static final Function<DataObject, Object> INDEX_FUNCTION = new Function<DataObject, Object>() { + @Override + public Object apply(final DataObject input) { + return input instanceof Identifiable<?> + ? ((Identifiable<?>) input).getKey() + : input; + } + }; + + + private final ListVppWriterCustomizer<D, K> customizer; + + public CompositeListVppWriter(@Nonnull final Class<D> type, + @Nonnull final List<ChildVppWriter<? extends ChildOf<D>>> childWriters, + @Nonnull final List<ChildVppWriter<? extends Augmentation<D>>> augWriters, + @Nonnull final ListVppWriterCustomizer<D, K> customizer) { + super(type, childWriters, augWriters); + this.customizer = customizer; + } + + public CompositeListVppWriter(@Nonnull final Class<D> type, + @Nonnull final List<ChildVppWriter<? extends ChildOf<D>>> childWriters, + @Nonnull final ListVppWriterCustomizer<D, K> customizer) { + this(type, childWriters, VppRWUtils.<D>emptyAugWriterList(), customizer); + } + + public CompositeListVppWriter(@Nonnull final Class<D> type, + @Nonnull final ListVppWriterCustomizer<D, K> customizer) { + this(type, VppRWUtils.<D>emptyChildWriterList(), VppRWUtils.<D>emptyAugWriterList(), customizer); + + } + + @Override + protected void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D data, + @Nonnull final WriteContext ctx) { + customizer.writeCurrentAttributes(id, data, ctx.getContext()); + } + + @Override + protected void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataBefore, + @Nonnull final WriteContext ctx) { + customizer.deleteCurrentAttributes(id, dataBefore, ctx.getContext()); + } + + @Override + protected void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataBefore, + @Nonnull final D dataAfter, @Nonnull final WriteContext ctx) { + customizer.updateCurrentAttributes(id, dataBefore, dataAfter, ctx.getContext()); + } + + @Override + public void writeChild(@Nonnull final InstanceIdentifier<? extends DataObject> parentId, + @Nonnull final DataObject parentData, + @Nonnull final WriteContext ctx) { + final InstanceIdentifier<D> currentId = VppRWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final List<D> currentData = customizer.extract(currentId, parentData); + for (D entry : currentData) { + writeCurrent(currentId, entry, ctx); + } + } + + @Override + public void deleteChild(@Nonnull final InstanceIdentifier<? extends DataObject> parentId, + @Nonnull final DataObject parentDataBefore, + @Nonnull final WriteContext ctx) { + final InstanceIdentifier<D> currentId = VppRWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final List<D> dataBefore = customizer.extract(currentId, parentDataBefore); + for (D entry : dataBefore) { + deleteCurrent(currentId, entry, ctx); + } + } + + @Override + public void updateChild(@Nonnull final InstanceIdentifier<? extends DataObject> parentId, + @Nonnull final DataObject parentDataBefore, @Nonnull final DataObject parentDataAfter, + @Nonnull final WriteContext ctx) { + final InstanceIdentifier<D> currentId = VppRWUtils.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); + + for (Map.Entry<Object, D> after : dataAfter.entrySet()) { + final D before = dataBefore.get(after.getKey()); + if(before == null) { + writeCurrent(currentId, after.getValue(), ctx); + } else { + updateCurrent(currentId, before, after.getValue(), ctx); + } + } + + // Delete the rest in dataBefore + for (Object deletedNodeKey : Sets.difference(dataBefore.keySet(), dataAfter.keySet())) { + final D deleted = dataBefore.get(deletedNodeKey); + deleteCurrent(currentId, deleted, ctx); + } + + } + + @Override + protected void writeCurrent(final InstanceIdentifier<D> id, final D data, final WriteContext ctx) { + // Make sure the key is present + if(isWildcarded(id)) { + super.writeCurrent(getSpecificId(id, data), data, ctx); + } else { + super.writeCurrent(id, data, ctx); + } + } + + @Override + protected void updateCurrent(final InstanceIdentifier<D> id, final D dataBefore, final D dataAfter, + final WriteContext ctx) { + // Make sure the key is present + if(isWildcarded(id)) { + super.updateCurrent(getSpecificId(id, dataBefore), dataBefore, dataAfter, ctx); + } else { + super.updateCurrent(id, dataBefore, dataAfter, ctx); + } + } + + @Override + protected void deleteCurrent(final InstanceIdentifier<D> id, final D dataBefore, final WriteContext ctx) { + // Make sure the key is present + if(isWildcarded(id)) { + super.deleteCurrent(getSpecificId(id, dataBefore), dataBefore, ctx); + } else { + super.deleteCurrent(id, dataBefore, ctx); + } + } + + private boolean isWildcarded(final InstanceIdentifier<D> id) { + return id.firstIdentifierOf(getManagedDataObjectType().getTargetType()).isWildcarded(); + } + + private InstanceIdentifier<D> getSpecificId(final InstanceIdentifier<D> currentId, final D current) { + return VppRWUtils.replaceLastInId(currentId, + new InstanceIdentifier.IdentifiableItem<>(currentId.getTargetType(), current.getKey())); + } +} diff --git a/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/CompositeRootVppWriter.java b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/CompositeRootVppWriter.java new file mode 100644 index 000000000..7bcb8dbbb --- /dev/null +++ b/v3po/vpp-facade-impl/src/main/java/io/fd/honeycomb/v3po/vpp/facade/impl/write/CompositeRootVppWriter.java @@ -0,0 +1,72 @@ +/* + * 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 io.fd.honeycomb.v3po.vpp.facade.impl.util.VppRWUtils; +import io.fd.honeycomb.v3po.vpp.facade.spi.write.RootVppWriterCustomizer; +import io.fd.honeycomb.v3po.vpp.facade.write.ChildVppWriter; +import io.fd.honeycomb.v3po.vpp.facade.write.WriteContext; +import java.util.List; +import javax.annotation.Nonnull; +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; + +public class CompositeRootVppWriter<D extends DataObject> extends AbstractCompositeVppWriter<D> { + + private final RootVppWriterCustomizer<D> customizer; + + public CompositeRootVppWriter(@Nonnull final Class<D> type, + @Nonnull final List<ChildVppWriter<? extends ChildOf<D>>> childWriters, + @Nonnull final List<ChildVppWriter<? extends Augmentation<D>>> augWriters, + @Nonnull final RootVppWriterCustomizer<D> customizer) { + super(type, childWriters, augWriters); + this.customizer = customizer; + } + + public CompositeRootVppWriter(@Nonnull final Class<D> type, + @Nonnull final List<ChildVppWriter<? extends ChildOf<D>>> childWriters, + @Nonnull final RootVppWriterCustomizer<D> customizer) { + this(type, childWriters, VppRWUtils.<D>emptyAugWriterList(), customizer); + } + + public CompositeRootVppWriter(@Nonnull final Class<D> type, + @Nonnull final RootVppWriterCustomizer<D> customizer) { + this(type, VppRWUtils.<D>emptyChildWriterList(), VppRWUtils.<D>emptyAugWriterList(), customizer); + } + + @Override + protected void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D data, + @Nonnull final WriteContext ctx) { + customizer.writeCurrentAttributes(id, data, ctx.getContext()); + } + + @Override + protected void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataBefore, + @Nonnull final WriteContext ctx) { + customizer.deleteCurrentAttributes(id, dataBefore, ctx.getContext()); + } + + @Override + protected void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, + @Nonnull final D dataBefore, + @Nonnull final D dataAfter, + @Nonnull final WriteContext ctx) { + customizer.updateCurrentAttributes(id, dataBefore, dataAfter, ctx.getContext()); + } +} |