Merge "Bug 1875 - Used variables for nexusproxy host, externalized versions"
[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
15 import java.util.concurrent.BlockingQueue;
16 import java.util.concurrent.LinkedBlockingQueue;
17
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;
27
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;
32
33     private final BlockingQueue<TopologyOperation> queue = new LinkedBlockingQueue<>(OPERATION_QUEUE_DEPTH);
34     private final DataBroker dataBroker;
35     private final BindingTransactionChain transactionChain;
36
37     OperationProcessor(final DataBroker dataBroker) {
38         this.dataBroker = Preconditions.checkNotNull(dataBroker);
39         transactionChain = this.dataBroker.createTransactionChain(this);
40     }
41
42     void enqueueOperation(final TopologyOperation task) {
43         try {
44             queue.put(task);
45         } catch (InterruptedException e) {
46             LOG.warn("Interrupted while submitting task {}", task, e);
47         }
48     }
49
50     @Override
51     public void run() {
52         try {
53             for (; ; ) {
54                 TopologyOperation op = queue.take();
55
56                 LOG.debug("New {} operation available, starting transaction", op);
57
58                 final ReadWriteTransaction tx = transactionChain.newReadWriteTransaction();
59
60                 int ops = 0;
61                 do {
62                     op.applyOperation(tx);
63
64                     ops++;
65                     if (ops < MAX_TRANSACTION_OPERATIONS) {
66                         op = queue.poll();
67                     } else {
68                         op = null;
69                     }
70
71                     LOG.debug("Next operation {}", op);
72                 } while (op != null);
73
74                 LOG.debug("Processed {} operations, submitting transaction", ops);
75
76                 CheckedFuture<Void, TransactionCommitFailedException> txResultFuture = tx.submit();
77                 Futures.addCallback(txResultFuture, new FutureCallback<Void>() {
78                     @Override
79                     public void onSuccess(Void notUsed) {
80                         LOG.debug("Topology export successful for tx :{}", tx.getIdentifier());
81                     }
82
83                     @Override
84                     public void onFailure(Throwable throwable) {
85                         LOG.error("Topology export transaction {} failed", tx.getIdentifier(), throwable.getCause());
86                     }
87                 });
88             }
89         } catch (InterruptedException e) {
90             LOG.info("Interrupted processing, terminating", e);
91         }
92
93         // Drain all events, making sure any blocked threads are unblocked
94         while (!queue.isEmpty()) {
95             queue.poll();
96         }
97     }
98
99     @Override
100     public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
101         LOG.error("Failed to export Topology manager operations, Transaction {} failed.", transaction.getIdentifier(), cause);
102     }
103
104     @Override
105     public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
106         //NOOP
107     }
108
109     @Override
110     public void close() throws Exception {
111         if (transactionChain != null) {
112             transactionChain.close();
113         }
114
115     }
116 }