2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.device;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 * org.opendaylight.openflowplugin.impl.device
28 * Package protected class for controlling {@link WriteTransaction} life cycle. It is
29 * a {@link TransactionChainListener} and provide package protected methods for writeToTransaction
30 * method (wrapped {@link WriteTransaction#put(LogicalDatastoreType, InstanceIdentifier, DataObject)})
31 * and submitTransaction method (wrapped {@link WriteTransaction#submit()})
33 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
35 * Created: Apr 2, 2015
38 class TransactionChainManager implements TransactionChainListener {
40 private final DataBroker dataBroker;
41 private final long maxTx;
42 private BindingTransactionChain txChainFactory;
43 private WriteTransaction wTx;
44 private long nrOfActualTx;
45 private boolean counterIsEnabled;
47 TransactionChainManager(@Nonnull final DataBroker dataBroker, final long maxTx) {
48 this.dataBroker = Preconditions.checkNotNull(dataBroker);
50 txChainFactory = dataBroker.createTransactionChain(TransactionChainManager.this);
54 synchronized <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store,
55 final InstanceIdentifier<T> path, final T data) {
57 wTx = txChainFactory.newWriteOnlyTransaction();
59 wTx.put(store, path, data, true);
60 if ( ! counterIsEnabled) {
64 if (nrOfActualTx == maxTx) {
69 synchronized void submitTransaction() {
77 synchronized void enableCounter() {
78 counterIsEnabled = true;
82 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
83 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
84 txChainFactory.close();
85 txChainFactory = dataBroker.createTransactionChain(TransactionChainManager.this);
89 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
90 // NOOP - only yet, here is probably place for notification to get new WriteTransaction