From 799ee62f15dff07534beaf98a3ac551deffade38 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Tue, 17 May 2016 09:10:39 +0200 Subject: HONEYCOMB-61: Add BA broker for context data tree With broker, context data can be accessed in a transactional manner, same as config data + Renamed data-api concepts to not include DataTree + Renamed context related concepts to better distinguish between them + Now passing full ReadContext to read customizers + Naming context is backed by context data broker Change-Id: I0b2876dd74a31a9ced7d9b5145672868e12f8b82 Signed-off-by: Maros Marsalek --- .../translate/util/read/NoopReaderCustomizer.java | 4 +- .../util/write/TransactionMappingContext.java | 75 ++++++++++++++++++++++ .../util/write/TransactionWriteContext.java | 44 +++++++++---- 3 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 v3po/translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/util/write/TransactionMappingContext.java (limited to 'v3po/translate-utils/src/main/java/io') 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 { @Override - public void readCurrentAttributes(InstanceIdentifier id, final B builder, final Context context) throws + public void readCurrentAttributes(InstanceIdentifier 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 Optional read(@Nonnull final InstanceIdentifier 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 void merge(final InstanceIdentifier path, T data) { + readWriteTransaction.merge(LogicalDatastoreType.OPERATIONAL, path, data, true); + } + + @Override + public void put(final InstanceIdentifier path, T data) { + readWriteTransaction.put(LogicalDatastoreType.OPERATIONAL, path, data, true); + } + + public CheckedFuture 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 readBefore(@Nonnull final InstanceIdentifier currentId) { + public Optional readBefore(@Nonnull final InstanceIdentifier currentId) { return read(currentId, beforeTx); } - private Optional read(final InstanceIdentifier currentId, - final DOMDataReadOnlyTransaction tx) { + @Override + public Optional readAfter(@Nonnull final InstanceIdentifier currentId) { + return read(currentId, afterTx); + } + + + private Optional read(final InstanceIdentifier currentId, + final DOMDataReadOnlyTransaction tx) { final YangInstanceIdentifier path = serializer.toYangInstanceIdentifier(currentId); final CheckedFuture>, ReadFailedException> read = @@ -75,20 +88,25 @@ public final class TransactionWriteContext implements WriteContext { final NormalizedNode data = optional.get(); final Map.Entry, DataObject> entry = serializer.fromNormalizedNode(path, data); - return Optional.of(entry.getValue()); + final Class 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 readAfter(@Nonnull final InstanceIdentifier 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(); } } -- cgit 1.2.3-korg