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.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 final class OperationProcessor implements Runnable {
22 private static final Logger LOG = LoggerFactory.getLogger(OperationProcessor.class);
23 private static final int MAX_TRANSACTION_OPERATIONS = 100;
24 private static final int OPERATION_QUEUE_DEPTH = 500;
26 private final BlockingQueue<TopologyOperation> queue = new LinkedBlockingQueue<>(OPERATION_QUEUE_DEPTH);
27 private final DataBroker dataBroker;
29 OperationProcessor(final DataBroker dataBroker) {
30 this.dataBroker = Preconditions.checkNotNull(dataBroker);
33 void enqueueOperation(final TopologyOperation task) {
36 } catch (InterruptedException e) {
37 LOG.warn("Interrupted while submitting task {}", task, e);
45 TopologyOperation op = queue.take();
47 LOG.debug("New operations available, starting transaction");
48 final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
52 op.applyOperation(tx);
55 if (ops < MAX_TRANSACTION_OPERATIONS) {
62 LOG.debug("Processed {} operations, submitting transaction", ops);
64 final CheckedFuture txResultFuture = tx.submit();
65 Futures.addCallback(txResultFuture, new FutureCallback() {
67 public void onSuccess(Object o) {
68 LOG.debug("Topology export successful for tx :{}", tx.getIdentifier());
72 public void onFailure(Throwable throwable) {
73 LOG.error("Topology export transaction {} failed", tx.getIdentifier(), throwable.getCause());
77 } catch (InterruptedException e) {
78 LOG.info("Interrupted processing, terminating", e);
81 // Drain all events, making sure any blocked threads are unblocked
82 while (!queue.isEmpty()) {