Bug 8228 - metadata service fix made cleaner
[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.ClusteredDataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
18 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
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 ClusteredDataTreeChangeListener} implementations.
28  *
29  * @param <T> target class
30  */
31 public abstract class DataTreeChangeHandler<T extends DataObject> implements ClusteredDataTreeChangeListener<T>, AutoCloseable {
32
33     protected final DataBroker dataProvider;
34     protected ListenerRegistration<DataTreeChangeHandler<T>> registeredListener;
35
36     /**
37      *
38      * @param dataProvider cannot be {@code null}
39      * @throws NullPointerException if <b>dataProvider</b> is {@code null}
40      */
41     protected DataTreeChangeHandler(DataBroker dataProvider) {
42         this.dataProvider = checkNotNull(dataProvider);
43     }
44
45     /**
46      *
47      * @param pointOfInterest identifier of root node
48      * @throws NullPointerException if <b>pointOfInterest</b> is {@code null}
49      */
50     protected void registerDataTreeChangeListener(DataTreeIdentifier<T> pointOfInterest) {
51         registeredListener = dataProvider.registerDataTreeChangeListener(checkNotNull(pointOfInterest), this);
52     }
53
54     @Override
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()) {
60                 case WRITE:
61                     onWrite(rootNode, rootIdentifier);
62                     break;
63                 case DELETE:
64                     onDelete(rootNode, rootIdentifier);
65                     break;
66                 case SUBTREE_MODIFIED:
67                     onSubtreeModified(rootNode, rootIdentifier);
68                     break;
69             }
70         }
71     }
72
73     /**
74      * Handles case where {@link DataObjectModification#getModificationType()} is
75      * {@link ModificationType#WRITE}. <br>
76      * <b>Parameters of this method are never {@code null}.</b>
77      *
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()}
82      */
83     protected abstract void onWrite(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
84
85     /**
86      * Handles case where {@link DataObjectModification#getModificationType()} is
87      * {@link ModificationType#DELETE}. <br>
88      * <b>Parameters of this method are never {@code null}.</b>
89      *
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()}
94      */
95     protected abstract void onDelete(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
96
97     /**
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>
101      *
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()}
106      */
107     protected abstract void onSubtreeModified(DataObjectModification<T> rootNode, InstanceIdentifier<T> rootIdentifier);
108
109     @Override
110     public void close() {
111         closeRegisteredListener();
112     }
113
114     /**
115      * For child classes which override close() method.
116      */
117     protected void closeRegisteredListener() {
118         registeredListener.close();
119     }
120
121 }