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;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
28 * org.opendaylight.openflowplugin.impl.device
30 * Package protected class for controlling {@link WriteTransaction} life cycle. It is
31 * a {@link TransactionChainListener} and provide package protected methods for writeToTransaction
32 * method (wrapped {@link WriteTransaction#put(LogicalDatastoreType, InstanceIdentifier, DataObject)})
33 * and submitTransaction method (wrapped {@link WriteTransaction#submit()})
35 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
37 * Created: Apr 2, 2015
40 class TransactionChainManager implements TransactionChainListener {
42 private static Logger LOG = LoggerFactory.getLogger(TransactionChainManager.class);
44 private final DataBroker dataBroker;
45 private final long maxTx;
46 private BindingTransactionChain txChainFactory;
47 private WriteTransaction wTx;
48 private long nrOfActualTx;
49 private boolean counterIsEnabled;
51 TransactionChainManager(@Nonnull final DataBroker dataBroker, final long maxTx) {
52 this.dataBroker = Preconditions.checkNotNull(dataBroker);
54 txChainFactory = dataBroker.createTransactionChain(TransactionChainManager.this);
56 LOG.debug("created txChainManager with operation limit {}", maxTx);
59 synchronized <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store,
60 final InstanceIdentifier<T> path, final T data) {
62 wTx = txChainFactory.newWriteOnlyTransaction();
64 wTx.put(store, path, data);
65 if ( ! counterIsEnabled) {
69 if (nrOfActualTx == maxTx) {
74 synchronized <T extends DataObject> void addDeleteOperationTotTxChain(final LogicalDatastoreType store,
75 final InstanceIdentifier<T> path) {
77 wTx = txChainFactory.newWriteOnlyTransaction();
79 wTx.delete(store, path);
80 if ( ! counterIsEnabled) {
84 if (nrOfActualTx == maxTx) {
89 synchronized void submitTransaction() {
91 LOG.trace("submitting transaction, counter: {}", nrOfActualTx);
98 synchronized void enableCounter() {
99 counterIsEnabled = true;
103 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
104 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
105 LOG.debug("txChain failed -> recreating");
106 LOG.trace("reason", cause);
107 txChainFactory.close();
108 txChainFactory = dataBroker.createTransactionChain(TransactionChainManager.this);
112 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
113 // NOOP - only yet, here is probably place for notification to get new WriteTransaction