remove apparently un-used Class<K> eventClazz from AsyncListeners
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / datastoreutils / AsyncDataTreeChangeListenerBase.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.genius.datastoreutils;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.concurrent.LinkedBlockingQueue;
14 import java.util.concurrent.ThreadPoolExecutor;
15 import java.util.concurrent.TimeUnit;
16 import javax.annotation.PostConstruct;
17 import javax.annotation.PreDestroy;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.genius.utils.SuperTypeUtil;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28
29 public abstract class AsyncDataTreeChangeListenerBase<T extends DataObject, K extends DataTreeChangeListener<T>>
30         implements DataTreeChangeListener<T>, ChainableDataTreeChangeListener<T>, AutoCloseable {
31
32     private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_CORE_SIZE = 1;
33     private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_MAX_SIZE = 1;
34     private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_KEEP_ALIVE_TIME_SECS = 300;
35
36     private ListenerRegistration<K> listenerRegistration;
37     private final ChainableDataTreeChangeListenerImpl<T> chainingDelegate = new ChainableDataTreeChangeListenerImpl<>();
38
39     private static ThreadPoolExecutor dataTreeChangeHandlerExecutor = new ThreadPoolExecutor(
40             DATATREE_CHANGE_HANDLER_THREAD_POOL_CORE_SIZE,
41             DATATREE_CHANGE_HANDLER_THREAD_POOL_MAX_SIZE,
42             DATATREE_CHANGE_HANDLER_THREAD_POOL_KEEP_ALIVE_TIME_SECS,
43             TimeUnit.SECONDS,
44             new LinkedBlockingQueue<>());
45
46     protected final Class<T> clazz;
47
48     protected AsyncDataTreeChangeListenerBase() {
49         this.clazz = SuperTypeUtil.getTypeParameter(getClass(), 0);
50     }
51
52     @Deprecated
53     public AsyncDataTreeChangeListenerBase(Class<T> clazz, Class<K> eventClazz) {
54         this.clazz = Preconditions.checkNotNull(clazz, "Class can not be null!");
55     }
56
57     @Override
58     public void addBeforeListener(DataTreeChangeListener<T> listener) {
59         chainingDelegate.addBeforeListener(listener);
60     }
61
62     @Override
63     public void addAfterListener(DataTreeChangeListener<T> listener) {
64         chainingDelegate.addAfterListener(listener);
65     }
66
67     @Override
68     public void onDataTreeChanged(Collection<DataTreeModification<T>> changes) {
69         if (changes == null || changes.isEmpty()) {
70             return;
71         }
72
73         DataTreeChangeHandler dataTreeChangeHandler = new DataTreeChangeHandler(changes);
74         dataTreeChangeHandlerExecutor.execute(dataTreeChangeHandler);
75     }
76
77     public void registerListener(LogicalDatastoreType dsType, final DataBroker db) {
78         final DataTreeIdentifier<T> treeId = new DataTreeIdentifier<>(dsType, getWildCardPath());
79         listenerRegistration = db.registerDataTreeChangeListener(treeId, getDataTreeChangeListener());
80     }
81
82     /**
83      * Subclasses override this and place initialization logic here, notably
84      * calls to registerListener(). Note that the overriding method MUST repeat
85      * the PostConstruct annotation, because JSR 250 specifies that lifecycle
86      * methods "are called unless a subclass of the declaring class overrides
87      * the method without repeating the annotation".  (The blueprint-maven-plugin
88      * would gen. XML which calls PostConstruct annotated methods even if they are
89      * in a subclass without repeating the annotation, but this is wrong and not
90      * JSR 250 compliant, and while working in BP, then causes issues e.g. when
91      * wiring with Guice for tests, so do always repeat it.)
92      */
93     @PostConstruct
94     protected void init() {
95     }
96
97     @Override
98     @PreDestroy
99     public void close() throws Exception {
100         if (listenerRegistration != null) {
101             try {
102                 listenerRegistration.close();
103             } finally {
104                 listenerRegistration = null;
105             }
106         }
107     }
108
109     protected abstract InstanceIdentifier<T> getWildCardPath();
110
111     protected abstract void remove(InstanceIdentifier<T> key, T dataObjectModification);
112
113     protected abstract void update(InstanceIdentifier<T> key,
114             T dataObjectModificationBefore, T dataObjectModificationAfter);
115
116     protected abstract void add(InstanceIdentifier<T> key, T dataObjectModification);
117
118     protected abstract K getDataTreeChangeListener();
119
120     public class DataTreeChangeHandler implements Runnable {
121         private final Collection<DataTreeModification<T>> changes;
122
123         public DataTreeChangeHandler(Collection<DataTreeModification<T>> changes) {
124             chainingDelegate.notifyBeforeOnDataTreeChanged(changes);
125             this.changes = changes;
126         }
127
128         @Override
129         public void run() {
130             for (DataTreeModification<T> change : changes) {
131                 final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
132                 final DataObjectModification<T> mod = change.getRootNode();
133
134                 switch (mod.getModificationType()) {
135                     case DELETE:
136                         remove(key, mod.getDataBefore());
137                         break;
138                     case SUBTREE_MODIFIED:
139                         update(key, mod.getDataBefore(), mod.getDataAfter());
140                         break;
141                     case WRITE:
142                         if (mod.getDataBefore() == null) {
143                             add(key, mod.getDataAfter());
144                         } else {
145                             update(key, mod.getDataBefore(), mod.getDataAfter());
146                         }
147                         break;
148                     default:
149                         // FIXME: May be not a good idea to throw.
150                         throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
151                 }
152             }
153             chainingDelegate.notifyAfterOnDataTreeChanged(changes);
154         }
155     }
156 }