Merge "Re-added config.version to config-module-archetype."
[controller.git] / opendaylight / md-sal / inventory-manager / src / main / java / org / opendaylight / controller / md / inventory / manager / FlowCapableInventoryProvider.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.controller.md.inventory.manager;
9
10 import java.util.concurrent.BlockingQueue;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.LinkedBlockingDeque;
13
14 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
15 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
16 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
17 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
18 import org.opendaylight.yangtools.concepts.Registration;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.base.Preconditions;
24
25 class FlowCapableInventoryProvider implements AutoCloseable, Runnable {
26     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableInventoryProvider.class);
27     private static final int QUEUE_DEPTH = 500;
28     private static final int MAX_BATCH = 100;
29
30     private final BlockingQueue<InventoryOperation> queue = new LinkedBlockingDeque<>(QUEUE_DEPTH);
31     private final NotificationProviderService notificationService;
32     private final DataProviderService dataService;
33     private Registration<?> listenerRegistration;
34     private Thread thread;
35
36     FlowCapableInventoryProvider(final DataProviderService dataService, final NotificationProviderService notificationService) {
37         this.dataService = Preconditions.checkNotNull(dataService);
38         this.notificationService = Preconditions.checkNotNull(notificationService);
39     }
40
41     void start() {
42         final NodeChangeCommiter changeCommiter = new NodeChangeCommiter(FlowCapableInventoryProvider.this);
43         this.listenerRegistration = this.notificationService.registerNotificationListener(changeCommiter);
44
45         thread = new Thread(this);
46         thread.setDaemon(true);
47         thread.setName("FlowCapableInventoryProvider");
48         thread.start();
49
50         LOG.info("Flow Capable Inventory Provider started.");
51     }
52
53     void enqueue(final InventoryOperation op) {
54         try {
55             queue.put(op);
56         } catch (InterruptedException e) {
57             LOG.warn("Failed to enqueue operation {}", op, e);
58         }
59     }
60
61     @Override
62     public void close() throws InterruptedException {
63         LOG.info("Flow Capable Inventory Provider stopped.");
64         if (this.listenerRegistration != null) {
65             try {
66                 this.listenerRegistration.close();
67             } catch (Exception e) {
68                 LOG.error("Failed to stop inventory provider", e);
69             }
70             listenerRegistration = null;
71         }
72
73         if (thread != null) {
74             thread.interrupt();
75             thread.join();
76             thread = null;
77         }
78
79
80     }
81
82     @Override
83     public void run() {
84         try {
85             for (;;) {
86                 InventoryOperation op = queue.take();
87
88                 final DataModificationTransaction tx = dataService.beginTransaction();
89                 LOG.debug("New operations available, starting transaction {}", tx.getIdentifier());
90
91                 int ops = 0;
92                 do {
93                     op.applyOperation(tx);
94
95                     ops++;
96                     if (ops < MAX_BATCH) {
97                         op = queue.poll();
98                     } else {
99                         op = null;
100                     }
101                 } while (op != null);
102
103                 LOG.debug("Processed {} operations, submitting transaction {}", ops, tx.getIdentifier());
104
105                 try {
106                     final RpcResult<TransactionStatus> result = tx.commit().get();
107                     if(!result.isSuccessful()) {
108                         LOG.error("Transaction {} failed", tx.getIdentifier());
109                     }
110                 } catch (ExecutionException e) {
111                     LOG.warn("Failed to commit inventory change", e.getCause());
112                 }
113             }
114         } catch (InterruptedException e) {
115             LOG.info("Processing interrupted, terminating", e);
116         }
117
118         // Drain all events, making sure any blocked threads are unblocked
119         while (!queue.isEmpty()) {
120             queue.poll();
121         }
122     }
123 }