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 java.util.concurrent.BlockingQueue;
11 import java.util.concurrent.LinkedBlockingDeque;
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.opendaylight.controller.sal.binding.api.NotificationProviderService;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.common.base.Preconditions;
26 import com.google.common.util.concurrent.CheckedFuture;
27 import com.google.common.util.concurrent.FutureCallback;
28 import com.google.common.util.concurrent.Futures;
30 class FlowCapableInventoryProvider implements AutoCloseable, Runnable, TransactionChainListener {
31 private static final Logger LOG = LoggerFactory.getLogger(FlowCapableInventoryProvider.class);
32 private static final int QUEUE_DEPTH = 500;
33 private static final int MAX_BATCH = 100;
35 private final BlockingQueue<InventoryOperation> queue = new LinkedBlockingDeque<>(QUEUE_DEPTH);
36 private final NotificationProviderService notificationService;
38 private final DataBroker dataBroker;
39 private BindingTransactionChain txChain;
40 private ListenerRegistration<?> listenerRegistration;
41 private Thread thread;
43 FlowCapableInventoryProvider(final DataBroker dataBroker, final NotificationProviderService notificationService) {
44 this.dataBroker = Preconditions.checkNotNull(dataBroker);
45 this.notificationService = Preconditions.checkNotNull(notificationService);
49 final NodeChangeCommiter changeCommiter = new NodeChangeCommiter(FlowCapableInventoryProvider.this);
50 this.listenerRegistration = this.notificationService.registerNotificationListener(changeCommiter);
52 this.txChain = dataBroker.createTransactionChain(this);
53 thread = new Thread(this);
54 thread.setDaemon(true);
55 thread.setName("FlowCapableInventoryProvider");
58 LOG.info("Flow Capable Inventory Provider started.");
61 void enqueue(final InventoryOperation op) {
64 } catch (final InterruptedException e) {
65 LOG.warn("Failed to enqueue operation {}", op, e);
70 public void close() throws InterruptedException {
71 LOG.info("Flow Capable Inventory Provider stopped.");
72 if (this.listenerRegistration != null) {
74 this.listenerRegistration.close();
75 } catch (final Exception e) {
76 LOG.error("Failed to stop inventory provider", e);
78 listenerRegistration = null;
86 if (txChain != null) {
98 InventoryOperation op = queue.take();
100 ReadWriteTransaction tx;
102 tx = txChain.newReadWriteTransaction();
103 } catch (final IllegalStateException e) {
105 txChain = dataBroker.createTransactionChain(this);
106 tx = txChain.newReadWriteTransaction();
108 LOG.debug("New operations available, starting transaction {}", tx.getIdentifier());
112 op.applyOperation(tx);
115 if (ops < MAX_BATCH) {
120 } while (op != null);
122 LOG.debug("Processed {} operations, submitting transaction {}", ops, tx.getIdentifier());
124 final CheckedFuture<Void, TransactionCommitFailedException> result = tx.submit();
125 final Object ident = tx.getIdentifier();
126 Futures.addCallback(result, new FutureCallback<Void>() {
128 public void onSuccess(final Void aVoid) {
133 public void onFailure(final Throwable throwable) {
134 LOG.error("Transaction {} failed.", ident, throwable);
138 } catch (final InterruptedException e) {
139 LOG.info("Processing interrupted, terminating", e);
142 // Drain all events, making sure any blocked threads are unblocked
143 while (!queue.isEmpty()) {
149 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
150 final Throwable cause) {
151 LOG.error("Failed to export Flow Capable Inventory, Transaction {} failed.", transaction.getIdentifier(), cause);
156 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {