Bug 1894: Add LISP configuration options to etc/custom.properties in Karaf
[controller.git] / opendaylight / md-sal / topology-manager / src / main / java / org / opendaylight / md / controller / topology / manager / OperationProcessor.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.md.controller.topology.manager;
9
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;
24
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;
29
30     private final BlockingQueue<TopologyOperation> queue = new LinkedBlockingQueue<>(OPERATION_QUEUE_DEPTH);
31     private final DataBroker dataBroker;
32     private final BindingTransactionChain transactionChain;
33
34     OperationProcessor(final DataBroker dataBroker) {
35         this.dataBroker = Preconditions.checkNotNull(dataBroker);
36         transactionChain = this.dataBroker.createTransactionChain(this);
37     }
38
39     void enqueueOperation(final TopologyOperation task) {
40         try {
41             queue.put(task);
42         } catch (InterruptedException e) {
43             LOG.warn("Interrupted while submitting task {}", task, e);
44         }
45     }
46
47     @Override
48     public void run() {
49         try {
50             for (; ; ) {
51                 TopologyOperation op = queue.take();
52
53                 LOG.debug("New operations available, starting transaction");
54                 final ReadWriteTransaction tx = transactionChain.newReadWriteTransaction();
55
56
57                 int ops = 0;
58                 do {
59                     op.applyOperation(tx);
60
61                     ops++;
62                     if (ops < MAX_TRANSACTION_OPERATIONS) {
63                         op = queue.poll();
64                     } else {
65                         op = null;
66                     }
67                 } while (op != null);
68
69                 LOG.debug("Processed {} operations, submitting transaction", ops);
70
71                 final CheckedFuture txResultFuture = tx.submit();
72                 Futures.addCallback(txResultFuture, new FutureCallback() {
73                     @Override
74                     public void onSuccess(Object o) {
75                         LOG.debug("Topology export successful for tx :{}", tx.getIdentifier());
76                     }
77
78                     @Override
79                     public void onFailure(Throwable throwable) {
80                         LOG.error("Topology export transaction {} failed", tx.getIdentifier(), throwable.getCause());
81                     }
82                 });
83             }
84         } catch (InterruptedException e) {
85             LOG.info("Interrupted processing, terminating", e);
86         }
87
88         // Drain all events, making sure any blocked threads are unblocked
89         while (!queue.isEmpty()) {
90             queue.poll();
91         }
92     }
93
94     @Override
95     public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
96         LOG.error("Failed to export Topology manager operations, Transaction {} failed.", transaction.getIdentifier(), cause);
97     }
98
99     @Override
100     public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
101         //NOOP
102     }
103
104     @Override
105     public void close() throws Exception {
106         if (transactionChain != null) {
107             transactionChain.close();
108         }
109
110     }
111 }