Merge "Removed `which` dependency, now using proper shell builtin."
[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 java.util.concurrent.BlockingQueue;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.LinkedBlockingQueue;
13
14 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
15 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
16 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.common.base.Preconditions;
22
23 final class OperationProcessor implements Runnable {
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     // FIXME: Flow capable topology exporter should use transaction chaining API
30     private final DataProviderService dataService;
31
32     OperationProcessor(final DataProviderService dataService) {
33         this.dataService = Preconditions.checkNotNull(dataService);
34     }
35
36     void enqueueOperation(final TopologyOperation task) {
37         try {
38             queue.put(task);
39         } catch (InterruptedException e) {
40             LOG.warn("Interrupted while submitting task {}", task, e);
41         }
42     }
43
44     @Override
45     public void run() {
46         try {
47             for (;;) {
48                 TopologyOperation op = queue.take();
49
50                 LOG.debug("New operations available, starting transaction");
51                 final DataModificationTransaction tx = dataService.beginTransaction();
52
53                 int ops = 0;
54                 do {
55                     op.applyOperation(tx);
56
57                     ops++;
58                     if (ops < MAX_TRANSACTION_OPERATIONS) {
59                         op = queue.poll();
60                     } else {
61                         op = null;
62                     }
63                 } while (op != null);
64
65                 LOG.debug("Processed {} operations, submitting transaction", ops);
66
67                 try {
68                     final RpcResult<TransactionStatus> s = tx.commit().get();
69                     if (!s.isSuccessful()) {
70                         LOG.error("Topology export failed for Tx:{}", tx.getIdentifier());
71                     }
72                 } catch (ExecutionException e) {
73                     LOG.error("Topology export transaction {} failed", tx.getIdentifier(), e.getCause());
74                 }
75             }
76         } catch (InterruptedException e) {
77             LOG.info("Interrupted processing, terminating", e);
78         }
79
80         // Drain all events, making sure any blocked threads are unblocked
81         while (!queue.isEmpty()) {
82             queue.poll();
83         }
84     }
85 }