From 0dfaa335dfbf99893f432ac6effd2c93cd035df7 Mon Sep 17 00:00:00 2001 From: David Suarez Date: Mon, 19 Jun 2017 16:03:27 +0200 Subject: [PATCH] Add a basic framework to create DataTreeChangeListeners Add a basic framework to create DataTreeChangeListeners. Change-Id: Iaf9ddea9bf312ba7659a57f1d63d5d93fa949938 Signed-off-by: David Suarez Signed-off-by: David Suarez --- mdsalutil/mdsalutil-api/pom.xml | 197 ++++++++++-------- .../AbstractAsyncDataTreeChangeListener.java | 40 ++++ ...tClusteredAsyncDataTreeChangeListener.java | 42 ++++ ...ctClusteredSyncDataTreeChangeListener.java | 31 +++ .../AbstractSyncDataTreeChangeListener.java | 30 +++ .../DataTreeChangeListenerActions.java | 87 ++++++++ .../listeners/package-info.java | 15 ++ 7 files changed, 353 insertions(+), 89 deletions(-) create mode 100644 mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractAsyncDataTreeChangeListener.java create mode 100644 mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractClusteredAsyncDataTreeChangeListener.java create mode 100644 mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractClusteredSyncDataTreeChangeListener.java create mode 100644 mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractSyncDataTreeChangeListener.java create mode 100644 mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/DataTreeChangeListenerActions.java create mode 100644 mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/package-info.java diff --git a/mdsalutil/mdsalutil-api/pom.xml b/mdsalutil/mdsalutil-api/pom.xml index d803e8d19..54df5ee6e 100644 --- a/mdsalutil/mdsalutil-api/pom.xml +++ b/mdsalutil/mdsalutil-api/pom.xml @@ -6,7 +6,8 @@ This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html --> - + 4.0.0 @@ -24,102 +25,109 @@ and is available at http://www.eclipse.org/legal/epl-v10.html build failure. Please do not modify this unless you have a good reason. --> ODL :: genius :: ${project.artifactId} - - - org.opendaylight.yangtools - util - - - org.opendaylight.openflowplugin.model - model-flow-base - ${openflowplugin.version} - - - org.opendaylight.openflowplugin.model - model-flow-service - ${openflowplugin.version} - - - org.opendaylight.controller - liblldp - ${liblldp.version} - - - org.opendaylight.controller - sal-binding-api - - - org.opendaylight.openflowplugin - openflowplugin-extension-nicira - ${openflowplugin.version} - - - org.opendaylight.openflowplugin - openflowjava-extension-nicira - ${openflowplugin.version} - - - org.opendaylight.ovsdb - hwvtepsouthbound-api - ${genius.ovsdb.version} - - - org.opendaylight.ovsdb - utils.config - ${genius.ovsdb.version} - - + + + org.opendaylight.yangtools + util + + + org.opendaylight.openflowplugin.model + model-flow-base + ${openflowplugin.version} + + + org.opendaylight.openflowplugin.model + model-flow-service + ${openflowplugin.version} + + + org.opendaylight.controller + liblldp + ${liblldp.version} + + + org.opendaylight.controller + sal-binding-api + + + org.opendaylight.openflowplugin + openflowplugin-extension-nicira + ${openflowplugin.version} + + + org.opendaylight.openflowplugin + openflowjava-extension-nicira + ${openflowplugin.version} + + + org.opendaylight.ovsdb + hwvtepsouthbound-api + ${genius.ovsdb.version} + + + org.opendaylight.ovsdb + utils.config + ${genius.ovsdb.version} + + org.opendaylight.infrautils counters-api ${genius.infrautils.version} - - + + org.opendaylight.infrautils inject ${genius.infrautils.version} - - - org.immutables - value - - - org.osgi - org.osgi.core - + + + org.immutables + value + + + org.osgi + org.osgi.core + + + org.ops4j.pax.cdi + pax-cdi-api + true + + - - org.opendaylight.yangtools - testutils - test - - - org.awaitility - awaitility - test - - - org.opendaylight.mdsal - mdsal-binding-test-utils - test - - - org.opendaylight.infrautils - infrautils-testutils - ${genius.infrautils.version} - test - - - com.google.truth - truth - test - - - com.google.guava - guava-testlib - - - + + org.opendaylight.yangtools + testutils + test + + + org.awaitility + awaitility + test + + + org.opendaylight.mdsal + mdsal-binding-test-utils + test + + + org.opendaylight.infrautils + infrautils-testutils + ${genius.infrautils.version} + test + + + com.google.truth + truth + test + + + com.google.guava + guava-testlib + + + + org.apache.felix @@ -144,6 +152,17 @@ and is available at http://www.eclipse.org/legal/epl-v10.html + + org.apache.maven.plugins + maven-jar-plugin + + + + test-jar + + + + diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractAsyncDataTreeChangeListener.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractAsyncDataTreeChangeListener.java new file mode 100644 index 000000000..f28ea9d5a --- /dev/null +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractAsyncDataTreeChangeListener.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 Ericsson S.A. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.genius.datastoreutils.listeners; + +import java.util.Collection; +import java.util.concurrent.ExecutorService; +import javax.inject.Inject; +import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.yangtools.yang.binding.DataObject; + +/** + * Abstract class providing some common functionality to specific listeners. + * This listener launches the received notifications in a different thread by + * using the queuing functionality of the {@link ExecutorService}. + * + * @author David Suárez (david.suarez.fuentes@gmail.com) + * + * @param + * type of the data object the listener is registered to. + */ +public abstract class AbstractAsyncDataTreeChangeListener + implements DataTreeChangeListenerActions, DataTreeChangeListener { + private final ExecutorService executorService; + + @Inject + public AbstractAsyncDataTreeChangeListener(ExecutorService executorService) { + this.executorService = executorService; + } + + @Override + public final void onDataTreeChanged(Collection> collection) { + executorService.submit(() -> DataTreeChangeListenerActions.super.onDataTreeChanged(collection)); + } +} diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractClusteredAsyncDataTreeChangeListener.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractClusteredAsyncDataTreeChangeListener.java new file mode 100644 index 000000000..fa9fa8da8 --- /dev/null +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractClusteredAsyncDataTreeChangeListener.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2017 Ericsson S.A. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.genius.datastoreutils.listeners; + +import java.util.Collection; +import java.util.concurrent.ExecutorService; +import javax.inject.Inject; +import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.yangtools.yang.binding.DataObject; + +/** + * Abstract class providing some common functionality to specific listeners. + * This listener launches the received notifications in a different thread by + * using the queuing functionality of the {@link ExecutorService}. This listener + * should be used in clustered deployments. + * + * @author David Suárez (david.suarez.fuentes@gmail.com) + * + * @param + * type of the data object the listener is registered to. + */ +public abstract class AbstractClusteredAsyncDataTreeChangeListener + implements DataTreeChangeListenerActions, ClusteredDataTreeChangeListener { + + private final ExecutorService executorService; + + @Inject + public AbstractClusteredAsyncDataTreeChangeListener(ExecutorService executorService) { + this.executorService = executorService; + } + + @Override + public final void onDataTreeChanged(Collection> collection) { + executorService.submit(() -> DataTreeChangeListenerActions.super.onDataTreeChanged(collection)); + } +} diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractClusteredSyncDataTreeChangeListener.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractClusteredSyncDataTreeChangeListener.java new file mode 100644 index 000000000..aa6a6557d --- /dev/null +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractClusteredSyncDataTreeChangeListener.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2017 Ericsson S.A. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.genius.datastoreutils.listeners; + +import java.util.Collection; +import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.yangtools.yang.binding.DataObject; + +/** + * Abstract class providing some common functionality to specific listeners. + * This listener should be used in clustered deployments. + * + * @author David Suárez (david.suarez.fuentes@gmail.com) + * + * @param + * type of the data object the listener is registered to. + */ +public abstract class AbstractClusteredSyncDataTreeChangeListener + implements DataTreeChangeListenerActions, ClusteredDataTreeChangeListener { + + @Override + public final void onDataTreeChanged(Collection> collection) { + DataTreeChangeListenerActions.super.onDataTreeChanged(collection); + } +} diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractSyncDataTreeChangeListener.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractSyncDataTreeChangeListener.java new file mode 100644 index 000000000..e127c9dfd --- /dev/null +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/AbstractSyncDataTreeChangeListener.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017 Ericsson S.A. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.genius.datastoreutils.listeners; + +import java.util.Collection; +import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.yangtools.yang.binding.DataObject; + +/** + * Abstract class providing some common functionality to specific listeners. + * + * @author David Suárez (david.suarez.fuentes@gmail.com) + * + * @param + * type of the data object the listener is registered to. + */ +public abstract class AbstractSyncDataTreeChangeListener + implements DataTreeChangeListenerActions, DataTreeChangeListener { + + @Override + public final void onDataTreeChanged(Collection> collection) { + DataTreeChangeListenerActions.super.onDataTreeChanged(collection); + } +} diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/DataTreeChangeListenerActions.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/DataTreeChangeListenerActions.java new file mode 100644 index 000000000..eac2914d0 --- /dev/null +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/DataTreeChangeListenerActions.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2017 Ericsson, S.A. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.genius.datastoreutils.listeners; + +import java.io.Closeable; +import java.util.Collection; +import javax.annotation.Nonnull; +import javax.inject.Singleton; +import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.yangtools.yang.binding.DataObject; + +/** + * Interface to be implemented by classes interested in receiving notifications + * about data tree changes. It implements a default method to handle the data + * tree modifications. Those notifications will be forwarded to the appropriate + * methods (add, update, remove) depending on their action type. The listeners + * implementing this interface will need to be annotated as {@link Singleton}. + * + * @author David Suárez (david.suarez.fuentes@gmail.com) + * + * @param + * type of the data object the listener is registered to. + */ +public interface DataTreeChangeListenerActions extends Closeable { + + /** + * Default method invoked upon data tree change, in turn it calls the + * appropriate method (add, update, remove) depending on the type of change. + * + * @param collection + * collection of changes + */ + default void onDataTreeChanged(@Nonnull Collection> collection) { + for (final DataTreeModification dataTreeModification : collection) { + final DataObjectModification dataObjectModification = dataTreeModification.getRootNode(); + switch (dataObjectModification.getModificationType()) { + case SUBTREE_MODIFIED: + update(dataObjectModification.getDataBefore(), dataObjectModification.getDataAfter()); + break; + case DELETE: + remove(dataObjectModification.getDataBefore()); + break; + case WRITE: + if (dataObjectModification.getDataBefore() == null) { + add(dataObjectModification.getDataAfter()); + } else { + update(dataObjectModification.getDataBefore(), dataObjectModification.getDataAfter()); + } + break; + default: + break; + } + } + } + + /** + * Invoked when new data object added. + * + * @param newDataObject + * newly added object + */ + void add(@Nonnull T newDataObject); + + /** + * Invoked when the data object has been removed. + * + * @param removedDataObject + * existing object being removed + */ + void remove(@Nonnull T removedDataObject); + + /** + * Invoked when there is a change in the data object. + * + * @param originalDataObject + * existing object being modified + * @param updatedDataObject + * modified data object + */ + void update(@Nonnull T originalDataObject, T updatedDataObject); +} diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/package-info.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/package-info.java new file mode 100644 index 000000000..d7beab5fd --- /dev/null +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/listeners/package-info.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2017 Ericsson S.A. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +/** + * Incubator package for a listeners framework. This code might be proposed into + * upstream projects once merged and proven to be useful here for a while. + * + * @author David Suárez (david.suarez.fuentes@gmail.com) + * + */ +package org.opendaylight.genius.datastoreutils.listeners; -- 2.36.6