Add a basic framework to create DataTreeChangeListeners
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / datastoreutils / listeners / AbstractAsyncDataTreeChangeListener.java
1 /*
2  * Copyright (c) 2017 Ericsson S.A. 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 package org.opendaylight.genius.datastoreutils.listeners;
9
10 import java.util.Collection;
11 import java.util.concurrent.ExecutorService;
12 import javax.inject.Inject;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16
17 /**
18  * Abstract class providing some common functionality to specific listeners.
19  * This listener launches the received notifications in a different thread by
20  * using the queuing functionality of the {@link ExecutorService}.
21  *
22  * @author David Suárez (david.suarez.fuentes@gmail.com)
23  *
24  * @param <T>
25  *            type of the data object the listener is registered to.
26  */
27 public abstract class AbstractAsyncDataTreeChangeListener<T extends DataObject>
28         implements DataTreeChangeListenerActions<T>, DataTreeChangeListener<T> {
29     private final ExecutorService executorService;
30
31     @Inject
32     public AbstractAsyncDataTreeChangeListener(ExecutorService executorService) {
33         this.executorService = executorService;
34     }
35
36     @Override
37     public final void onDataTreeChanged(Collection<DataTreeModification<T>> collection) {
38         executorService.submit(() -> DataTreeChangeListenerActions.super.onDataTreeChanged(collection));
39     }
40 }