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.mdsal.dom.store.inmemory;
10 import static com.google.common.base.Preconditions.checkState;
12 import java.util.Collection;
13 import java.util.Optional;
14 import java.util.concurrent.ExecutorService;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
17 import org.opendaylight.mdsal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
18 import org.opendaylight.mdsal.dom.spi.store.AbstractDOMStoreTreeChangePublisher;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
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;
30 final class InMemoryDOMStoreTreeChangePublisher extends AbstractDOMStoreTreeChangePublisher {
31 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreTreeChangePublisher.class);
33 private final QueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
36 InMemoryDOMStoreTreeChangePublisher(final ExecutorService listenerExecutor, final int maxQueueSize) {
37 notificationManager = QueuedNotificationManager.create(listenerExecutor, (listener, notifications) -> {
38 // FIXME: we are not checking for listener being closed
39 listener.getInstance().onDataTreeChanged(notifications);
40 }, maxQueueSize, "DataTreeChangeListenerQueueMgr");
43 private InMemoryDOMStoreTreeChangePublisher(final QueuedNotificationManager<
44 AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> notificationManager) {
45 this.notificationManager = notificationManager;
48 QueuedNotificationManager<?, ?> getNotificationManager() {
49 return notificationManager;
53 protected void notifyListener(final AbstractDOMDataTreeChangeListenerRegistration<?> registration,
54 final Collection<DataTreeCandidate> changes) {
55 LOG.debug("Enqueueing candidates {} for registration {}", changes, registration);
56 notificationManager.submitNotifications(registration, changes);
60 protected synchronized void registrationRemoved(
61 final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
62 LOG.debug("Closing registration {}", registration);
64 // FIXME: remove the queue for this registration and make sure we clear it
67 <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
68 final YangInstanceIdentifier treeId, final L listener, final DataTreeSnapshot snapshot) {
69 final AbstractDOMDataTreeChangeListenerRegistration<L> reg = registerTreeChangeListener(treeId, listener);
70 final Optional<NormalizedNode<?, ?>> preExistingData = snapshot.readNode(YangInstanceIdentifier.EMPTY);
71 if (!preExistingData.isPresent()) {
72 listener.onInitialData();
76 final NormalizedNode<?, ?> data = preExistingData.get();
77 if (treeId.isEmpty()) {
78 checkState(data instanceof DataContainerNode, "Unexpected root node %s", data);
79 if (((DataContainerNode) data).getValue().isEmpty()) {
80 // If we are listening on root of data tree we still get empty normalized node, root is always present,
81 // we should filter this out separately and notify it by 'onInitialData()' once.
82 // Otherwise, it is just a valid data node with empty value which also should be notified by
83 // "onDataTreeChanged(Collection<DataTreeCandidate>)".
84 listener.onInitialData();
89 final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(YangInstanceIdentifier.EMPTY, data);
90 final InMemoryDOMStoreTreeChangePublisher publisher = new InMemoryDOMStoreTreeChangePublisher(
92 publisher.registerTreeChangeListener(treeId, listener);
93 if (!publisher.publishChange(candidate)) {
94 // There is no data in the conceptual data tree then
95 // notify with 'onInitialData()'.
96 listener.onInitialData();
102 synchronized boolean publishChange(final @NonNull DataTreeCandidate candidate) {
103 // Runs synchronized with registrationRemoved()
104 return processCandidateTree(candidate);