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 if (!listener.isClosed()) {
39 listener.getInstance().onDataTreeChanged(notifications);
41 }, maxQueueSize, "DataTreeChangeListenerQueueMgr");
44 private InMemoryDOMStoreTreeChangePublisher(final QueuedNotificationManager<
45 AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> notificationManager) {
46 this.notificationManager = notificationManager;
49 QueuedNotificationManager<?, ?> getNotificationManager() {
50 return notificationManager;
54 protected void notifyListener(final AbstractDOMDataTreeChangeListenerRegistration<?> registration,
55 final Collection<DataTreeCandidate> changes) {
56 LOG.debug("Enqueueing candidates {} for registration {}", changes, registration);
57 notificationManager.submitNotifications(registration, changes);
61 protected synchronized void registrationRemoved(
62 final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
63 LOG.debug("Closing registration {}", registration);
65 // FIXME: remove the queue for this registration and make sure we clear it
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 final Optional<NormalizedNode<?, ?>> preExistingData = snapshot.readNode(YangInstanceIdentifier.empty());
72 if (!preExistingData.isPresent()) {
73 listener.onInitialData();
77 final NormalizedNode<?, ?> data = preExistingData.get();
78 if (treeId.isEmpty()) {
79 checkState(data instanceof DataContainerNode, "Unexpected root node %s", data);
80 if (((DataContainerNode) data).getValue().isEmpty()) {
81 // If we are listening on root of data tree we still get empty normalized node, root is always present,
82 // we should filter this out separately and notify it by 'onInitialData()' once.
83 // Otherwise, it is just a valid data node with empty value which also should be notified by
84 // "onDataTreeChanged(Collection<DataTreeCandidate>)".
85 listener.onInitialData();
90 final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(YangInstanceIdentifier.empty(), data);
91 final InMemoryDOMStoreTreeChangePublisher publisher = new InMemoryDOMStoreTreeChangePublisher(
93 publisher.registerTreeChangeListener(treeId, listener);
94 if (!publisher.publishChange(candidate)) {
95 // There is no data in the conceptual data tree then
96 // notify with 'onInitialData()'.
97 listener.onInitialData();
103 synchronized boolean publishChange(final @NonNull DataTreeCandidate candidate) {
104 // Runs synchronized with registrationRemoved()
105 return processCandidateTree(candidate);