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.List;
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.EqualityQueuedNotificationManager;
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 // Registrations use identity for equality, hence we can skip wrapping them
34 private final EqualityQueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
37 InMemoryDOMStoreTreeChangePublisher(final String dsName, final ExecutorService listenerExecutor,
38 final int maxQueueSize) {
39 notificationManager = new EqualityQueuedNotificationManager<>("DataTreeChangeListenerQueueMgr + dsName",
40 listenerExecutor, maxQueueSize,
41 (listener, notifications) -> {
42 if (!listener.isClosed()) {
43 listener.getInstance().onDataTreeChanged(notifications);
48 private InMemoryDOMStoreTreeChangePublisher(final EqualityQueuedNotificationManager<
49 AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> notificationManager) {
50 this.notificationManager = notificationManager;
53 EqualityQueuedNotificationManager<?, ?> getNotificationManager() {
54 return notificationManager;
58 protected void notifyListener(final AbstractDOMDataTreeChangeListenerRegistration<?> registration,
59 final List<DataTreeCandidate> changes) {
60 LOG.debug("Enqueueing candidates {} for registration {}", changes, registration);
61 notificationManager.submitNotifications(registration, changes);
65 protected synchronized void registrationRemoved(
66 final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
67 LOG.debug("Closing registration {}", registration);
69 // FIXME: remove the queue for this registration and make sure we clear it
72 <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
73 final YangInstanceIdentifier treeId, final L listener, final DataTreeSnapshot snapshot) {
74 final AbstractDOMDataTreeChangeListenerRegistration<L> reg = registerTreeChangeListener(treeId, listener);
75 final Optional<NormalizedNode> preExistingData = snapshot.readNode(YangInstanceIdentifier.empty());
76 if (!preExistingData.isPresent()) {
77 listener.onInitialData();
81 final NormalizedNode data = preExistingData.get();
82 if (treeId.isEmpty()) {
83 checkState(data instanceof DataContainerNode, "Unexpected root node %s", data);
84 if (((DataContainerNode) data).isEmpty()) {
85 // If we are listening on root of data tree we still get empty normalized node, root is always present,
86 // we should filter this out separately and notify it by 'onInitialData()' once.
87 // Otherwise, it is just a valid data node with empty value which also should be notified by
88 // "onDataTreeChanged(List<DataTreeCandidate>)".
89 listener.onInitialData();
94 final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(YangInstanceIdentifier.empty(), data);
95 final InMemoryDOMStoreTreeChangePublisher publisher = new InMemoryDOMStoreTreeChangePublisher(
97 publisher.registerTreeChangeListener(treeId, listener);
98 if (!publisher.publishChange(candidate)) {
99 // There is no data in the conceptual data tree then
100 // notify with 'onInitialData()'.
101 listener.onInitialData();
107 synchronized boolean publishChange(final @NonNull DataTreeCandidate candidate) {
108 // Runs synchronized with registrationRemoved()
109 return processCandidateTree(candidate);