2 * Copyright (c) 2015 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.controller.sal.core.spi.data;
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;
27 * Abstract base class for {@link DOMStoreTreeChangePublisher} implementations.
29 public abstract class AbstractDOMStoreTreeChangePublisher extends AbstractRegistrationTree<AbstractDOMDataTreeChangeListenerRegistration<?>> implements DOMStoreTreeChangePublisher {
30 private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMStoreTreeChangePublisher.class);
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)}.
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
40 protected abstract void notifyListeners(@Nonnull Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations, @Nonnull YangInstanceIdentifier path, @Nonnull DataTreeCandidateNode node);
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.
48 * @param registration Registration which is being closed
50 protected abstract void registrationRemoved(@Nonnull AbstractDOMDataTreeChangeListenerRegistration<?> registration);
53 * Process a candidate tree with respect to registered listeners.
55 * @param candidate candidate three which needs to be processed
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);
64 try (final RegistrationTreeSnapshot<AbstractDOMDataTreeChangeListenerRegistration<?>> snapshot = takeSnapshot()) {
65 lookupAndNotify(candidate.getRootPath().getPathArguments(), 0, snapshot.getRootNode(), candidate);
70 public final <L extends DOMDataTreeChangeListener> AbstractDOMDataTreeChangeListenerRegistration<L> registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener) {
71 // Take the write lock
74 final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> node = findNodeFor(treeId.getPathArguments());
75 final AbstractDOMDataTreeChangeListenerRegistration<L> reg = new AbstractDOMDataTreeChangeListenerRegistration<L>(listener) {
77 protected void removeRegistration() {
78 AbstractDOMStoreTreeChangePublisher.this.removeRegistration(node, this);
79 registrationRemoved(this);
83 addRegistration(node, reg);
86 // Always release the lock
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);
95 final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> exactChild = node.getExactChild(arg);
96 if (exactChild != null) {
97 lookupAndNotify(args, offset + 1, exactChild, candidate);
100 for (RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> c : node.getInexactChildren(arg)) {
101 lookupAndNotify(args, offset + 1, c, candidate);
104 notifyNode(candidate.getRootPath(), node, candidate.getRootNode());
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);
114 final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> regs = regNode.getRegistrations();
115 if (!regs.isEmpty()) {
116 notifyListeners(regs, path, candNode);
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);
126 for (RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> rc : regNode.getInexactChildren(candChild.getIdentifier())) {
127 notifyNode(path.node(candChild.getIdentifier()), rc, candChild);