write node to DS when connected
[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
24 /**
25  * openflowplugin-impl
26  * org.opendaylight.openflowplugin.impl.device
27  *
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()})
32  *
33  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
34  *
35  * Created: Apr 2, 2015
36  */
37 @VisibleForTesting
38 class TransactionChainManager implements TransactionChainListener {
39
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;
46
47     TransactionChainManager(@Nonnull final DataBroker dataBroker, final long maxTx) {
48         this.dataBroker = Preconditions.checkNotNull(dataBroker);
49         this.maxTx = maxTx;
50         txChainFactory = dataBroker.createTransactionChain(TransactionChainManager.this);
51         nrOfActualTx = 0L;
52     }
53
54     synchronized <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store,
55             final InstanceIdentifier<T> path, final T data) {
56         if (wTx == null) {
57             wTx = txChainFactory.newWriteOnlyTransaction();
58         }
59         wTx.put(store, path, data, true);
60         if ( ! counterIsEnabled) {
61             return;
62         }
63         nrOfActualTx += 1L;
64         if (nrOfActualTx == maxTx) {
65             submitTransaction();
66         }
67     }
68
69     synchronized void submitTransaction() {
70         if (wTx != null) {
71             wTx.submit();
72             wTx = null;
73             nrOfActualTx = 0L;
74         }
75     }
76
77     synchronized void enableCounter() {
78         counterIsEnabled = true;
79     }
80
81     @Override
82     public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
83             final AsyncTransaction<?, ?> transaction, final Throwable cause) {
84         txChainFactory.close();
85         txChainFactory = dataBroker.createTransactionChain(TransactionChainManager.this);
86     }
87
88     @Override
89     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
90         // NOOP - only yet, here is probably place for notification to get new WriteTransaction
91     }
92 }