Bump upstreams
[openflowplugin.git] / samples / learning-switch / src / main / java / org / opendaylight / openflowplugin / learningswitch / LearningSwitchManagerSimpleImpl.java
index 9b97e801eb71e947cbc43b831e63f7530ba2ac1e..26c582bbc65706e46e646aefb2f4e56deb8954c7 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,15 +7,17 @@
  */
 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.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.NotificationService;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 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;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -23,7 +25,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Listens to packetIn notification and
+ * Listens to packetIn notification.
  * <ul>
  * <li>in HUB mode simply floods all switch ports (except ingress port)</li>
  * <li>in LSWITCH mode collects source MAC address of packetIn and bind it with ingress port.
@@ -31,96 +33,86 @@ import org.slf4j.LoggerFactory;
  * corresponding MACs)</li>
  * </ul>
  */
-public class LearningSwitchManagerSimpleImpl implements DataChangeListenerRegistrationHolder,
-        LearningSwitchManager {
-
-    private static final Logger LOG = LoggerFactory
-            .getLogger(LearningSwitchManagerSimpleImpl.class);
+public class LearningSwitchManagerSimpleImpl
+        implements DataTreeChangeListenerRegistrationHolder, LearningSwitchManager {
+    private static final Logger LOG = LoggerFactory.getLogger(LearningSwitchManagerSimpleImpl.class);
 
-    private NotificationService notificationService;
+    private NotificationService notificationService = null;
     private PacketProcessingService packetProcessingService;
-    private DataBroker data;
-
-    private Registration packetInRegistration;
-
-    private ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
+    private DataBroker data = null;
+    private Registration packetInRegistration = null;
+    private ListenerRegistration<DataTreeChangeListener> dataTreeChangeListenerRegistration = null;
 
     /**
+     * Sets the NotificationService.
+     *
      * @param notificationService the notificationService to set
      */
     @Override
-    public void setNotificationService(NotificationService notificationService) {
+    public void setNotificationService(final NotificationService notificationService) {
         this.notificationService = notificationService;
     }
 
     /**
+     * Sets the PacketProcessingService.
+     *
      * @param packetProcessingService the packetProcessingService to set
      */
     @Override
     public void setPacketProcessingService(
-            PacketProcessingService packetProcessingService) {
+            final PacketProcessingService packetProcessingService) {
         this.packetProcessingService = packetProcessingService;
     }
 
     /**
-     * @param data the data to set
+     * Sets the DataBroker.
      */
     @Override
-    public void setDataBroker(DataBroker data) {
-        this.data = data;
+    public void setDataBroker(final DataBroker broker) {
+        data = broker;
     }
 
     /**
-     * starting learning switch
+     * Starts learning switch.
      */
     @Override
     public void start() {
         LOG.debug("start() -->");
         FlowCommitWrapper dataStoreAccessor = new FlowCommitWrapperImpl(data);
 
-        LearningSwitchHandlerSimpleImpl learningSwitchHandler = new LearningSwitchHandlerSimpleImpl();
-        learningSwitchHandler.setRegistrationPublisher(this);
-        learningSwitchHandler.setDataStoreAccessor(dataStoreAccessor);
-        learningSwitchHandler.setPacketProcessingService(packetProcessingService);
-        packetInRegistration = notificationService.registerNotificationListener(learningSwitchHandler);
+        LearningSwitchHandlerSimpleImpl learningSwitchHandler = new LearningSwitchHandlerSimpleImpl(dataStoreAccessor,
+                packetProcessingService, this);
+        packetInRegistration = notificationService.registerListener(PacketReceived.class, learningSwitchHandler);
 
         WakeupOnNode wakeupListener = new WakeupOnNode();
         wakeupListener.setLearningSwitchHandler(learningSwitchHandler);
-        dataChangeListenerRegistration = data.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
-                InstanceIdentifier.builder(Nodes.class)
-                    .child(Node.class)
-                    .augmentation(FlowCapableNode.class)
-                    .child(Table.class).build(),
-                wakeupListener,
-                DataBroker.DataChangeScope.SUBTREE);
+        final InstanceIdentifier<Table> instanceIdentifier = InstanceIdentifier.create(Nodes.class)
+                .child(Node.class)
+                .augmentation(FlowCapableNode.class)
+                .child(Table.class);
+        final DataTreeIdentifier<Table> dataTreeIdentifier =
+                DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
+        dataTreeChangeListenerRegistration = data.registerDataTreeChangeListener(dataTreeIdentifier, wakeupListener);
         LOG.debug("start() <--");
     }
 
     /**
-     * stopping learning switch
+     * Stops the learning switch.
      */
     @Override
     public void stop() {
         LOG.debug("stop() -->");
         //TODO: remove flow (created in #start())
-        try {
-            packetInRegistration.close();
-        } catch (Exception e) {
-            LOG.warn("closing packetInRegistration failed: {}", e.getMessage());
-            LOG.debug("closing packetInRegistration failed..", e);
-        }
-        try {
-            dataChangeListenerRegistration.close();
-        } catch (Exception e) {
-            LOG.warn("failed to close dataChangeListenerRegistration: {}", e.getMessage());
-            LOG.debug("failed to close dataChangeListenerRegistration..", e);
-        }
+
+        packetInRegistration.close();
+
+        dataTreeChangeListenerRegistration.close();
+
         LOG.debug("stop() <--");
     }
 
-
     @Override
-    public ListenerRegistration<DataChangeListener> getDataChangeListenerRegistration() {
-        return dataChangeListenerRegistration;
+    public ListenerRegistration<DataTreeChangeListener> getDataTreeChangeListenerRegistration() {
+        return dataTreeChangeListenerRegistration;
     }
 }