From e1743c8eccee7d5ea8ad2c247d2575e8fd219fe4 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Tue, 12 Apr 2016 10:13:18 +0200 Subject: HONEYCOMB-9: Remove references to VPP from translation layer Change-Id: I281db366a112edc08203e8cb392a212708d4552a Signed-off-by: Maros Marsalek --- v3po/translate-impl/pom.xml | 88 +++++++ .../impl/read/AbstractCompositeReader.java | 232 ++++++++++++++++++ .../translate/impl/read/CompositeChildReader.java | 104 ++++++++ .../translate/impl/read/CompositeListReader.java | 137 +++++++++++ .../translate/impl/read/CompositeRootReader.java | 91 +++++++ .../impl/write/AbstractCompositeWriter.java | 270 +++++++++++++++++++++ .../translate/impl/write/CompositeChildWriter.java | 105 ++++++++ .../translate/impl/write/CompositeListWriter.java | 179 ++++++++++++++ .../translate/impl/write/CompositeRootWriter.java | 72 ++++++ 9 files changed, 1278 insertions(+) create mode 100644 v3po/translate-impl/pom.xml create mode 100644 v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java create mode 100644 v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeChildReader.java create mode 100644 v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeListReader.java create mode 100644 v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeRootReader.java create mode 100644 v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/AbstractCompositeWriter.java create mode 100644 v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeChildWriter.java create mode 100644 v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeListWriter.java create mode 100644 v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeRootWriter.java (limited to 'v3po/translate-impl') diff --git a/v3po/translate-impl/pom.xml b/v3po/translate-impl/pom.xml new file mode 100644 index 000000000..214d48ff0 --- /dev/null +++ b/v3po/translate-impl/pom.xml @@ -0,0 +1,88 @@ + + + + + io.fd.honeycomb.common + api-parent + 1.0.0-SNAPSHOT + ../../common/api-parent + + + 4.0.0 + io.fd.honeycomb.v3po + translate-impl + 1.0.0-SNAPSHOT + bundle + + + + ${project.groupId} + translate-api + ${project.version} + + + ${project.groupId} + translate-spi + ${project.version} + + + ${project.groupId} + translate-utils + ${project.version} + + + + + ${project.groupId} + v3po-api + ${project.version} + test + + + junit + junit + test + + + org.mockito + mockito-all + test + + + + + + + maven-jar-plugin + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.groupId}.${project.artifactId} + + io.fd.honeycomb.v3po.translate.impl.* + + + + + + + + diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java new file mode 100644 index 000000000..f9fa0eeaa --- /dev/null +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.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.translate.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.translate.util.ReflectionUtils; +import io.fd.honeycomb.v3po.translate.util.RWUtils; +import io.fd.honeycomb.v3po.translate.read.ChildReader; +import io.fd.honeycomb.v3po.translate.read.ReadContext; +import io.fd.honeycomb.v3po.translate.read.ReadFailedException; +import io.fd.honeycomb.v3po.translate.read.Reader; +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 AbstractCompositeReader> implements Reader { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeReader.class); + + private final Map, ChildReader>> childReaders; + private final Map, ChildReader>> augReaders; + private final InstanceIdentifier instanceIdentifier; + + AbstractCompositeReader(final Class managedDataObjectType, + final List>> childReaders, + final List>> augReaders) { + this.childReaders = RWUtils.uniqueLinkedIndex(childReaders, RWUtils.MANAGER_CLASS_FUNCTION); + this.augReaders = RWUtils.uniqueLinkedIndex(augReaders, RWUtils.MANAGER_CLASS_AUG_FUNCTION); + this.instanceIdentifier = InstanceIdentifier.create(managedDataObjectType); + } + + @Nonnull + @Override + public final InstanceIdentifier getManagedDataObjectType() { + return instanceIdentifier; + } + + /** + * @param id {@link InstanceIdentifier} pointing to current node. In case of keyed list, key must be present. + * + */ + protected Optional readCurrent(final InstanceIdentifier 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 (ChildReader> child : childReaders.values()) { + LOG.debug("{}: Reading child from: {}", this, child); + child.read(id, builder, ctx); + } + + for (ChildReader> 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 read = built.equals(emptyValue) + ? Optional.absent() + : Optional.of(built); + + LOG.debug("{}: Current node read successfully. Result: {}", this, read); + return read; + } + + @Nonnull + @Override + @SuppressWarnings("unchecked") + public Optional read(@Nonnull final InstanceIdentifier id, + @Nonnull final ReadContext ctx) + throws ReadFailedException { + LOG.trace("{}: Reading : {}", this, id); + if (id.getTargetType().equals(getManagedDataObjectType().getTargetType())) { + return readCurrent((InstanceIdentifier) id, ctx); + } else { + return readSubtree(id, ctx); + } + } + + private Optional readSubtree(final InstanceIdentifier id, + @Nonnull final ReadContext ctx) + throws ReadFailedException { + LOG.debug("{}: Reading subtree: {}", this, id); + final Class next = RWUtils.getNextId(id, getManagedDataObjectType()).getType(); + final ChildReader> reader = childReaders.get(next); + + if (reader != null) { + LOG.debug("{}: Reading subtree: {} from: {}", this, id, reader); + return reader.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 currentId = RWUtils.cutId(id, getManagedDataObjectType()); + final Optional current = readCurrent(currentId, ctx); + // then perform post-reading filtering (return only requested sub-node) + final Optional 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 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 id); + + // TODO move filtering out of here into a dedicated Filter ifc + @Nonnull + private static Optional filterSubtree(@Nonnull final DataObject parent, + @Nonnull final InstanceIdentifier 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 = + RWUtils.getNextId(absolutPath, InstanceIdentifier.create(parent.getClass())); + + Optional method = ReflectionUtils.findMethodReflex(managedType, "get", + Collections.>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.>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 filterList(final DataObject parent, + final InstanceIdentifier.PathArgument nextId, + final Method method) { + final List invoke = (List) 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() { + @Override + public boolean apply(@Nullable final DataObject input) { + final Optional keyGetter = + ReflectionUtils.findMethodReflex(nextId.getType(), "get", + Collections.>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/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeChildReader.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeChildReader.java new file mode 100644 index 000000000..46a59bb90 --- /dev/null +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeChildReader.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.translate.impl.read; + +import com.google.common.annotations.Beta; +import com.google.common.base.Optional; +import io.fd.honeycomb.v3po.translate.read.ReadContext; +import io.fd.honeycomb.v3po.translate.read.ReadFailedException; +import io.fd.honeycomb.v3po.translate.util.RWUtils; +import io.fd.honeycomb.v3po.translate.read.ChildReader; +import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer; +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 ChildReader} able to place the read result into + * parent builder object. + */ +@Beta +@ThreadSafe +public final class CompositeChildReader> extends AbstractCompositeReader + implements ChildReader { + + private final ChildReaderCustomizer customizer; + + /** + * Create new {@link CompositeChildReader} + * + * @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 CompositeChildReader(@Nonnull final Class managedDataObjectType, + @Nonnull final List>> childReaders, + @Nonnull final List>> augReaders, + @Nonnull final ChildReaderCustomizer customizer) { + super(managedDataObjectType, childReaders, augReaders); + this.customizer = customizer; + } + + /** + * @see {@link CompositeChildReader#CompositeChildReader(Class, List, List, ChildReaderCustomizer)} + */ + public CompositeChildReader(@Nonnull final Class managedDataObjectType, + @Nonnull final List>> childReaders, + @Nonnull final ChildReaderCustomizer customizer) { + this(managedDataObjectType, childReaders, RWUtils.emptyAugReaderList(), customizer); + } + + /** + * @see {@link CompositeChildReader#CompositeChildReader(Class, List, List, ChildReaderCustomizer)} + */ + public CompositeChildReader(@Nonnull final Class managedDataObjectType, + @Nonnull final ChildReaderCustomizer customizer) { + this(managedDataObjectType, RWUtils.emptyChildReaderList(), RWUtils.emptyAugReaderList(), + customizer); + } + + @Override + public final void read(@Nonnull final InstanceIdentifier parentId, + @Nonnull final Builder parentBuilder, + @Nonnull final ReadContext ctx) throws ReadFailedException { + final Optional read = readCurrent(RWUtils.appendTypeToId(parentId, getManagedDataObjectType()), ctx); + + if(read.isPresent()) { + customizer.merge(parentBuilder, read.get()); + } + } + + @Override + protected void readCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final B builder, + @Nonnull final ReadContext ctx) + throws ReadFailedException { + customizer.readCurrentAttributes(id, builder, ctx.getContext()); + } + + @Override + protected B getBuilder(@Nonnull final InstanceIdentifier id) { + return customizer.getBuilder(id); + } + +} diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeListReader.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeListReader.java new file mode 100644 index 000000000..fd7b279ca --- /dev/null +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeListReader.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.translate.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.translate.read.ReadFailedException; +import io.fd.honeycomb.v3po.translate.util.RWUtils; +import io.fd.honeycomb.v3po.translate.read.ChildReader; +import io.fd.honeycomb.v3po.translate.read.ListReader; +import io.fd.honeycomb.v3po.translate.read.ReadContext; +import io.fd.honeycomb.v3po.translate.spi.read.ListReaderCustomizer; +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 ChildReader} 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 CompositeListReader, K extends Identifier, B extends Builder> + extends AbstractCompositeReader implements ChildReader, ListReader { + + private static final Logger LOG = LoggerFactory.getLogger(CompositeListReader.class); + + private final ListReaderCustomizer customizer; + + /** + * Create new {@link CompositeListReader} + * + * @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 CompositeListReader(@Nonnull final Class managedDataObjectType, + @Nonnull final List>> childReaders, + @Nonnull final List>> augReaders, + @Nonnull final ListReaderCustomizer customizer) { + super(managedDataObjectType, childReaders, augReaders); + this.customizer = customizer; + } + + /** + * @see {@link CompositeListReader#CompositeListReader(Class, List, List, ListReaderCustomizer)} + */ + public CompositeListReader(@Nonnull final Class managedDataObjectType, + @Nonnull final List>> childReaders, + @Nonnull final ListReaderCustomizer customizer) { + this(managedDataObjectType, childReaders, RWUtils.emptyAugReaderList(), customizer); + } + + /** + * @see {@link CompositeListReader#CompositeListReader(Class, List, List, ListReaderCustomizer)} + */ + public CompositeListReader(@Nonnull final Class managedDataObjectType, + @Nonnull final ListReaderCustomizer customizer) { + this(managedDataObjectType, RWUtils.emptyChildReaderList(), RWUtils.emptyAugReaderList(), + customizer); + } + + @Override + public void read(@Nonnull final InstanceIdentifier id, + @Nonnull final Builder parentBuilder, + @Nonnull final ReadContext ctx) throws ReadFailedException { + // Create ID pointing to current node + final InstanceIdentifier currentId = RWUtils.appendTypeToId(id, getManagedDataObjectType()); + // Read all, since current ID is definitely wildcarded + final List ifcs = readList(currentId, ctx); + customizer.merge(parentBuilder, ifcs); + } + + @Override + @Nonnull + public List readList(@Nonnull final InstanceIdentifier id, + @Nonnull final ReadContext ctx) throws ReadFailedException { + LOG.trace("{}: Reading all list entries", this); + final List allIds = customizer.getAllIds(id, ctx.getContext()); + LOG.debug("{}: Reading list entries for: {}", this, allIds); + + final ArrayList allEntries = new ArrayList<>(allIds.size()); + for (K key : allIds) { + final InstanceIdentifier.IdentifiableItem currentBdItem = + RWUtils.getCurrentIdItem(id, key); + final InstanceIdentifier keyedId = RWUtils.replaceLastInId(id, currentBdItem); + final Optional 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 id, @Nonnull final B builder, + @Nonnull final ReadContext ctx) + throws ReadFailedException { + customizer.readCurrentAttributes(id, builder, ctx.getContext()); + } + + @Override + protected B getBuilder(@Nonnull final InstanceIdentifier id) { + return customizer.getBuilder(id); + } + +} diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeRootReader.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeRootReader.java new file mode 100644 index 000000000..f297abb76 --- /dev/null +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/CompositeRootReader.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.translate.impl.read; + +import com.google.common.annotations.Beta; +import io.fd.honeycomb.v3po.translate.read.ReadFailedException; +import io.fd.honeycomb.v3po.translate.util.RWUtils; +import io.fd.honeycomb.v3po.translate.read.ChildReader; +import io.fd.honeycomb.v3po.translate.read.ReadContext; +import io.fd.honeycomb.v3po.translate.read.Reader; +import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer; +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 Reader} + */ +@Beta +@ThreadSafe +public final class CompositeRootReader> extends AbstractCompositeReader + implements Reader { + + private final RootReaderCustomizer customizer; + + /** + * Create new {@link CompositeRootReader} + * + * @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 CompositeRootReader(@Nonnull final Class managedDataObjectType, + @Nonnull final List>> childReaders, + @Nonnull final List>> augReaders, + @Nonnull final RootReaderCustomizer customizer) { + super(managedDataObjectType, childReaders, augReaders); + this.customizer = customizer; + } + + /** + * @see {@link CompositeRootReader#CompositeRootReader(Class, List, List, RootReaderCustomizer)} + */ + public CompositeRootReader(@Nonnull final Class managedDataObjectType, + @Nonnull final List>> childReaders, + @Nonnull final RootReaderCustomizer customizer) { + this(managedDataObjectType, childReaders, RWUtils.emptyAugReaderList(), customizer); + } + + /** + * @see {@link CompositeRootReader#CompositeRootReader(Class, List, List, RootReaderCustomizer)} + */ + public CompositeRootReader(@Nonnull final Class managedDataObjectType, + @Nonnull final RootReaderCustomizer customizer) { + this(managedDataObjectType, RWUtils.emptyChildReaderList(), RWUtils.emptyAugReaderList(), + customizer); + } + + @Override + protected void readCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final B builder, + @Nonnull final ReadContext ctx) throws ReadFailedException { + customizer.readCurrentAttributes(id, builder, ctx.getContext()); + } + + @Override + protected B getBuilder(@Nonnull final InstanceIdentifier id) { + return customizer.getBuilder(id); + } + +} diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/AbstractCompositeWriter.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/AbstractCompositeWriter.java new file mode 100644 index 000000000..e971fbeff --- /dev/null +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/AbstractCompositeWriter.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.translate.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.translate.TranslationException; +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.Writer; +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 AbstractCompositeWriter implements Writer { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeWriter.class); + + private final Map, ChildWriter>> childWriters; + private final Map, ChildWriter>> augWriters; + private final InstanceIdentifier instanceIdentifier; + + public AbstractCompositeWriter(final Class type, + final List>> childWriters, + final List>> augWriters) { + this.instanceIdentifier = InstanceIdentifier.create(type); + this.childWriters = RWUtils.uniqueLinkedIndex(childWriters, RWUtils.MANAGER_CLASS_FUNCTION); + this.augWriters = RWUtils.uniqueLinkedIndex(augWriters, RWUtils.MANAGER_CLASS_AUG_FUNCTION); + } + + protected void writeCurrent(final InstanceIdentifier 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 (ChildWriter> child : childWriters.values()) { + LOG.debug("{}: Writing child in: {}", this, child); + child.writeChild(id, data, ctx); + } + + for (ChildWriter> 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 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 (ChildWriter> child : childWriters.values()) { + LOG.debug("{}: Updating child in: {}", this, child); + child.updateChild(id, dataBefore, dataAfter, ctx); + } + + for (ChildWriter> 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 id, final D dataBefore, final WriteContext ctx) { + LOG.debug("{}: Deleting current: {} dataBefore: {}", this, id, dataBefore); + + // delete in reversed order + for (ChildWriter> child : reverseCollection(augWriters.values())) { + LOG.debug("{}: Deleting augment in: {}", this, child); + child.deleteChild(id, dataBefore, ctx); + } + + for (ChildWriter> 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 id, + @Nullable final DataObject dataBefore, + @Nullable final DataObject dataAfter, + @Nonnull final WriteContext ctx) throws TranslationException { + LOG.debug("{}: Updating : {}", this, id); + LOG.trace("{}: Updating : {}, from: {} to: {}", this, id, dataBefore, dataAfter); + + if (idPointsToCurrent(id)) { + if(isWrite(dataBefore, dataAfter)) { + writeCurrent((InstanceIdentifier) id, castToManaged(dataAfter), ctx); + } else if(isDelete(dataBefore, dataAfter)) { + deleteCurrent((InstanceIdentifier) id, castToManaged(dataBefore), ctx); + } else { + checkArgument(dataBefore != null && dataAfter != null, "No data to process"); + updateCurrent((InstanceIdentifier) 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 id, + final DataObject dataAfter, final WriteContext ctx) throws TranslationException { + LOG.debug("{}: Writing subtree: {}", this, id); + final Writer> writer = getNextWriter(id); + + if (writer != null) { + LOG.debug("{}: Writing subtree: {} in: {}", this, id, writer); + writer.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 currentId = RWUtils.cutId(id, getManagedDataObjectType()); + Optional currentDataAfter = ctx.readAfter(currentId); + LOG.debug("{}: Dedicated subtree writer missing for: {}. Writing current.", this, + RWUtils.getNextId(id, getManagedDataObjectType()).getType(), currentDataAfter); + writeCurrent(currentId, castToManaged(currentDataAfter.get()), ctx); + } + } + + private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier id) { + return id.getTargetType().equals(getManagedDataObjectType().getTargetType()); + } + + @SuppressWarnings("unchecked") + private void deleteSubtree(final InstanceIdentifier id, + final DataObject dataBefore, final WriteContext ctx) throws TranslationException { + LOG.debug("{}: Deleting subtree: {}", this, id); + final Writer> writer = getNextWriter(id); + + if (writer != null) { + LOG.debug("{}: Deleting subtree: {} in: {}", this, id, writer); + writer.update(id, dataBefore, null, ctx); + } else { + updateSubtreeFromCurrent(id, ctx); + } + } + + @SuppressWarnings("unchecked") + private void updateSubtreeFromCurrent(final InstanceIdentifier id, final WriteContext ctx) { + final InstanceIdentifier currentId = RWUtils.cutId(id, getManagedDataObjectType()); + Optional currentDataBefore = ctx.readBefore(currentId); + Optional currentDataAfter = ctx.readAfter(currentId); + LOG.debug("{}: Dedicated subtree writer missing for: {}. Updating current without subtree", this, + RWUtils.getNextId(id, getManagedDataObjectType()).getType(), currentDataAfter); + updateCurrent((InstanceIdentifier) id, castToManaged(currentDataBefore.orNull()), + castToManaged(currentDataAfter.orNull()), ctx); + } + + @SuppressWarnings("unchecked") + private void updateSubtree(final InstanceIdentifier id, + final DataObject dataBefore, + final DataObject dataAfter, + final WriteContext ctx) throws TranslationException { + LOG.debug("{}: Updating subtree: {}", this, id); + final Writer> writer = getNextWriter(id); + + if (writer != null) { + LOG.debug("{}: Updating subtree: {} in: {}", this, id, writer); + writer.update(id, dataBefore, dataAfter, ctx); + } else { + updateSubtreeFromCurrent(id, ctx); + } + } + + private Writer> getNextWriter(final InstanceIdentifier id) { + final Class next = RWUtils.getNextId(id, getManagedDataObjectType()).getType(); + return childWriters.get(next); + } + + private static List reverseCollection(final Collection original) { + // TODO find a better reverse mechanism (probably a different collection for child writers is necessary) + final ArrayList list = Lists.newArrayList(original); + Collections.reverse(list); + return list; + } + + protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier id, + @Nonnull final D data, + @Nonnull final WriteContext ctx); + + protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier id, + @Nonnull final D dataBefore, + @Nonnull final WriteContext ctx); + + protected abstract void updateCurrentAttributes(@Nonnull final InstanceIdentifier id, + @Nonnull final D dataBefore, + @Nonnull final D dataAfter, + @Nonnull final WriteContext ctx); + + @Nonnull + @Override + public InstanceIdentifier getManagedDataObjectType() { + return instanceIdentifier; + } + + + @Override + public String toString() { + return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName()); + } +} diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeChildWriter.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeChildWriter.java new file mode 100644 index 000000000..fbfeffbdb --- /dev/null +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeChildWriter.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.translate.impl.write; + +import com.google.common.base.Optional; +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.spi.write.ChildWriterCustomizer; +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 CompositeChildWriter extends AbstractCompositeWriter + implements ChildWriter { + + private final ChildWriterCustomizer customizer; + + public CompositeChildWriter(@Nonnull final Class type, + @Nonnull final List>> childWriters, + @Nonnull final List>> augWriters, + @Nonnull final ChildWriterCustomizer customizer) { + super(type, childWriters, augWriters); + this.customizer = customizer; + } + + public CompositeChildWriter(@Nonnull final Class type, + @Nonnull final List>> childWriters, + @Nonnull final ChildWriterCustomizer customizer) { + this(type, childWriters, RWUtils.emptyAugWriterList(), customizer); + } + + public CompositeChildWriter(@Nonnull final Class type, + @Nonnull final ChildWriterCustomizer customizer) { + this(type, RWUtils.emptyChildWriterList(), RWUtils.emptyAugWriterList(), customizer); + } + + @Override + protected void writeCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final D data, + @Nonnull final WriteContext ctx) { + customizer.writeCurrentAttributes(id, data, ctx.getContext()); + } + + @Override + protected void deleteCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final D dataBefore, + @Nonnull WriteContext ctx) { + customizer.deleteCurrentAttributes(id, dataBefore, ctx.getContext()); + } + + @Override + protected void updateCurrentAttributes(@Nonnull final InstanceIdentifier 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 parentId, + @Nonnull final DataObject parentData, @Nonnull WriteContext ctx) { + final InstanceIdentifier currentId = RWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final Optional currentData = customizer.extract(currentId, parentData); + if(currentData.isPresent()) { + writeCurrent(currentId, currentData.get(), ctx); + } + } + + @Override + public void deleteChild(@Nonnull final InstanceIdentifier parentId, + @Nonnull final DataObject parentData, + @Nonnull final WriteContext ctx) { + final InstanceIdentifier currentId = RWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final Optional currentData = customizer.extract(currentId, parentData); + if(currentData.isPresent()) { + deleteCurrent(currentId, currentData.get(), ctx); + } + } + + @Override + public void updateChild(@Nonnull final InstanceIdentifier parentId, + @Nonnull final DataObject parentDataBefore, @Nonnull final DataObject parentDataAfter, + @Nonnull final WriteContext ctx) { + final InstanceIdentifier currentId = RWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final Optional before = customizer.extract(currentId, parentDataBefore); + final Optional after = customizer.extract(currentId, parentDataAfter); + if(before.isPresent() && after.isPresent()) { + updateCurrent(currentId, before.get(), after.get(), ctx); + } + } +} 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 new file mode 100644 index 000000000..dc924fe39 --- /dev/null +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeListWriter.java @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.v3po.translate.impl.write; + +import 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.translate.spi.write.ListWriterCustomizer; +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 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 CompositeListWriter, K extends Identifier> extends + AbstractCompositeWriter + implements ChildWriter { + + public static final Function INDEX_FUNCTION = new Function() { + @Override + public Object apply(final DataObject input) { + return input instanceof Identifiable + ? ((Identifiable) input).getKey() + : input; + } + }; + + + private final ListWriterCustomizer customizer; + + public CompositeListWriter(@Nonnull final Class type, + @Nonnull final List>> childWriters, + @Nonnull final List>> augWriters, + @Nonnull final ListWriterCustomizer customizer) { + super(type, childWriters, augWriters); + this.customizer = customizer; + } + + public CompositeListWriter(@Nonnull final Class type, + @Nonnull final List>> childWriters, + @Nonnull final ListWriterCustomizer customizer) { + this(type, childWriters, RWUtils.emptyAugWriterList(), customizer); + } + + public CompositeListWriter(@Nonnull final Class type, + @Nonnull final ListWriterCustomizer customizer) { + this(type, RWUtils.emptyChildWriterList(), RWUtils.emptyAugWriterList(), customizer); + + } + + @Override + protected void writeCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final D data, + @Nonnull final WriteContext ctx) { + customizer.writeCurrentAttributes(id, data, ctx.getContext()); + } + + @Override + protected void deleteCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final D dataBefore, + @Nonnull final WriteContext ctx) { + customizer.deleteCurrentAttributes(id, dataBefore, ctx.getContext()); + } + + @Override + protected void updateCurrentAttributes(@Nonnull final InstanceIdentifier 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 parentId, + @Nonnull final DataObject parentData, + @Nonnull final WriteContext ctx) { + final InstanceIdentifier currentId = RWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final List currentData = customizer.extract(currentId, parentData); + for (D entry : currentData) { + writeCurrent(currentId, entry, ctx); + } + } + + @Override + public void deleteChild(@Nonnull final InstanceIdentifier parentId, + @Nonnull final DataObject parentDataBefore, + @Nonnull final WriteContext ctx) { + final InstanceIdentifier currentId = RWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final List dataBefore = customizer.extract(currentId, parentDataBefore); + for (D entry : dataBefore) { + deleteCurrent(currentId, entry, ctx); + } + } + + @Override + public void updateChild(@Nonnull final InstanceIdentifier parentId, + @Nonnull final DataObject parentDataBefore, @Nonnull final DataObject parentDataAfter, + @Nonnull final WriteContext ctx) { + final InstanceIdentifier currentId = RWUtils.appendTypeToId(parentId, getManagedDataObjectType()); + final ImmutableMap + dataBefore = Maps.uniqueIndex(customizer.extract(currentId, parentDataBefore), INDEX_FUNCTION); + final ImmutableMap + dataAfter = Maps.uniqueIndex(customizer.extract(currentId, parentDataAfter), INDEX_FUNCTION); + + for (Map.Entry 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 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 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 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 id) { + return id.firstIdentifierOf(getManagedDataObjectType().getTargetType()).isWildcarded(); + } + + private InstanceIdentifier getSpecificId(final InstanceIdentifier currentId, final D current) { + return RWUtils.replaceLastInId(currentId, + new InstanceIdentifier.IdentifiableItem<>(currentId.getTargetType(), current.getKey())); + } +} diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeRootWriter.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeRootWriter.java new file mode 100644 index 000000000..acf2cfd2e --- /dev/null +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/write/CompositeRootWriter.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.translate.impl.write; + +import io.fd.honeycomb.v3po.translate.write.WriteContext; +import io.fd.honeycomb.v3po.translate.util.RWUtils; +import io.fd.honeycomb.v3po.translate.spi.write.RootWriterCustomizer; +import io.fd.honeycomb.v3po.translate.write.ChildWriter; +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 CompositeRootWriter extends AbstractCompositeWriter { + + private final RootWriterCustomizer customizer; + + public CompositeRootWriter(@Nonnull final Class type, + @Nonnull final List>> childWriters, + @Nonnull final List>> augWriters, + @Nonnull final RootWriterCustomizer customizer) { + super(type, childWriters, augWriters); + this.customizer = customizer; + } + + public CompositeRootWriter(@Nonnull final Class type, + @Nonnull final List>> childWriters, + @Nonnull final RootWriterCustomizer customizer) { + this(type, childWriters, RWUtils.emptyAugWriterList(), customizer); + } + + public CompositeRootWriter(@Nonnull final Class type, + @Nonnull final RootWriterCustomizer customizer) { + this(type, RWUtils.emptyChildWriterList(), RWUtils.emptyAugWriterList(), customizer); + } + + @Override + protected void writeCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final D data, + @Nonnull final WriteContext ctx) { + customizer.writeCurrentAttributes(id, data, ctx.getContext()); + } + + @Override + protected void deleteCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final D dataBefore, + @Nonnull final WriteContext ctx) { + customizer.deleteCurrentAttributes(id, dataBefore, ctx.getContext()); + } + + @Override + protected void updateCurrentAttributes(@Nonnull final InstanceIdentifier id, + @Nonnull final D dataBefore, + @Nonnull final D dataAfter, + @Nonnull final WriteContext ctx) { + customizer.updateCurrentAttributes(id, dataBefore, dataAfter, ctx.getContext()); + } +} -- cgit 1.2.3-korg