2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.genius.datastoreutils;
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.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public abstract class AsyncDataTreeChangeListenerBase<T extends DataObject, K extends DataTreeChangeListener>
31 implements DataTreeChangeListener<T>, AutoCloseable {
33 private static final Logger LOG = LoggerFactory.getLogger(AsyncDataTreeChangeListenerBase.class);
35 private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_CORE_SIZE = 1;
36 private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_MAX_SIZE = 1;
37 private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_KEEP_ALIVE_TIME_SECS = 300;
38 private static final int STARTUP_LOOP_TICK = 500;
39 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
41 private ListenerRegistration<K> listenerRegistration;
43 private static ThreadPoolExecutor dataTreeChangeHandlerExecutor = new ThreadPoolExecutor(
44 DATATREE_CHANGE_HANDLER_THREAD_POOL_CORE_SIZE,
45 DATATREE_CHANGE_HANDLER_THREAD_POOL_MAX_SIZE,
46 DATATREE_CHANGE_HANDLER_THREAD_POOL_KEEP_ALIVE_TIME_SECS,
48 new LinkedBlockingQueue<Runnable>());
50 protected final Class<T> clazz;
51 private final Class<K> eventClazz;
53 public AsyncDataTreeChangeListenerBase(Class<T> clazz, Class<K> eventClazz) {
54 this.clazz = Preconditions.checkNotNull(clazz, "Class can not be null!");
55 this.eventClazz = Preconditions.checkNotNull(eventClazz, "eventClazz can not be null!");
59 public void onDataTreeChanged(Collection<DataTreeModification<T>> changes) {
60 if (changes == null || changes.isEmpty()) {
64 DataTreeChangeHandler dataTreeChangeHandler = new DataTreeChangeHandler(changes);
65 dataTreeChangeHandlerExecutor.execute(dataTreeChangeHandler);
68 public void registerListener(LogicalDatastoreType dsType, final DataBroker db) {
69 final DataTreeIdentifier<T> treeId = new DataTreeIdentifier<>(dsType, getWildCardPath());
71 TaskRetryLooper looper = new TaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
72 listenerRegistration = looper.loopUntilNoException(() -> db.registerDataTreeChangeListener(treeId, getDataTreeChangeListener()));
73 } catch (final Exception e) {
74 LOG.warn("{}: Data Tree Change listener registration failed.", eventClazz.getName());
75 LOG.debug("{}: Data Tree Change listener registration failed: {}", eventClazz.getName(), e);
76 throw new IllegalStateException( eventClazz.getName() + "{}startup failed. System needs restart.", e);
81 * Subclasses override this and place initialization logic here, notably
82 * calls to registerListener(). Note that the overriding method MUST repeat
83 * the PostConstruct annotation, because JSR 250 specifies that lifecycle
84 * methods "are called unless a subclass of the declaring class overrides
85 * the method without repeating the annotation". (The blueprint-maven-plugin
86 * would gen. XML which calls PostConstruct annotated methods even if they are
87 * in a subclass without repeating the annotation, but this is wrong and not
88 * JSR 250 compliant, and while working in BP, then causes issues e.g. when
89 * wiring with Guice for tests, so do always repeat it.)
92 protected void init() {
97 public void close() throws Exception {
98 if (listenerRegistration != null) {
100 listenerRegistration.close();
101 } catch (final Exception e) {
102 LOG.error("Error when cleaning up DataTreeChangeListener.", e);
104 listenerRegistration = null;
108 protected abstract InstanceIdentifier<T> getWildCardPath();
109 protected abstract void remove(InstanceIdentifier<T> key, T dataObjectModification);
110 protected abstract void update(InstanceIdentifier<T> key, T dataObjectModificationBefore, T dataObjectModificationAfter);
111 protected abstract void add(InstanceIdentifier<T> key, T dataObjectModification);
112 protected abstract K getDataTreeChangeListener();
114 public class DataTreeChangeHandler implements Runnable {
115 Collection<DataTreeModification<T>> changes;
117 public DataTreeChangeHandler(Collection<DataTreeModification<T>> changes) {
118 this.changes = changes;
123 for (DataTreeModification<T> change : changes) {
124 final InstanceIdentifier<T> key = change.getRootPath().getRootIdentifier();
125 final DataObjectModification<T> mod = change.getRootNode();
127 switch (mod.getModificationType()) {
129 remove(key, mod.getDataBefore());
131 case SUBTREE_MODIFIED:
132 update(key, mod.getDataBefore(), mod.getDataAfter());
135 if (mod.getDataBefore() == null) {
136 add(key, mod.getDataAfter());
138 update(key, mod.getDataBefore(), mod.getDataAfter());
142 // FIXME: May be not a good idea to throw.
143 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());