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
9 package org.opendaylight.groupbasedpolicy.util;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import java.util.Collection;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 * The purpose of this class is to eliminate boilerplate code used in most of
27 * {@link DataTreeChangeListener} implementations.
29 * @param <T> target class
31 public abstract class DataTreeChangeHandler<T extends DataObject> implements DataTreeChangeListener<T>, AutoCloseable {
33 protected final DataBroker dataProvider;
34 protected ListenerRegistration<DataTreeChangeHandler<T>> registeredListener;
38 * @param dataProvider cannot be {@code null}
39 * @throws NullPointerException if <b>dataProvider</b> is {@code null}
41 protected DataTreeChangeHandler(DataBroker dataProvider) {
42 this.dataProvider = checkNotNull(dataProvider);
47 * @param pointOfInterest identifier of root node
48 * @throws NullPointerException if <b>pointOfInterest</b> is {@code null}
50 protected void registerDataTreeChangeListener(DataTreeIdentifier<T> pointOfInterest) {
51 registeredListener = dataProvider.registerDataTreeChangeListener(checkNotNull(pointOfInterest), this);
55 public void onDataTreeChanged(Collection<DataTreeModification<T>> changes) {
56 for (DataTreeModification<T> change : changes) {
57 DataObjectModification<T> rootNode = change.getRootNode();
58 InstanceIdentifier<T> rootIdentifier = change.getRootPath().getRootIdentifier();
59 switch (rootNode.getModificationType()) {
61 onWrite(rootNode, rootIdentifier);
64 onDelete(rootNode, rootIdentifier);
66 case SUBTREE_MODIFIED:
67 onSubtreeModified(rootNode, rootIdentifier);
74 * Handles case where {@link DataObjectModification#getModificationType()} is
75 * {@link ModificationType#WRITE}. <br>
76 * <b>Parameters of this method are never {@code null}.</b>
78 * @param rootNode represents {@link DataObjectModification} as result of
79 * {@link DataTreeModification#getRootNode()}
80 * @param rootIdentifier represents {@link InstanceIdentifier} obtained from result of
81 * {@link DataTreeModification#getRootPath()}
83 protected abstract void onWrite(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
86 * Handles case where {@link DataObjectModification#getModificationType()} is
87 * {@link ModificationType#DELETE}. <br>
88 * <b>Parameters of this method are never {@code null}.</b>
90 * @param rootNode represents {@link DataObjectModification} as result of
91 * {@link DataTreeModification#getRootNode()}
92 * @param rootIdentifier represents {@link InstanceIdentifier} obtained from result of
93 * {@link DataTreeModification#getRootPath()}
95 protected abstract void onDelete(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
98 * Handles case where {@link DataObjectModification#getModificationType()} is
99 * {@link ModificationType#SUBTREE_MODIFIED}. <br>
100 * <b>Parameters of this method are never {@code null}.</b>
102 * @param rootNode represents {@link DataObjectModification} as result of
103 * {@link DataTreeModification#getRootNode()}
104 * @param rootIdentifier represents {@link InstanceIdentifier} obtained from result of
105 * {@link DataTreeModification#getRootPath()}
107 protected abstract void onSubtreeModified(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
110 public void close() throws Exception {
111 registeredListener.close();