Class AbstractAsyncDataTreeChangeListener<T extends org.opendaylight.yangtools.yang.binding.DataObject>

  • Type Parameters:
    T - type of the data object the listener is registered to.
    All Implemented Interfaces:
    AutoCloseable, EventListener, org.opendaylight.mdsal.binding.api.DataTreeChangeListener<T>, ChainableDataTreeChangeListener<T>

    public abstract class AbstractAsyncDataTreeChangeListener<T extends org.opendaylight.yangtools.yang.binding.DataObject>
    extends Object
    Abstract class providing some common functionality to specific listeners. This listener launches the received notifications by using an ExecutorService.

    The ExecutorService passed to the constructor will depend on the use case. Here we have some examples:

    - If the listener is fast enough and non-blocking: MoreExecutors.directExecutor might be used, or even better, just use the AbstractSyncDataTreeChangeListener.

    - If the listener is heavy or could be blocked: use a multi-threaded executor. We recommend using one of the factory methods in SpecialExecutors. You could and probably should share such an Executor among several listeners in your project.

    - If the listener needs to preserve the order of notifications, then (only) use a single thread executor typically an org.opendaylight.infrautils.utils.concurrent.Executors#newSingleThreadExecutor(String, org.slf4j.Logger).

    - If there are multiple listeners: they could even share an Executor as the ones in SpecialExecutors,

    Subclasses are also encouraged to, in addition to passing the ExecutorService for use in production (by Blueprint wiring) based on above via super(), expose a public constructor letting tests specify an alternative ExecutorService; this is useful e.g. to inject infrautils' AwaitableExecutorService for testing.

    Author:
    David Suárez (david.suarez.fuentes@gmail.com)
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractAsyncDataTreeChangeListener​(org.opendaylight.mdsal.binding.api.DataBroker dataBroker, org.opendaylight.mdsal.binding.api.DataTreeIdentifier<T> dataTreeIdentifier, ExecutorService executorService)  
      AbstractAsyncDataTreeChangeListener​(org.opendaylight.mdsal.binding.api.DataBroker dataBroker, org.opendaylight.mdsal.common.api.LogicalDatastoreType datastoreType, org.opendaylight.yangtools.yang.binding.InstanceIdentifier<T> instanceIdentifier, ExecutorService executorService)  
      AbstractAsyncDataTreeChangeListener​(org.opendaylight.mdsal.binding.api.DataBroker dataBroker, org.opendaylight.mdsal.common.api.LogicalDatastoreType datastoreType, org.opendaylight.yangtools.yang.binding.InstanceIdentifier<T> instanceIdentifier, ExecutorService executorService, MetricProvider metricProvider)  
    • Method Summary

      All Methods Instance Methods Concrete Methods Default Methods Deprecated Methods 
      Modifier and Type Method Description
      default void add​(@NonNull org.opendaylight.yangtools.yang.binding.InstanceIdentifier<T> instanceIdentifier, @NonNull T newDataObject)
      Invoked when a new data object is added.
      void add​(T newDataObject)
      Deprecated.
      void addAfterListener​(org.opendaylight.mdsal.binding.api.DataTreeChangeListener<T> listener)
      Adds a "chained" DataTreeChangeListener, to which DataTreeChangeListener.onDataTreeChanged(java.util.Collection) calls are forwarded AFTER having been processed by this DataTreeChangeListener.
      void addBeforeListener​(org.opendaylight.mdsal.binding.api.DataTreeChangeListener<T> listener)
      Adds a "chained" DataTreeChangeListener, to which DataTreeChangeListener.onDataTreeChanged(java.util.Collection) calls are forwarded BEFORE having been processed by this DataTreeChangeListener.
      void close()  
      protected org.opendaylight.mdsal.binding.api.DataBroker getDataBroker()  
      protected org.opendaylight.serviceutils.tools.listener.DataStoreMetrics getDataStoreMetrics()  
      protected ExecutorService getExecutorService()
      Returns the ExecutorService provided when constructing this instance.
      void onDataTreeChanged​(Collection<org.opendaylight.mdsal.binding.api.DataTreeModification<T>> collection)  
      default void onDataTreeChanged​(Collection<org.opendaylight.mdsal.binding.api.DataTreeModification<T>> changes, org.opendaylight.serviceutils.tools.listener.DataStoreMetrics dataStoreMetrics)
      Default method invoked upon data tree change, in turn it calls the appropriate method (add, update, remove) depending on the type of change.
      void register()  
      default void remove​(@NonNull org.opendaylight.yangtools.yang.binding.InstanceIdentifier<T> instanceIdentifier, @NonNull T removedDataObject)
      Invoked when the data object has been removed.
      void remove​(T removedDataObject)
      Deprecated.
      default void update​(@NonNull org.opendaylight.yangtools.yang.binding.InstanceIdentifier<T> instanceIdentifier, @NonNull T originalDataObject, @NonNull T updatedDataObject)
      Invoked when there is a change in the data object.
      void update​(T originalDataObject, T updatedDataObject)
      Deprecated.
      • Methods inherited from interface org.opendaylight.mdsal.binding.api.DataTreeChangeListener

        onInitialData
    • Constructor Detail

      • AbstractAsyncDataTreeChangeListener

        public AbstractAsyncDataTreeChangeListener​(org.opendaylight.mdsal.binding.api.DataBroker dataBroker,
                                                   org.opendaylight.mdsal.binding.api.DataTreeIdentifier<T> dataTreeIdentifier,
                                                   ExecutorService executorService)
      • AbstractAsyncDataTreeChangeListener

        public AbstractAsyncDataTreeChangeListener​(org.opendaylight.mdsal.binding.api.DataBroker dataBroker,
                                                   org.opendaylight.mdsal.common.api.LogicalDatastoreType datastoreType,
                                                   org.opendaylight.yangtools.yang.binding.InstanceIdentifier<T> instanceIdentifier,
                                                   ExecutorService executorService)
      • AbstractAsyncDataTreeChangeListener

        public AbstractAsyncDataTreeChangeListener​(org.opendaylight.mdsal.binding.api.DataBroker dataBroker,
                                                   org.opendaylight.mdsal.common.api.LogicalDatastoreType datastoreType,
                                                   org.opendaylight.yangtools.yang.binding.InstanceIdentifier<T> instanceIdentifier,
                                                   ExecutorService executorService,
                                                   MetricProvider metricProvider)
    • Method Detail

      • onDataTreeChanged

        public final void onDataTreeChanged​(Collection<org.opendaylight.mdsal.binding.api.DataTreeModification<T>> collection)
      • getExecutorService

        protected ExecutorService getExecutorService()
        Returns the ExecutorService provided when constructing this instance. If the subclass owns the ExecutorService, it should be shut down when closing the listener using this getter.
        Returns:
        executor service
      • addBeforeListener

        public void addBeforeListener​(org.opendaylight.mdsal.binding.api.DataTreeChangeListener<T> listener)
        Description copied from interface: ChainableDataTreeChangeListener
        Adds a "chained" DataTreeChangeListener, to which DataTreeChangeListener.onDataTreeChanged(java.util.Collection) calls are forwarded BEFORE having been processed by this DataTreeChangeListener.

        If an asychronous DataTreeChangeListener supports chaining, it must forward the onDataTreeChanged() call BEFORE event are submitted to its async executor for processing.

        Specified by:
        addBeforeListener in interface ChainableDataTreeChangeListener<T extends org.opendaylight.yangtools.yang.binding.DataObject>
        Parameters:
        listener - the chained DataTreeChangeListener to invoke after this one
      • addAfterListener

        public void addAfterListener​(org.opendaylight.mdsal.binding.api.DataTreeChangeListener<T> listener)
        Description copied from interface: ChainableDataTreeChangeListener
        Adds a "chained" DataTreeChangeListener, to which DataTreeChangeListener.onDataTreeChanged(java.util.Collection) calls are forwarded AFTER having been processed by this DataTreeChangeListener.

        If an asychronous DataTreeChangeListener supports chaining, it must forward the onDataTreeChanged() call ONLY AFTER its async executor processed the event.

        Specified by:
        addAfterListener in interface ChainableDataTreeChangeListener<T extends org.opendaylight.yangtools.yang.binding.DataObject>
        Parameters:
        listener - the chained DataTreeChangeListener to invoke after this one
      • register

        @PostConstruct
        public void register()
      • getDataBroker

        protected org.opendaylight.mdsal.binding.api.DataBroker getDataBroker()
      • getDataStoreMetrics

        protected org.opendaylight.serviceutils.tools.listener.DataStoreMetrics getDataStoreMetrics()
      • close

        @PreDestroy
        public void close()
        Specified by:
        close in interface AutoCloseable
      • add

        @Deprecated
        public void add​(T newDataObject)
        Deprecated.
      • remove

        @Deprecated
        public void remove​(T removedDataObject)
        Deprecated.
      • update

        @Deprecated
        public void update​(T originalDataObject,
                           T updatedDataObject)
        Deprecated.
      • onDataTreeChanged

        public default void onDataTreeChanged​(Collection<org.opendaylight.mdsal.binding.api.DataTreeModification<T>> changes,
                                              org.opendaylight.serviceutils.tools.listener.DataStoreMetrics dataStoreMetrics)
        Default method invoked upon data tree change, in turn it calls the appropriate method (add, update, remove) depending on the type of change.
        Parameters:
        changes - collection of changes
        dataStoreMetrics - data store metrics
      • add

        public default void add​(@NonNull org.opendaylight.yangtools.yang.binding.InstanceIdentifier<T> instanceIdentifier,
                                @NonNull T newDataObject)
        Invoked when a new data object is added.
        Parameters:
        instanceIdentifier - instance id for this data object
        newDataObject - newly added object
      • remove

        public default void remove​(@NonNull org.opendaylight.yangtools.yang.binding.InstanceIdentifier<T> instanceIdentifier,
                                   @NonNull T removedDataObject)
        Invoked when the data object has been removed.
        Parameters:
        instanceIdentifier - instance id for this data object
        removedDataObject - existing object being removed
      • update

        public default void update​(@NonNull org.opendaylight.yangtools.yang.binding.InstanceIdentifier<T> instanceIdentifier,
                                   @NonNull T originalDataObject,
                                   @NonNull T updatedDataObject)
        Invoked when there is a change in the data object.
        Parameters:
        instanceIdentifier - instance id for this data object
        originalDataObject - existing object being modified
        updatedDataObject - modified data object