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.mdsal.dom.spi.store;
10 import org.opendaylight.mdsal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
11 import org.opendaylight.mdsal.dom.spi.AbstractRegistrationTree;
12 import org.opendaylight.mdsal.dom.spi.RegistrationTreeNode;
13 import org.opendaylight.mdsal.dom.spi.RegistrationTreeSnapshot;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
16 import com.google.common.collect.ImmutableList;
17 import java.util.Collection;
18 import java.util.List;
19 import javax.annotation.Nonnull;
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;
29 * Abstract base class for {@link DOMStoreTreeChangePublisher} implementations.
31 public abstract class AbstractDOMStoreTreeChangePublisher extends AbstractRegistrationTree<AbstractDOMDataTreeChangeListenerRegistration<?>> implements DOMStoreTreeChangePublisher {
32 private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMStoreTreeChangePublisher.class);
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)}.
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
42 protected abstract void notifyListeners(@Nonnull Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations, @Nonnull YangInstanceIdentifier path, @Nonnull DataTreeCandidateNode node);
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 org.opendaylight.yangtools.concepts.ListenerRegistration#close()}
47 * method is executing. Subclasses can use this callback to properly remove any delayed notifications pending
48 * towards the registration.
50 * @param registration Registration which is being closed
52 protected abstract void registrationRemoved(@Nonnull AbstractDOMDataTreeChangeListenerRegistration<?> registration);
55 * Process a candidate tree with respect to registered listeners.
57 * @param candidate candidate three which needs to be processed
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);
66 try (final RegistrationTreeSnapshot<AbstractDOMDataTreeChangeListenerRegistration<?>> snapshot = takeSnapshot()) {
67 final List<PathArgument> toLookup = ImmutableList.copyOf(candidate.getRootPath().getPathArguments());
68 lookupAndNotify(toLookup, 0, snapshot.getRootNode(), candidate);
73 public final <L extends DOMDataTreeChangeListener> AbstractDOMDataTreeChangeListenerRegistration<L> registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener) {
74 // Take the write lock
77 final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> node = findNodeFor(treeId.getPathArguments());
78 final AbstractDOMDataTreeChangeListenerRegistration<L> reg = new AbstractDOMDataTreeChangeListenerRegistration<L>(listener) {
80 protected void removeRegistration() {
81 AbstractDOMStoreTreeChangePublisher.this.removeRegistration(node, this);
82 registrationRemoved(this);
86 addRegistration(node, reg);
89 // Always release the lock
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);
98 final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> exactChild = node.getExactChild(arg);
99 if (exactChild != null) {
100 lookupAndNotify(args, offset + 1, exactChild, candidate);
103 for (RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> c : node.getInexactChildren(arg)) {
104 lookupAndNotify(args, offset + 1, c, candidate);
107 notifyNode(candidate.getRootPath(), node, candidate.getRootNode());
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);
117 final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> regs = regNode.getRegistrations();
118 if (!regs.isEmpty()) {
119 notifyListeners(regs, path, candNode);
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);
129 for (RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> rc : regNode.getInexactChildren(candChild.getIdentifier())) {
130 notifyNode(path.node(candChild.getIdentifier()), rc, candChild);