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.
31 public abstract class DataTreeChangeHandler<T extends DataObject> implements DataTreeChangeListener<T>, AutoCloseable {
33 protected final DataBroker dataProvider;
34 protected final ListenerRegistration<DataTreeChangeHandler<T>> registeredListener;
37 * Registers {@link DataTreeChangeListener} for {@code pointOfInterest} by using
38 * {@code dataProvider}
40 * @param dataProvider cannot be {@code null}
41 * @param pointOfInterest cannot be {@code null}
42 * @throws NullPointerException if at least one paramter is {@code null}
44 protected DataTreeChangeHandler(DataBroker dataProvider, DataTreeIdentifier<T> pointOfInterest) {
45 this.dataProvider = checkNotNull(dataProvider);
46 registeredListener = dataProvider.registerDataTreeChangeListener(checkNotNull(pointOfInterest), this);
50 public void onDataTreeChanged(Collection<DataTreeModification<T>> changes) {
51 for (DataTreeModification<T> change : changes) {
52 DataObjectModification<T> rootNode = change.getRootNode();
53 InstanceIdentifier<T> rootIdentifier = change.getRootPath().getRootIdentifier();
54 switch (rootNode.getModificationType()) {
56 onWrite(rootNode, rootIdentifier);
59 onDelete(rootNode, rootIdentifier);
61 case SUBTREE_MODIFIED:
62 onSubtreeModified(rootNode, rootIdentifier);
69 * Handles case where {@link DataObjectModification#getModificationType()} is
70 * {@link ModificationType#WRITE}. <br>
71 * <b>Parameters of this method are never {@code null}.</b>
73 * @param rootNode represents {@link DataObjectModification} as result of
74 * {@link DataTreeModification#getRootNode()}
75 * @param rootIdentifier represents {@link InstanceIdentifier} obtained from result of
76 * {@link DataTreeModification#getRootPath()}
78 protected abstract void onWrite(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
81 * Handles case where {@link DataObjectModification#getModificationType()} is
82 * {@link ModificationType#DELETE}. <br>
83 * <b>Parameters of this method are never {@code null}.</b>
85 * @param rootNode represents {@link DataObjectModification} as result of
86 * {@link DataTreeModification#getRootNode()}
87 * @param rootIdentifier represents {@link InstanceIdentifier} obtained from result of
88 * {@link DataTreeModification#getRootPath()}
90 protected abstract void onDelete(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
93 * Handles case where {@link DataObjectModification#getModificationType()} is
94 * {@link ModificationType#SUBTREE_MODIFIED}. <br>
95 * <b>Parameters of this method are never {@code null}.</b>
97 * @param rootNode represents {@link DataObjectModification} as result of
98 * {@link DataTreeModification#getRootNode()}
99 * @param rootIdentifier represents {@link InstanceIdentifier} obtained from result of
100 * {@link DataTreeModification#getRootPath()}
102 protected abstract void onSubtreeModified(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
105 public void close() throws Exception {
106 registeredListener.close();