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.BindingTransactionChain;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 class FlowCapableInventoryProvider implements AutoCloseable, Runnable, TransactionChainListener {
29 private static final Logger LOG = LoggerFactory.getLogger(FlowCapableInventoryProvider.class);
30 private static final int QUEUE_DEPTH = 500;
31 private static final int MAX_BATCH = 100;
33 private final BlockingQueue<InventoryOperation> queue = new LinkedBlockingDeque<>(QUEUE_DEPTH);
34 private final NotificationProviderService notificationService;
36 private final DataBroker dataBroker;
37 private BindingTransactionChain txChain;
38 private ListenerRegistration<?> listenerRegistration;
39 private Thread thread;
41 FlowCapableInventoryProvider(final DataBroker dataBroker, final NotificationProviderService notificationService) {
42 this.dataBroker = Preconditions.checkNotNull(dataBroker);
43 this.notificationService = Preconditions.checkNotNull(notificationService);
47 final NodeChangeCommiter changeCommiter = new NodeChangeCommiter(FlowCapableInventoryProvider.this);
48 this.listenerRegistration = this.notificationService.registerNotificationListener(changeCommiter);
50 this.txChain = dataBroker.createTransactionChain(this);
51 thread = new Thread(this);
52 thread.setDaemon(true);
53 thread.setName("FlowCapableInventoryProvider");
56 LOG.info("Flow Capable Inventory Provider started.");
59 void enqueue(final InventoryOperation op) {
62 } catch (InterruptedException e) {
63 LOG.warn("Failed to enqueue operation {}", op, e);
68 public void close() throws InterruptedException {
69 LOG.info("Flow Capable Inventory Provider stopped.");
70 if (this.listenerRegistration != null) {
72 this.listenerRegistration.close();
73 } catch (Exception e) {
74 LOG.error("Failed to stop inventory provider", e);
76 listenerRegistration = null;
84 if (txChain != null) {
96 InventoryOperation op = queue.take();
98 final ReadWriteTransaction tx = txChain.newReadWriteTransaction();
99 LOG.debug("New operations available, starting transaction {}", tx.getIdentifier());
103 op.applyOperation(tx);
106 if (ops < MAX_BATCH) {
111 } while (op != null);
113 LOG.debug("Processed {} operations, submitting transaction {}", ops, tx.getIdentifier());
115 final CheckedFuture<Void, TransactionCommitFailedException> result = tx.submit();
116 Futures.addCallback(result, new FutureCallback<Void>() {
118 public void onSuccess(final Void aVoid) {
123 public void onFailure(final Throwable throwable) {
124 LOG.error("Transaction {} failed.", tx.getIdentifier(), throwable);
128 } catch (InterruptedException e) {
129 LOG.info("Processing interrupted, terminating", e);
132 // Drain all events, making sure any blocked threads are unblocked
133 while (!queue.isEmpty()) {
139 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
140 final Throwable cause) {
141 LOG.error("Failed to export Flow Capable Inventory, Transaction {} failed.", transaction.getIdentifier(), cause);
146 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {