diff options
Diffstat (limited to 'v3po/translate-utils/src/main/java')
3 files changed, 109 insertions, 14 deletions
diff --git a/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/read/NoopReaderCustomizer.java b/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/read/NoopReaderCustomizer.java index 3b703da3f..cf494efbe 100644 --- a/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/read/NoopReaderCustomizer.java +++ b/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/read/NoopReaderCustomizer.java @@ -16,7 +16,7 @@ package io.fd.honeycomb.v3po.translate.util.read; -import io.fd.honeycomb.v3po.translate.Context; +import io.fd.honeycomb.v3po.translate.read.ReadContext; import io.fd.honeycomb.v3po.translate.read.ReadFailedException; import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer; import org.opendaylight.yangtools.concepts.Builder; @@ -27,7 +27,7 @@ public abstract class NoopReaderCustomizer<C extends DataObject, B extends Build RootReaderCustomizer<C, B> { @Override - public void readCurrentAttributes(InstanceIdentifier<C> id, final B builder, final Context context) throws + public void readCurrentAttributes(InstanceIdentifier<C> id, final B builder, final ReadContext context) throws ReadFailedException { // Noop } diff --git a/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/write/TransactionMappingContext.java b/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/write/TransactionMappingContext.java new file mode 100644 index 000000000..60f4282f5 --- /dev/null +++ b/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/write/TransactionMappingContext.java @@ -0,0 +1,75 @@ +/* + * 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.util.write; + +import com.google.common.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; +import io.fd.honeycomb.v3po.translate.MappingContext; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Binding Transaction backed mapping context + */ +public class TransactionMappingContext implements MappingContext { + + private final ReadWriteTransaction readWriteTransaction; + + // TODO make async + + public TransactionMappingContext(final ReadWriteTransaction readWriteTransaction) { + this.readWriteTransaction = readWriteTransaction; + } + + @Override + public <T extends DataObject> Optional<T> read(@Nonnull final InstanceIdentifier<T> currentId) { + try { + return readWriteTransaction.read(LogicalDatastoreType.OPERATIONAL, currentId).checkedGet(); + } catch (ReadFailedException e) { + throw new IllegalStateException("Unable to perform read", e); + } + } + + @Override + public void delete(final InstanceIdentifier<?> path) { + readWriteTransaction.delete(LogicalDatastoreType.OPERATIONAL, path); + } + + @Override + public <T extends DataObject> void merge(final InstanceIdentifier<T> path, T data) { + readWriteTransaction.merge(LogicalDatastoreType.OPERATIONAL, path, data, true); + } + + @Override + public <T extends DataObject> void put(final InstanceIdentifier<T> path, T data) { + readWriteTransaction.put(LogicalDatastoreType.OPERATIONAL, path, data, true); + } + + public CheckedFuture<Void, TransactionCommitFailedException> submit() { + return readWriteTransaction.submit(); + } + + @Override + public void close() { + readWriteTransaction.cancel(); + } +} diff --git a/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/write/TransactionWriteContext.java b/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/write/TransactionWriteContext.java index 37756d5b8..20bbf19e0 100644 --- a/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/write/TransactionWriteContext.java +++ b/v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/write/TransactionWriteContext.java @@ -16,9 +16,12 @@ package io.fd.honeycomb.v3po.translate.util.write; +import static com.google.common.base.Preconditions.checkState; + import com.google.common.base.Optional; import com.google.common.util.concurrent.CheckedFuture; -import io.fd.honeycomb.v3po.translate.Context; +import io.fd.honeycomb.v3po.translate.MappingContext; +import io.fd.honeycomb.v3po.translate.ModificationCache; import io.fd.honeycomb.v3po.translate.write.WriteContext; import java.util.Map; import javax.annotation.Nonnull; @@ -38,27 +41,37 @@ public final class TransactionWriteContext implements WriteContext { private final DOMDataReadOnlyTransaction beforeTx; private final DOMDataReadOnlyTransaction afterTx; - private final Context ctx; + private final ModificationCache ctx; private final BindingNormalizedNodeSerializer serializer; + private MappingContext mappingContext; public TransactionWriteContext(final BindingNormalizedNodeSerializer serializer, final DOMDataReadOnlyTransaction beforeTx, - final DOMDataReadOnlyTransaction afterTx) { + final DOMDataReadOnlyTransaction afterTx, + final MappingContext mappingContext) { this.serializer = serializer; + // TODO do we have a BA transaction adapter ? If so, use it here and don't pass serializer this.beforeTx = beforeTx; this.afterTx = afterTx; - this.ctx = new Context(); + this.mappingContext = mappingContext; + this.ctx = new ModificationCache(); } // TODO make this asynchronous @Override - public Optional<DataObject> readBefore(@Nonnull final InstanceIdentifier<? extends DataObject> currentId) { + public <T extends DataObject> Optional<T> readBefore(@Nonnull final InstanceIdentifier<T> currentId) { return read(currentId, beforeTx); } - private Optional<DataObject> read(final InstanceIdentifier<? extends DataObject> currentId, - final DOMDataReadOnlyTransaction tx) { + @Override + public <T extends DataObject> Optional<T> readAfter(@Nonnull final InstanceIdentifier<T> currentId) { + return read(currentId, afterTx); + } + + + private <T extends DataObject> Optional<T> read(final InstanceIdentifier<T> currentId, + final DOMDataReadOnlyTransaction tx) { final YangInstanceIdentifier path = serializer.toYangInstanceIdentifier(currentId); final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = @@ -75,20 +88,25 @@ public final class TransactionWriteContext implements WriteContext { final NormalizedNode<?, ?> data = optional.get(); final Map.Entry<InstanceIdentifier<?>, DataObject> entry = serializer.fromNormalizedNode(path, data); - return Optional.of(entry.getValue()); + final Class<T> targetType = currentId.getTargetType(); + checkState(targetType.isAssignableFrom(entry.getValue().getClass()), + "Unexpected data object type, should be: %s, but was: %s", targetType, entry.getValue().getClass()); + return Optional.of(targetType.cast(entry.getValue())); } catch (ReadFailedException e) { throw new IllegalStateException("Unable to perform read", e); } } + @Nonnull @Override - public Optional<DataObject> readAfter(@Nonnull final InstanceIdentifier<? extends DataObject> currentId) { - return read(currentId, afterTx); + public ModificationCache getModificationCache() { + return ctx; } + @Nonnull @Override - public Context getContext() { - return ctx; + public MappingContext getMappingContext() { + return mappingContext; } /** @@ -97,5 +115,7 @@ public final class TransactionWriteContext implements WriteContext { @Override public void close() { ctx.close(); + beforeTx.close(); + afterTx.close(); } } |