2 * Copyright (c) 2014 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
8 package org.opendaylight.md.controller.topology.manager;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
15 import java.util.concurrent.BlockingQueue;
16 import java.util.concurrent.LinkedBlockingQueue;
18 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 final class OperationProcessor implements AutoCloseable, Runnable, TransactionChainListener {
29 private static final Logger LOG = LoggerFactory.getLogger(OperationProcessor.class);
30 private static final int MAX_TRANSACTION_OPERATIONS = 100;
31 private static final int OPERATION_QUEUE_DEPTH = 500;
33 private final BlockingQueue<TopologyOperation> queue = new LinkedBlockingQueue<>(OPERATION_QUEUE_DEPTH);
34 private final DataBroker dataBroker;
35 private final BindingTransactionChain transactionChain;
37 OperationProcessor(final DataBroker dataBroker) {
38 this.dataBroker = Preconditions.checkNotNull(dataBroker);
39 transactionChain = this.dataBroker.createTransactionChain(this);
42 void enqueueOperation(final TopologyOperation task) {
45 } catch (InterruptedException e) {
46 LOG.warn("Interrupted while submitting task {}", task, e);
54 TopologyOperation op = queue.take();
56 LOG.debug("New {} operation available, starting transaction", op);
58 final ReadWriteTransaction tx = transactionChain.newReadWriteTransaction();
62 op.applyOperation(tx);
65 if (ops < MAX_TRANSACTION_OPERATIONS) {
71 LOG.debug("Next operation {}", op);
74 LOG.debug("Processed {} operations, submitting transaction", ops);
76 CheckedFuture<Void, TransactionCommitFailedException> txResultFuture = tx.submit();
77 Futures.addCallback(txResultFuture, new FutureCallback<Void>() {
79 public void onSuccess(Void notUsed) {
80 LOG.debug("Topology export successful for tx :{}", tx.getIdentifier());
84 public void onFailure(Throwable throwable) {
85 LOG.error("Topology export transaction {} failed", tx.getIdentifier(), throwable.getCause());
89 } catch (InterruptedException e) {
90 LOG.info("Interrupted processing, terminating", e);
93 // Drain all events, making sure any blocked threads are unblocked
94 while (!queue.isEmpty()) {
100 public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
101 LOG.error("Failed to export Topology manager operations, Transaction {} failed.", transaction.getIdentifier(), cause);
105 public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
110 public void close() throws Exception {
111 if (transactionChain != null) {
112 transactionChain.close();