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 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.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Abstract base class for {@link DOMStoreTreeChangePublisher} implementations.
30 public abstract class AbstractDOMStoreTreeChangePublisher extends AbstractRegistrationTree<AbstractDOMDataTreeChangeListenerRegistration<?>> implements DOMStoreTreeChangePublisher {
31 private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMStoreTreeChangePublisher.class);
34 * Callback for subclass to notify specified registrations of a candidate at a specified path. This method is guaranteed
35 * to be only called from within {@link #processCandidateTree(DataTreeCandidate)}.
37 * @param registrations Registrations which are affected by the candidate node
38 * @param path Path of changed candidate node. Guaranteed to match the path specified by the registration
39 * @param node Candidate node
41 protected abstract void notifyListeners(@Nonnull Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations, @Nonnull YangInstanceIdentifier path, @Nonnull DataTreeCandidateNode node);
44 * Callback notifying the subclass that the specified registration is being closed and it's user no longer
45 * wishes to receive notifications. This notification is invoked while the {@link org.opendaylight.yangtools.concepts.ListenerRegistration#close()}
46 * method is executing. Subclasses can use this callback to properly remove any delayed notifications pending
47 * towards the registration.
49 * @param registration Registration which is being closed
51 protected abstract void registrationRemoved(@Nonnull AbstractDOMDataTreeChangeListenerRegistration<?> registration);
54 * Process a candidate tree with respect to registered listeners.
56 * @param candidate candidate three which needs to be processed
58 protected final void processCandidateTree(@Nonnull final DataTreeCandidate candidate) {
59 final DataTreeCandidateNode node = candidate.getRootNode();
60 if (node.getModificationType() == ModificationType.UNMODIFIED) {
61 LOG.debug("Skipping unmodified candidate {}", candidate);
65 try (final RegistrationTreeSnapshot<AbstractDOMDataTreeChangeListenerRegistration<?>> snapshot = takeSnapshot()) {
66 final List<PathArgument> toLookup = ImmutableList.copyOf(candidate.getRootPath().getPathArguments());
67 lookupAndNotify(toLookup, 0, snapshot.getRootNode(), candidate);
72 public final <L extends DOMDataTreeChangeListener> AbstractDOMDataTreeChangeListenerRegistration<L> registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener) {
73 // Take the write lock
76 final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> node = findNodeFor(treeId.getPathArguments());
77 final AbstractDOMDataTreeChangeListenerRegistration<L> reg = new AbstractDOMDataTreeChangeListenerRegistration<L>(listener) {
79 protected void removeRegistration() {
80 AbstractDOMStoreTreeChangePublisher.this.removeRegistration(node, this);
81 registrationRemoved(this);
85 addRegistration(node, reg);
88 // Always release the lock
93 private void lookupAndNotify(final List<PathArgument> args, final int offset, final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> node, final DataTreeCandidate candidate) {
94 if (args.size() != offset) {
95 final PathArgument arg = args.get(offset);
97 final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> exactChild = node.getExactChild(arg);
98 if (exactChild != null) {
99 lookupAndNotify(args, offset + 1, exactChild, candidate);
102 for (RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> c : node.getInexactChildren(arg)) {
103 lookupAndNotify(args, offset + 1, c, candidate);
106 notifyNode(candidate.getRootPath(), node, candidate.getRootNode());
110 private void notifyNode(final YangInstanceIdentifier path, final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> regNode, final DataTreeCandidateNode candNode) {
111 if (candNode.getModificationType() == ModificationType.UNMODIFIED) {
112 LOG.debug("Skipping unmodified candidate {}", path);
116 final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> regs = regNode.getRegistrations();
117 if (!regs.isEmpty()) {
118 notifyListeners(regs, path, candNode);
121 for (DataTreeCandidateNode candChild : candNode.getChildNodes()) {
122 if (candChild.getModificationType() != ModificationType.UNMODIFIED) {
123 final RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> regChild = regNode.getExactChild(candChild.getIdentifier());
124 if (regChild != null) {
125 notifyNode(path.node(candChild.getIdentifier()), regChild, candChild);
128 for (RegistrationTreeNode<AbstractDOMDataTreeChangeListenerRegistration<?>> rc : regNode.getInexactChildren(candChild.getIdentifier())) {
129 notifyNode(path.node(candChild.getIdentifier()), rc, candChild);