DeviceContext exposes delete operation for DS
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / TransactionChainManager.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.device;
10
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;
25
26 /**
27  * openflowplugin-impl
28  * org.opendaylight.openflowplugin.impl.device
29  *
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()})
34  *
35  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
36  *
37  * Created: Apr 2, 2015
38  */
39 @VisibleForTesting
40 class TransactionChainManager implements TransactionChainListener {
41
42     private static Logger LOG = LoggerFactory.getLogger(TransactionChainManager.class);
43
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;
50
51     TransactionChainManager(@Nonnull final DataBroker dataBroker, final long maxTx) {
52         this.dataBroker = Preconditions.checkNotNull(dataBroker);
53         this.maxTx = maxTx;
54         txChainFactory = dataBroker.createTransactionChain(TransactionChainManager.this);
55         nrOfActualTx = 0L;
56         LOG.debug("created txChainManager with operation limit {}", maxTx);
57     }
58
59     synchronized <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store,
60             final InstanceIdentifier<T> path, final T data) {
61         if (wTx == null) {
62             wTx = txChainFactory.newWriteOnlyTransaction();
63         }
64         wTx.put(store, path, data);
65         if ( ! counterIsEnabled) {
66             return;
67         }
68         nrOfActualTx += 1L;
69         if (nrOfActualTx == maxTx) {
70             submitTransaction();
71         }
72     }
73
74     synchronized <T extends DataObject> void addDeleteOperationTotTxChain(final LogicalDatastoreType store,
75                                                                 final InstanceIdentifier<T> path) {
76         if (wTx == null) {
77             wTx = txChainFactory.newWriteOnlyTransaction();
78         }
79         wTx.delete(store, path);
80         if ( ! counterIsEnabled) {
81             return;
82         }
83         nrOfActualTx += 1L;
84         if (nrOfActualTx == maxTx) {
85             submitTransaction();
86         }
87     }
88
89     synchronized void submitTransaction() {
90         if (wTx != null) {
91             LOG.trace("submitting transaction, counter: {}", nrOfActualTx);
92             wTx.submit();
93             wTx = null;
94             nrOfActualTx = 0L;
95         }
96     }
97
98     synchronized void enableCounter() {
99         counterIsEnabled = true;
100     }
101
102     @Override
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);
109     }
110
111     @Override
112     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
113         // NOOP - only yet, here is probably place for notification to get new WriteTransaction
114     }
115 }