diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2016-03-04 12:32:10 +0100 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2016-03-21 12:22:54 +0100 |
commit | 133b3268457442abf2c44a54e9f5ce58fca7622b (patch) | |
tree | 09b253bb3a26a79fd36b656e84d976468f0866fd /v3po/impl/src/main | |
parent | e1a4b927003d8ccf90201943940820f8e3174ea4 (diff) |
Data-tree based DataBroker for Honeycomb agent.
Change-Id: I2cda490bfc47d748052587066b3f63d5c27d518c
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'v3po/impl/src/main')
4 files changed, 444 insertions, 0 deletions
diff --git a/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/ReadWriteTransaction.java b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/ReadWriteTransaction.java new file mode 100644 index 000000000..7faeba54a --- /dev/null +++ b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/ReadWriteTransaction.java @@ -0,0 +1,101 @@ +/* + * 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.impl.data; + +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.ListenableFuture; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.common.api.TransactionStatus; +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.controller.md.sal.dom.api.DOMDataReadOnlyTransaction; +import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction; +import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction; +import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; + +/** + * Composite DOM transaction that delegates reads to a {@link DOMDataReadTransaction} delegate and writes to a {@link + * DOMDataWriteTransaction} delegate. + */ +final class ReadWriteTransaction implements DOMDataReadWriteTransaction { + + private final DOMDataReadOnlyTransaction delegateReadTx; + private final DOMDataWriteTransaction delegateWriteTx; + + ReadWriteTransaction(@Nonnull final DOMDataReadOnlyTransaction delegateReadTx, + @Nonnull final DOMDataWriteTransaction delegateWriteTx) { + this.delegateReadTx = Preconditions.checkNotNull(delegateReadTx, "delegateReadTx should not be null"); + this.delegateWriteTx = Preconditions.checkNotNull(delegateWriteTx, "delegateWriteTx should not be null"); + } + + @Override + public boolean cancel() { + delegateReadTx.close(); + return delegateWriteTx.cancel(); + } + + @Override + public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, + final NormalizedNode<?, ?> data) { + delegateWriteTx.put(store, path, data); + } + + @Override + public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, + final NormalizedNode<?, ?> data) { + delegateWriteTx.merge(store, path, data); + } + + @Override + public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) { + delegateWriteTx.delete(store, path); + } + + @Override + public CheckedFuture<Void, TransactionCommitFailedException> submit() { + return delegateWriteTx.submit(); + } + + @Override + public ListenableFuture<RpcResult<TransactionStatus>> commit() { + return delegateWriteTx.commit(); + } + + @Override + public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store, + final YangInstanceIdentifier path) { + return delegateReadTx.read(store, path); + } + + @Override + public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, + final YangInstanceIdentifier path) { + return delegateReadTx.exists(store, path); + } + + @Override + public Object getIdentifier() { + return this; + } +} + diff --git a/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/VppDataBroker.java b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/VppDataBroker.java new file mode 100644 index 000000000..97697da65 --- /dev/null +++ b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/VppDataBroker.java @@ -0,0 +1,95 @@ +/* + * 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.impl.data; + +import com.google.common.base.Preconditions; +import java.util.Collections; +import java.util.Map; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener; +import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; +import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension; +import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener; +import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction; +import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction; +import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction; +import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain; +import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; + +/** + * Data Broker which provides data transaction functionality for VPP using {@link NormalizedNode} data format. + */ +public class VppDataBroker implements DOMDataBroker { + + private final ReadableVppDataTree operationalData; + private final VppDataTree configDataTree; + + /** + * Creates VppDataBroker instance. + * + * @param operationalData VPP operational data + * @param configDataTree VPP configuration data + */ + public VppDataBroker(@Nonnull final ReadableVppDataTree operationalData, + @Nonnull final VppDataTree configDataTree) { + this.operationalData = Preconditions.checkNotNull(operationalData, "operationalData should not be null"); + this.configDataTree = Preconditions.checkNotNull(configDataTree, "configDataProxy should not be null"); + } + + @Override + public DOMDataReadOnlyTransaction newReadOnlyTransaction() { + return new VppReadOnlyTransaction(operationalData, configDataTree.takeSnapshot()); + } + + @Override + public DOMDataReadWriteTransaction newReadWriteTransaction() { + // todo use the same snapshot + final VppDataTreeSnapshot configSnapshot = configDataTree.takeSnapshot(); + final DOMDataReadOnlyTransaction readOnlyTx = new VppReadOnlyTransaction(operationalData, configSnapshot); + final DOMDataWriteTransaction writeOnlyTx = new VppWriteTransaction(configDataTree, configSnapshot); + return new ReadWriteTransaction(readOnlyTx, writeOnlyTx); + } + + @Override + public DOMDataWriteTransaction newWriteOnlyTransaction() { + return new VppWriteTransaction(configDataTree); + } + + @Override + public ListenerRegistration<DOMDataChangeListener> registerDataChangeListener(final LogicalDatastoreType store, + final YangInstanceIdentifier path, + final DOMDataChangeListener listener, + final DataChangeScope triggeringScope) { + throw new UnsupportedOperationException("Not supported"); + } + + @Override + public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) { + throw new UnsupportedOperationException("Not supported"); + } + + @Nonnull + @Override + public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() { + return Collections.emptyMap(); + } +} + + diff --git a/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/VppReadOnlyTransaction.java b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/VppReadOnlyTransaction.java new file mode 100644 index 000000000..94cef679f --- /dev/null +++ b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/VppReadOnlyTransaction.java @@ -0,0 +1,103 @@ +/* + * 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.impl.data; + +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +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.dom.api.DOMDataReadOnlyTransaction; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final class VppReadOnlyTransaction implements DOMDataReadOnlyTransaction { + + private static final Logger LOG = LoggerFactory.getLogger(VppReadOnlyTransaction.class); + private volatile ReadableVppDataTree operationalData; + private volatile VppDataTreeSnapshot configSnapshot; + + VppReadOnlyTransaction(@Nonnull final ReadableVppDataTree operationalData, + @Nonnull final VppDataTreeSnapshot configSnapshot) { + this.operationalData = Preconditions.checkNotNull(operationalData, "operationalData should not be null"); + this.configSnapshot = Preconditions.checkNotNull(configSnapshot, "config should not be null"); + } + + @Override + public void close() { + configSnapshot = null; + operationalData = null; + } + + @Override + public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read( + final LogicalDatastoreType store, + final YangInstanceIdentifier path) { + LOG.debug("VppReadOnlyTransaction.read(), store={}, path={}", store, path); + + Preconditions.checkState(configSnapshot != null, "Transaction was closed"); + + if (store == LogicalDatastoreType.OPERATIONAL) { + return operationalData.read(path); + } else { + return configSnapshot.read(path); + } + } + + @Override + public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, + final YangInstanceIdentifier path) { + LOG.debug("VppReadOnlyTransaction.exists() store={}, path={}", store, path); + + ListenableFuture<Boolean> listenableFuture = Futures.transform(read(store, path), IS_NODE_PRESENT); + + return Futures.makeChecked(listenableFuture, ANY_EX_TO_READ_FAILED_EXCEPTION_MAPPER); + } + + @Nonnull + @Override + public Object getIdentifier() { + return this; + } + + + private static final Function<? super Optional<NormalizedNode<?, ?>>, ? extends Boolean> IS_NODE_PRESENT = + new Function<Optional<NormalizedNode<?, ?>>, Boolean>() { + @Nullable + @Override + public Boolean apply(@Nullable final Optional<NormalizedNode<?, ?>> input) { + return input == null + ? Boolean.FALSE + : input.isPresent(); + } + }; + + private static final Function<? super Exception, ReadFailedException> ANY_EX_TO_READ_FAILED_EXCEPTION_MAPPER = + new Function<Exception, ReadFailedException>() { + @Override + public ReadFailedException apply(@Nullable final Exception e) { + return new ReadFailedException("Exists failed", e); + } + }; +}
\ No newline at end of file diff --git a/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/VppWriteTransaction.java b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/VppWriteTransaction.java new file mode 100644 index 000000000..b7aa2f854 --- /dev/null +++ b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/data/VppWriteTransaction.java @@ -0,0 +1,145 @@ +/* + * 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.impl.data; + +import static org.opendaylight.controller.md.sal.common.api.TransactionStatus.CANCELED; +import static org.opendaylight.controller.md.sal.common.api.TransactionStatus.COMMITED; +import static org.opendaylight.controller.md.sal.common.api.TransactionStatus.FAILED; +import static org.opendaylight.controller.md.sal.common.api.TransactionStatus.NEW; +import static org.opendaylight.controller.md.sal.common.api.TransactionStatus.SUBMITED; + +import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import io.fd.honeycomb.v3po.impl.trans.VppApiInvocationException; +import javax.annotation.Nonnull; +import javax.annotation.concurrent.NotThreadSafe; +import org.opendaylight.controller.md.sal.common.api.TransactionStatus; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@NotThreadSafe +final class VppWriteTransaction implements DOMDataWriteTransaction { + + private static final Logger LOG = LoggerFactory.getLogger(VppWriteTransaction.class); + private final VppDataTree configDataTree; + private DataTreeModification modification; + private TransactionStatus status; + + VppWriteTransaction(@Nonnull final VppDataTree configDataTree, + @Nonnull final VppDataTreeSnapshot configSnapshot) { + this.configDataTree = Preconditions.checkNotNull(configDataTree, "configDataTree should not be null"); + Preconditions.checkNotNull(configSnapshot, "configSnapshot should not be null"); + // initialize transaction state: + modification = configSnapshot.newModification(); + status = NEW; + } + + VppWriteTransaction(@Nonnull final VppDataTree configDataTree) { + this(configDataTree, configDataTree.takeSnapshot()); + } + + private static void checkConfigurationWrite(final LogicalDatastoreType store) { + Preconditions.checkArgument(LogicalDatastoreType.CONFIGURATION == store, "Write is not supported for operational data store"); + } + + private void checkIsNew() { + Preconditions.checkState(status == NEW, "Transaction was submitted or canceled"); + Preconditions.checkState(modification != null, "VPPDataTree modification should not be null"); + } + + @Override + public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, + final NormalizedNode<?, ?> data) { + LOG.debug("VppWriteTransaction.put() store={}, path={}, data={}", store, path, data); + checkIsNew(); + checkConfigurationWrite(store); + modification.write(path, data); + } + + @Override + public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, + final NormalizedNode<?, ?> data) { + LOG.debug("VppWriteTransaction.merge() store={}, path={}, data={}", store, path, data); + checkIsNew(); + checkConfigurationWrite(store); + modification.merge(path, data); + } + + @Override + public boolean cancel() { + if (status != NEW) { + // only NEW transactions can be cancelled + return false; + } else { + status = CANCELED; + modification = null; + return true; + } + } + + @Override + public void delete(LogicalDatastoreType store, final YangInstanceIdentifier path) { + LOG.debug("VppWriteTransaction.delete() store={}, path={}", store, path); + checkIsNew(); + checkConfigurationWrite(store); + modification.delete(path); + } + + @Override + public CheckedFuture<Void, TransactionCommitFailedException> submit() { + LOG.debug("VppWriteTransaction.submit()"); + checkIsNew(); + + // seal transaction: + modification.ready(); + status = SUBMITED; + + try { + configDataTree.commit(modification); + status = COMMITED; + } catch (DataValidationFailedException | VppApiInvocationException e) { + status = FAILED; + LOG.error("Failed to commit VPP state modification", e); + return Futures.immediateFailedCheckedFuture( + new TransactionCommitFailedException("Failed to validate DataTreeModification", e)); + } finally { + modification = null; + } + return Futures.immediateCheckedFuture(null); + } + + @Override + @Deprecated + public ListenableFuture<RpcResult<TransactionStatus>> commit() { + throw new UnsupportedOperationException("deprecated"); + } + + @Override + public Object getIdentifier() { + return this; + } +} |