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