2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.inventory.manager;
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;
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;
29 private final BlockingQueue<InventoryOperation> queue = new LinkedBlockingDeque<>(QUEUE_DEPTH);
30 private final NotificationProviderService notificationService;
32 private final DataBroker dataBroker;
33 private ListenerRegistration<?> listenerRegistration;
34 private Thread thread;
36 FlowCapableInventoryProvider(final DataBroker dataBroker, final NotificationProviderService notificationService) {
37 this.dataBroker = Preconditions.checkNotNull(dataBroker);
38 this.notificationService = Preconditions.checkNotNull(notificationService);
42 final NodeChangeCommiter changeCommiter = new NodeChangeCommiter(FlowCapableInventoryProvider.this);
43 this.listenerRegistration = this.notificationService.registerNotificationListener(changeCommiter);
45 thread = new Thread(this);
46 thread.setDaemon(true);
47 thread.setName("FlowCapableInventoryProvider");
50 LOG.info("Flow Capable Inventory Provider started.");
53 void enqueue(final InventoryOperation op) {
56 } catch (InterruptedException e) {
57 LOG.warn("Failed to enqueue operation {}", op, e);
62 public void close() throws InterruptedException {
63 LOG.info("Flow Capable Inventory Provider stopped.");
64 if (this.listenerRegistration != null) {
66 this.listenerRegistration.close();
67 } catch (Exception e) {
68 LOG.error("Failed to stop inventory provider", e);
70 listenerRegistration = null;
86 InventoryOperation op = queue.take();
88 final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
89 LOG.debug("New operations available, starting transaction {}", tx.getIdentifier());
93 op.applyOperation(tx);
96 if (ops < MAX_BATCH) {
101 } while (op != null);
103 LOG.debug("Processed {} operations, submitting transaction {}", ops, tx.getIdentifier());
105 final CheckedFuture<Void, TransactionCommitFailedException> result = tx.submit();
106 Futures.addCallback(result, new FutureCallback<Void>() {
108 public void onSuccess(Void aVoid) {
113 public void onFailure(Throwable throwable) {
114 LOG.error("Transaction {} failed.", tx.getIdentifier(), throwable);
118 } catch (InterruptedException e) {
119 LOG.info("Processing interrupted, terminating", e);
122 // Drain all events, making sure any blocked threads are unblocked
123 while (!queue.isEmpty()) {