Cleanup: Remove passing around of DataPersistenceProvider
[controller.git] / opendaylight / md-sal / sal-dom-spi / src / main / java / org / opendaylight / controller / sal / core / spi / data / 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.controller.sal.core.spi.data;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.Collection;
12 import java.util.List;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
15 import org.opendaylight.controller.md.sal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
16 import org.opendaylight.controller.md.sal.dom.spi.AbstractRegistrationTree;
17 import org.opendaylight.controller.md.sal.dom.spi.RegistrationTreeNode;
18 import org.opendaylight.controller.md.sal.dom.spi.RegistrationTreeSnapshot;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Abstract base class for {@link DOMStoreTreeChangePublisher} implementations.
30  */
31 public abstract class AbstractDOMStoreTreeChangePublisher extends AbstractRegistrationTree<AbstractDOMDataTreeChangeListenerRegistration<?>> implements DOMStoreTreeChangePublisher {
32     private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMStoreTreeChangePublisher.class);
33
34     /**
35      * Callback for subclass to notify specified registrations of a candidate at a specified path. This method is guaranteed
36      * to be only called from within {@link #processCandidateTree(DataTreeCandidate)}.
37      *
38      * @param registrations Registrations which are affected by the candidate node
39      * @param path Path of changed candidate node. Guaranteed to match the path specified by the registration
40      * @param node Candidate node
41      */
42     protected abstract void notifyListeners(@Nonnull Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations, @Nonnull YangInstanceIdentifier path, @Nonnull DataTreeCandidateNode node);
43
44     /**
45      * Callback notifying the subclass that the specified registration is being closed and it's user no longer
46      * wishes to receive notifications. This notification is invoked while the {@link ListenerRegistration#close()}
47      * method is executing. Subclasses can use this callback to properly remove any delayed notifications pending
48      * towards the registration.
49      *
50      * @param registration Registration which is being closed
51      */
52     protected abstract void registrationRemoved(@Nonnull AbstractDOMDataTreeChangeListenerRegistration<?> registration);
53
54     /**
55      * Process a candidate tree with respect to registered listeners.
56      *
57      * @param candidate candidate three which needs to be processed
58      */
59     protected final void processCandidateTree(@Nonnull final DataTreeCandidate candidate) {
60         final DataTreeCandidateNode node = candidate.getRootNode();
61         if (node.getModificationType() == ModificationType.UNMODIFIED) {
62             LOG.debug("Skipping unmodified candidate {}", candidate);
63             return;
64         }
65
66         try (final RegistrationTreeSnapshot<AbstractDOMDataTreeChangeListenerRegistration<?>> snapshot = takeSnapshot()) {
67             final List<PathArgument> toLookup = ImmutableList.copyOf(candidate.getRootPath().getPathArguments());
68             lookupAndNotify(toLookup, 0, snapshot.getRootNode(), candidate);
69         }
70     }
71
72     @Override
73     public final <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener) {
74         // Take the write lock
75         takeLock();
76         try {
77             final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> node = findNodeFor(treeId.getPathArguments());
78             final AbstractDOMDataTreeChangeListenerRegistration<L> reg = new AbstractDOMDataTreeChangeListenerRegistration<L>(listener) {
79                 @Override
80                 protected void removeRegistration() {
81                     AbstractDOMStoreTreeChangePublisher.this.removeRegistration(node, this);
82                     registrationRemoved(this);
83                 }
84             };
85
86             addRegistration(node, reg);
87             return reg;
88         } finally {
89             // Always release the lock
90             releaseLock();
91         }
92     }
93
94     private void lookupAndNotify(final List<PathArgument> args, final int offset, final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> node, final DataTreeCandidate candidate) {
95         if (args.size() != offset) {
96             final PathArgument arg = args.get(offset);
97
98             final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> exactChild = node.getExactChild(arg);
99             if (exactChild != null) {
100                 lookupAndNotify(args, offset + 1, exactChild, candidate);
101             }
102
103             for (RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> c : node.getInexactChildren(arg)) {
104                 lookupAndNotify(args, offset + 1, c, candidate);
105             }
106         } else {
107             notifyNode(candidate.getRootPath(), node, candidate.getRootNode());
108         }
109     }
110
111     private void notifyNode(final YangInstanceIdentifier path, final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> regNode, final DataTreeCandidateNode candNode) {
112         if (candNode.getModificationType() == ModificationType.UNMODIFIED) {
113             LOG.debug("Skipping unmodified candidate {}", path);
114             return;
115         }
116
117         final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> regs = regNode.getRegistrations();
118         if (!regs.isEmpty()) {
119             notifyListeners(regs, path, candNode);
120         }
121
122         for (DataTreeCandidateNode candChild : candNode.getChildNodes()) {
123             if (candChild.getModificationType() != ModificationType.UNMODIFIED) {
124                 final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> regChild = regNode.getExactChild(candChild.getIdentifier());
125                 if (regChild != null) {
126                     notifyNode(path.node(candChild.getIdentifier()), regChild, candChild);
127                 }
128
129                 for (RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> rc : regNode.getInexactChildren(candChild.getIdentifier())) {
130                     notifyNode(path.node(candChild.getIdentifier()), rc, candChild);
131                 }
132             }
133         }
134     }
135 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.