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.ExecutionException;
12 import java.util.concurrent.LinkedBlockingDeque;
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.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import com.google.common.base.Preconditions;
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;
30 private final BlockingQueue<InventoryOperation> queue = new LinkedBlockingDeque<>(QUEUE_DEPTH);
31 private final NotificationProviderService notificationService;
32 private final DataProviderService dataService;
33 private ListenerRegistration<?> listenerRegistration;
34 private Thread thread;
36 FlowCapableInventoryProvider(final DataProviderService dataService, final NotificationProviderService notificationService) {
37 this.dataService = Preconditions.checkNotNull(dataService);
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 DataModificationTransaction tx = dataService.beginTransaction();
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());
106 final RpcResult<TransactionStatus> result = tx.commit().get();
107 if(!result.isSuccessful()) {
108 LOG.error("Transaction {} failed", tx.getIdentifier());
110 } catch (ExecutionException e) {
111 LOG.warn("Failed to commit inventory change", e.getCause());
114 } catch (InterruptedException e) {
115 LOG.info("Processing interrupted, terminating", e);
118 // Drain all events, making sure any blocked threads are unblocked
119 while (!queue.isEmpty()) {