/* * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.md.sal.binding.impl; import com.google.common.base.Preconditions; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction; import org.opendaylight.controller.md.sal.common.api.data.TransactionChain; import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener; import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; 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.Delegator; class BindingTranslatedTransactionChain implements BindingTransactionChain, Delegator { private final DOMTransactionChain delegate; private final BindingToNormalizedNodeCodec codec; private final DelegateChainListener delegatingListener; private final TransactionChainListener listener; public BindingTranslatedTransactionChain(final DOMDataBroker chainFactory, final BindingToNormalizedNodeCodec codec, final TransactionChainListener listener) { Preconditions.checkNotNull(chainFactory, "DOM Transaction chain factory must not be null"); this.delegatingListener = new DelegateChainListener(); this.listener = listener; this.delegate = chainFactory.createTransactionChain(listener); this.codec = codec; } @Override public DOMTransactionChain getDelegate() { return delegate; } @Override public ReadOnlyTransaction newReadOnlyTransaction() { DOMDataReadOnlyTransaction delegateTx = delegate.newReadOnlyTransaction(); ReadOnlyTransaction bindingTx = new BindingDataReadTransactionImpl(delegateTx, codec); return bindingTx; } @Override public ReadWriteTransaction newReadWriteTransaction() { DOMDataReadWriteTransaction delegateTx = delegate.newReadWriteTransaction(); ReadWriteTransaction bindingTx = new BindingDataReadWriteTransactionImpl(delegateTx, codec) { @Override public CheckedFuture submit() { return listenForFailure(this,super.submit()); } }; return bindingTx; } @Override public WriteTransaction newWriteOnlyTransaction() { final DOMDataWriteTransaction delegateTx = delegate.newWriteOnlyTransaction(); WriteTransaction bindingTx = new BindingDataWriteTransactionImpl(delegateTx, codec) { @Override public CheckedFuture submit() { return listenForFailure(this,super.submit()); }; }; return bindingTx; } protected CheckedFuture listenForFailure( final WriteTransaction tx, CheckedFuture future) { Futures.addCallback(future, new FutureCallback() { @Override public void onFailure(Throwable t) { failTransactionChain(tx,t); } @Override public void onSuccess(Void result) { // Intentionally NOOP } }); return future; } protected void failTransactionChain(WriteTransaction tx, Throwable t) { // We asume correct state change for underlaying transaction // chain, so we are not changing any of our internal state // to mark that we failed. this.delegatingListener.onTransactionChainFailed(this, tx, t); } @Override public void close() { delegate.close(); } private final class DelegateChainListener implements TransactionChainListener { @Override public void onTransactionChainFailed(final TransactionChain chain, final AsyncTransaction transaction, final Throwable cause) { /* * Intentionally NOOP, callback for failure, since we * are also listening on each transaction for failure. * * by listening on submit future for Binding transaction * in order to provide Binding transaction (which was seen by client * of this transaction chain, instead of */ } @Override public void onTransactionChainSuccessful(final TransactionChain chain) { Preconditions.checkState(delegate.equals(chain), "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain); listener.onTransactionChainSuccessful(BindingTranslatedTransactionChain.this); } } }