From 62cbc5f4ffd63866c026c0c12b9554ba95e79b73 Mon Sep 17 00:00:00 2001 From: David Suarez Date: Thu, 16 Nov 2017 21:23:53 +0100 Subject: [PATCH] Migrate sfc-ovs listeners to Genius' Listeners framework Change-Id: I02019c49787c45cd45d3d27d089e5bc0a140416d Signed-off-by: David Suarez --- sfc-ovs/pom.xml | 9 +++ .../ovs/listener/SfcOvsNodeDataListener.java | 60 +++++++------------ .../listener/SfcOvsSffEntryDataListener.java | 57 +++++------------- .../org/opendaylight/blueprint/sfc-ovs.xml | 26 +++----- .../listener/SfcOvsNodeDataListenerTest.java | 3 - .../SfcOvsSffEntryDataListenerTest.java | 1 - .../SfcOvsSffEntryDataListenerTest.java | 1 - 7 files changed, 53 insertions(+), 104 deletions(-) diff --git a/sfc-ovs/pom.xml b/sfc-ovs/pom.xml index d3c709bf9..df0c9cabf 100644 --- a/sfc-ovs/pom.xml +++ b/sfc-ovs/pom.xml @@ -63,6 +63,15 @@ + + org.apache.aries.blueprint + blueprint-maven-plugin + + + org.opendaylight.sfc.ovs + + + org.apache.felix maven-bundle-plugin diff --git a/sfc-ovs/src/main/java/org/opendaylight/sfc/ovs/listener/SfcOvsNodeDataListener.java b/sfc-ovs/src/main/java/org/opendaylight/sfc/ovs/listener/SfcOvsNodeDataListener.java index 18228df3d..5648293fd 100755 --- a/sfc-ovs/src/main/java/org/opendaylight/sfc/ovs/listener/SfcOvsNodeDataListener.java +++ b/sfc-ovs/src/main/java/org/opendaylight/sfc/ovs/listener/SfcOvsNodeDataListener.java @@ -18,18 +18,19 @@ package org.opendaylight.sfc.ovs.listener; import com.google.common.base.Optional; -import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; +import javax.annotation.Nonnull; +import javax.inject.Inject; +import javax.inject.Singleton; import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; import org.opendaylight.controller.md.sal.binding.api.ReadTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.opendaylight.genius.datastoreutils.listeners.AbstractSyncDataTreeChangeListener; import org.opendaylight.ovsdb.southbound.SouthboundConstants; import org.opendaylight.sfc.ovs.provider.SfcOvsUtil; -import org.opendaylight.sfc.provider.listeners.AbstractDataTreeChangeListener; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.ServiceFunctionForwarders; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation; @@ -38,43 +39,27 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology. import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; -import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SfcOvsNodeDataListener extends AbstractDataTreeChangeListener { +@Singleton +public class SfcOvsNodeDataListener extends AbstractSyncDataTreeChangeListener { + private static final Logger LOG = LoggerFactory.getLogger(SfcOvsNodeDataListener.class); + private final DataBroker dataBroker; - private ListenerRegistration listenerRegistration; + @Inject public SfcOvsNodeDataListener(final DataBroker dataBroker) { + super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(NetworkTopology.class) + .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID)) + .child(Node.class)); this.dataBroker = dataBroker; } - public void init() { - LOG.debug("Initializing..."); - registerListeners(); - } - - private void registerListeners() { - final DataTreeIdentifier treeId = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, - InstanceIdentifier.create(NetworkTopology.class) - .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID)) - .child(Node.class)); - listenerRegistration = dataBroker.registerDataTreeChangeListener(treeId, this); - } - - @Override - public void close() throws Exception { - LOG.debug("Closing listener..."); - if (listenerRegistration != null) { - listenerRegistration.close(); - } - } - @Override - protected void add(Node createdNode) { + public void add(@Nonnull Node node) { /* * NODE CREATION When user puts SFF into config DS, reading from * topology is involved to write OVSDB bridge and termination point @@ -82,17 +67,15 @@ public class SfcOvsNodeDataListener extends AbstractDataTreeChangeListener * might put SFF into config DS before topology in operational DS gets * populated. */ + LOG.debug("Created OVS Node: {}", node.toString()); - LOG.debug("\nCreated OVS Node: {}", createdNode.toString()); - - OvsdbNodeAugmentation ovsdbNodeAugmentation = createdNode.getAugmentation(OvsdbNodeAugmentation.class); + OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class); if (ovsdbNodeAugmentation != null) { final ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo(); if (connectionInfo != null) { - CheckedFuture, ReadFailedException> - exitsingSffs = readServiceFunctionForwarders(); - Futures.addCallback(exitsingSffs, new FutureCallback>() { + ListenableFuture> exitsingSffs = readServiceFunctionForwarders(); + Futures.addCallback(exitsingSffs, new FutureCallback>() { @Override public void onSuccess(Optional optionalSffs) { if (optionalSffs != null && optionalSffs.isPresent()) { @@ -116,7 +99,7 @@ public class SfcOvsNodeDataListener extends AbstractDataTreeChangeListener } @Override - protected void remove(Node deletedNode) { + public void remove(@Nonnull Node node) { /* * NODE UPDATE and NODE DELETE This case would mean, that user has * modified vSwitch state directly by ovs command, which is not handled @@ -125,7 +108,7 @@ public class SfcOvsNodeDataListener extends AbstractDataTreeChangeListener } @Override - protected void update(Node originalNode, Node updatedNode) { + public void update(@Nonnull Node originalNode, @Nonnull Node updatedNode) { /* * NODE UPDATE and NODE DELETE This case would mean, that user has * modified vSwitch state directly by ovs command, which is not handled @@ -133,11 +116,10 @@ public class SfcOvsNodeDataListener extends AbstractDataTreeChangeListener */ } - private CheckedFuture, ReadFailedException> readServiceFunctionForwarders() { + private ListenableFuture> readServiceFunctionForwarders() { ReadTransaction transaction = dataBroker.newReadOnlyTransaction(); InstanceIdentifier sffIid = InstanceIdentifier .builder(ServiceFunctionForwarders.class).build(); return transaction.read(LogicalDatastoreType.CONFIGURATION, sffIid); } - } diff --git a/sfc-ovs/src/main/java/org/opendaylight/sfc/ovs/listener/SfcOvsSffEntryDataListener.java b/sfc-ovs/src/main/java/org/opendaylight/sfc/ovs/listener/SfcOvsSffEntryDataListener.java index a04994b23..5852adf44 100755 --- a/sfc-ovs/src/main/java/org/opendaylight/sfc/ovs/listener/SfcOvsSffEntryDataListener.java +++ b/sfc-ovs/src/main/java/org/opendaylight/sfc/ovs/listener/SfcOvsSffEntryDataListener.java @@ -17,12 +17,13 @@ package org.opendaylight.sfc.ovs.listener; import java.util.List; +import javax.annotation.Nonnull; +import javax.inject.Singleton; import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.genius.datastoreutils.listeners.AbstractSyncDataTreeChangeListener; import org.opendaylight.sfc.ovs.api.SfcSffToOvsMappingAPI; import org.opendaylight.sfc.ovs.provider.SfcOvsUtil; -import org.opendaylight.sfc.provider.listeners.AbstractDataTreeChangeListener; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.ovs.rev140701.SffOvsLocatorOptionsAugmentation; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.ServiceFunctionForwarders; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarder.base.SffDataPlaneLocator; @@ -30,57 +31,36 @@ import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev1407 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sl.rev140701.VxlanGpe; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; -import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SfcOvsSffEntryDataListener extends AbstractDataTreeChangeListener { +@Singleton +public class SfcOvsSffEntryDataListener extends AbstractSyncDataTreeChangeListener { private static final Logger LOG = LoggerFactory.getLogger(SfcOvsSffEntryDataListener.class); - private final DataBroker dataBroker; - private ListenerRegistration listenerRegistration; public SfcOvsSffEntryDataListener(final DataBroker dataBroker) { - this.dataBroker = dataBroker; - } - - public void init() { - LOG.debug("Initializing..."); - registerListeners(); - } - - private void registerListeners() { - final DataTreeIdentifier treeId = new DataTreeIdentifier<>( - LogicalDatastoreType.CONFIGURATION, - InstanceIdentifier.create(ServiceFunctionForwarders.class).child(ServiceFunctionForwarder.class)); - listenerRegistration = dataBroker.registerDataTreeChangeListener(treeId, this); + super(dataBroker, LogicalDatastoreType.CONFIGURATION, + InstanceIdentifier.create(ServiceFunctionForwarders.class).child(ServiceFunctionForwarder.class)); } @Override - public void close() throws Exception { - LOG.debug("Closing listener..."); - if (listenerRegistration != null) { - listenerRegistration.close(); - } - } - - @Override - protected void add(ServiceFunctionForwarder serviceFunctionForwarder) { - LOG.info("\nCreated Service Function Forwarder: {}", serviceFunctionForwarder.toString()); + public void add(@Nonnull ServiceFunctionForwarder serviceFunctionForwarder) { + LOG.info("Created Service Function Forwarder: {}", serviceFunctionForwarder.toString()); // add augmentations for serviceFunctionForwarder addOvsdbAugmentations(serviceFunctionForwarder); } @Override - protected void remove(ServiceFunctionForwarder deletedServiceFunctionForwarder) { - LOG.info("\nDeleted Service Function Forwarder: {}", deletedServiceFunctionForwarder.toString()); + public void remove(@Nonnull ServiceFunctionForwarder deletedServiceFunctionForwarder) { + LOG.info("Deleted Service Function Forwarder: {}", deletedServiceFunctionForwarder.toString()); deleteOvsdbAugmentations(deletedServiceFunctionForwarder); } @Override - protected void update(ServiceFunctionForwarder originalServiceFunctionForwarder, - ServiceFunctionForwarder updatedServiceFunctionForwarder) { + public void update(@Nonnull ServiceFunctionForwarder originalServiceFunctionForwarder, + @Nonnull ServiceFunctionForwarder updatedServiceFunctionForwarder) { // Notice: Adding an SffDpl to an existing SFF will trigger this // listener, not a separate SffDplListener // which means 2 different listeners are not needed. This was tested @@ -92,7 +72,7 @@ public class SfcOvsSffEntryDataListener extends AbstractDataTreeChangeListener sffDataPlaneLocatorList = sff.getSffDataPlaneLocator(); if (sffDataPlaneLocatorList == null || sffDataPlaneLocatorList.isEmpty()) { return; } - NodeId ovsdbBridgeNodeId = SfcOvsUtil.getOvsdbAugmentationNodeIdBySff(sff); for (SffDataPlaneLocator sffDpl : sffDataPlaneLocatorList) { // Only delete the port if this SFF is OVS augmented and the @@ -145,6 +121,5 @@ public class SfcOvsSffEntryDataListener extends AbstractDataTreeChangeListener + xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0" + odl:use-default-for-reference-types="true"> - + - + - - - - - - - - - + diff --git a/sfc-ovs/src/test/java/org/opendaylight/sfc/ovs/listener/SfcOvsNodeDataListenerTest.java b/sfc-ovs/src/test/java/org/opendaylight/sfc/ovs/listener/SfcOvsNodeDataListenerTest.java index 3f01c1bf5..892e473b8 100644 --- a/sfc-ovs/src/test/java/org/opendaylight/sfc/ovs/listener/SfcOvsNodeDataListenerTest.java +++ b/sfc-ovs/src/test/java/org/opendaylight/sfc/ovs/listener/SfcOvsNodeDataListenerTest.java @@ -87,9 +87,6 @@ public class SfcOvsNodeDataListenerTest extends AbstractDataStoreManager { dataTreeModification = mock(DataTreeModification.class); dataObjectModification = mock(DataObjectModification.class); sfcOvsNodeDataListener = new SfcOvsNodeDataListener(getDataBroker()); - // Dont initialize it since the listener may launch when we're not ready - // yet - // sfcOvsNodeDataListener.init(); } @After diff --git a/sfc-ovs/src/test/java/org/opendaylight/sfc/ovs/listener/SfcOvsSffEntryDataListenerTest.java b/sfc-ovs/src/test/java/org/opendaylight/sfc/ovs/listener/SfcOvsSffEntryDataListenerTest.java index ca788e3b1..d5ca9a0dc 100644 --- a/sfc-ovs/src/test/java/org/opendaylight/sfc/ovs/listener/SfcOvsSffEntryDataListenerTest.java +++ b/sfc-ovs/src/test/java/org/opendaylight/sfc/ovs/listener/SfcOvsSffEntryDataListenerTest.java @@ -86,7 +86,6 @@ public class SfcOvsSffEntryDataListenerTest extends AbstractDataStoreManager { dataTreeModification = mock(DataTreeModification.class); dataObjectModification = mock(DataObjectModification.class); sfcOvsSffEntryDataListener = new SfcOvsSffEntryDataListener(getDataBroker()); - sfcOvsSffEntryDataListener.init(); } @After diff --git a/sfc-ovs/src/test/java/org/opendaylight/sfc/sfc_ovs/provider/listener/SfcOvsSffEntryDataListenerTest.java b/sfc-ovs/src/test/java/org/opendaylight/sfc/sfc_ovs/provider/listener/SfcOvsSffEntryDataListenerTest.java index 1c53190e9..27dce01e0 100644 --- a/sfc-ovs/src/test/java/org/opendaylight/sfc/sfc_ovs/provider/listener/SfcOvsSffEntryDataListenerTest.java +++ b/sfc-ovs/src/test/java/org/opendaylight/sfc/sfc_ovs/provider/listener/SfcOvsSffEntryDataListenerTest.java @@ -87,7 +87,6 @@ public class SfcOvsSffEntryDataListenerTest extends AbstractDataStoreManager { dataTreeModification = mock(DataTreeModification.class); dataObjectModification = mock(DataObjectModification.class); sfcOvsSffEntryDataListener = new SfcOvsSffEntryDataListener(getDataBroker()); - sfcOvsSffEntryDataListener.init(); } @After -- 2.36.6