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 com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import javax.annotation.concurrent.GuardedBy;
20 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
27 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
29 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
41 * org.opendaylight.openflowplugin.impl.device
43 * Package protected class for controlling {@link WriteTransaction} life cycle. It is
44 * a {@link TransactionChainListener} and provide package protected methods for writeToTransaction
45 * method (wrapped {@link WriteTransaction#put(LogicalDatastoreType, InstanceIdentifier, DataObject)})
46 * and submitTransaction method (wrapped {@link WriteTransaction#submit()})
48 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
50 * Created: Apr 2, 2015
52 class TransactionChainManager implements TransactionChainListener, AutoCloseable {
54 private static final Logger LOG = LoggerFactory.getLogger(TransactionChainManager.class);
56 private final Object txLock = new Object();
57 private final KeyedInstanceIdentifier<Node, NodeKey> nodeII;
58 private final DataBroker dataBroker;
59 private final LifecycleConductor conductor;
62 private WriteTransaction wTx;
64 private BindingTransactionChain txChainFactory;
66 private boolean submitIsEnabled;
68 private ListenableFuture<Void> lastSubmittedFuture;
70 private boolean initCommit;
72 public TransactionChainManagerStatus getTransactionChainManagerStatus() {
73 return transactionChainManagerStatus;
77 private TransactionChainManagerStatus transactionChainManagerStatus = TransactionChainManagerStatus.SLEEPING;
79 TransactionChainManager(@Nonnull final DataBroker dataBroker,
80 @Nonnull final DeviceState deviceState,
81 @Nonnull final LifecycleConductor conductor) {
82 this.dataBroker = Preconditions.checkNotNull(dataBroker);
83 this.conductor = Preconditions.checkNotNull(conductor);
84 this.nodeII = Preconditions.checkNotNull(deviceState.getNodeInstanceIdentifier());
85 this.transactionChainManagerStatus = TransactionChainManagerStatus.SLEEPING;
86 lastSubmittedFuture = Futures.immediateFuture(null);
87 LOG.debug("created txChainManager for {}", nodeII);
90 private NodeId nodeId() {
91 return nodeII.getKey().getId();
95 private void createTxChain() {
96 if (txChainFactory != null) {
97 txChainFactory.close();
99 txChainFactory = dataBroker.createTransactionChain(TransactionChainManager.this);
102 void initialSubmitWriteTransaction() {
104 submitWriteTransaction();
108 * Method change status for TxChainManager to {@link TransactionChainManagerStatus#WORKING} and it has to make
109 * registration for this class instance as {@link TransactionChainListener} to provide possibility a make DS
110 * transactions. Call this method for MASTER role only.
112 public void activateTransactionManager() {
113 LOG.trace("activateTransactionManager for node {} transaction submit is set to {}", nodeId(), submitIsEnabled);
114 synchronized (txLock) {
115 if (TransactionChainManagerStatus.SLEEPING.equals(transactionChainManagerStatus)) {
116 LOG.debug("Transaction Factory create {}", nodeId());
117 Preconditions.checkState(txChainFactory == null, "TxChainFactory survive last close.");
118 Preconditions.checkState(wTx == null, "We have some unexpected WriteTransaction.");
119 this.transactionChainManagerStatus = TransactionChainManagerStatus.WORKING;
120 this.submitIsEnabled = false;
121 this.initCommit = true;
124 LOG.debug("Transaction is active {}", nodeId());
130 * Method change status for TxChainManger to {@link TransactionChainManagerStatus#SLEEPING} and it unregisters
131 * this class instance as {@link TransactionChainListener} so it broke a possibility to write something to DS.
132 * Call this method for SLAVE only.
135 public ListenableFuture<Void> deactivateTransactionManager() {
136 final ListenableFuture<Void> future;
137 synchronized (txLock) {
138 if (TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus)) {
139 LOG.debug("Submitting all transactions if we were in status WORKING for Node {}", nodeId());
140 transactionChainManagerStatus = TransactionChainManagerStatus.SLEEPING;
141 future = txChainShuttingDown();
142 Preconditions.checkState(wTx == null, "We have some unexpected WriteTransaction.");
143 LOG.debug("Transaction Factory deactivate for Node {}", nodeId());
144 Futures.addCallback(future, new FutureCallback<Void>() {
146 public void onSuccess(final Void result) {
147 txChainFactory.close();
148 txChainFactory = null;
152 public void onFailure(final Throwable t) {
153 txChainFactory.close();
154 txChainFactory = null;
158 // TODO : ignoring redundant deactivate invocation
159 future = Futures.immediateCheckedFuture(null);
165 boolean submitWriteTransaction() {
166 synchronized (txLock) {
167 if (!submitIsEnabled) {
168 LOG.trace("transaction not committed - submit block issued");
172 LOG.trace("nothing to commit - submit returns true");
175 Preconditions.checkState(TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus),
176 "we have here Uncompleted Transaction for node {} and we are not MASTER", nodeII);
177 final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = wTx.submit();
178 Futures.addCallback(submitFuture, new FutureCallback<Void>() {
180 public void onSuccess(final Void result) {
187 public void onFailure(final Throwable t) {
188 if (t instanceof TransactionCommitFailedException) {
189 LOG.error("Transaction commit failed. {}", t);
191 LOG.error("Exception during transaction submitting. {}", t);
194 LOG.error("Initial commit failed. {}", t);
195 conductor.closeConnection(nodeId());
199 lastSubmittedFuture = submitFuture;
205 <T extends DataObject> void addDeleteOperationTotTxChain(final LogicalDatastoreType store,
206 final InstanceIdentifier<T> path) throws Exception {
207 final WriteTransaction writeTx = getTransactionSafely();
208 if (writeTx != null) {
209 LOG.trace("addDeleteOperation called with path {} ", path);
210 writeTx.delete(store, path);
212 LOG.debug("WriteTx is null for node {}. Delete {} was not realized.", nodeII, path);
213 throw new Exception("Cannot write into transaction.");
217 <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store,
218 final InstanceIdentifier<T> path, final T data) throws Exception {
219 final WriteTransaction writeTx = getTransactionSafely();
220 if (writeTx != null) {
221 LOG.trace("writeToTransaction called with path {} ", path);
222 writeTx.put(store, path, data);
224 LOG.debug("WriteTx is null for node {}. Write data for {} was not realized.", nodeII, path);
225 throw new Exception("Cannot write into transaction.");
230 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
231 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
232 if (transactionChainManagerStatus.equals(TransactionChainManagerStatus.WORKING)) {
233 LOG.warn("txChain failed -> recreating due to {}", cause);
239 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
243 private void recreateTxChain() {
244 synchronized (txLock) {
251 private WriteTransaction getTransactionSafely() {
252 if (wTx == null && TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus)) {
253 synchronized (txLock) {
254 if (wTx == null && TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus)) {
255 if (wTx == null && txChainFactory != null) {
256 wTx = txChainFactory.newWriteOnlyTransaction();
265 void enableSubmit() {
266 synchronized (txLock) {
267 /* !!!IMPORTANT: never set true without txChainFactory */
268 submitIsEnabled = txChainFactory != null;
272 ListenableFuture<Void> shuttingDown() {
273 LOG.debug("TxManager is going SHUTTING_DOWN for node {}", nodeII);
274 ListenableFuture<Void> future;
275 synchronized (txLock) {
276 this.transactionChainManagerStatus = TransactionChainManagerStatus.SHUTTING_DOWN;
277 future = txChainShuttingDown();
283 private ListenableFuture<Void> txChainShuttingDown() {
284 submitIsEnabled = false;
285 ListenableFuture<Void> future;
286 if (txChainFactory == null) {
287 // stay with actual thread
288 future = Futures.immediateCheckedFuture(null);
289 } else if (wTx == null) {
290 // hijack md-sal thread
291 future = lastSubmittedFuture;
293 // hijack md-sal thread
294 future = wTx.submit();
301 public void close() {
302 LOG.debug("Setting transactionChainManagerStatus to SHUTTING_DOWN for {}, will wait for ownershipservice to notify"
304 Preconditions.checkState(TransactionChainManagerStatus.SHUTTING_DOWN.equals(transactionChainManagerStatus));
305 Preconditions.checkState(wTx == null);
306 synchronized (txLock) {
307 if (txChainFactory != null) {
308 txChainFactory.close();
309 txChainFactory = null;
312 Preconditions.checkState(txChainFactory == null);
315 private enum TransactionChainManagerStatus {
316 /** txChainManager is sleeping - is not active (SLAVE or default init value) */
318 /** txChainManager is working - is active (MASTER) */
320 /** txChainManager is trying to be closed - device disconnecting */