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