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;
14 import java.util.concurrent.BlockingQueue;
15 import java.util.concurrent.LinkedBlockingQueue;
16 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 final class OperationProcessor implements AutoCloseable, Runnable, TransactionChainListener {
26 private static final Logger LOG = LoggerFactory.getLogger(OperationProcessor.class);
27 private static final int MAX_TRANSACTION_OPERATIONS = 100;
28 private static final int OPERATION_QUEUE_DEPTH = 500;
30 private final BlockingQueue<TopologyOperation> queue = new LinkedBlockingQueue<>(OPERATION_QUEUE_DEPTH);
31 private final DataBroker dataBroker;
32 private final BindingTransactionChain transactionChain;
34 OperationProcessor(final DataBroker dataBroker) {
35 this.dataBroker = Preconditions.checkNotNull(dataBroker);
36 transactionChain = this.dataBroker.createTransactionChain(this);
39 void enqueueOperation(final TopologyOperation task) {
42 } catch (InterruptedException e) {
43 LOG.warn("Interrupted while submitting task {}", task, e);
51 TopologyOperation op = queue.take();
53 LOG.debug("New operations available, starting transaction");
54 final ReadWriteTransaction tx = transactionChain.newReadWriteTransaction();
59 op.applyOperation(tx);
62 if (ops < MAX_TRANSACTION_OPERATIONS) {
69 LOG.debug("Processed {} operations, submitting transaction", ops);
71 final CheckedFuture txResultFuture = tx.submit();
72 Futures.addCallback(txResultFuture, new FutureCallback() {
74 public void onSuccess(Object o) {
75 LOG.debug("Topology export successful for tx :{}", tx.getIdentifier());
79 public void onFailure(Throwable throwable) {
80 LOG.error("Topology export transaction {} failed", tx.getIdentifier(), throwable.getCause());
84 } catch (InterruptedException e) {
85 LOG.info("Interrupted processing, terminating", e);
88 // Drain all events, making sure any blocked threads are unblocked
89 while (!queue.isEmpty()) {
95 public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
96 LOG.error("Failed to export Topology manager operations, Transaction {} failed.", transaction.getIdentifier(), cause);
100 public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
105 public void close() throws Exception {
106 if (transactionChain != null) {
107 transactionChain.close();