Fixed Subtree typo
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / util / DataTreeChangeHandler.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.util;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import java.util.Collection;
14
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;
24
25 /**
26  * The purpose of this class is to eliminate boilerplate code used in most of
27  * {@link DataTreeChangeListener} implementations.
28  *
29  * @param <T>
30  */
31 public abstract class DataTreeChangeHandler<T extends DataObject> implements DataTreeChangeListener<T>, AutoCloseable {
32
33     protected final DataBroker dataProvider;
34     protected final ListenerRegistration<DataTreeChangeHandler<T>> registeredListener;
35
36     /**
37      * Registers {@link DataTreeChangeListener} for {@code pointOfInterest} by using
38      * {@code dataProvider}
39      *
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}
43      */
44     protected DataTreeChangeHandler(DataBroker dataProvider, DataTreeIdentifier<T> pointOfInterest) {
45         this.dataProvider = checkNotNull(dataProvider);
46         registeredListener = dataProvider.registerDataTreeChangeListener(checkNotNull(pointOfInterest), this);
47     }
48
49     @Override
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()) {
55                 case WRITE:
56                     onWrite(rootNode, rootIdentifier);
57                     break;
58                 case DELETE:
59                     onDelete(rootNode, rootIdentifier);
60                     break;
61                 case SUBTREE_MODIFIED:
62                     onSubtreeModified(rootNode, rootIdentifier);
63                     break;
64             }
65         }
66     }
67
68     /**
69      * Handles case where {@link DataObjectModification#getModificationType()} is
70      * {@link ModificationType#WRITE}. <br>
71      * <b>Parameters of this method are never {@code null}.</b>
72      *
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()}
77      */
78     protected abstract void onWrite(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
79
80     /**
81      * Handles case where {@link DataObjectModification#getModificationType()} is
82      * {@link ModificationType#DELETE}. <br>
83      * <b>Parameters of this method are never {@code null}.</b>
84      *
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()}
89      */
90     protected abstract void onDelete(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
91
92     /**
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>
96      *
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()}
101      */
102     protected abstract void onSubtreeModified(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
103
104     @Override
105     public void close() throws Exception {
106         registeredListener.close();
107     }
108
109 }