From adaad41d95a11d661075a0114eb25d08ecb6e5e5 Mon Sep 17 00:00:00 2001 From: Martin Bobak Date: Sun, 9 Nov 2014 21:27:39 +0100 Subject: [PATCH] learning switch shifted to new data broker API Change-Id: Ib74861c69490a5dc259bfe94c2c03c10ec91cb57 Signed-off-by: Martin Bobak --- .../learningswitch/Activator.java | 19 ++++---- .../DataChangeListenerRegistrationHolder.java | 2 +- .../learningswitch/FlowCommitWrapper.java | 4 +- .../learningswitch/FlowCommitWrapperImpl.java | 26 +++++----- .../learningswitch/LearningSwitchManager.java | 33 +++++++------ .../LearningSwitchManagerSimpleImpl.java | 16 ++++--- .../learningswitch/WakeupOnNode.java | 23 ++++----- .../multi/LearningSwitchManagerMultiImpl.java | 47 ++++++++++--------- 8 files changed, 90 insertions(+), 80 deletions(-) diff --git a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/Activator.java b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/Activator.java index 75e65f708b..9ee9a1e681 100644 --- a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/Activator.java +++ b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/Activator.java @@ -7,10 +7,10 @@ */ package org.opendaylight.openflowplugin.learningswitch; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareConsumer; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext; import org.opendaylight.controller.sal.binding.api.NotificationService; -import org.opendaylight.controller.sal.binding.api.data.DataBrokerService; import org.opendaylight.openflowplugin.learningswitch.multi.LearningSwitchManagerMultiImpl; import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService; import org.osgi.framework.BundleContext; @@ -19,26 +19,25 @@ import org.slf4j.LoggerFactory; /** * learning switch activator - * + *

* Activator is derived from AbstractBindingAwareConsumer, which takes care * of looking up MD-SAL in Service Registry and registering consumer * when MD-SAL is present. */ public class Activator extends AbstractBindingAwareConsumer implements AutoCloseable { - + private static final Logger LOG = LoggerFactory.getLogger(Activator.class); private LearningSwitchManager learningSwitch; - + @Override protected void startImpl(BundleContext context) { LOG.info("startImpl() passing"); } - + /** * Invoked when consumer is registered to the MD-SAL. - * */ @Override public void onSessionInitialized(ConsumerContext session) { @@ -46,15 +45,15 @@ public class Activator extends AbstractBindingAwareConsumer implements AutoClose /** * We create instance of our LearningSwitchManager * and set all required dependencies, - * + * * which are * Data Broker (data storage service) - for configuring flows and reading stored switch state * PacketProcessingService - for sending out packets * NotificationService - for receiving notifications such as packet in. - * + * */ learningSwitch = new LearningSwitchManagerMultiImpl(); - learningSwitch.setDataBroker(session.getSALService(DataBrokerService.class)); + learningSwitch.setDataBroker(session.getSALService(DataBroker.class)); learningSwitch.setPacketProcessingService(session.getRpcService(PacketProcessingService.class)); learningSwitch.setNotificationService(session.getSALService(NotificationService.class)); learningSwitch.start(); @@ -67,7 +66,7 @@ public class Activator extends AbstractBindingAwareConsumer implements AutoClose learningSwitch.stop(); } } - + @Override protected void stopImpl(BundleContext context) { close(); diff --git a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/DataChangeListenerRegistrationHolder.java b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/DataChangeListenerRegistrationHolder.java index b52951fdf6..a480ec4915 100644 --- a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/DataChangeListenerRegistrationHolder.java +++ b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/DataChangeListenerRegistrationHolder.java @@ -8,7 +8,7 @@ package org.opendaylight.openflowplugin.learningswitch; -import org.opendaylight.controller.sal.binding.api.data.DataChangeListener; +import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; import org.opendaylight.yangtools.concepts.ListenerRegistration; /** diff --git a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/FlowCommitWrapper.java b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/FlowCommitWrapper.java index 3294a1bd12..4d3de684ca 100644 --- a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/FlowCommitWrapper.java +++ b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/FlowCommitWrapper.java @@ -8,9 +8,11 @@ package org.opendaylight.openflowplugin.learningswitch; +import com.google.common.util.concurrent.CheckedFuture; import java.util.concurrent.Future; import org.opendaylight.controller.md.sal.common.api.TransactionStatus; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.RpcResult; @@ -29,6 +31,6 @@ public interface FlowCommitWrapper { * @return transaction commit * */ - Future> writeFlowToConfig(InstanceIdentifier flowPath, Flow flowBody); + CheckedFuture writeFlowToConfig(InstanceIdentifier flowPath, Flow flowBody); } diff --git a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/FlowCommitWrapperImpl.java b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/FlowCommitWrapperImpl.java index ae12b0eecf..b8af120fce 100644 --- a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/FlowCommitWrapperImpl.java +++ b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/FlowCommitWrapperImpl.java @@ -8,35 +8,37 @@ package org.opendaylight.openflowplugin.learningswitch; +import com.google.common.util.concurrent.CheckedFuture; import java.util.concurrent.Future; - +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; import org.opendaylight.controller.md.sal.common.api.TransactionStatus; -import org.opendaylight.controller.sal.binding.api.data.DataBrokerService; -import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.RpcResult; /** - * + * */ public class FlowCommitWrapperImpl implements FlowCommitWrapper { - - private DataBrokerService dataBrokerService; + + private DataBroker dataBrokerService; /** * @param dataBrokerService */ - public FlowCommitWrapperImpl(DataBrokerService dataBrokerService) { + public FlowCommitWrapperImpl(DataBroker dataBrokerService) { this.dataBrokerService = dataBrokerService; } @Override - public Future> writeFlowToConfig(InstanceIdentifier flowPath, - Flow flowBody) { - DataModificationTransaction addFlowTransaction = dataBrokerService.beginTransaction(); - addFlowTransaction.putConfigurationData(flowPath, flowBody); - return addFlowTransaction.commit(); + public CheckedFuture writeFlowToConfig(InstanceIdentifier flowPath, + Flow flowBody) { + ReadWriteTransaction addFlowTransaction = dataBrokerService.newReadWriteTransaction(); + addFlowTransaction.put(LogicalDatastoreType.CONFIGURATION, flowPath, flowBody); + return addFlowTransaction.submit(); } } diff --git a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/LearningSwitchManager.java b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/LearningSwitchManager.java index 9316506a7a..3b992b2330 100644 --- a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/LearningSwitchManager.java +++ b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/LearningSwitchManager.java @@ -8,12 +8,13 @@ package org.opendaylight.openflowplugin.learningswitch; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.sal.binding.api.NotificationService; import org.opendaylight.controller.sal.binding.api.data.DataBrokerService; import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService; /** - * + * */ public interface LearningSwitchManager { @@ -29,27 +30,28 @@ public interface LearningSwitchManager { /** * Set's Data Broker dependency. - * + *

* Data Broker is used to access overal operational and configuration * tree. - * - * In simple Learning Switch handler, data broker is used to listen - * for changes in Openflow tables and to configure flows which will - * be provisioned down to the Openflow switch. - * - * inject {@link DataBrokerService} + *

+ * In simple Learning Switch handler, data broker is used to listen + * for changes in Openflow tables and to configure flows which will + * be provisioned down to the Openflow switch. + *

+ * inject {@link DataBroker} + * * @param data */ - void setDataBroker(DataBrokerService data); + void setDataBroker(DataBroker data); /** * Set's Packet Processing dependency. - * + *

* Packet Processing service is used to send packet Out on Openflow * switch. - * + *

* inject {@link PacketProcessingService} - * + * * @param packetProcessingService */ void setPacketProcessingService( @@ -57,11 +59,12 @@ public interface LearningSwitchManager { /** * Set's Notification service dependency. - * - * Notification service is used to register for listening + *

+ * Notification service is used to register for listening * packet-in notifications. - * + *

* inject {@link NotificationService} + * * @param notificationService */ void setNotificationService(NotificationService notificationService); diff --git a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/LearningSwitchManagerSimpleImpl.java b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/LearningSwitchManagerSimpleImpl.java index 78169fe8d0..2debfe2f4f 100644 --- a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/LearningSwitchManagerSimpleImpl.java +++ b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/LearningSwitchManagerSimpleImpl.java @@ -7,9 +7,10 @@ */ package org.opendaylight.openflowplugin.learningswitch; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.sal.binding.api.NotificationService; -import org.opendaylight.controller.sal.binding.api.data.DataBrokerService; -import org.opendaylight.controller.sal.binding.api.data.DataChangeListener; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; @@ -39,11 +40,11 @@ public class LearningSwitchManagerSimpleImpl implements DataChangeListenerRegist private NotificationService notificationService; private PacketProcessingService packetProcessingService; - private DataBrokerService data; + private DataBroker data; private Registration packetInRegistration; - private ListenerRegistration dataChangeListenerRegistration; + private ListenerRegistration dataChangeListenerRegistration; /** * @param notificationService the notificationService to set @@ -66,7 +67,7 @@ public class LearningSwitchManagerSimpleImpl implements DataChangeListenerRegist * @param data the data to set */ @Override - public void setDataBroker(DataBrokerService data) { + public void setDataBroker(DataBroker data) { this.data = data; } @@ -86,12 +87,13 @@ public class LearningSwitchManagerSimpleImpl implements DataChangeListenerRegist WakeupOnNode wakeupListener = new WakeupOnNode(); wakeupListener.setLearningSwitchHandler(learningSwitchHandler); - dataChangeListenerRegistration = data.registerDataChangeListener( + dataChangeListenerRegistration = data.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(Nodes.class) .child(Node.class) .augmentation(FlowCapableNode.class) .child(Table.class).toInstance(), - wakeupListener); + wakeupListener, + DataBroker.DataChangeScope.SUBTREE); LOG.debug("start() <--"); } diff --git a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/WakeupOnNode.java b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/WakeupOnNode.java index 3ce3dd0bc3..7a860ffed7 100644 --- a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/WakeupOnNode.java +++ b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/WakeupOnNode.java @@ -10,9 +10,8 @@ package org.opendaylight.openflowplugin.learningswitch; import java.util.Map; import java.util.Map.Entry; - -import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent; -import org.opendaylight.controller.sal.binding.api.data.DataChangeListener; +import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; +import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; @@ -20,27 +19,28 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * + * */ public class WakeupOnNode implements DataChangeListener { - + private static final Logger LOG = LoggerFactory .getLogger(WakeupOnNode.class); - + private LearningSwitchHandler learningSwitchHandler; @Override - public void onDataChanged(DataChangeEvent, DataObject> change) { + public void onDataChanged(AsyncDataChangeEvent, DataObject> change) { Short requiredTableId = 0; - + // TODO add flow - Map, DataObject> updated = change.getUpdatedOperationalData(); + + Map, DataObject> updated = change.getUpdatedData(); for (Entry, DataObject> updateItem : updated.entrySet()) { DataObject table = updateItem.getValue(); if (table instanceof Table) { Table tableSure = (Table) table; LOG.trace("table: {}", table); - + if (requiredTableId.equals(tableSure.getId())) { @SuppressWarnings("unchecked") InstanceIdentifier tablePath = (InstanceIdentifier
) updateItem.getKey(); @@ -49,7 +49,7 @@ public class WakeupOnNode implements DataChangeListener { } } } - + /** * @param learningSwitchHandler the learningSwitchHandler to set */ @@ -57,4 +57,5 @@ public class WakeupOnNode implements DataChangeListener { LearningSwitchHandler learningSwitchHandler) { this.learningSwitchHandler = learningSwitchHandler; } + } diff --git a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/multi/LearningSwitchManagerMultiImpl.java b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/multi/LearningSwitchManagerMultiImpl.java index aa3d565f5b..7af21a7565 100644 --- a/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/multi/LearningSwitchManagerMultiImpl.java +++ b/samples/learning-switch/src/main/java/org/opendaylight/openflowplugin/learningswitch/multi/LearningSwitchManagerMultiImpl.java @@ -7,13 +7,14 @@ */ package org.opendaylight.openflowplugin.learningswitch.multi; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.sal.binding.api.NotificationService; -import org.opendaylight.controller.sal.binding.api.data.DataBrokerService; -import org.opendaylight.controller.sal.binding.api.data.DataChangeListener; import org.opendaylight.openflowplugin.learningswitch.DataChangeListenerRegistrationHolder; -import org.opendaylight.openflowplugin.learningswitch.LearningSwitchManager; import org.opendaylight.openflowplugin.learningswitch.FlowCommitWrapper; import org.opendaylight.openflowplugin.learningswitch.FlowCommitWrapperImpl; +import org.opendaylight.openflowplugin.learningswitch.LearningSwitchManager; import org.opendaylight.openflowplugin.learningswitch.WakeupOnNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table; @@ -23,33 +24,32 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.Pa import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.binding.NotificationListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Listens to packetIn notification and + * Listens to packetIn notification and *
    *
  • in HUB mode simply floods all switch ports (except ingress port)
  • - *
  • in LSWITCH mode collects source MAC address of packetIn and bind it with ingress port. - * If target MAC address is already bound then a flow is created (for direct communication between + *
  • in LSWITCH mode collects source MAC address of packetIn and bind it with ingress port. + * If target MAC address is already bound then a flow is created (for direct communication between * corresponding MACs)
  • *
*/ public class LearningSwitchManagerMultiImpl implements DataChangeListenerRegistrationHolder, LearningSwitchManager { - + protected static final Logger LOG = LoggerFactory .getLogger(LearningSwitchManagerMultiImpl.class); private NotificationService notificationService; private PacketProcessingService packetProcessingService; - private DataBrokerService data; + private DataBroker data; private Registration packetInRegistration; - private ListenerRegistration dataChangeListenerRegistration; - + private ListenerRegistration dataChangeListenerRegistration; + /** * @param notificationService the notificationService to set */ @@ -66,12 +66,12 @@ public class LearningSwitchManagerMultiImpl implements DataChangeListenerRegistr PacketProcessingService packetProcessingService) { this.packetProcessingService = packetProcessingService; } - + /** * @param data the data to set */ @Override - public void setDataBroker(DataBrokerService data) { + public void setDataBroker(DataBroker data) { this.data = data; } @@ -90,20 +90,21 @@ public class LearningSwitchManagerMultiImpl implements DataChangeListenerRegistr learningSwitchHandler.setPacketProcessingService(packetProcessingService); learningSwitchHandler.setPacketInDispatcher(packetInDispatcher); packetInRegistration = notificationService.registerNotificationListener(packetInDispatcher); - + WakeupOnNode wakeupListener = new WakeupOnNode(); wakeupListener.setLearningSwitchHandler(learningSwitchHandler); - dataChangeListenerRegistration = data.registerDataChangeListener( + dataChangeListenerRegistration = data.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(Nodes.class) - .child(Node.class) - .augmentation(FlowCapableNode.class) - .child(Table.class).toInstance(), - wakeupListener); + .child(Node.class) + .augmentation(FlowCapableNode.class) + .child(Table.class).toInstance(), + wakeupListener, + DataBroker.DataChangeScope.SUBTREE); LOG.debug("start() <--"); } - + /** - * stopping learning switch + * stopping learning switch */ @Override public void stop() { @@ -121,8 +122,8 @@ public class LearningSwitchManagerMultiImpl implements DataChangeListenerRegistr } LOG.debug("stop() <--"); } - - + + @Override public ListenerRegistration getDataChangeListenerRegistration() { return dataChangeListenerRegistration; -- 2.36.6