write /nodes/node/table/0 with ensure parents when node first appears
[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 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.LinkedBlockingDeque;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 class FlowCapableInventoryProvider implements AutoCloseable, Runnable {
25     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableInventoryProvider.class);
26     private static final int QUEUE_DEPTH = 500;
27     private static final int MAX_BATCH = 100;
28
29     private final BlockingQueue<InventoryOperation> queue = new LinkedBlockingDeque<>(QUEUE_DEPTH);
30     private final NotificationProviderService notificationService;
31
32     private final DataBroker dataBroker;
33     private ListenerRegistration<?> listenerRegistration;
34     private Thread thread;
35
36     FlowCapableInventoryProvider(final DataBroker dataBroker, final NotificationProviderService notificationService) {
37         this.dataBroker = Preconditions.checkNotNull(dataBroker);
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 ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
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                 final CheckedFuture<Void, TransactionCommitFailedException> result = tx.submit();
106                 Futures.addCallback(result, new FutureCallback<Void>() {
107                     @Override
108                     public void onSuccess(Void aVoid) {
109                         //NOOP
110                     }
111
112                     @Override
113                     public void onFailure(Throwable throwable) {
114                         LOG.error("Transaction {} failed.", tx.getIdentifier(), throwable);
115                     }
116                 });
117             }
118         } catch (InterruptedException e) {
119             LOG.info("Processing interrupted, terminating", e);
120         }
121
122         // Drain all events, making sure any blocked threads are unblocked
123         while (!queue.isEmpty()) {
124             queue.poll();
125         }
126     }
127 }