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;
18 import java.util.Objects;
19 import java.util.concurrent.CancellationException;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import javax.annotation.concurrent.GuardedBy;
23 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
33 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
45 * org.opendaylight.openflowplugin.impl.device
47 * Package protected class for controlling {@link WriteTransaction} life cycle. It is
48 * a {@link TransactionChainListener} and provide package protected methods for writeToTransaction
49 * method (wrapped {@link WriteTransaction#put(LogicalDatastoreType, InstanceIdentifier, DataObject)})
50 * and submitTransaction method (wrapped {@link WriteTransaction#submit()})
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 LifecycleService lifecycleService;
62 private WriteTransaction wTx;
64 private BindingTransactionChain txChainFactory;
66 private boolean submitIsEnabled;
68 private ListenableFuture<Void> lastSubmittedFuture;
70 private boolean initCommit;
73 private TransactionChainManagerStatus transactionChainManagerStatus = TransactionChainManagerStatus.SLEEPING;
75 TransactionChainManager(@Nonnull final DataBroker dataBroker,
76 @Nonnull final DeviceInfo deviceInfo) {
77 this.dataBroker = Preconditions.checkNotNull(dataBroker);
78 this.nodeII = deviceInfo.getNodeInstanceIdentifier();
79 this.transactionChainManagerStatus = TransactionChainManagerStatus.SLEEPING;
80 lastSubmittedFuture = Futures.immediateFuture(null);
81 LOG.debug("created txChainManager for {}", this.nodeII);
84 private NodeId nodeId() {
85 return nodeII.getKey().getId();
89 private void createTxChain() {
90 if (txChainFactory != null) {
91 txChainFactory.close();
93 txChainFactory = dataBroker.createTransactionChain(TransactionChainManager.this);
96 public void setLifecycleService(final LifecycleService lifecycleService) {
97 this.lifecycleService = lifecycleService;
100 void initialSubmitWriteTransaction() {
102 submitWriteTransaction();
106 * Method change status for TxChainManager to {@link TransactionChainManagerStatus#WORKING} and it has to make
107 * registration for this class instance as {@link TransactionChainListener} to provide possibility a make DS
108 * transactions. Call this method for MASTER role only.
110 void activateTransactionManager() {
111 LOG.trace("activateTransactionManager for node {} transaction submit is set to {}", nodeId(), submitIsEnabled);
112 synchronized (txLock) {
113 if (TransactionChainManagerStatus.SLEEPING.equals(transactionChainManagerStatus)) {
114 LOG.debug("Transaction Factory create {}", nodeId());
115 Preconditions.checkState(txChainFactory == null, "TxChainFactory survive last close.");
116 Preconditions.checkState(wTx == null, "We have some unexpected WriteTransaction.");
117 this.transactionChainManagerStatus = TransactionChainManagerStatus.WORKING;
118 this.submitIsEnabled = false;
119 this.initCommit = true;
122 LOG.debug("Transaction is active {}", nodeId());
128 * Method change status for TxChainManger to {@link TransactionChainManagerStatus#SLEEPING} and it unregisters
129 * this class instance as {@link TransactionChainListener} so it broke a possibility to write something to DS.
130 * Call this method for SLAVE only.
133 ListenableFuture<Void> deactivateTransactionManager() {
134 final ListenableFuture<Void> future;
135 synchronized (txLock) {
136 if (TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus)) {
137 LOG.debug("Submitting all transactions if we were in status WORKING for Node {}", nodeId());
138 transactionChainManagerStatus = TransactionChainManagerStatus.SLEEPING;
139 future = txChainShuttingDown();
140 Preconditions.checkState(wTx == null, "We have some unexpected WriteTransaction.");
141 LOG.debug("Transaction Factory deactivate for Node {}", nodeId());
142 Futures.addCallback(future, new FutureCallback<Void>() {
144 public void onSuccess(final Void result) {
145 txChainFactory.close();
146 txChainFactory = null;
150 public void onFailure(final Throwable t) {
151 txChainFactory.close();
152 txChainFactory = null;
156 // TODO : ignoring redundant deactivate invocation
157 future = Futures.immediateCheckedFuture(null);
163 boolean submitWriteTransaction() {
164 synchronized (txLock) {
165 if (!submitIsEnabled) {
166 LOG.trace("transaction not committed - submit block issued");
170 LOG.trace("nothing to commit - submit returns true");
173 Preconditions.checkState(TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus),
174 "we have here Uncompleted Transaction for node {} and we are not MASTER", nodeII);
175 final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = wTx.submit();
176 Futures.addCallback(submitFuture, new FutureCallback<Void>() {
178 public void onSuccess(final Void result) {
185 public void onFailure(final Throwable t) {
186 if (t instanceof TransactionCommitFailedException) {
187 LOG.error("Transaction commit failed. ", t);
189 if (t instanceof CancellationException) {
190 LOG.warn("Submit task was canceled");
191 LOG.trace("Submit exception: ", t);
193 LOG.error("Exception during transaction submitting. ", t);
197 LOG.warn("Initial commit failed. ", t);
199 if (Objects.nonNull(lifecycleService)) {
200 lifecycleService.closeConnection();
205 lastSubmittedFuture = submitFuture;
211 <T extends DataObject> void addDeleteOperationTotTxChain(final LogicalDatastoreType store,
212 final InstanceIdentifier<T> path){
213 final WriteTransaction writeTx = getTransactionSafely();
214 if (writeTx != null) {
215 LOG.trace("addDeleteOperation called with path {} ", path);
216 writeTx.delete(store, path);
218 LOG.debug("WriteTx is null for node {}. Delete {} was not realized.", nodeII, path);
219 throw new TransactionChainClosedException("Cannot write into transaction.");
223 <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store,
224 final InstanceIdentifier<T> path,
226 final boolean createParents){
227 final WriteTransaction writeTx = getTransactionSafely();
228 if (writeTx != null) {
229 LOG.trace("writeToTransaction called with path {} ", path);
230 writeTx.put(store, path, data, createParents);
232 LOG.debug("WriteTx is null for node {}. Write data for {} was not realized.", nodeII, path);
233 throw new TransactionChainClosedException("Cannot write into transaction.");
238 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
239 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
240 if (transactionChainManagerStatus.equals(TransactionChainManagerStatus.WORKING)) {
241 LOG.warn("txChain failed -> recreating due to {}", cause);
247 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
251 private void recreateTxChain() {
252 synchronized (txLock) {
259 private WriteTransaction getTransactionSafely() {
260 synchronized (txLock) {
261 if (wTx == null && TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus)) {
262 if (wTx == null && txChainFactory != null) {
263 wTx = txChainFactory.newWriteOnlyTransaction();
271 void enableSubmit() {
272 synchronized (txLock) {
273 /* !!!IMPORTANT: never set true without txChainFactory */
274 submitIsEnabled = txChainFactory != null;
278 ListenableFuture<Void> shuttingDown() {
279 LOG.debug("TxManager is going SHUTTING_DOWN for node {}", nodeII.getKey().getId().getValue());
280 ListenableFuture<Void> future;
281 synchronized (txLock) {
282 this.transactionChainManagerStatus = TransactionChainManagerStatus.SHUTTING_DOWN;
283 future = txChainShuttingDown();
289 private ListenableFuture<Void> txChainShuttingDown() {
290 submitIsEnabled = false;
291 ListenableFuture<Void> future;
292 if (txChainFactory == null) {
293 // stay with actual thread
294 future = Futures.immediateCheckedFuture(null);
295 } else if (wTx == null) {
296 // hijack md-sal thread
297 future = lastSubmittedFuture;
299 // hijack md-sal thread
300 future = wTx.submit();
307 public void close() {
308 LOG.debug("Setting transactionChainManagerStatus to SHUTTING_DOWN for {}, will wait for ownershipservice to notify"
310 Preconditions.checkState(TransactionChainManagerStatus.SHUTTING_DOWN.equals(transactionChainManagerStatus));
311 Preconditions.checkState(wTx == null);
312 synchronized (txLock) {
313 if (txChainFactory != null) {
314 txChainFactory.close();
315 txChainFactory = null;
318 Preconditions.checkState(txChainFactory == null);
321 private enum TransactionChainManagerStatus {
322 /** txChainManager is sleeping - is not active (SLAVE or default init value) */
324 /** txChainManager is working - is active (MASTER) */
326 /** txChainManager is trying to be closed - device disconnecting */