d486237d54efd3d7e3ff27f8af504dbaaf44e525
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / store / AbstractDOMStoreTreeChangePublisher.java
1 /*
2  * Copyright (c) 2015 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.spi.store;
9
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.Multimap;
12 import com.google.common.collect.Multimaps;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.IdentityHashMap;
16 import java.util.List;
17 import java.util.Map;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
20 import org.opendaylight.mdsal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
21 import org.opendaylight.mdsal.dom.spi.AbstractRegistrationTree;
22 import org.opendaylight.mdsal.dom.spi.RegistrationTreeNode;
23 import org.opendaylight.mdsal.dom.spi.RegistrationTreeSnapshot;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Abstract base class for {@link DOMStoreTreeChangePublisher} implementations.
35  */
36 public abstract class AbstractDOMStoreTreeChangePublisher
37     extends AbstractRegistrationTree<AbstractDOMDataTreeChangeListenerRegistration<?>>
38         implements DOMStoreTreeChangePublisher {
39     private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMStoreTreeChangePublisher.class);
40
41     /**
42      * Callback for subclass to notify a specified registration of a list of candidates. This method is guaranteed
43      * to be only called from within {@link #processCandidateTree(DataTreeCandidate)}.
44      * @param registration the registration to notify
45      * @param changes the list of DataTreeCandidate changes
46      */
47     protected abstract void notifyListener(@NonNull AbstractDOMDataTreeChangeListenerRegistration<?> registration,
48             @NonNull Collection<DataTreeCandidate> changes);
49
50     /**
51      * Callback notifying the subclass that the specified registration is being
52      * closed and it's user no longer
53      * wishes to receive notifications. This notification is invoked while
54      * the {@link org.opendaylight.yangtools.concepts.ListenerRegistration#close()}
55      * method is executing. Subclasses can use this callback to properly
56      * remove any delayed notifications pending
57      * towards the registration.
58      *
59      * @param registration Registration which is being closed
60      */
61     protected abstract void registrationRemoved(
62             @NonNull AbstractDOMDataTreeChangeListenerRegistration<?> registration);
63
64     /**
65      * Process a candidate tree with respect to registered listeners.
66      *
67      * @param candidate candidate three which needs to be processed
68      * @return true if at least one listener was notified or false.
69      */
70     protected final boolean processCandidateTree(final @NonNull DataTreeCandidate candidate) {
71         final DataTreeCandidateNode node = candidate.getRootNode();
72         if (node.getModificationType() == ModificationType.UNMODIFIED) {
73             LOG.debug("Skipping unmodified candidate {}", candidate);
74             return false;
75         }
76
77         try (RegistrationTreeSnapshot<AbstractDOMDataTreeChangeListenerRegistration<?>> snapshot
78                 = takeSnapshot()) {
79             final List<PathArgument> toLookup = ImmutableList.copyOf(candidate.getRootPath().getPathArguments());
80             final Multimap<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> listenerChanges =
81                     Multimaps.newListMultimap(new IdentityHashMap<>(), ArrayList::new);
82             lookupAndNotify(toLookup, 0, snapshot.getRootNode(), candidate, listenerChanges);
83
84             for (Map.Entry<AbstractDOMDataTreeChangeListenerRegistration<?>, Collection<DataTreeCandidate>> entry:
85                     listenerChanges.asMap().entrySet()) {
86                 notifyListener(entry.getKey(), entry.getValue());
87             }
88
89             return !listenerChanges.isEmpty();
90         }
91     }
92
93     @Override
94     public <L extends DOMDataTreeChangeListener> AbstractDOMDataTreeChangeListenerRegistration<L>
95         registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener) {
96         // Take the write lock
97         takeLock();
98         try {
99             final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> node =
100                     findNodeFor(treeId.getPathArguments());
101             final var reg = new AbstractDOMDataTreeChangeListenerRegistration<>(listener) {
102                 @Override
103                 protected void removeRegistration() {
104                     AbstractDOMStoreTreeChangePublisher.this.removeRegistration(node, this);
105                     registrationRemoved(this);
106                 }
107             };
108
109             addRegistration(node, reg);
110             return reg;
111         } finally {
112             // Always release the lock
113             releaseLock();
114         }
115     }
116
117     private void lookupAndNotify(final List<PathArgument> args,
118             final int offset, final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> node,
119             final DataTreeCandidate candidate,
120             final Multimap<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> listenerChanges) {
121         if (args.size() != offset) {
122             final PathArgument arg = args.get(offset);
123
124             final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> exactChild
125                 = node.getExactChild(arg);
126             if (exactChild != null) {
127                 lookupAndNotify(args, offset + 1, exactChild, candidate, listenerChanges);
128             }
129
130             for (RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> c :
131                     node.getInexactChildren(arg)) {
132                 lookupAndNotify(args, offset + 1, c, candidate, listenerChanges);
133             }
134         } else {
135             notifyNode(candidate.getRootPath(), node, candidate.getRootNode(), listenerChanges);
136         }
137     }
138
139     private void notifyNode(final YangInstanceIdentifier path,
140             final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> regNode,
141             final DataTreeCandidateNode candNode,
142             final Multimap<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> listenerChanges) {
143         if (candNode.getModificationType() == ModificationType.UNMODIFIED) {
144             LOG.debug("Skipping unmodified candidate {}", path);
145             return;
146         }
147
148         final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> regs = regNode.getRegistrations();
149         if (!regs.isEmpty()) {
150             addToListenerChanges(regs, path, candNode, listenerChanges);
151         }
152
153         for (DataTreeCandidateNode candChild : candNode.getChildNodes()) {
154             if (candChild.getModificationType() != ModificationType.UNMODIFIED) {
155                 final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> regChild =
156                         regNode.getExactChild(candChild.getIdentifier());
157                 if (regChild != null) {
158                     notifyNode(path.node(candChild.getIdentifier()), regChild, candChild, listenerChanges);
159                 }
160
161                 for (RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> rc :
162                     regNode.getInexactChildren(candChild.getIdentifier())) {
163                     notifyNode(path.node(candChild.getIdentifier()), rc, candChild, listenerChanges);
164                 }
165             }
166         }
167     }
168
169     private static void addToListenerChanges(
170             final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations,
171             final YangInstanceIdentifier path, final DataTreeCandidateNode node,
172             final Multimap<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> listenerChanges) {
173         final DataTreeCandidate dataTreeCandidate = DataTreeCandidates.newDataTreeCandidate(path, node);
174
175         for (AbstractDOMDataTreeChangeListenerRegistration<?> reg : registrations) {
176             listenerChanges.put(reg, dataTreeCandidate);
177         }
178     }
179 }