5e11a216f3add8366eabc73dc8d664cbe8e68cb7
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / InMemoryDOMStoreTreeChangePublisher.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.mdsal.dom.store.inmemory;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableList;
12 import java.util.Collection;
13 import java.util.concurrent.ExecutorService;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
16 import org.opendaylight.mdsal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
17 import org.opendaylight.mdsal.dom.spi.store.AbstractDOMStoreTreeChangePublisher;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
20 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.BatchedInvoker;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 final class InMemoryDOMStoreTreeChangePublisher extends AbstractDOMStoreTreeChangePublisher {
31     private static final BatchedInvoker<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
32         MANAGER_INVOKER = (listener, notifications) -> {
33             // FIXME: this is inefficient, as we could grab the entire queue for the listener and post it
34             final DOMDataTreeChangeListener inst = listener.getInstance();
35             if (inst != null) {
36                 inst.onDataTreeChanged(ImmutableList.copyOf(notifications));
37             }
38         };
39     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreTreeChangePublisher.class);
40
41     private final QueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
42         notificationManager;
43
44     InMemoryDOMStoreTreeChangePublisher(final ExecutorService listenerExecutor, final int maxQueueSize) {
45         notificationManager = QueuedNotificationManager.create(listenerExecutor, MANAGER_INVOKER, maxQueueSize,
46                 "DataTreeChangeListenerQueueMgr");
47     }
48
49     @Override
50     protected void notifyListeners(final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations,
51             final YangInstanceIdentifier path, final DataTreeCandidateNode node) {
52         final DataTreeCandidate candidate = DataTreeCandidates.newDataTreeCandidate(path, node);
53
54         for (AbstractDOMDataTreeChangeListenerRegistration<?> reg : registrations) {
55             LOG.debug("Enqueueing candidate {} to registration {}", candidate, registrations);
56             notificationManager.submitNotification(reg, candidate);
57         }
58     }
59
60     @Override
61     protected synchronized void registrationRemoved(
62             final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
63         LOG.debug("Closing registration {}", registration);
64
65         // FIXME: remove the queue for this registration and make sure we clear it
66     }
67
68     <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
69             final YangInstanceIdentifier treeId, final L listener, final DataTreeSnapshot snapshot) {
70         final AbstractDOMDataTreeChangeListenerRegistration<L> reg = registerTreeChangeListener(treeId, listener);
71
72         final Optional<NormalizedNode<?, ?>> node = snapshot.readNode(treeId);
73         if (node.isPresent()) {
74             final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(treeId, node.get());
75             notificationManager.submitNotification(reg, candidate);
76         }
77
78         return reg;
79     }
80
81     synchronized void publishChange(@Nonnull final DataTreeCandidate candidate) {
82         // Runs synchronized with registrationRemoved()
83         processCandidateTree(candidate);
84     }
85 }