Merge "Removed duplicate declaration in pom.xml."
[openflowplugin.git] / applications / topology-manager / src / main / java / org / opendaylight / openflowplugin / applications / 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.openflowplugin.applications.topology.manager;
9
10 import com.google.common.base.Preconditions;
11 import java.util.concurrent.BlockingQueue;
12 import java.util.concurrent.LinkedBlockingQueue;
13 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public final class OperationProcessor implements AutoCloseable, Runnable, TransactionChainListener {
24     private static final Logger LOG = LoggerFactory.getLogger(OperationProcessor.class);
25     private static final int MAX_TRANSACTION_OPERATIONS = 100;
26     private static final int OPERATION_QUEUE_DEPTH = 500;
27
28     private final BlockingQueue<TopologyOperation> queue = new LinkedBlockingQueue<>(OPERATION_QUEUE_DEPTH);
29     private final DataBroker dataBroker;
30     private final Thread thread;
31     private BindingTransactionChain transactionChain;
32     private volatile boolean finishing = false;
33
34     public OperationProcessor(final DataBroker dataBroker) {
35         this.dataBroker = Preconditions.checkNotNull(dataBroker);
36         transactionChain = this.dataBroker.createTransactionChain(this);
37
38         thread = new Thread(this);
39         thread.setDaemon(true);
40         thread.setName("FlowCapableTopologyExporter-" + FlowCapableTopologyProvider.TOPOLOGY_ID);
41     }
42
43     void enqueueOperation(final TopologyOperation task) {
44         try {
45             queue.put(task);
46         } catch (InterruptedException e) {
47             LOG.warn("Interrupted while submitting task {}", task, e);
48         }
49     }
50
51     public void start() {
52         thread.start();
53     }
54
55     @Override
56     public void run() {
57             while (!finishing) {
58                 try {
59                     TopologyOperation op = queue.take();
60
61                     LOG.debug("New {} operation available, starting transaction", op);
62
63                     final ReadWriteTransaction tx = transactionChain.newReadWriteTransaction();
64
65                     int ops = 0;
66                     do {
67                         op.applyOperation(tx);
68
69                         ops++;
70                         if (ops < MAX_TRANSACTION_OPERATIONS) {
71                             op = queue.poll();
72                         } else {
73                             op = null;
74                         }
75
76                         LOG.debug("Next operation {}", op);
77                     } while (op != null);
78
79                     LOG.debug("Processed {} operations, submitting transaction", ops);
80                     submitTransaction(tx);
81                 } catch (final IllegalStateException e) {
82                     LOG.warn("Stat DataStoreOperation unexpected State!", e);
83                     transactionChain.close();
84                     transactionChain = dataBroker.createTransactionChain(this);
85                     cleanDataStoreOperQueue();
86                 } catch (final InterruptedException e) {
87                     // This should mean we're shutting down.
88                     LOG.debug("Stat Manager DS Operation thread interrupted!", e);
89                     finishing = true;
90                 } catch (final Exception e) {
91                     LOG.warn("Stat DataStore Operation executor fail!", e);
92                 }
93             }
94         // Drain all events, making sure any blocked threads are unblocked
95         cleanDataStoreOperQueue();
96     }
97
98     private void submitTransaction(ReadWriteTransaction tx) {
99         try {
100             tx.submit().checkedGet();
101         } catch (final TransactionCommitFailedException e) {
102             LOG.warn("Stat DataStoreOperation unexpected State!", e);
103             transactionChain.close();
104             transactionChain = dataBroker.createTransactionChain(this);
105             cleanDataStoreOperQueue();
106         }
107     }
108
109     private void cleanDataStoreOperQueue() {
110         while (!queue.isEmpty()) {
111             queue.poll();
112         }
113     }
114
115     @Override
116     public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
117         LOG.warn("Failed to export Topology manager operations, Transaction {} failed: {}", transaction.getIdentifier(), cause.getMessage());
118         LOG.debug("Failed to export Topology manager operations.. ", cause);
119         transactionChain.close();
120         transactionChain = dataBroker.createTransactionChain(this);
121         cleanDataStoreOperQueue();
122     }
123
124     @Override
125     public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
126         //NOOP
127     }
128
129     @Override
130     public void close() {
131         thread.interrupt();
132         try {
133             thread.join();
134         } catch(InterruptedException e) {
135             LOG.debug("Join of thread {} was interrupted", thread.getName(), e);
136         }
137
138         if (transactionChain != null) {
139             transactionChain.close();
140         }
141
142         LOG.debug("OperationProcessor closed");
143     }
144 }