Adopt odlparent-10.0.0/yangtools-8.0.0-SNAPSHOT
[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 static com.google.common.base.Preconditions.checkState;
11
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.tree.api.DataTreeCandidate;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
26 import org.opendaylight.yangtools.yang.data.tree.spi.DataTreeCandidates;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 final class InMemoryDOMStoreTreeChangePublisher extends AbstractDOMStoreTreeChangePublisher {
31     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreTreeChangePublisher.class);
32
33     // Registrations use identity for equality, hence we can skip wrapping them
34     private final EqualityQueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
35         notificationManager;
36
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);
44                 }
45             });
46     }
47
48     private InMemoryDOMStoreTreeChangePublisher(final EqualityQueuedNotificationManager<
49             AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> notificationManager) {
50         this.notificationManager = notificationManager;
51     }
52
53     EqualityQueuedNotificationManager<?, ?> getNotificationManager() {
54         return notificationManager;
55     }
56
57     @Override
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);
62     }
63
64     @Override
65     protected synchronized void registrationRemoved(
66             final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
67         LOG.debug("Closing registration {}", registration);
68
69         // FIXME: remove the queue for this registration and make sure we clear it
70     }
71
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();
78             return reg;
79         }
80
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();
90                 return reg;
91             }
92         }
93
94         final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(YangInstanceIdentifier.empty(), data);
95         final InMemoryDOMStoreTreeChangePublisher publisher = new InMemoryDOMStoreTreeChangePublisher(
96             notificationManager);
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();
102         }
103
104         return reg;
105     }
106
107     synchronized boolean publishChange(final @NonNull DataTreeCandidate candidate) {
108         // Runs synchronized with registrationRemoved()
109         return processCandidateTree(candidate);
110     }
111 }